-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPong.py
264 lines (220 loc) · 7.4 KB
/
Pong.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import tkinter as tk
import random
import math
import time
Play = False
Restart = False
A, B = 0, 0 # Score
ScoreReset = False
width = 1200
height = 650
Latency = 30
Speed = 10
SpeedLimit = 50
directions = [-math.pi / 4, math.pi / 4, 3 * math.pi / 4, -3 * math.pi / 4]
direction = random.choice(directions)
Y_Speed = -Speed * math.sin(direction)
X_Speed = Speed * math.cos(direction)
TimeL = 0
TimeR = 0
HitTime = 0
UpDownMovel = 0
UpDownMoveR = 0
def PlayPause():
global Play
if Play:
Play = False
else:
Play = True
window.after(0, moving)
def playpause(event):
global Play
if Play:
Play = False
else:
Play = True
window.after(0, moving)
def restart():
global Restart, ScoreReset
if not Restart:
Restart = True
ScoreReset = True
def reset_canvas():
global Restart, ScoreReset, A, B
global direction, Y_Speed, X_Speed
global canvas, line, ball, leftRect, rightRect, Score
global Play
if Restart:
if ScoreReset:
A, B = 0, 0
canvas.delete('all')
line = canvas.create_line(width // 2, 40, width // 2, height, dash=(50, 50), fill="white")
ball = canvas.create_oval(0.5 * width - 15, 0.5 * height - 15, 0.5 * width + 15, 0.5 * height + 15,
fill='#f75a4f')
leftRect = canvas.create_rectangle(5, 0.5 * height - 100, 20, 0.5 * height + 100, fill="white")
rightRect = canvas.create_rectangle(width - 5, 0.5 * height - 100, width - 20, 0.5 * height + 100,
fill="white")
Score = canvas.create_text(width // 2, 20, fill="white", font=("Arial", "20"), text="0 - 0")
canvas.pack()
direction = random.choice(directions)
Y_Speed = -Speed * math.sin(direction)
X_Speed = Speed * math.cos(direction)
Restart = False
ScoreReset = False
Play = False
window.after(0, moving)
def rUp(event):
global TimeR, UpDownMoveR
TimeR = time.time()
# print("Right Move at:" + str(TimeR))
UpDownMoveR = 1
x1r, y1r, x2r, y2r = canvas.coords(rightRect)
if y1r - 10 > 0:
canvas.move(rightRect, 0, -30)
def rDown(event):
global TimeR, UpDownMoveR
TimeR = time.time()
# print("Right Move at:" + str(TimeR))
UpDownMoveR = -1
global height
x1r, y1r, x2r, y2r = canvas.coords(rightRect)
if y2r + 10 < height:
canvas.move(rightRect, 0, 30)
def lUp(event):
global TimeL, UpDownMovel
UpDownMovel = 1
TimeL = time.time()
# print("Left Move at:" + str(TimeL))
x1l, y1l, x2l, y2l = canvas.coords(leftRect)
if y1l - 10 > 0:
canvas.move(leftRect, 0, -30)
def lDown(event):
global TimeL, UpDownMovel
TimeL = time.time()
# print("Left Move at:" + str(TimeL))
UpDownMovel = -1
global height
x1l, y1l, x2l, y2l = canvas.coords(leftRect)
if y2l + 10 < height:
canvas.move(leftRect, 0, 30)
def UpDown_collision():
global height
global Y_Speed
global width
global ball
x1, y1, x2, y2 = canvas.coords(ball)
if y1 + Y_Speed < 0 or y2 + Y_Speed > height:
Y_Speed *= -1
def LeftRight_collisions():
global width
global X_Speed, Y_Speed
global width, HitTime
global ball, leftRect, rightRect
x1, y1, x2, y2 = canvas.coords(ball)
x1l, y1l, x2l, y2l = canvas.coords(leftRect)
x1r, y1r, x2r, y2r = canvas.coords(rightRect)
test = X_Speed, Y_Speed
if x1 + X_Speed < x2l and y1l < y2 + Y_Speed < y2l:
HitTime = time.time()
SpecialMovesL()
if abs(X_Speed) < SpeedLimit:
X_Speed *= -1.1
else:
X_Speed *= -1
'''if random.randint(1, 10) == 5:
SpeedSquared = X_Speed ** 2 + Y_Speed ** 2
i = random.uniform(-math.pi/6, math.pi/6)
X_Speed *= math.cos(i)
Y_Speed *= math.sqrt(SpeedSquared - X_Speed ** 2)'''
# print(X_Speed / test[0], Y_Speed / test[1])
elif x1 + X_Speed > x2r - 45 and y1r < y2 + Y_Speed < y2r:
HitTime = time.time()
SpecialMovesR()
if abs(X_Speed) < SpeedLimit:
X_Speed *= -1.1
else:
X_Speed *= -1
'''if random.randint(1, 10) == 5:
SpeedSquared = X_Speed**2 + Y_Speed**2
i = -random.uniform(-math.pi/6, math.pi/6)
X_Speed *= math.cos(i)
Y_Speed *= math.sqrt(SpeedSquared - X_Speed**2)'''
# print(X_Speed / test[0], Y_Speed / test[1])
def autoReset():
global ball, Score, width, Restart, A, B
x1, y1, x2, y2 = canvas.coords(ball)
if x1 < -20:
time.sleep(1)
Restart = True
reset_canvas()
A += 1
Score = canvas.itemconfig(Score, text=str(A) + " - " + str(B))
canvas.delete(Score)
canvas.pack()
elif x2 > width + 20:
time.sleep(1)
Restart = True
reset_canvas()
B += 1
Score = canvas.itemconfig(Score, text=str(A) + " - " + str(B))
canvas.delete(Score)
canvas.pack()
def SpecialMovesL():
global Y_Speed, X_Speed
# print("HitTime " + str(HitTime))
# print("hit - TimeL "+str(HitTime - TimeL))
if HitTime - TimeL < 0.1:
if Y_Speed * UpDownMovel < 0:
X_Speed *= 1.1
elif Y_Speed * UpDownMovel > 0:
if random.randint(1, 5) == 3:
X_Speed *= 1.1
Y_Speed *= -1
else:
X_Speed *= 0.9
def SpecialMovesR():
global Y_Speed, X_Speed
# print("HitTime " + str(HitTime))
# print("hit - TimeR "+str(HitTime - TimeR))
if HitTime - TimeR < 0.1:
if Y_Speed * UpDownMovel > 0:
X_Speed *= 1.1
elif Y_Speed * UpDownMovel < 0:
if random.randint(1, 5) == 3:
X_Speed *= 1.1
Y_Speed *= -1
else:
X_Speed *= 0.9
def moving():
global ball
global X_Speed
global Y_Speed, Play
if Restart:
reset_canvas()
if Play:
UpDown_collision()
LeftRight_collisions()
canvas.move(ball, X_Speed, Y_Speed)
autoReset()
window.after(Latency, moving)
window = tk.Tk()
canvas = tk.Canvas(window, bg="light blue", width=width, height=height)
line = canvas.create_line(width // 2, 40, width // 2, height, dash=(50, 50), fill="white")
ball = canvas.create_oval(0.5 * width - 15, 0.5 * height - 15, 0.5 * width + 15, 0.5 * height + 15, fill='#f75a4f')
leftRect = canvas.create_rectangle(5, 0.5 * height - 100, 20, 0.5 * height + 100, fill="white")
rightRect = canvas.create_rectangle(width - 5, 0.5 * height - 100, width - 20, 0.5 * height + 100, fill="white")
Score = canvas.create_text(width // 2, 20, fill="white", font=("Arial", "20"), text=str(A) + " - " + str(B))
canvas.pack()
button1 = tk.Button(window, text='Quit', command=window.destroy)
button2 = tk.Button(window, text='Play/Pause', command=PlayPause)
button2.pack()
button3 = tk.Button(window, text='Reset', command=restart)
button3.pack()
button1.pack()
window.bind("<Up>", rUp)
window.bind("<Down>", rDown)
window.bind("<z>", lUp)
window.bind("<s>", lDown)
window.bind("<space>", playpause)
moving()
window.mainloop()