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
|
import pygame
import random
from splitscreen_duo.command import Command
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BALL_SIZE = 25
class Ball:
def __init__(self, screen_width, screen_height):
self.x = random.randrange(BALL_SIZE, screen_width - BALL_SIZE)
self.y = random.randrange(BALL_SIZE, screen_height - BALL_SIZE)
self.change_x = random.randrange(-2, 3)
self.change_y = random.randrange(-2, 3)
def main_loop(screen, serial):
clock = pygame.time.Clock()
ball_list = []
balls = 1
ball = Ball(screen.get_width(), screen.get_height())
is_running = True
ball_list.append(ball)
while is_running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return {"command": Command.QUIT.value, "action": None, "value": None}
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
balls = balls * 2
for _ in range(balls - 1):
ball = Ball(screen.get_width(), screen.get_height())
ball_list.append(ball)
elif event.key == pygame.K_ESCAPE:
return None
for ball in ball_list:
ball.x += ball.change_x
ball.y += ball.change_y
if ball.y > screen.get_height() - BALL_SIZE or ball.y < BALL_SIZE:
ball.change_y *= -1
if ball.x > screen.get_width() - BALL_SIZE or ball.x < BALL_SIZE:
ball.change_x *= -1
screen.fill(BLACK)
for ball in ball_list:
pygame.draw.circle(screen, WHITE, [int(ball.x), int(ball.y)], BALL_SIZE)
pygame.display.flip()
# clock.tick(60)
clock.tick()
# print(f"fps: {clock.get_fps():.2f}, balls: {balls}")
return None
|