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