diff --git a/game2048/display_grid.py b/game2048/display_grid.py index fc47781557d1f70ced54798ee40dfa868ddcc278..4ec8c465b729775787117abebf6530cec06b462c 100644 --- a/game2048/display_grid.py +++ b/game2048/display_grid.py @@ -1,5 +1,7 @@ from tkinter import * +from tkinter.messagebox import * from run_2048 import * +from functools import partial THEMES = {"0": {"name": "Default", 0: "", 2: "2", 4: "4", 8: "8", 16: "16", 32:"32", 64: "64", 128: "128", 256: "256", 512: "512", 1024: "1024", 2048: "2048",4096: "4096", 8192: "8192"}, "1": {"name": "Chemistry", 0: "", 2: "H", 4: "He", 8:"Li", 16: "Be", 32: "B", 64: "C", 128: "N", 256: "O", 512: "F", 1024: "Ne", 2048:"Na", 4096: "Mg", 8192: "Al"}, "2": {"name": "Alphabet", 0: "", 2: "A", 4: "B", 8:"C", 16: "D", 32: "E", 64: "F", 128: "G", 256: "H", 512: "I", 1024: "J", 2048: "K",4096: "L", 8192: "M"}} @@ -16,34 +18,49 @@ TILES_FG_COLOR = {0: "#776e65", 2: "#776e65", 4: "#776e65", 8: "#f9f6f2", \ TILES_FONT = {"Verdana", 40, "bold"} -def main_run(): - - root = Tk() - background = Frame(root) - grid_size = 4 - theme = THEMES["0"] - graphical_grid = [] - labels = [] - game_grid = init_game(grid_size) - +def graphical_update(): + global graphical_grid,game_grid,grid_size,theme for i in range(grid_size): - graphical_grid.append([]) - labels.append([]) for j in range(grid_size): - graphical_grid[i].append(Label(master = background, bd = 1,relief = "ridge", bg = TILES_BG_COLOR[0], text = "",height = 5, width = 10)) - graphical_grid[i][j].grid(column = i, row = j) + graphical_grid[i][j].config(bg = TILES_BG_COLOR[game_grid[i][j]], fg = TILES_FG_COLOR[game_grid[i][j]], text = theme[game_grid[i][j]]) + return - graphical_grid = graphical_update(graphical_grid,game_grid,grid_size,theme) - background.pack() +def grid_update(direction,fen): + global graphical_grid,game_grid,grid_size,theme + if not is_game_over(game_grid): + possibles = list_move_possible(game_grid) + if direction in possibles: + game_grid = move_grid(game_grid,direction) + game_grid = grid_add_new_tile(game_grid) + graphical_update() + elif did_you_win(game_grid): + showinfo("Victoire","Vous avez gagné !") + else: + showinfo("Echec","Vous avez perdu...") - root.mainloop() -def graphical_update(graphical_grid,game_grid,grid_size,theme): +root = Tk() +background = Frame(root) +grid_size = 4 +theme = THEMES["0"] +graphical_grid = [] +labels = [] +game_grid = init_game(grid_size) - for i in range(grid_size): - for j in range(grid_size): - graphical_grid[i][j].config(bg = TILES_BG_COLOR[game_grid[i][j]], fg = TILES_FG_COLOR[game_grid[i][j]], text = theme[game_grid[i][j]]) +for i in range(grid_size): + graphical_grid.append([]) + labels.append([]) + for j in range(grid_size): + graphical_grid[i].append(Label(master = background, bd = 1,relief = "ridge", bg = TILES_BG_COLOR[0], text = "",height = 5, width = 10)) + graphical_grid[i][j].grid(column = j, row = i) + +graphical_update() +background.pack() + +root.bind("<KeyPress-Down>",partial(grid_update,"down")) +root.bind("<KeyPress-Up>",partial(grid_update,"up")) +root.bind("<KeyPress-Right>",partial(grid_update,"right")) +root.bind("<KeyPress-Left>",partial(grid_update,"left")) - return graphical_grid +root.mainloop() -main_run()