-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.py
185 lines (154 loc) · 7.58 KB
/
move.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from tkinter import *
class Move:
def __init__(self, master=None):
self.master = master
master.geometry("1200x1200")
master.title("Kuboble")
master.config(background="black")
Label(self.master, text="Use arrow keys for GREEN Object", font=('Helvetica 25 bold')).pack(padx=100)
Label(self.master, text="Use W,A,S,D keys for RED Object", font=('Helvetica 25 bold')).pack(padx=100)
# width and height of canvas
self.WIDTH = 600
self.HEIGHT = 600
# moves counter
self.counter = 0
# canvas object to create shape
self.canvas = Canvas(master, width=self.WIDTH, height=self.HEIGHT)
self.canvas.config(background="black")
# creating rectangle
self.greenObj = self.canvas.create_rectangle(0, 0, 200, 200, fill="green")
self.redObj = self.canvas.create_rectangle(200, 0, 400, 200, fill="red")
self.greenWin = self.canvas.create_rectangle(200, 400, 400, 600, outline="green", width=5)
self.redWin = self.canvas.create_rectangle(0, 400, 200, 600, outline="red", width=5)
self.canvas.pack(padx=50, pady=50)
# for motion in negative x direction
def left(self, event):
sideGreenObj = self.canvas.coords(self.greenObj)
sideRedObj = self.canvas.coords(self.redObj)
if str(event.keysym) == "Left":
# for green collision
if (sideGreenObj[0] == sideRedObj[2]) & (sideGreenObj[1] - sideRedObj[1] == 0):
self.canvas.move(self.greenObj, 0, 0)
# for one gap left move
elif (sideGreenObj[0] - sideRedObj[2] == 200) & (sideGreenObj[1] - sideRedObj[1] == 0):
self.canvas.move(self.greenObj, -200, 0)
self.counter += 1
else:
# for normal move
self.canvas.move(self.greenObj, 0 - sideGreenObj[0], 0)
if (0 - sideGreenObj[0]) != 0:
self.counter += 1
elif str(event.keysym) == "a":
# for red collision
if (sideRedObj[0] == sideGreenObj[2]) & (sideRedObj[1] - sideGreenObj[1] == 0):
self.canvas.move(self.redObj, 0, 0)
# for one gap left move
elif (sideRedObj[0] - sideGreenObj[2] == 200) & (sideRedObj[1] - sideGreenObj[1] == 0):
self.canvas.move(self.redObj, -200, 0)
self.counter += 1
else:
# for normal move
self.canvas.move(self.redObj, 0 - sideRedObj[0], 0)
if (0 - sideRedObj[0]) != 0:
self.counter += 1
self.ifWin(sideGreenObj, sideRedObj)
# for motion in positive x direction
def right(self, event):
sideGreenObj = self.canvas.coords(self.greenObj)
sideRedObj = self.canvas.coords(self.redObj)
if str(event.keysym) == "Right":
# for green collision
if (sideGreenObj[2] == sideRedObj[0]) & (sideGreenObj[1] - sideRedObj[1] == 0):
self.canvas.move(self.greenObj, 0, 0)
# for one gap right move
elif (sideGreenObj[2] - sideRedObj[0] == -200) & (sideGreenObj[1] - sideRedObj[1] == 0):
self.canvas.move(self.greenObj, 200, 0)
self.counter += 1
else:
# for normal move
self.canvas.move(self.greenObj, 600 - sideGreenObj[2], 0)
if (600 - sideGreenObj[2]) != 0:
self.counter += 1
elif str(event.keysym) == "d":
# for red collision
if (sideRedObj[2] == sideGreenObj[0]) & (sideRedObj[1] - sideGreenObj[1] == 0):
self.canvas.move(self.greenObj, 0, 0)
# for one gap right move
elif (sideRedObj[2] - sideGreenObj[0] == -200) & (sideRedObj[1] - sideGreenObj[1] == 0):
self.canvas.move(self.redObj, 200, 0)
self.counter += 1
else:
# for normal move
self.canvas.move(self.redObj, 600 - sideRedObj[2], 0)
if (600 - sideRedObj[2]) != 0:
self.counter += 1
self.ifWin(sideGreenObj, sideRedObj)
# for motion in positive y direction
def up(self, event):
sideGreenObj = self.canvas.coords(self.greenObj)
sideRedObj = self.canvas.coords(self.redObj)
if str(event.keysym) == "Up":
# for green collision
if (sideGreenObj[1] == sideRedObj[3]) & (sideGreenObj[2] - sideRedObj[2] == 0):
self.canvas.move(self.greenObj, 0, 0)
# for one gap up move
elif (sideGreenObj[1] - sideRedObj[3] == 200) & (sideGreenObj[2] - sideRedObj[2] == 0):
self.canvas.move(self.greenObj, 0, -200)
self.counter += 1
else:
# for normal move
self.canvas.move(self.greenObj, 0, 0 - sideGreenObj[1])
if (0 - sideGreenObj[1]) != 0:
self.counter += 1
elif str(event.keysym) == "w":
# for red collision
if (sideRedObj[1] == sideGreenObj[3]) & (sideRedObj[2] - sideGreenObj[2] == 0):
self.canvas.move(self.redObj, 0, 0)
# for one gap up move
elif (sideRedObj[1] - sideGreenObj[3] == 200) & (sideRedObj[2] - sideGreenObj[2] == 0):
self.canvas.move(self.redObj, 0, -200)
self.counter += 1
else:
# for normal move
self.canvas.move(self.redObj, 0, 0 - sideRedObj[1])
if (0 - sideGreenObj[0]) != 0:
self.counter += 1
self.ifWin(sideGreenObj, sideRedObj)
# for motion in negative y direction
def down(self, event):
sideGreenObj = self.canvas.coords(self.greenObj)
sideRedObj = self.canvas.coords(self.redObj)
if str(event.keysym) == "Down":
# for green collision
if (sideGreenObj[3] == sideRedObj[1]) & (sideGreenObj[2] - sideRedObj[2] == 0):
self.canvas.move(self.greenObj, 0, 0)
# for one gap down move
elif (sideGreenObj[3] - sideRedObj[1] == -200) & (sideGreenObj[2] - sideRedObj[2] == 0):
self.canvas.move(self.greenObj, 0, 200)
self.counter += 1
else:
# for normal move
self.canvas.move(self.greenObj, 0, 600 - sideGreenObj[3])
if (600 - sideGreenObj[3]) != 0:
self.counter += 1
elif str(event.keysym) == "s":
# for red collision
if (sideRedObj[3] == sideGreenObj[1]) & (sideRedObj[2] - sideGreenObj[2] == 0):
self.canvas.move(self.redObj, 0, 0)
# for one gap down move
elif (sideRedObj[3] - sideGreenObj[1] == -200) & (sideRedObj[2] - sideGreenObj[2] == 0):
self.canvas.move(self.redObj, 0, 200)
self.counter += 1
else:
# for normal move
self.canvas.move(self.redObj, 0, 600 - sideRedObj[3])
if (600 - sideRedObj[3]) != 0:
self.counter += 1
self.ifWin(sideGreenObj, sideRedObj)
# for checking if wins after any move
def ifWin(self, firstObject, secondObject):
print(firstObject)
print(secondObject)
if (self.canvas.coords(self.greenWin) == firstObject) & (self.canvas.coords(self.redWin) == secondObject):
Label(self.master, text="Congratulations", font=('Helvetica 35 bold')).pack(pady=20)
Label(self.master, text="Total Moves = " + str(self.counter), font=('Helvetica 20 bold')).pack()