-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_run.py
75 lines (60 loc) · 2.07 KB
/
game_run.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import tkinter as tk
import game_logic
game_logic.newboard()
current_game = game_logic.grid
class App(object):
def __init__(self):
self.right = False
self.left = False
self.up = False
self.down = False
def output():
print(20*"\n")
print("***2048***")
print( "Press arrow keys or WASD to move tiles! (Escape key to exit):","\n")
print(current_game,"\n","\n","Score: %d"%game_logic.score,"\n")
def keyPressed(self,event):
if event.keysym == 'Escape':
root.destroy()
elif event.keysym == 'Right'or event.keysym == 'd':
self.right = True
elif event.keysym == 'Left' or event.keysym == 'a':
self.left = True
elif event.keysym == 'Up' or event.keysym == 'w':
self.up = True
elif event.keysym == 'Down' or event.keysym == 's':
self.down = True
def keyReleased(self,event):
if event.keysym == 'Right'or event.keysym == 'd':
self.right = False
elif event.keysym == 'Left' or event.keysym == 'a':
self.left = False
elif event.keysym == 'Up' or event.keysym == 'w':
self.up = False
elif event.keysym == 'Down' or event.keysym == 's':
self.down = False
def task(self):
if self.right:
game_logic.moveright(current_game)
App.output()
elif self.left:
game_logic.moveleft(current_game)
App.output()
elif self.up:
game_logic.moveup(current_game)
App.output()
elif self.down:
game_logic.movedown(current_game)
App.output()
root.after(100,self.task)
application = App()
root = tk.Tk()
print(20*"\n")
print("***2048***")
print( "Press arrow keys or WASD to move tiles! (Escape key to exit):","\n")
print(current_game,"\n","\n","Score: %d"%game_logic.score,"\n")
root.bind_all('<Key>', application.keyPressed)
root.bind_all('<KeyRelease>', application.keyReleased)
root.after(100,application.task)
root.withdraw()
root.mainloop()