forked from OpenBMB/ChatDev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
48 lines (48 loc) · 1.57 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'''
2048 Game
'''
import tkinter as tk
from game import Game
class GameApp:
def __init__(self, master):
self.master = master
self.master.title("2048 Game")
self.game = Game()
self.create_widgets()
def create_widgets(self):
self.canvas = tk.Canvas(self.master, width=400, height=400, bg="white")
self.canvas.pack()
self.canvas.bind("<Key>", self.handle_keypress)
self.canvas.focus_set()
def handle_keypress(self, event):
if event.keysym == "Up":
self.game.move_up()
elif event.keysym == "Down":
self.game.move_down()
elif event.keysym == "Left":
self.game.move_left()
elif event.keysym == "Right":
self.game.move_right()
self.update_grid()
if self.game.is_game_over():
self.canvas.unbind("<Key>")
self.canvas.create_text(200, 200, text="Game Over", font=("Arial", 24), fill="red")
def update_grid(self):
self.canvas.delete("all")
for row in range(10):
for col in range(10):
value = self.game.grid[row][col]
x1 = col * 40
y1 = row * 40
x2 = x1 + 40
y2 = y1 + 40
self.canvas.create_rectangle(x1, y1, x2, y2, fill="light gray")
if value != 0:
self.canvas.create_text((x1 + x2) / 2, (y1 + y2) / 2, text=str(value))
self.canvas.update()
def main():
root = tk.Tk()
app = GameApp(root)
root.mainloop()
if __name__ == "__main__":
main()