-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.py
96 lines (75 loc) · 2.7 KB
/
ball.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
from typing import Any
import numpy as np
import numpy.typing as npt
from colorama import Back, Fore, Style
import settings
from sprite import Sprite
from velocity import Velocity
class Ball(Sprite):
def __init__(self, screen_height, paddle_y, paddle_width):
super().__init__(screen_height - 2, (2 * paddle_y + paddle_width) // 2)
self.reset_ascii()
self._dead = True
self._thru = False
self._fast = False
self._velocity = Velocity(0, 0)
def __repr__(self) -> str:
return f"Ball(y={self.y}, x={self.x}, vx={self._velocity.vx}, \
vy={self._velocity.vy}, dead={self._dead}, thru={self._thru}, \
fast={self._fast})"
def reset_ascii(self, **kwargs) -> np.ndarray:
"""
Resets the ASCII representation of the sprite.
Additional keyword arguments can be provided.
"""
self._ascii = np.array(
[Style.BRIGHT + Fore.RED + "(", Style.BRIGHT + Fore.RED + ")"],
dtype="object",
).reshape(1, -1)
def go_fast(self):
self._velocity.vx = (
settings.MAX_SPEED if self._velocity.vx > 0 else -settings.MAX_SPEED
)
self._velocity.vy = (
settings.MAX_SPEED if self._velocity.vy > 0 else -settings.MAX_SPEED
)
def move(self, screen_height: int, screen_width: int, key: str = None):
if key is not None:
if key == settings.MOVE_RIGHT_KEY:
self.move_right()
elif key == settings.MOVE_LEFT_KEY:
self.move_left()
return
super().move(self._velocity.vx, self._velocity.vy)
self.x = max(0, min(self.x, screen_height - self.height))
self.y = max(0, min(self.y, screen_width - self.width))
def reverse_vy(self):
self._velocity.vy = -self._velocity.vy
def reverse_vx(self):
self._velocity.vx = -self._velocity.vx
def set_vx(self, vx: int):
self._velocity.vx = vx
def set_vy(self, vy: int):
self._velocity.vy = vy
@property
def vx(self):
return self._velocity.vx
@vx.setter
def vx(self, vx: int):
self._velocity.vx = vx
@property
def vy(self):
return self._velocity.vy
@vy.setter
def vy(self, vy: int):
self._velocity.vy = vy
def intersects(self, bricks) -> bool:
right_ball = self.y + self.width - 1
top_ball = self.x
for brick in bricks:
left_brick = brick.y
right_brick = brick.y + brick.width - 1
top_brick = brick.x
if top_brick == top_ball and left_brick <= right_ball <= right_brick:
return True
return False