-
Notifications
You must be signed in to change notification settings - Fork 0
/
multipong.py
97 lines (82 loc) · 2.71 KB
/
multipong.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
"""
Pong with several balls and one paddle at the bottom of the screen
"""
# pylint: disable=no-member
import random
import pygame
import shapes
WINDOW_DIMENSIONS = (1000, 500)
PADDLE_DIMENSIONS = (200, 20)
PADDLE_SPEED = 2
BALL_SPEED = 0.1
BACKGROUND_COLOR = (0, 0, 0)
game_over = False # pylint: disable=invalid-name
pygame.init()
window = pygame.display.set_mode(WINDOW_DIMENSIONS)
font = pygame.font.SysFont('Arial', 64)
def random_ball() -> shapes.Ball:
"""
Creates and returns a new randomly generated ball
:returns: A shapes.Ball object with a random position and color
"""
return shapes.Ball(
(random.randint(20, WINDOW_DIMENSIONS[0]-20),
random.randint(20, WINDOW_DIMENSIONS[1]//2)),
window,
color = (
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255)
)
)
def random_ball_speed() -> list[float, float]:
"""
Generates a random ball speed and direction
:returns: A list of two speeds, the x and y speeds of the ball, that are\
randomly multiplied by either 1 or -1
"""
return [
random.choice((-BALL_SPEED, BALL_SPEED))*random.uniform(0.8, 1.2),
random.choice((-BALL_SPEED, BALL_SPEED))*random.uniform(0.8, 1.2)
]
paddle = shapes.Paddle(
((WINDOW_DIMENSIONS[0]-PADDLE_DIMENSIONS[0])/2, WINDOW_DIMENSIONS[1]-30),
window,
PADDLE_DIMENSIONS
)
balls = [random_ball()]
ball_speeds = [random_ball_speed()]
while not pygame.event.get(pygame.QUIT) and not game_over:
window.fill(BACKGROUND_COLOR)
# Get input, move paddle
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
paddle.move(PADDLE_SPEED, index=0)
if keys[pygame.K_LEFT]:
paddle.move(-PADDLE_SPEED, index=0)
# Check all balls and draw them
for i, ball in enumerate(balls):
if ball.collision_wall(index=0):
ball_speeds[i][0] *= -1
if ball.collision_wall(index=1):
ball_speeds[i][1] *= -1
if ball.position[1] > WINDOW_DIMENSIONS[1]-20:
game_over = True # pylint: disable=invalid-name
break
if ball.collision_paddle([paddle]):
# Change the ball speed
ball_speeds[i][0] *= random.choice((-1, 1))
ball_speeds[i][1] *= -1
# Add a new ball
balls.append(random_ball())
ball_speeds.append(random_ball_speed())
ball.move(ball_speeds[i])
ball.draw()
# Draw paddle and score, update game
paddle.draw()
score = font.render(str(len(balls)), True, (255, 0, 0))
score_rect = score.get_rect()
window.blit(score, score_rect)
pygame.display.update()
print(f'Score: {len(balls)}')
pygame.quit()