-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOUNCE.pyw
331 lines (286 loc) · 10.7 KB
/
BOUNCE.pyw
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
""" BOUNCE - game by Piotr Goldys """
import random
import time
import threading
from gui import *
import variables as v
# Clock and level info
class Clock:
def __init__(self, place, color):
self.canvas = place
self.color = color
self.id = place.create_text(850, 100, text=int(v.timer), fill=color,
font=('Comic Sans MS', 80), tags='clock')
self.lvl_info = place.create_text(850, 250,
text='Level: {}'.format(v.lvl),
fill=color, font=('Comic Sans MS',
20))
self.slomo_info = place.create_text(850, 400,
text='Slow motion: {}'.format(
v.slow_timer), fill=color,
font=('Comic Sans MS', 20))
self.slomo_time = place.create_text(850, 460, text=v.slomo_amount,
fill=color,
font=('Comic Sans MS', 25))
# Ball
class Ball:
def __init__(self, place, color):
self.canvas = place
self.color = color
self.id = place.create_oval(0, 0, 20, 20, fill=color, tags='ball')
self.canvas.move(self.id, 340, 600)
self.x = 0
self.y = -3.5 - 0.12 * v.lvl
while abs(
self.x) < v.epsilon: # Prevent ball from going almost
# straight up at the beginning
self.x = random.uniform(-3.5 - 0.12 * v.lvl, 3.5 + 0.12 * v.lvl)
# Bounce the ball if it hits end of the screen
def border(self):
# pos[0] - left
# pos[1] - up
# pos[2] - right
# pos[3] - down
pos = self.canvas.coords(self.id)
if pos[0] <= 1 or pos[0] >= 680:
self.x = self.x * -1
elif pos[1] <= 1:
self.y = self.y * -1
elif pos[1] >= 685:
v.bottom = True
# Move the ball
def move_ball(self):
self.canvas.move(self.id, self.x, self.y)
# Paddle
class Paddle:
def __init__(self, place, color):
self.canvas = place
self.color = color
self.id = place.create_rectangle(250 + 1.6 * v.lvl, 702,
450 - 1.6 * v.lvl,
682, fill=color, tags='paddle')
self.x = 0
# Move the paddle
def move_paddle(self):
# pos[0] - left
# pos[1] - up
# pos[2] - right
# pos[3] - down
paddle_pos = self.canvas.coords(self.id)
self.canvas.move(self.id, self.x, 0)
if paddle_pos[0] <= 3:
self.x = 3 - (v.lvl * 0.01)
self.canvas.move(self.id, 3 - (v.lvl * 0.01), 0)
elif paddle_pos[2] >= 700:
self.x = -3 + (v.lvl * 0.01)
self.canvas.move(self.id, -3 + (v.lvl * 0.01), 0)
# Move paddle while pressing <Left>
def move_left(self, event):
self.x = -3 + (v.lvl * 0.01)
# Move paddle while pressing <Right>
def move_right(self, event):
self.x = 3 - (v.lvl * 0.01)
# Binding functions to the arrow keys
def bindings():
paddle.canvas.bind_all('<Right>', paddle.move_right)
paddle.canvas.bind_all('<Left>', paddle.move_left)
paddle.canvas.bind_all('<d>', paddle.move_right)
paddle.canvas.bind_all('<a>', paddle.move_left)
button1.bind('<Button-1>', show_help)
paddle.canvas.bind_all('<space>', pause)
paddle.canvas.bind_all('<Control_L>', slo_mo)
paddle.canvas.bind_all('<Control_R>', slo_mo)
# Show help window
def show_help(event):
help_window = Toplevel()
help_window.title("Help")
help_window.wm_resizable(0, 0)
help_window.wm_attributes("-topmost", 1)
canvas2 = Canvas(help_window, width=500, height=365, highlightthickness=5,
highlightbackground='green', background='white')
canvas2.pack()
canvas2.create_text(20, 20, anchor=NW, fill='black', font=('Courier', 11),
width=500,
text='Game difficulty will be adjusted by '
'manipulating\n1) Ball speed\n2) Paddle '
'speed\n3) '
'Paddle size\n4) '
'Game speed itself')
canvas2.create_text(20, 140, anchor=NW, fill='black',
font=('Courier bold', 15), width=500,
text='- Use <A/D> or <Left/Right> to control the '
'paddle\n- Press <Spacebar> to pause the game\n'
'- Press <Ctrl> to activate slow motion\nYou '
'can save your slow motions and use them '
'later!')
canvas2.create_image(-20, 260, image=pic1, anchor=NW)
canvas2.create_image(160, 260, image=pic2, anchor=NW)
canvas2.create_image(340, 260, image=pic3, anchor=NW)
# Pause the game when help window is active
if v.run:
pause('<space>')
# Pause / unpause the game
def pause(event):
if v.run:
v.run = False
canvas.create_text(350, 120, text='PAUSED', fill='red',
font=('Comic Sans MS', 60), tags='text')
elif not v.run:
v.run = True
canvas.delete('text')
game_loop()
# Bounce the ball when it hits the paddle
def hit_paddle():
# pos[0] - left
# pos[1] - up
# pos[2] - right
# pos[3] - down
ball_pos = ball.canvas.coords(ball.id)
paddle_pos = paddle.canvas.coords(paddle.id)
if ball_pos[3] >= paddle_pos[1] and \
((paddle_pos[0] <= ball_pos[2] <= paddle_pos[2]) or
(paddle_pos[2] >= ball_pos[0] >= paddle_pos[0])):
if 688 <= ball_pos[3]:
ball.x += 0.5 * paddle.x
else:
ball.y = ball.y * -1
# Change ball's direction slightly when it hits flank of the paddle
if ball_pos[2] < (
paddle_pos[0] + 0.25 * (paddle_pos[2] - paddle_pos[0])):
ball.x -= 0.75
elif ball_pos[0] > (
paddle_pos[0] + 0.75 * (paddle_pos[2] - paddle_pos[0])):
ball.x += 0.75
# Change ball's direction slightly according to the movement of
# the paddle
if paddle.x < 0:
ball.x -= 0.5
elif paddle.x > 0:
ball.x += 0.5
# Change ball's direction even more when it hits the very edge
# of the paddle
if ball_pos[0] <= paddle_pos[0] <= ball_pos[2]:
ball.x -= 0.75
ball.y /= 1.2
elif ball_pos[0] <= paddle_pos[2] <= ball_pos[2]:
ball.x += 0.75
ball.y /= 1.2
# Ball hits the bottom of the screen.
def hit_bottom():
if v.bottom:
v.clock_run = False
canvas.delete('text')
canvas.create_text(350, 300, text='YOU MISSED', fill='black',
font=('Comic Sans MS', 50), tags='text')
tk.update()
time.sleep(1)
v.death_counter += 1
if v.death_counter == 4:
v.lvl -= 1
restart()
# Restarts the game, resets objects, etc.
def restart():
v.death_counter = 0
v.timer = v.default_timer
v.clock_run = True
canvas.delete('text')
ball.canvas.coords(ball.id, (330, 590, 350, 610))
ball.x = 0
paddle.x = 0
ball.y = -3.5 - 0.12 * v.lvl
while abs(
ball.x) < v.epsilon: # Prevent ball from going almost straight up
# at the beginning
ball.x = random.uniform(-3.5 - 0.12 * v.lvl, 3.5 + 0.12 * v.lvl)
tk.update()
v.bottom = False
paddle.canvas.coords(paddle.id,
(250 + 1.6 * v.lvl, 702, 450 - 1.6 * v.lvl, 682))
# Starts next level
def next_lvl():
if v.timer <= 0 and not v.bottom:
v.timer = 0
canvas.delete('text')
canvas.create_text(350, 300, text='GOOD JOB', fill='green',
font=('Comic Sans MS', 50), tags='text')
tk.update()
v.clock_run = False
time.sleep(1)
v.death_counter += 1
if v.death_counter == 3:
v.lvl += 1
restart()
# Player wants to use slow motion.
def slo_mo(event):
if v.slomo_amount > 0 and v.timer > 0 and not v.bottom:
v.slomo = True
# Activates slow motion.
def slo_motion():
if v.slomo:
if v.slow_timer2 > 0 and v.timer >= 0 and not v.bottom:
canvas.delete('text')
canvas.create_text(350, 500, text=v.slow_timer2, fill='PaleGreen3',
font=('Comic Sans MS', 30), tags='text')
time.sleep(0.03)
slo_mo('<Control_L>')
v.slow_timer2 -= 1
else:
v.slow_timer = v.slow_timer_default
v.slow_timer2 = v.slow_timer2_default
v.slomo = False
v.slomo_amount -= 1
canvas.delete('text')
# Handle all time-related things in background
def clock_update():
time.sleep(1)
if v.run and v.clock_run:
# Slow down time when in slow motion
if v.slomo:
v.timer -= 0.3
elif not v.bottom:
v.timer -= 1
if v.timer > 0 and not v.bottom and not v.slomo:
if v.slow_timer > 0:
v.slow_timer -= 1
else:
v.slomo_amount += 1
v.slow_timer = v.slow_timer_default
clock_update()
# Run clock_update() in background
t1 = threading.Thread(target=clock_update)
t1.start()
# Create object instances
ball = Ball(canvas, 'red')
paddle = Paddle(canvas, 'blue')
clock = Clock(canvas, 'black')
# Game loop
def game_loop():
while v.run:
tk.update()
ball.move_ball()
ball.border()
hit_paddle()
paddle.move_paddle()
bindings()
hit_bottom()
time.sleep(0.005 - 0.00015 * v.lvl)
slo_motion()
canvas.itemconfigure(clock.id, text=int(v.timer))
canvas.itemconfigure(clock.lvl_info, text='Level: {}'.format(v.lvl))
canvas.itemconfigure(clock.slomo_time, text=v.slomo_amount,
justify=CENTER)
canvas.itemconfigure(clock.slomo_info,
text='Slow motion: {}'.format(v.slow_timer))
next_lvl()
if v.slomo_amount > 0:
canvas.itemconfigure(clock.slomo_time, fill='purple',
font=('Comic Sans MS', 45))
canvas.config(highlightbackground='purple')
else:
canvas.itemconfigure(clock.slomo_time, fill=clock.color,
font=('Comic Sans MS', 25))
canvas.config(highlightbackground='green')
tk.update()
tk.update_idletasks()
game_loop()
tk.mainloop()