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
|
import pygame
import random
from ..command import Command
import logging
import json
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
PADDLE_WIDTH = 100
PADDLE_HEIGHT = 10
BALL_SIZE = 10
BRICK_WIDTH = 80
BRICK_HEIGHT = 30
BRICK_ROWS = 5
BRICK_COLS = 10
SPEED = 5
logger = logging.getLogger(__name__)
class Breakout:
def __init__(self, screen_width, screen_height):
self.screen_width = screen_width
self.screen_height = screen_height
self.reset()
def reset(self):
self.paddle = [
self.screen_width // 2 - PADDLE_WIDTH // 2,
self.screen_height - 40,
]
self.ball = [self.screen_width // 2, self.screen_height // 2]
self.ball_dx = SPEED
self.ball_dy = -SPEED
self.bricks = []
for row in range(BRICK_ROWS):
for col in range(BRICK_COLS):
self.bricks.append(
[
col * (BRICK_WIDTH + 5) + 5,
row * (BRICK_HEIGHT + 5) + 5,
BRICK_WIDTH,
BRICK_HEIGHT,
]
)
self.score = 0
def move_paddle(self, dx):
self.paddle[0] += dx
if self.paddle[0] < 0:
self.paddle[0] = 0
if self.paddle[0] > self.screen_width - PADDLE_WIDTH:
self.paddle[0] = self.screen_width - PADDLE_WIDTH
def move_ball(self):
self.ball[0] += self.ball_dx
self.ball[1] += self.ball_dy
if self.ball[0] <= BALL_SIZE or self.ball[0] >= self.screen_width - BALL_SIZE:
self.ball_dx *= -1
if self.ball[1] <= BALL_SIZE:
self.ball_dy *= -1
paddle_rect = pygame.Rect(
self.paddle[0], self.paddle[1], PADDLE_WIDTH, PADDLE_HEIGHT
)
ball_rect = pygame.Rect(
self.ball[0] - BALL_SIZE,
self.ball[1] - BALL_SIZE,
BALL_SIZE * 2,
BALL_SIZE * 2,
)
if ball_rect.colliderect(paddle_rect) and self.ball_dy > 0:
self.ball_dy *= -1
hit_pos = (self.ball[0] - self.paddle[0]) / PADDLE_WIDTH
self.ball_dx = SPEED * (hit_pos - 0.5) * 2
for brick in self.bricks[:]:
brick_rect = pygame.Rect(brick[0], brick[1], brick[2], brick[3])
if ball_rect.colliderect(brick_rect):
self.bricks.remove(brick)
self.score += 1
self.ball_dy *= -1
logger.debug(f"brick hit, score: {self.score}")
break
def check_game_over(self):
if self.ball[1] >= self.screen_height:
return "lose"
if not self.bricks:
return "win"
return None
def main_loop(screen, serial, instance):
clock = pygame.time.Clock()
breakout = Breakout(screen.get_width(), screen.get_height())
font = pygame.font.Font(None, 36)
is_running = True
opponent_dead = False
my_score = 0
opponent_score = 0
waiting = False
score_display_time = 0
while is_running:
if waiting:
screen.fill(BLACK)
text = font.render("waiting for opponent ...", True, WHITE)
screen.blit(
text,
(
screen.get_width() // 2 - text.get_width() // 2,
screen.get_height() // 2,
),
)
pygame.display.flip()
if serial.in_waiting() > 0:
data = serial.readline().decode("utf-8").strip()
message = json.loads(data)
if message.get("command") == Command.SCORE.value:
opponent_score = message.get("value", 0)
waiting = False
score_display_time = pygame.time.get_ticks()
elif message.get("command") == Command.QUIT.value:
return {
"command": Command.QUIT.value,
"action": None,
"value": None,
}
elif score_display_time:
screen.fill(BLACK)
my_text = font.render(f"Your Score: {my_score}", True, WHITE)
opp_text = font.render(
f"Your Opponent's Score: {opponent_score}", True, WHITE
)
screen.blit(
my_text,
(
screen.get_width() // 2 - my_text.get_width() // 2,
screen.get_height() // 2 - 20,
),
)
screen.blit(
opp_text,
(
screen.get_width() // 2 - opp_text.get_width() // 2,
screen.get_height() // 2 + 20,
),
)
pygame.display.flip()
if pygame.time.get_ticks() - score_display_time > 3000:
return None
else:
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_LEFT:
breakout.move_paddle(-20)
elif event.key == pygame.K_RIGHT:
breakout.move_paddle(20)
elif event.key == pygame.K_ESCAPE:
return None
breakout.paddle[0] = pygame.mouse.get_pos()[0] - PADDLE_WIDTH // 2
breakout.move_paddle(0)
if serial.in_waiting() > 0:
data = serial.readline().decode("utf-8").strip()
message = json.loads(data)
if message.get("command") == Command.SCORE.value:
opponent_dead = True
opponent_score = message.get("value", 0)
breakout.move_ball()
game_over = breakout.check_game_over()
if game_over:
my_score = breakout.score
serial.write(
json.dumps(
{
"command": Command.SCORE.value,
"action": None,
"value": my_score,
}
).encode("utf-8")
)
if opponent_dead:
score_display_time = pygame.time.get_ticks()
else:
waiting = True
continue
screen.fill(BLACK)
pygame.draw.rect(
screen,
BLUE,
[breakout.paddle[0], breakout.paddle[1], PADDLE_WIDTH, PADDLE_HEIGHT],
)
pygame.draw.circle(
screen, WHITE, [int(breakout.ball[0]), int(breakout.ball[1])], BALL_SIZE
)
for brick in breakout.bricks:
pygame.draw.rect(screen, RED, brick)
score_text = font.render(f"Score: {breakout.score}", True, WHITE)
screen.blit(score_text, (10, 10))
pygame.display.flip()
clock.tick(60)
return None
|