|
| 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 |
0 commit comments