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
|
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()
screen_width = screen.get_width()
screen_height = screen.get_height()
global BALL_SIZE
BALL_SIZE = max(15, min(25, min(screen_width, screen_height) // 20))
is_vertical = screen_height > screen_width * 1.5
ball_list = []
balls = 1
ball = Ball(screen_width, screen_height)
is_running = True
font = pygame.font.Font(None, 36)
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
current_width = screen.get_width()
current_height = screen.get_height()
if current_width != screen_width or current_height != screen_height:
screen_width = current_width
screen_height = current_height
BALL_SIZE = max(15, min(25, min(screen_width, screen_height) // 20))
is_vertical = screen_height > screen_width * 1.5
for b in ball_list:
if b.x > screen_width - BALL_SIZE:
b.x = screen_width - BALL_SIZE
if b.y > screen_height - BALL_SIZE:
b.y = screen_height - BALL_SIZE
screen.fill(BLACK)
for ball in ball_list:
pygame.draw.circle(screen, WHITE, [int(ball.x), int(ball.y)], BALL_SIZE)
fps = clock.get_fps()
fps_text = font.render(f"FPS: {fps:.1f}", True, WHITE)
ball_text = font.render(f"Balls: {balls}", True, WHITE)
if is_vertical:
screen.blit(fps_text, (screen_width // 2 - fps_text.get_width() // 2, 10))
screen.blit(ball_text, (screen_width // 2 - ball_text.get_width() // 2, 40))
else:
screen.blit(fps_text, (10, 10))
screen.blit(ball_text, (10, 40))
pygame.display.flip()
# clock.tick(60)
clock.tick()
# print(f"fps: {clock.get_fps():.2f}, balls: {balls}")
return None
|