-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentitys.py
224 lines (190 loc) · 7.56 KB
/
entitys.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
""" Define elements of the game, like a ball """
import random
import math
import pygame
import settings
import sound
class Paddle:
""" move with keys, collide with walls and powerups """
def __init__(self, pos: tuple[float, float], keybinds) -> None:
super().__init__()
self.speed = settings.PADDLE_SPEED
self.direction = pygame.Vector2(0, 0)
self.image: pygame.Surface = pygame.image.load(
file='assets/Paddles/Neo/Neo_Paddle_128x28.png'
).convert()
self.image.set_colorkey('#ff00ff')
#self.image.set_colorkey('#ff00d3')
self.image = pygame.transform.rotate(
surface=self.image,
angle=90,
)
self.keybinds = keybinds
self.frect: pygame.FRect = self.image.get_frect()
self.frect.center = pos
def update(self, keys: set[str]) -> None:
""" change the direction, move and collide """
# update direction with arrows
if self.keybinds.UP in keys:
self.direction.y = -1
elif self.keybinds.DOWN in keys:
self.direction.y = 1
else:
self.direction.y = 0
# move the paddle
self.frect.x += self.speed * self.direction.x
self.frect.y += self.speed * self.direction.y
# collide powerups
# for powerup in powerups:
# if self.frect.collideFrect(powerup.FRect):
# powerup.activate()
# collide with walls
# y-axis
if self.frect.bottom > settings.HEIGHT:
self.frect.bottom = settings.HEIGHT
#keys.remove('DOWN')
elif self.frect.top < 0:
self.frect.top = 0
def render(self, canvas: pygame.Surface) -> None:
""" blit it's image to a surface """
canvas.blit(self.image, self.frect)
if settings.SHOW_HITBOX:
pygame.draw.rect(
surface=canvas,
color=settings.HITBOX_COLOR,
rect=self.frect,
width=1
)
if settings.DEBUG_POS:
print(f'paddle position : {self.frect.x}, {self.frect.y}')
if settings.SHOW_DIRECTIONS:
pygame.draw.line(
surface=canvas,
color=settings.DIRECTION_COLOR,
start_pos=self.frect.center,
end_pos=(
self.frect.centerx + self.direction.x * self.speed * 20,
self.frect.centery + self.direction.y * self.speed * 20
),
width=2,
)
class Ball:
""" ball class, collide with other entities """
def __init__(self, pos: tuple[float, float]) -> None:
super().__init__()
self.speed: int = settings.BALL_SPEED
self.direction: pygame.Vector2 = pygame.Vector2(
x=random.choice([-1, 1]),
y=0)
self.image: pygame.Surface = pygame.image.load(
file='assets/Balls/Glass/Ball_Blue_Glass-32x32.png'
).convert()
self.image.set_colorkey('#ff00ff')
self.frect: pygame.FRect = self.image.get_frect()
self.frect.center = pos
"""Sounds"""
self.ball_hit = sound.ball_hit
def update(
self,
paddles: list[Paddle],
) -> None:
"""change the position of the ball"""
self.frect.x += self.speed * self.direction.x
self.frect.y += self.speed * self.direction.y
self.collide(paddles)
def collide(
self,
paddles: list[Paddle],
) -> None:
""" bounce on walls and paddle. """
self.collide_with_paddle(paddles=paddles)
self.collide_with_walls()
if self.direction.magnitude() > 1:
self.direction.normalize_ip()
def collide_with_walls(self) -> None:
""" bounce on walls and ceiling """
# left
if self.frect.left < 0:
if (self.frect.top < settings.GOAL_TOP or self.frect.bottom > settings.GOAL_BOTTOM):
self.frect.left = 0
self.direction.x = 1
pygame.mixer.Sound.play(self.ball_hit)
else:
settings.score['RIGHT'] += 1
self.frect.center = settings.WIDTH/2, settings.HEIGHT/2
self.direction.y = 0
self.direction.x = -1
pygame.mixer.Sound.play(self.ball_hit)
# right
if self.frect.right > settings.WIDTH:
if (self.frect.top < settings.GOAL_TOP or self.frect.bottom > settings.GOAL_BOTTOM):
self.frect.right = settings.WIDTH
self.direction.x = -1
pygame.mixer.Sound.play(self.ball_hit)
else:
self.frect.center = settings.WIDTH/2, settings.HEIGHT/2
settings.score['LEFT'] += 1
self.direction.y = 0
self.direction.x = 1
pygame.mixer.Sound.play(self.ball_hit)
# ceiling
if self.frect.top < 0:
self.frect.top = 0
self.direction.y = 1
pygame.mixer.Sound.play(self.ball_hit)
# floor
if self.frect.bottom > settings.HEIGHT:
self.direction.y = -1
self.frect.bottom = settings.HEIGHT
pygame.mixer.Sound.play(self.ball_hit)
def collide_with_paddle(self, paddles: list[Paddle]) -> None:
""" bounce on paddle, calculate bounce angle """
for paddle in paddles:
if self.frect.colliderect(paddle.frect):
# calculate angle
distance = self.frect.centery - paddle.frect.centery
normalized_distance = distance/(paddle.frect.height/2)
bounce_angle = settings.MAX_BOUNCE_ANGLE * normalized_distance
bounce_angle_in_radian = math.radians(bounce_angle)
self.direction.y = math.sin(bounce_angle_in_radian)
# clamp left or right direction depending on the paddle position
# if the paddle is on the right the ball bounce to the left
if self.frect.x > settings.WIDTH/2:
self.direction.x = -math.cos(bounce_angle_in_radian)
else:
self.direction.x = math.cos(bounce_angle_in_radian)
pygame.mixer.Sound.play(self.ball_hit)
def render(self, canvas: pygame.Surface) -> None:
""" blit it's image to a surface """
# rotate the image
angle_radian = math.atan2(self.direction.x, self.direction.y)
rotated_image = pygame.transform.rotate(self.image, math.degrees(angle_radian))
rotated_image_frect = rotated_image.get_frect()
rotated_image_frect.center = self.frect.center
canvas.blit(rotated_image, rotated_image_frect)
if settings.SHOW_HITBOX:
pygame.draw.rect(
surface=canvas,
color='#ffff00',
rect=rotated_image_frect,
width=1,
)
pygame.draw.rect(
surface=canvas,
color=settings.HITBOX_COLOR,
rect=self.frect,
width=1
)
if settings.SHOW_DIRECTIONS:
pygame.draw.line(
surface=canvas,
color=settings.DIRECTION_COLOR,
start_pos=self.frect.center,
end_pos=(
self.frect.centerx + self.direction.x * self.speed * 10,
self.frect.centery + self.direction.y * self.speed * 10
),
width=2,
)
if settings.DEBUG_POS:
print(f'ball position : {self.frect.x}, {self.frect.y}')