-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPong.py
309 lines (255 loc) · 8.98 KB
/
Pong.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env python3
# Based on https://python101.readthedocs.io/pl/latest/pygame/pong/#
import pygame
from typing import Type
import skfuzzy as fuzz
import skfuzzy.control as fuzzcontrol
FPS = 30
class Board:
def __init__(self, width: int, height: int):
self.surface = pygame.display.set_mode((width, height), 0, 32)
pygame.display.set_caption("AIFundamentals - PongGame")
def draw(self, *args):
background = (0, 0, 0)
self.surface.fill(background)
for drawable in args:
drawable.draw_on(self.surface)
pygame.display.update()
class Drawable:
def __init__(self, x: int, y: int, width: int, height: int, color=(255, 255, 255)):
self.width = width
self.height = height
self.color = color
self.surface = pygame.Surface(
[width, height], pygame.SRCALPHA, 32
).convert_alpha()
self.rect = self.surface.get_rect(x=x, y=y)
def draw_on(self, surface):
surface.blit(self.surface, self.rect)
class Ball(Drawable):
def __init__(
self,
x: int,
y: int,
radius: int = 20,
color=(255, 10, 0),
speed: int = 3,
):
super(Ball, self).__init__(x, y, radius, radius, color)
pygame.draw.ellipse(self.surface, self.color, [0, 0, self.width, self.height])
self.x_speed = speed
self.y_speed = speed
self.start_speed = speed
self.start_x = x
self.start_y = y
self.start_color = color
self.last_collision = 0
def bounce_y(self):
self.y_speed *= -1
def bounce_x(self):
self.x_speed *= -1
def bounce_y_power(self):
self.color = (
self.color[0],
self.color[1] + 10 if self.color[1] < 255 else self.color[1],
self.color[2],
)
pygame.draw.ellipse(self.surface, self.color, [0, 0, self.width, self.height])
self.x_speed *= 1.1
self.y_speed *= 1.1
self.bounce_y()
def reset(self):
self.rect.x = self.start_x
self.rect.y = self.start_y
self.x_speed = self.start_speed
self.y_speed = self.start_speed
self.color = self.start_color
self.bounce_y()
def move(self, board: Board, *args):
self.rect.x += round(self.x_speed)
self.rect.y += round(self.y_speed)
if self.rect.x < 0 or self.rect.x > (
board.surface.get_width() - self.rect.width
):
self.bounce_x()
if self.rect.y < 0 or self.rect.y > (
board.surface.get_height() - self.rect.height
):
self.reset()
timestamp = pygame.time.get_ticks()
if timestamp - self.last_collision < FPS * 4:
return
for racket in args:
if self.rect.colliderect(racket.rect):
self.last_collision = pygame.time.get_ticks()
if (self.rect.right < racket.rect.left + racket.rect.width // 4) or (
self.rect.left > racket.rect.right - racket.rect.width // 4
):
self.bounce_y_power()
else:
self.bounce_y()
class Racket(Drawable):
def __init__(
self,
x: int,
y: int,
width: int = 80,
height: int = 20,
color=(255, 255, 255),
max_speed: int = 10,
):
super(Racket, self).__init__(x, y, width, height, color)
self.max_speed = max_speed
self.surface.fill(color)
def move(self, x: int, board: Board):
delta = x - self.rect.x
delta = self.max_speed if delta > self.max_speed else delta
delta = -self.max_speed if delta < -self.max_speed else delta
delta = 0 if (self.rect.x + delta) < 0 else delta
delta = (
0
if (self.rect.x + self.width + delta) > board.surface.get_width()
else delta
)
self.rect.x += delta
class Player:
def __init__(self, racket: Racket, ball: Ball, board: Board) -> None:
self.ball = ball
self.racket = racket
self.board = board
def move(self, x: int):
self.racket.move(x, self.board)
def move_manual(self, x: int):
"""
Do nothing, control is defined in derived classes
"""
pass
def act(self, x_diff: int, y_diff: int):
"""
Do nothing, control is defined in derived classes
"""
pass
class PongGame:
def __init__(
self, width: int, height: int, player1: Type[Player], player2: Type[Player]
):
pygame.init()
self.board = Board(width, height)
self.fps_clock = pygame.time.Clock()
self.ball = Ball(width // 2, height // 2)
self.opponent_paddle = Racket(x=width // 2, y=0)
self.oponent = player1(self.opponent_paddle, self.ball, self.board)
self.player_paddle = Racket(x=width // 2, y=height - 20)
self.player = player2(self.player_paddle, self.ball, self.board)
def run(self):
while not self.handle_events():
self.ball.move(self.board, self.player_paddle, self.opponent_paddle)
self.board.draw(
self.ball,
self.player_paddle,
self.opponent_paddle,
)
self.oponent.act(
self.oponent.racket.rect.centerx - self.ball.rect.centerx,
self.oponent.racket.rect.centery - self.ball.rect.centery,
)
self.player.act(
self.player.racket.rect.centerx - self.ball.rect.centerx,
self.player.racket.rect.centery - self.ball.rect.centery,
)
self.fps_clock.tick(FPS)
def handle_events(self):
for event in pygame.event.get():
if (event.type == pygame.QUIT) or (
event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE
):
pygame.quit()
return True
keys = pygame.key.get_pressed()
if keys[pygame.constants.K_LEFT]:
self.player.move_manual(0)
elif keys[pygame.constants.K_RIGHT]:
self.player.move_manual(self.board.surface.get_width())
return False
class NaiveOponent(Player):
def __init__(self, racket: Racket, ball: Ball, board: Board):
super(NaiveOponent, self).__init__(racket, ball, board)
def act(self, x_diff: int, y_diff: int):
x_cent = self.ball.rect.centerx
self.move(x_cent)
class HumanPlayer(Player):
def __init__(self, racket: Racket, ball: Ball, board: Board):
super(HumanPlayer, self).__init__(racket, ball, board)
def move_manual(self, x: int):
self.move(x)
# ----------------------------------
# DO NOT MODIFY CODE ABOVE THIS LINE
# ----------------------------------
# import numpy as np
# import matplotlib.pyplot as plt
class FuzzyPlayer(Player):
def __init__(self, racket: Racket, ball: Ball, board: Board):
super(FuzzyPlayer, self).__init__(racket, ball, board)
# for Mamdami:
# x_dist = fuzz.control.Antecedent...
# y_dist = fuzz.control.Antecedent...
# velocity = fuzz.control.Consequent...
# self.racket_controller = fuzz.control.ControlSystem...
# visualize Mamdami
# x_dist.view()
# ...
# for TSK:
# self.x_universe = np.arange...
# self.x_mf = {
# "far_left": fuzz.trapmf(
# self.x_universe,
# [
# ...
# ],
# ),
# ...
# }
# ...
# self.velocity_fx = {
# "f_slow_left": lambda x_diff, y_diff: -1 * (abs(x_diff) + y_diff),
# ...
# }
# visualize TSK
# plt.figure()
# for name, mf in self.x_mf.items():
# plt.plot(self.x_universe, mf, label=name)
# plt.legend()
# plt.show()
# ...
def act(self, x_diff: int, y_diff: int):
velocity = self.make_decision(x_diff, y_diff)
self.move(self.racket.rect.x + velocity)
def make_decision(self, x_diff: int, y_diff: int):
# for Mamdami:
# self.racket_controller.compute()
# velocity = self.racket_controller.o..
# for TSK:
# x_vals = {
# name: fuzz.interp_membership(self.x_universe, mf, x_diff)
# for name, mf in self.x_mf.items()
# }
# ...
# rule activations with Zadeh norms
# activations = {
# "f_slow_left": max(
# [
# min(x_vals...),
# min(x_vals...),
# ]
# ),
# ...
# }
# velocity = sum(
# activations[val] * self.velocity_fx[val](x_diff, y_diff)
# for val in activations
# ) / sum(activations[val] for val in activations)
return 0
if __name__ == "__main__":
game = PongGame(800, 400, NaiveOponent, HumanPlayer)
# game = PongGame(800, 400, NaiveOponent, FuzzyPlayer)
game.run()