|
| 1 | +import time |
| 2 | +import thumby |
| 3 | +import random |
| 4 | + |
| 5 | +print("Memory Match") |
| 6 | +print("by @TezFraser") |
| 7 | + |
| 8 | +game_started = False |
| 9 | + |
| 10 | + |
| 11 | +is_first_selection = True |
| 12 | +last_selection = 0 |
| 13 | +show_main_menu = True |
| 14 | +show_win = False |
| 15 | + |
| 16 | + |
| 17 | +# BITMAP: width: 9, height: 9 |
| 18 | +questionMark = bytearray([0, 0, 4, 2, 178, 10, 4, 0, 0, |
| 19 | + 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 20 | + |
| 21 | +# BITMAP: width: 9, height: 9 CIRCLE |
| 22 | +circle = bytearray([0, 56, 124, 254, 254, 254, 124, 56, 0, |
| 23 | + 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 24 | + |
| 25 | +# BITMAP: width: 9, height: 9 Square |
| 26 | +squareShape = bytearray([0,254,254,254,254,254,254,254,0, |
| 27 | + 0,0,0,0,0,0,0,0,0]) |
| 28 | + |
| 29 | +# BITMAP: width: 9, height: 9 |
| 30 | +quadSquare = bytearray([0,254,146,146,254,146,146,254,0, |
| 31 | + 0,0,0,0,0,0,0,0,0]) |
| 32 | + |
| 33 | +# BITMAP: width: 9, height: 9 diamond |
| 34 | +diamond = bytearray([0, 16, 56, 124, 254, 124, 56, 16, 0, |
| 35 | + 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 36 | + |
| 37 | +# BITMAP: width: 9, height: 9 |
| 38 | +cross = bytearray([0, 56, 56, 254, 254, 254, 56, 56, 0, |
| 39 | + 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 40 | + |
| 41 | +# BITMAP: width: 9, height: 9 Unused |
| 42 | +arrowUp = bytearray([0, 16, 24, 252, 254, 252, 24, 16, 0, |
| 43 | + 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 44 | + |
| 45 | +# BITMAP: width: 9, height: 9 |
| 46 | +heart = bytearray([0, 28, 62, 124, 248, 124, 62, 28, 0, |
| 47 | + 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 48 | + |
| 49 | +# BITMAP: width: 9, height: 9 |
| 50 | +arrowRight = bytearray([0, 56, 56, 56, 254, 124, 56, 16, 0, |
| 51 | + 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 52 | + |
| 53 | +# BITMAP: width: 3, height: 5 small select arrow |
| 54 | +#arrow_bitmap = bytearray([31, 14, 4]) |
| 55 | + |
| 56 | +thumby.display.setFPS(25) |
| 57 | + |
| 58 | +# Updated table for 4x3 grid (4 columns, 3 rows) |
| 59 | +table = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] # 4x3 grid |
| 60 | + |
| 61 | +check_table = [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6] |
| 62 | + |
| 63 | +selected_square = 0 # Top left |
| 64 | +square_size = 12 |
| 65 | + |
| 66 | +x_padding = 10 |
| 67 | +y_padding = 2 |
| 68 | + |
| 69 | +## Drawing functions |
| 70 | + |
| 71 | +def draw_selection(): |
| 72 | + position = get_position(selected_square) |
| 73 | + thumby.display.drawRectangle(position[0] + 1, position[1] + 1, square_size - 1, square_size - 1, 1) |
| 74 | + |
| 75 | +def get_position(square): # Top left corner of the square |
| 76 | + pos_in_table = [(square % 4) * square_size, int(square / 4) * square_size] # Adjusted for 4 columns |
| 77 | + return [int(pos_in_table[0] + x_padding), pos_in_table[1] + y_padding] |
| 78 | + |
| 79 | +def update_selected_square(value): |
| 80 | + global selected_square |
| 81 | + new_value = selected_square + value |
| 82 | + if new_value >= 0 and new_value < len(table): |
| 83 | + selected_square = new_value |
| 84 | + |
| 85 | +def draw_players(): |
| 86 | + for square, value in enumerate(table): |
| 87 | + position = get_position(square) |
| 88 | + if value == 1: |
| 89 | + draw_player(circle, position) |
| 90 | + elif value == 2: |
| 91 | + draw_player(quadSquare, position) |
| 92 | + elif value == 3: |
| 93 | + draw_player(diamond,position) |
| 94 | + elif value == 4: |
| 95 | + draw_player(cross,position) |
| 96 | + elif value == 5: |
| 97 | + draw_player(arrowRight,position) |
| 98 | + elif value == 6: |
| 99 | + draw_player(heart,position) |
| 100 | + else: |
| 101 | + draw_player(questionMark,position) |
| 102 | + |
| 103 | +def draw_player(bitmap, position): |
| 104 | + player_sprite_size = 9 |
| 105 | + thumby.display.blit(bitmap, position[0] + 2, position[1] + 2, player_sprite_size, player_sprite_size, 0, 0, 0) |
| 106 | + |
| 107 | +def user_input(): |
| 108 | + global is_first_selection |
| 109 | + global last_selection |
| 110 | + if thumby.buttonU.justPressed(): |
| 111 | + update_selected_square(-4) # Move up (4 columns) |
| 112 | + if thumby.buttonD.justPressed(): |
| 113 | + update_selected_square(4) # Move down (4 columns) |
| 114 | + if thumby.buttonL.justPressed(): |
| 115 | + update_selected_square(-1) # Move left |
| 116 | + if thumby.buttonR.justPressed(): |
| 117 | + update_selected_square(1) # Move right |
| 118 | + |
| 119 | + if thumby.buttonA.justPressed(): |
| 120 | + if table[selected_square] == 0: |
| 121 | + thumby.audio.play(3000, 80) |
| 122 | + if is_first_selection: |
| 123 | + is_first_selection = False |
| 124 | + table[selected_square] = check_table[selected_square] |
| 125 | + #Uncomment if debugging |
| 126 | + #print(table[selected_square]) |
| 127 | + last_selection = selected_square |
| 128 | + else: |
| 129 | + if table[last_selection] == check_table[selected_square]: |
| 130 | + table[selected_square] = check_table[selected_square] |
| 131 | + last_selection = -1 |
| 132 | + is_first_selection = True |
| 133 | + else: |
| 134 | + table[selected_square] = check_table[selected_square] |
| 135 | + full_display_update() |
| 136 | + time.sleep(2) |
| 137 | + table[selected_square] = 0 |
| 138 | + table[last_selection] = 0 |
| 139 | + is_first_selection = True |
| 140 | + last_selection = -1 |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +## Game Result Checks |
| 146 | + |
| 147 | +def check_endgame(): |
| 148 | + isValid = True |
| 149 | + for i in range(len(table)): |
| 150 | + if(check_table[i] != table[i]): |
| 151 | + isValid = False |
| 152 | + return isValid |
| 153 | + |
| 154 | + |
| 155 | +def decideOrder(): |
| 156 | + random.seed(time.ticks_ms()) |
| 157 | + global game_started |
| 158 | + global check_table |
| 159 | + #Random shuffle wasn't working so we rigged something together |
| 160 | + for j in range(10): |
| 161 | + newTable = [] |
| 162 | + for i in range(12): |
| 163 | + if(len(check_table) > 0): |
| 164 | + newTable.append(check_table.pop(random.randint(0, 11-i))) |
| 165 | + check_table = newTable |
| 166 | + #Uncomment if debugging |
| 167 | + #print(check_table) |
| 168 | + game_started = True |
| 169 | + |
| 170 | +def full_display_update(): |
| 171 | + thumby.display.fill(0) |
| 172 | + draw_players() |
| 173 | + draw_selection() |
| 174 | + thumby.display.update() |
| 175 | + |
| 176 | +def draw_mainmenu(): |
| 177 | + global show_main_menu |
| 178 | + thumby.display.fill(0) |
| 179 | + thumby.display.setFont("/lib/font5x7.bin", 5, 7, 1) |
| 180 | + thumby.display.drawText("MEMORY", 18, 1, 1) |
| 181 | + thumby.display.drawText("MATCH!", 18, 10, 1) |
| 182 | + |
| 183 | + thumby.display.setFont("/lib/font3x5.bin", 3, 5, 1) |
| 184 | + thumby.display.drawText("PRESS A OR B", 10, 32, 1) |
| 185 | + thumby.display.drawText("By: @TezFraser", 10, 20, 1) |
| 186 | + anyButton = thumby.buttonA.justPressed() or thumby.buttonB.justPressed() |
| 187 | + |
| 188 | + if anyButton: |
| 189 | + show_main_menu=False |
| 190 | + |
| 191 | +def draw_win(): |
| 192 | + global show_win |
| 193 | + thumby.display.fill(0) |
| 194 | + thumby.display.setFont("/lib/font8x8.bin", 8, 8, 1) |
| 195 | + thumby.display.drawText("YOU", 20, 10, 1) |
| 196 | + thumby.display.drawText("WIN!", 18, 19, 1) |
| 197 | + thumby.display.update() |
| 198 | + time.sleep(3) |
| 199 | + show_win = False |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | +# Main loop |
| 204 | +while True: |
| 205 | + thumby.display.fill(0) |
| 206 | + if show_main_menu: |
| 207 | + draw_mainmenu() |
| 208 | + |
| 209 | + elif show_win: |
| 210 | + draw_win() |
| 211 | + else: |
| 212 | + if not game_started: |
| 213 | + decideOrder() |
| 214 | + full_display_update() |
| 215 | + user_input() |
| 216 | + |
| 217 | + game_result = check_endgame() |
| 218 | + if game_result: |
| 219 | + full_display_update() |
| 220 | + time.sleep(1) |
| 221 | + show_win = True |
| 222 | + draw_win() |
| 223 | + decideOrder() |
| 224 | + table = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] # Reset grid |
| 225 | + is_first_selection = True # Reset player turn |
| 226 | + last_selection = -1 |
| 227 | + selected_square = 0 |
| 228 | + |
| 229 | + thumby.display.update() |
| 230 | + time.sleep(0.1) |
0 commit comments