Skip to content

Commit 6ce637a

Browse files
authored
Merge pull request #1115 from FoamyGuy/first_tilemap_game
First tilemap game
2 parents 42ba480 + f0defb3 commit 6ce637a

17 files changed

+1094
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import board
2+
import displayio
3+
import adafruit_imageload
4+
5+
# from tilegame_assets.controls_helper import controls
6+
import ugame
7+
8+
display = board.DISPLAY
9+
player_loc = {"x": 4, "y": 3}
10+
11+
# Load the sprite sheet (bitmap)
12+
sprite_sheet, palette = adafruit_imageload.load(
13+
"tilegame_assets/castle_sprite_sheet.bmp",
14+
bitmap=displayio.Bitmap,
15+
palette=displayio.Palette,
16+
)
17+
18+
# Create the sprite TileGrid
19+
sprite = displayio.TileGrid(
20+
sprite_sheet,
21+
pixel_shader=palette,
22+
width=1,
23+
height=1,
24+
tile_width=16,
25+
tile_height=16,
26+
default_tile=0,
27+
)
28+
29+
# Create the castle TileGrid
30+
castle = displayio.TileGrid(
31+
sprite_sheet,
32+
pixel_shader=palette,
33+
width=10,
34+
height=8,
35+
tile_width=16,
36+
tile_height=16,
37+
)
38+
39+
# Create a Group to hold the sprite and add it
40+
sprite_group = displayio.Group()
41+
sprite_group.append(sprite)
42+
43+
# Create a Group to hold the castle and add it
44+
castle_group = displayio.Group(scale=1)
45+
castle_group.append(castle)
46+
47+
# Create a Group to hold the sprite and castle
48+
group = displayio.Group()
49+
50+
# Add the sprite and castle to the group
51+
group.append(castle_group)
52+
group.append(sprite_group)
53+
54+
# Castle tile assignments
55+
# corners
56+
castle[0, 0] = 3 # upper left
57+
castle[9, 0] = 5 # upper right
58+
castle[0, 7] = 9 # lower left
59+
castle[9, 7] = 11 # lower right
60+
# top / bottom walls
61+
for x in range(1, 9):
62+
castle[x, 0] = 4 # top
63+
castle[x, 7] = 10 # bottom
64+
# left/ right walls
65+
for y in range(1, 7):
66+
castle[0, y] = 6 # left
67+
castle[9, y] = 8 # right
68+
# floor
69+
for x in range(1, 9):
70+
for y in range(1, 7):
71+
castle[x, y] = 7 # floor
72+
73+
# put the sprite somewhere in the castle
74+
sprite.x = 16 * player_loc["x"]
75+
sprite.y = 16 * player_loc["y"]
76+
77+
# Add the Group to the Display
78+
display.show(group)
79+
80+
prev_btn_vals = ugame.buttons.get_pressed()
81+
82+
while True:
83+
cur_btn_vals = ugame.buttons.get_pressed()
84+
if not prev_btn_vals & ugame.K_UP and cur_btn_vals & ugame.K_UP:
85+
player_loc["y"] = max(1, player_loc["y"] - 1)
86+
if not prev_btn_vals & ugame.K_DOWN and cur_btn_vals & ugame.K_DOWN:
87+
player_loc["y"] = min(6, player_loc["y"] + 1)
88+
89+
if not prev_btn_vals & ugame.K_RIGHT and cur_btn_vals & ugame.K_RIGHT:
90+
player_loc["x"] = min(8, player_loc["x"] + 1)
91+
if not prev_btn_vals & ugame.K_LEFT and cur_btn_vals & ugame.K_LEFT:
92+
player_loc["x"] = max(1, player_loc["x"] - 1)
93+
94+
# update the the player sprite position
95+
sprite.x = 16 * player_loc["x"]
96+
sprite.y = 16 * player_loc["y"]
97+
98+
# update the previous values
99+
prev_btn_vals = cur_btn_vals
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import board
2+
import displayio
3+
import adafruit_imageload
4+
import ugame
5+
6+
display = board.DISPLAY
7+
player_loc = {"x": 4, "y": 3}
8+
9+
# Load the sprite sheet (bitmap)
10+
sprite_sheet, palette = adafruit_imageload.load(
11+
"tilegame_assets/castle_sprite_sheet_edited.bmp",
12+
bitmap=displayio.Bitmap,
13+
palette=displayio.Palette,
14+
)
15+
# make the color at 0 index transparent.
16+
palette.make_transparent(0)
17+
18+
# Create the sprite TileGrid
19+
sprite = displayio.TileGrid(
20+
sprite_sheet,
21+
pixel_shader=palette,
22+
width=1,
23+
height=1,
24+
tile_width=16,
25+
tile_height=16,
26+
default_tile=0,
27+
)
28+
29+
# Create the castle TileGrid
30+
castle = displayio.TileGrid(
31+
sprite_sheet,
32+
pixel_shader=palette,
33+
width=10,
34+
height=8,
35+
tile_width=16,
36+
tile_height=16,
37+
)
38+
39+
# Create a Group to hold the sprite and add it
40+
sprite_group = displayio.Group()
41+
sprite_group.append(sprite)
42+
43+
# Create a Group to hold the castle and add it
44+
castle_group = displayio.Group(scale=1)
45+
castle_group.append(castle)
46+
47+
# Create a Group to hold the sprite and castle
48+
group = displayio.Group()
49+
50+
# Add the sprite and castle to the group
51+
group.append(castle_group)
52+
group.append(sprite_group)
53+
54+
# Castle tile assignments
55+
# corners
56+
castle[0, 0] = 3 # upper left
57+
castle[9, 0] = 5 # upper right
58+
castle[0, 7] = 9 # lower left
59+
castle[9, 7] = 11 # lower right
60+
# top / bottom walls
61+
for x in range(1, 9):
62+
castle[x, 0] = 4 # top
63+
castle[x, 7] = 10 # bottom
64+
# left/ right walls
65+
for y in range(1, 7):
66+
castle[0, y] = 6 # left
67+
castle[9, y] = 8 # right
68+
# floor
69+
for x in range(1, 9):
70+
for y in range(1, 7):
71+
castle[x, y] = 7 # floor
72+
73+
# put the sprite somewhere in the castle
74+
sprite.x = 16 * player_loc["x"]
75+
sprite.y = 16 * player_loc["y"]
76+
77+
# Add the Group to the Display
78+
display.show(group)
79+
80+
prev_btn_vals = ugame.buttons.get_pressed()
81+
82+
while True:
83+
cur_btn_vals = ugame.buttons.get_pressed()
84+
if not prev_btn_vals & ugame.K_UP and cur_btn_vals & ugame.K_UP:
85+
player_loc["y"] = max(1, player_loc["y"] - 1)
86+
if not prev_btn_vals & ugame.K_DOWN and cur_btn_vals & ugame.K_DOWN:
87+
player_loc["y"] = min(6, player_loc["y"] + 1)
88+
89+
if not prev_btn_vals & ugame.K_RIGHT and cur_btn_vals & ugame.K_RIGHT:
90+
player_loc["x"] = min(8, player_loc["x"] + 1)
91+
if not prev_btn_vals & ugame.K_LEFT and cur_btn_vals & ugame.K_LEFT:
92+
player_loc["x"] = max(1, player_loc["x"] - 1)
93+
94+
# update the the player sprite position
95+
sprite.x = 16 * player_loc["x"]
96+
sprite.y = 16 * player_loc["y"]
97+
98+
# update the previous values
99+
prev_btn_vals = cur_btn_vals
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import board
2+
import displayio
3+
import adafruit_imageload
4+
5+
display = board.DISPLAY
6+
7+
# Load the sprite sheet (bitmap)
8+
sprite_sheet, palette = adafruit_imageload.load(
9+
"tilegame_assets/castle_sprite_sheet.bmp",
10+
bitmap=displayio.Bitmap,
11+
palette=displayio.Palette,
12+
)
13+
14+
# Create the sprite TileGrid
15+
sprite = displayio.TileGrid(
16+
sprite_sheet,
17+
pixel_shader=palette,
18+
width=1,
19+
height=1,
20+
tile_width=16,
21+
tile_height=16,
22+
default_tile=0,
23+
)
24+
25+
# Create the castle TileGrid
26+
castle = displayio.TileGrid(
27+
sprite_sheet,
28+
pixel_shader=palette,
29+
width=10,
30+
height=8,
31+
tile_width=16,
32+
tile_height=16,
33+
)
34+
35+
# Create a Group to hold the sprite and add it
36+
sprite_group = displayio.Group()
37+
sprite_group.append(sprite)
38+
39+
# Create a Group to hold the castle and add it
40+
castle_group = displayio.Group(scale=1)
41+
castle_group.append(castle)
42+
43+
# Create a Group to hold the sprite and castle
44+
group = displayio.Group()
45+
46+
# Add the sprite and castle to the group
47+
group.append(castle_group)
48+
group.append(sprite_group)
49+
50+
# Castle tile assignments
51+
# corners
52+
castle[0, 0] = 3 # upper left
53+
castle[9, 0] = 5 # upper right
54+
castle[0, 7] = 9 # lower left
55+
castle[9, 7] = 11 # lower right
56+
# top / bottom walls
57+
for x in range(1, 9):
58+
castle[x, 0] = 4 # top
59+
castle[x, 7] = 10 # bottom
60+
# left/ right walls
61+
for y in range(1, 7):
62+
castle[0, y] = 6 # left
63+
castle[9, y] = 8 # right
64+
# floor
65+
for x in range(1, 9):
66+
for y in range(1, 7):
67+
castle[x, y] = 7 # floor
68+
69+
# put the sprite somewhere in the castle
70+
sprite.x = 16 * 4
71+
sprite.y = 16 * 3
72+
73+
# Add the Group to the Display
74+
display.show(group)
75+
while True:
76+
pass

0 commit comments

Comments
 (0)