Toggle Navigation
Hatchery
Eggs
2048
__init__.py
Login
Register
__init__.py
Content
import easydraw, system, badge, ugfx import random import time random.seed(int(time.time())) BOARD_SIZE = 4 POWERS = [2**x for x in range(1, 30)] POWERS[0] = 0 def free_space(game): free = [0 in game[x] for x in range(BOARD_SIZE)] #print(free) if True in free: return True else: return False def put_number_on_board(game, number=-1, x=-1, y=-1): if number < 0 or not number in POWERS: if random.randrange(10) == 4: number = 4 else: number = 2 if not free_space(game): return False if (x < 0 or x > 3) and (y < 0 or y > 3): loops = 0 while True: loops += 1 x = random.randrange(BOARD_SIZE) y = random.randrange(BOARD_SIZE) if game[y][x] == 0: break if loops >= BOARD_SIZE**2*10: return False game[y][x] = number # Nem return game def highest_number(game): return max([max(row) for row in game]) def copy(game): new_game = create_game() for row in range(len(game)): for col in range(len(game[row])): new_game[row][col] = game[row][col] return new_game def is_over(game): win = [2048 in game[x] for x in range(BOARD_SIZE)] winner = False over = False if True in win: #return True, True winner = True if not free_space(game): gameCopy = copy(game) _, succes_r, _ = move_right(gameCopy) gameCopy = copy(game) _, succes_l, _ = move_left(gameCopy) gameCopy = copy(game) _, succes_d, _ = move_down(gameCopy) gameCopy = copy(game) _, succes_u, _ = move_up(gameCopy) if succes_r or succes_l or succes_d or succes_u: over = False #return False, None else: over = True #return True, False #else: #return False, None return over, winner def vertical(game): verticalGame = [[0 for _ in range(BOARD_SIZE)] for x in range(BOARD_SIZE)] for y in range(BOARD_SIZE): for x in range(BOARD_SIZE): verticalGame[x][y] = game[y][x] game = verticalGame return game def create_game(): game = [[0 for _ in range(BOARD_SIZE)] for x in range(BOARD_SIZE)] for _ in range(2): # x = random.randrange(BOARD_SIZE) # y = random.randrange(BOARD_SIZE) # game[y][x] = random.choice([2, 4]) put_number_on_board(game) return game def move_left(game): modified = False point = 0 for row in game: col = -1 for x in range(BOARD_SIZE): if row[x] == 0: continue if col == -1: col = x continue if row[col] != row[x]: col = x continue if row[col] == row[x]: row[col] += row[x] point += row[col] row[x] = 0 col = -1 modified = True for i in range(BOARD_SIZE**2): x = i % BOARD_SIZE if x == BOARD_SIZE-1: continue if row[x] == 0 and row[x+1] != 0: row[x] = row[x+1] row[x+1] = 0 modified = True if modified: put_number_on_board(game) return game, modified, point def move_right(game): modified = False point = 0 for row in game: col = -1 for x in range(BOARD_SIZE-1, -1, -1): if row[x] == 0: continue if col == -1: col = x continue if row[col] != row[x]: col = x continue if row[col] == row[x]: row[col] += row[x] point += row[col] row[x] = 0 col = -1 modified = True for i in range(BOARD_SIZE**2-1, -1, -1): x = i % BOARD_SIZE if x == 0: continue if row[x] == 0 and row[x-1] != 0: row[x] = row[x-1] row[x-1] = 0 modified = True if modified: put_number_on_board(game) return game, modified, point def move_up(game): game = vertical(game) game, modified, point = move_left(game) game = vertical(game) return game, modified, point def move_down(game): game = vertical(game) game, modified, point = move_right(game) game = vertical(game) return game, modified, point WIDTH = ugfx.width() HEIGHT = ugfx.height() def action_exit(pushed): if (pushed): # to turn off all the leds at once badge.led(6,0,0,0) # go back to the home screen system.home() def check_game_over(board): global game_over_message_shown if game_over_message_shown or not game_started: return True over, win = is_over(board) if win or over: ugfx.clear() ugfx.flush() if win: easydraw.msg("Congratulations!", "You won!", True) elif over: easydraw.msg("Maybe next time!", "You lose!", True) easydraw.msg("Your points: {}".format(points)) easydraw.msg("Your highest number: {}".format(highest_number(board))) game_over_message_shown = True return True def render_game(board): ugfx.clear() for row in range(BOARD_SIZE): for col in range(BOARD_SIZE): x = int(WIDTH/BOARD_SIZE*col) y = int(HEIGHT/BOARD_SIZE*row) w = int(WIDTH/BOARD_SIZE) h = int(HEIGHT/BOARD_SIZE) if board[row][col] == 0: ugfx.box(x,y,w,h, 0x0) else: ugfx.area(x,y,w,h,0x0) ugfx.string(x, y, str(board[row][col]), "Roboto_Regular12", 0xFFFFFF) ugfx.flush() def action_left(pushed): global board global points if not pushed: return if check_game_over(board): return board, modified, point = move_left(board) points += point if modified: render_game(board) def action_right(pushed): global board global points if not pushed: return if check_game_over(board): return board, modified, point = move_right(board) points += point if modified: render_game(board) def action_up(pushed): global board global points if not pushed: return if check_game_over(board): return board, modified, point = move_up(board) points += point if modified: render_game(board) def action_down(pushed): global board global points if not pushed: return if check_game_over(board): return board, modified, point = move_down(board) points += point if modified: render_game(board) def action_start(pushed): global game_started if not game_started: render_game(board) game_started = True elif game_over_message_shown: system.reboot() board = create_game() points = 0 game_over_message_shown = False game_started = False def main(): easydraw.msg("Use your arrow keys to move the tiles.", "Introduction") time.sleep(2) easydraw.msg("When two tiles with the same number touch, they merge into one!") ugfx.input_init() ugfx.input_attach(ugfx.BTN_B, action_exit) ugfx.input_attach(ugfx.BTN_A, action_start) ugfx.input_attach(ugfx.JOY_UP, action_up) ugfx.input_attach(ugfx.JOY_DOWN, action_down) ugfx.input_attach(ugfx.JOY_LEFT, action_left) ugfx.input_attach(ugfx.JOY_RIGHT, action_right) main()