Skip to content

Commit 101e919

Browse files
committed
adding Circuit Python tilemap game
1 parent f22cb03 commit 101e919

17 files changed

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

0 commit comments

Comments
 (0)