Toggle Navigation
Hatchery
Eggs
Rainbow 2048
__init__.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
__init__.py
raw
Content
import display, keypad, random, time # adapted from the Sha2017 2048 game PROBABILITY_4 = 0.1 # probability of a new tile being a 4 rather than a 2 INITIAL_TILES = 2 # number of tiles on the board on a new game KEY_UP_0 = 1 KEY_UP_1 = 2 KEY_DOWN_0 = 13 KEY_DOWN_1 = 14 KEY_RIGHT_0 = 7 KEY_RIGHT_1 = 11 KEY_LEFT_0 = 4 KEY_LEFT_1 = 8 board = None num2col = [0x417505,0xF5A623,0xFF7F00,0xF8E71C, # green, yellow, orange, limegreen 0xD0021B,0xDD0DD5,0xF05858,0xFC0080, # red, purple, pink, magenta 0x0000ff,0x7E67A2,0xff8040,0x202080, # blue, lightblue, lightorange, darkblue 0x40ff80,0x808040,0xa00000,0xe9e9aD] # bright green, dimmed green, dimmed red, white def test_colors(): for y in range(0, 4): for x in range(0, 4): color = num2col[y*4 + x] display.drawPixel(x, y, color) display.flush() def draw_board(): print("drawing board") for y in range(0, 4): for x in range(0, 4): if board[y][x] is None: display.drawPixel(x, y, 0x000000) else: num = board[y][x] color = num2col[num-1] display.drawPixel(x, y, color) if not (can_move_up() or can_move_down() or can_move_left() or can_move_right()): print("Game Over!") display.flush() def add_tile(): while True: x = random.randint(0, 3) y = random.randint(0, 3) if board[y][x] is None: board[y][x] = 2 if random.random() < PROBABILITY_4 else 1 print("added tile") break def start_game(): global board random.seed(time.ticks_ms()) board = [[None, None, None, None] for i in range(0, 4)] for i in range(0, INITIAL_TILES): add_tile() draw_board() def can_move_up(): for x in range(0, 4): old_tiles = [board[y][x] for y in range(0, 4)] new_tiles = merge_tiles_left(old_tiles) if new_tiles != old_tiles: return True return False def can_move_down(): for x in range(0, 4): old_tiles = [board[y][x] for y in range(0, 4)] new_tiles = merge_tiles_right(old_tiles) if new_tiles != old_tiles: return True return False def can_move_left(): for y in range(0, 4): old_tiles = board[y] new_tiles = merge_tiles_left(old_tiles) if new_tiles != old_tiles: return True return False def can_move_right(): for y in range(0, 4): old_tiles = board[y] new_tiles = merge_tiles_right(old_tiles) if new_tiles != old_tiles: return True return False def merge_tiles_left(old_tiles): new_tiles = [] last_tile = None for tile in old_tiles: if tile is None: continue if tile == last_tile: new_tiles[-1] = tile+1 # * 2 last_tile = None else: new_tiles.append(tile) last_tile = tile return (new_tiles + [None, None, None, None])[:4] def merge_tiles_right(old_tiles): return merge_tiles_left(old_tiles[::-1])[::-1] def on_key(index, pressed): if pressed and (index == KEY_UP_0 or index == KEY_UP_1) and can_move_up(): for x in range(0, 4): new_tiles = merge_tiles_left([board[y][x] for y in range(0, 4)]) for y in range(0, 4): board[y][x] = new_tiles[y] add_tile() draw_board() elif pressed and (index == KEY_DOWN_0 or index == KEY_DOWN_1) and can_move_down(): for x in range(0, 4): new_tiles = merge_tiles_right([board[y][x] for y in range(0, 4)]) for y in range(0, 4): board[y][x] = new_tiles[y] add_tile() draw_board() elif pressed and (index == KEY_LEFT_0 or index == KEY_LEFT_1) and can_move_left(): for y in range(0, 4): new_tiles = merge_tiles_left(board[y]) for x in range(0, 4): board[y][x] = new_tiles[x] add_tile() draw_board() elif pressed and (index == KEY_RIGHT_0 or index == KEY_RIGHT_1) and can_move_right(): for y in range(0, 4): new_tiles = merge_tiles_right(board[y]) for x in range(0, 4): board[y][x] = new_tiles[x] add_tile() draw_board() keypad.add_handler(on_key) #test_colors() start_game()