-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pygame Hoops.py
534 lines (485 loc) · 22.2 KB
/
Pygame Hoops.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
from colors import *
import os
import sys
import pygame
import math
import time
import random
from pygame.locals import *
from enum import Enum
import pygame_menu
from typing import Tuple, Any, Optional, List
# Initialize Global Variables
WIDTH = 1500
HEIGHT = 900
# Global constants here
BLACK = (255, 255, 255)
BLACK = (0, 0, 0)
GREY = (50, 50, 50)
RED = (207, 0, 0)
class Display(Enum):
SPLASH = 1
PLAY = 2
RESULTS = 3
# Gravity in m/s²
class Celestial(Enum):
MERCURY = 3.7
VENUS = 8.87
EARTH = 9.807
MARS = 3.721
JUPITER = 24.79
SATURN = 11.44
NEPTUNE = 11.15
MOON = 1.62
SUN = 274
class Hoops:
def __init__(self):
self.buffer = 100
self.ball_size = 70
self.highdamp = 0.8
self.lowdamp = 0.95
self.hoop_back = pygame.image.load(r'hoop_back.png').convert_alpha()
self.hoop_front = pygame.image.load(r'hoop_front.png').convert_alpha()
self.glassboard = pygame.image.load(r'glassboard.png').convert_alpha()
self.gear = pygame.image.load(r'gear.png').convert_alpha()
self.debug = pygame.image.load(r'debug.png').convert_alpha()
self.player = pygame.image.load(
r'players/player_'+str(random.randint(1, 7))+'.png').convert_alpha()
self.font = pygame.font.Font('font/CoffeeTin.ttf', 150)
self.font2 = pygame.font.Font('font/IndianPoker.ttf', 75)
self.font2.set_bold(True)
self.balls = []
for i in range(1, 36):
self.balls.append(pygame.image.load(
r'balls/ball_'+str(i)+'.png').convert_alpha())
self.shoot = False
self.bounces = 0
self.celestialBody: Celestial = Celestial.EARTH
self.score = 0
self.debugEnabled = False
self.startText = self.font2.render("Welcome to Hoops!", 1, (yellow))
self.startSize = self.font2.size("Welcome to Hoops!")
# Initialize last position on grid user clicked
self.lastPos = (0, 0)
self.menu = pygame_menu.Menu(
height=HEIGHT * 0.6,
theme=pygame_menu.themes.THEME_DEFAULT.copy(),
title='Select Celestial Body',
width=WIDTH * 0.6
)
self.menu.add.selector(
'', [('MERCURY', 'MERCURY')], onchange=self.change_celestial, selector_id='MERCURY')
self.menu.add.selector(
'', [('VENUS', 'VENUS')], onchange=self.change_celestial, selector_id='VENUS')
self.menu.add.selector(
'', [('EARTH', 'EARTH')], onchange=self.change_celestial, selector_id='EARTH')
self.menu.add.selector(
'', [('MARS', 'MARS')], onchange=self.change_celestial, selector_id='MARS')
self.menu.add.selector(
'', [('JUPITER', 'JUPITER')], onchange=self.change_celestial, selector_id='JUPITER')
self.menu.add.selector(
'', [('SATURN', 'SATURN')], onchange=self.change_celestial, selector_id='SATURN')
self.menu.add.selector(
'', [('NEPTUNE', 'NEPTUNE')], onchange=self.change_celestial, selector_id='NEPTUNE')
self.menu.add.selector(
'', [('MOON', 'MOON')], onchange=self.change_celestial, selector_id='MOON')
self.menu.add.selector(
'', [('SUN', 'SUN')], onchange=self.change_celestial, selector_id='SUN')
self.menu.disable()
self.menu.full_reset()
self.start_up_init()
def change_celestial(self, value: Tuple[Any, int], body: str) -> None:
selected, index = value
print(f'Selected body: {body} at index {index}')
# Set the body
if body == Celestial.MERCURY.name:
self.celestialBody = Celestial.MERCURY
elif body == Celestial.VENUS.name:
self.celestialBody = Celestial.VENUS
elif body == Celestial.EARTH.name:
self.celestialBody = Celestial.EARTH
elif body == Celestial.MARS.name:
self.celestialBody = Celestial.MARS
elif body == Celestial.JUPITER.name:
self.celestialBody = Celestial.JUPITER
elif body == Celestial.SATURN.name:
self.celestialBody = Celestial.SATURN
elif body == Celestial.NEPTUNE.name:
self.celestialBody = Celestial.NEPTUNE
elif body == Celestial.MOON.name:
self.celestialBody = Celestial.MOON
elif body == Celestial.SUN.name:
self.celestialBody = Celestial.SUN
self.menu.disable()
self.menu.full_reset()
self.start_up_init()
self.reset_field(ball_pos=self.starting_ball_pos)
def start_up_init(self):
self.bx = (400/1400)*WIDTH
self.by = HEIGHT-400
self.floor_height = (80/800)*HEIGHT
self.gravity = self.celestialBody.value
print("Setting Celestial Body to", self.celestialBody.name,
"Gtavity", self.celestialBody.value)
# Starting ball position
self.starting_ball_pos = (
self.bx+round(self.ball_size/2), self.by+round(self.ball_size/2))
self.background = pygame.image.load(
r'backgrounds/'+self.celestialBody.name+'.png').convert_alpha()
self.background = pygame.transform.scale(
self.background, (WIDTH, HEIGHT))
# intitialize items for the startup section of the game
self.hx = WIDTH-140
self.hy = 250
self.skip_next_rim_check = False
self.skip_next_goal_check = False
self.front_rim = Rect(self.hx-2, self.hy, 10, 25)
self.back_glassboard = Rect(WIDTH-55, self.hy-60, 10, 120)
self.swish = True
self.startLoc = (WIDTH/2 - self.startSize[0]/2, self.buffer)
self.startButton = self.font2.render(" Start ", 1, BLACK)
self.startButtonSize = self.font2.size(" Start ")
self.startButtonLoc = (
WIDTH/2 - self.startButtonSize[0]/2, HEIGHT/3 - self.startButtonSize[1]/2)
self.startButtonRect = pygame.Rect(
self.startButtonLoc, self.startButtonSize)
self.startButtonRectOutline = pygame.Rect(
self.startButtonLoc, self.startButtonSize)
self.gearButtonSize = (self.gear.get_width(), self.gear.get_height())
self.gearButtonLoc = (WIDTH - 40, 5)
self.gearButtonRect = pygame.Rect(
self.gearButtonLoc, self.gearButtonSize)
self.debugButtonSize = (self.debug.get_width(),
self.debug.get_height())
self.debugButtonLoc = (WIDTH - 80, 5)
self.debugButtonRect = pygame.Rect(
self.debugButtonLoc, self.debugButtonSize)
self.state = 0
def main(self):
if self.state == 0:
self.show_splash_screen()
elif self.state == 1:
self.play()
# elif self.state == 2:
# self.results()
# Resets the field given the ball position
def reset_field(self, ball_pos=(0, 0), degree=0, display=Display.SPLASH):
degree = 0 if degree < 0 or degree > 35 else degree
SCREEN.blit(self.background, (0, 0))
pygame.Surface.set_colorkey(self.glassboard, [0, 0, 0])
SCREEN.blit(self.glassboard, (self.hx+50, self.hy-100))
# Back Side of Hoop
pygame.Surface.set_colorkey(self.hoop_back, [0, 0, 0])
SCREEN.blit(self.hoop_back, (self.hx+2, self.hy+12))
# Ball Itself
if display == Display.PLAY:
pygame.Surface.set_colorkey(self.balls[degree], [0, 0, 0])
SCREEN.blit(self.balls[degree], (ball_pos[0], ball_pos[1]))
# Front of the Hoop
pygame.Surface.set_colorkey(self.hoop_front, [0, 0, 0])
SCREEN.blit(self.hoop_front, (self.hx, self.hy))
# Score Bar
score_bar = pygame.Surface((WIDTH, 40), pygame.SRCALPHA)
score_bar.fill((255, 255, 255, 32))
tin = pygame.font.Font('font/IndianPoker.ttf', 25)
score_bar.blit(tin.render(
self.celestialBody.name, False, white), (10, 5))
score_bar.blit(tin.render(
"g:" + str(self.celestialBody.value), False, white), (200, 5))
score_bar.blit(tin.render("Score: "+str(self.score),
False, white), (WIDTH/2-50, 5))
score_bar.blit(self.gear, self.gearButtonLoc)
score_bar.blit(self.debug, self.debugButtonLoc)
SCREEN.blit(score_bar, (0, 0))
# Score
if display == Display.SPLASH:
# draw welcome text
SCREEN.blit(self.startText, self.startLoc)
pygame.Surface.set_colorkey(self.player, [0, 0, 0])
SCREEN.blit(self.player, ((850/1400)*WIDTH, HEIGHT-580))
# draw the start button
pygame.draw.rect(SCREEN, RED, self.startButtonRect)
pygame.draw.rect(SCREEN, BLACK, self.startButtonRectOutline, 2)
SCREEN.blit(self.startButton, self.startButtonLoc)
if self.debugEnabled:
# Rim Front edge
pygame.draw.rect(SCREEN, (green), self.front_rim)
# Rim Back Edge
pygame.draw.rect(SCREEN, (green), self.back_glassboard)
# Rim
pygame.draw.rect(SCREEN, (blue), Rect(
self.hx+5, self.hy+10, 70, 50))
# Rim1
pygame.draw.rect(SCREEN, (yellow), Rect(
self.hx, self.hy+25, 5, 35))
def show_score(self, msg, x, y, color, size):
tin = pygame.font.Font('font/IndianPoker.ttf', size)
msgobj = tin.render(msg, False, color)
SCREEN.blit(msgobj, (x, y))
def get_path(self, ball_pos, vx, vy):
path = []
velocity = []
vfx = 0
vfy = 0
height_gained = 0
t = .3
while True:
x = vx * t
y = vy * t + (self.gravity * t * t * 0.5) + ball_pos[1]
#print("vy", vy, "x", x + ball_pos[0], "\t", "y", y, "\t", "t", t, "\t")
t += 0.3
height_gained = y-ball_pos[1]
vertex_x = ball_pos[0] - vx*(vy/self.gravity)
vertex_y = ball_pos[1]-((0.5*vy*vy)/self.gravity)
vfx = vx
vfy = math.sqrt(abs((vy*vy)+(2*self.gravity*height_gained)))
if vy != 0:
vfy = vfy * (vy/abs(vy))
if vy < 0:
# Ball moving upward so will go through vertex
if (x + ball_pos[0])*(vx/abs(vx)) > vertex_x*(vx/abs(vx)):
vfy = -vfy
path.append((x+ball_pos[0], y))
velocity.append((vfx, vfy))
if abs(vx) < 0.2 or abs(vy) < 1:
print("slowball")
break
elif (x + ball_pos[0] + self.ball_size/2 >= WIDTH) and vx >= 0:
# Hitting Right Wall
vfx = -vx*self.highdamp
if x + ball_pos[0] > vertex_x:
vfy = math.sqrt((vy*vy)+(2*self.gravity*height_gained))*self.lowdamp
else:
vfy = -math.sqrt((vy*vy)+(2*self.gravity * height_gained))*self.lowdamp
break
elif (x + ball_pos[0] - self.ball_size/2 <= 0) and vx < 0:
# Hitting Left Wall
vfx = -vx*self.highdamp
if x + ball_pos[0] < vertex_x:
vfy = math.sqrt((vy*vy)+(2*self.gravity*height_gained))*self.lowdamp
else:
vfy = -math.sqrt((vy*vy)+(2*self.gravity * height_gained))*self.lowdamp
break
elif (y + self.ball_size/2 >= HEIGHT - self.floor_height):
# Hitting Floor
vfx = vx*self.lowdamp
vfy = -math.sqrt((vy*vy)+(2*self.gravity * height_gained))*self.highdamp
break
elif (y <= self.ball_size/2) and vy < 0:
# Hitting Ceiling
vfx = vx*self.lowdamp
vfy = math.sqrt((vy*vy)+(2*self.gravity*height_gained))*self.highdamp
break
return path, velocity, (x + ball_pos[0], y), vfx, vfy
def calc_trajectory(self, pos):
# calculate slope
if (self.starting_ball_pos[0]-pos[0]) != 0:
slope = (self.starting_ball_pos[1]-pos[1]) / \
(self.starting_ball_pos[0]-pos[0])
else:
slope = math.inf
# Using distance as a proxy for speed
speed = math.sqrt((self.starting_ball_pos[1]-pos[1])*(self.starting_ball_pos[1]-pos[1]) + (
self.starting_ball_pos[0]-pos[0])*(self.starting_ball_pos[0]-pos[0]))
# tan(θ) = slope, atan returns arc tangent of slope as a numeric value between -PI/2 and PI/2 radians (i.e. ±1.57)
angle = math.atan(slope)
# adjust angle for right two quadrants
if pos[0] > self.starting_ball_pos[0] and pos[1] >= self.starting_ball_pos[1]:
# clicking in bottom right quadrant
angle = angle + (math.pi)
elif pos[0] > self.starting_ball_pos[0] and pos[1] < self.starting_ball_pos[1]:
# clicking in top right quadrant
angle = angle - (math.pi)
return self.get_path(self.starting_ball_pos, speed * math.cos(angle), speed * math.sin(angle))
# Render path
def process_path(self, path, velocity, collision_point, vx, vy):
for i in range(len(path)):
time.sleep(.02)
# point p on the path
p = path[i]
# velocity at point p
v = velocity[i]
if self.debugEnabled:
print("p", p, "\t", "v", v)
degree = round(round(p[0] % (34*3))/3)
self.reset_field((p[0]-round(self.ball_size/2), p[1] - round(self.ball_size/2)), degree=degree, display=Display.PLAY)
pygame.display.update()
rim = pygame.Rect(self.hx, self.hy+4, 95, 40)
rim1 = pygame.Rect(self.hx, self.hy+25, 5, 35)
rim_front_edge = pygame.Rect(self.front_rim)
rim_back_edge = pygame.Rect(self.back_glassboard)
ball_rect = pygame.Rect(p[0] - round(self.ball_size/2) + 10, p[1] - round(
self.ball_size/2) + 10, self.ball_size - 20, self.ball_size - 20)
if p == collision_point:
# bounce the ball off the walls
self.skip_next_rim_check = False
self.skip_next_goal_check = False
self.swish = False
self.bounce_ball(collision_point, vx, vy)
elif rim_front_edge.colliderect(ball_rect) and not self.skip_next_rim_check:
# bounce off the rim edge
if self.debugEnabled:
print("Rim Front Edge", "\t", (p[0], p[1]), "\t", -v[0]*1.1, "\t", v[1]*1.1)
self.skip_next_rim_check = True
self.swish = False
self.bounce_ball((p[0], p[1]), -v[0]*1.1, v[1]*1.1)
break
elif rim_back_edge.colliderect(ball_rect) and not self.skip_next_rim_check:
# bounce off the rim edge
if self.debugEnabled:
print("Rim Back Edge", "\t", (p[0], p[1]), "\t", -v[0]*1.1, "\t", v[1]*1.1)
self.skip_next_rim_check = True
self.swish = False
self.bounce_ball((p[0], p[1]), -v[0]*0.8, v[1]*1.2)
break
elif rim1.colliderect(ball_rect):
self.skip_next_goal_check = True
self.swish = True # reset
continue # skip goal check
elif rim.collidepoint((p[0], p[1])) and not self.skip_next_goal_check:
if v[1] < 0:
print("Ball moving upward in goal")
else:
print("Swish !!") if self.swish else print("Goal !!")
if self.starting_ball_pos[0] < 240:
self.score += 3
else:
self.score += 2
self.skip_next_goal_check = True
# Make the ball fall down after hitting the goal
if self.swish:
self.bounce_ball(
(p[0], p[1]), v[0]/abs(v[0]), abs(v[1]*0.8))
else:
self.bounce_ball(
(p[0], p[1]), v[0]/abs(v[0]), abs(v[1]*0.7))
self.swish = True # reset
time.sleep(0.1)
break
else:
self.swish = True # reset
# Recursive function that bounces the ball off the walls
def bounce_ball(self, start_pos, vx, vy):
self.bounces += 1
path, velocity, collision_point, vx, vy = self.get_path(start_pos, vx, vy)
if abs(vx) < 0.2 or abs(vy) < 1:
print("speed is 0")
self.starting_ball_pos = collision_point
return
else:
self.process_path(path, velocity, collision_point, vx, vy)
def show_splash_screen(self):
global SCREEN, WIDTH, HEIGHT
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.VIDEORESIZE:
SCREEN = pygame.display.set_mode(
(event.w, event.h), pygame.RESIZABLE)
WIDTH = event.w
HEIGHT = event.h
self.start_up_init()
# when the user clicks the start button, change to the playing state
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
mouseRect = pygame.Rect(event.pos, (1, 1))
if mouseRect.colliderect(self.startButtonRect):
self.state = 1
self.play_init()
return
elif mouseRect.colliderect(self.gearButtonRect):
if self.menu.is_enabled():
self.menu.disable()
self.menu.full_reset()
else:
self.menu.enable()
return
elif mouseRect.colliderect(self.debugButtonRect):
self.debugEnabled = not self.debugEnabled
return
# Pass events to main_menu
if self.menu.is_enabled():
self.menu.mainloop(SCREEN, self.reset_field,
disable_loop=False, fps_limit=60)
# self.menu.update(events)
# Main Program -- Start ---
self.reset_field((self.starting_ball_pos[0]-round(self.ball_size/2),
self.starting_ball_pos[1]-round(self.ball_size/2)), display=Display.SPLASH)
pygame.display.flip()
def play_init(self):
# create the new variables
self.round = 0
degree = round(round(self.starting_ball_pos[0] % (34*3))/3)
self.reset_field((self.starting_ball_pos[0]-round(self.ball_size/2), self.starting_ball_pos[1]-round(
self.ball_size/2)), degree=degree, display=Display.PLAY)
def play(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if self.debugEnabled:
print("MOUSEBUTTONDOWN", event.pos)
if event.button == 1:
if self.gearButtonRect.collidepoint(event.pos):
if self.menu.is_enabled():
self.menu.disable()
self.menu.full_reset()
else:
self.menu.enable()
elif self.debugButtonRect.collidepoint(event.pos):
self.debugEnabled = not self.debugEnabled
else:
self.shoot = True
self.lastPos = (event.pos[0], event.pos[1])
path, velocity, collision_point, vx, vy = self.calc_trajectory(
self.lastPos)
for p in path:
pygame.draw.circle(
SCREEN, (white), (p[0], p[1]), 2)
pygame.display.update()
elif event.button == 3:
self.shoot = False
self.starting_ball_pos = event.pos
degree = round(round(self.starting_ball_pos[0] % (34*3))/3)
self.reset_field((self.starting_ball_pos[0]-round(self.ball_size/2), self.starting_ball_pos[1]-round(
self.ball_size/2)), degree=degree, display=Display.PLAY)
elif event.type == pygame.MOUSEMOTION:
if self.shoot:
degree = round(round(self.starting_ball_pos[0] % (34*3))/3)
self.reset_field((self.starting_ball_pos[0]-round(self.ball_size/2), self.starting_ball_pos[1]-round(
self.ball_size/2)), degree=degree, display=Display.PLAY)
self.lastPos = (event.pos[0], event.pos[1])
path, velocity, collision_point, vx, vy = self.calc_trajectory(
self.lastPos)
for p in path:
pygame.draw.circle(SCREEN, (white), (p[0], p[1]), 2)
pygame.display.update()
elif event.type == pygame.MOUSEBUTTONUP:
if self.shoot:
degree = round(round(self.starting_ball_pos[0] % (34*3))/3)
self.reset_field((self.starting_ball_pos[0]-round(self.ball_size/2), self.starting_ball_pos[1]-round(
self.ball_size/2)), degree=degree, display=Display.PLAY)
if event.button == 1:
path, velocity, collision_point, vx, vy = self.calc_trajectory(
self.lastPos)
self.process_path(
path, velocity, collision_point, vx, vy)
self.shoot = False
if self.menu.is_enabled():
self.menu.mainloop(SCREEN, self.reset_field,
disable_loop=False, fps_limit=60)
pygame.display.flip()
#############################################################
if __name__ == "__main__":
os.environ['SDL_VIDEO_CENTERED'] = '1' # center SCREEN
pygame.init()
pygame.display.set_caption("Hoops")
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
Runit = Hoops()
Myclock = pygame.time.Clock()
while 1:
Runit.main()
Myclock.tick(64)