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
|
import pygame
import random
import os
from splitscreen_duo.command import Command
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
ROTATION_ENABLED = os.getenv("DEVELOPMENT", "") in ["", "0"]
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 draw_game(surface, ball_list, fps, balls, is_vertical, screen_width, screen_height):
surface.fill(BLACK)
for ball in ball_list:
pygame.draw.circle(surface, WHITE, [int(ball.x), int(ball.y)], BALL_SIZE)
fps_text = pygame.font.render(f"FPS: {fps:.1f}", True, WHITE)
ball_text = pygame.font.render(f"Balls: {balls}", True, WHITE)
if is_vertical:
surface.blit(fps_text, (screen_width // 2 - fps_text.get_width() // 2, 10))
surface.blit(ball_text, (screen_width // 2 - ball_text.get_width() // 2, 40))
else:
surface.blit(fps_text, (10, 10))
surface.blit(ball_text, (10, 40))
def main_loop(screen, serial):
clock = pygame.time.Clock()
screen_width = screen.get_width()
screen_height = screen.get_height()
instance = os.getenv("INSTANCE", "Unknown")
rotation_angle = 0
if ROTATION_ENABLED:
if instance == "primary":
rotation_angle = 90 # Right is down
elif instance == "secondary":
rotation_angle = -90 # Left is down
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
fps = clock.get_fps()
if ROTATION_ENABLED and rotation_angle != 0 and not is_vertical:
render_surface = pygame.Surface((screen_width, screen_height))
draw_game(render_surface, ball_list, fps, balls, is_vertical, screen_width, screen_height)
rotated = pygame.transform.rotate(render_surface, rotation_angle)
screen.fill(BLACK)
pos_x = (screen_width - rotated.get_width()) // 2
pos_y = (screen_height - rotated.get_height()) // 2
screen.blit(rotated, (pos_x, pos_y))
else:
draw_game(screen, ball_list, fps, balls, is_vertical, screen_width, screen_height)
pygame.display.flip()
# clock.tick(60)
clock.tick()
# print(f"fps: {clock.get_fps():.2f}, balls: {balls}")
return None
|