-
Notifications
You must be signed in to change notification settings - Fork 12.5k
/
Copy pathball.py
49 lines (41 loc) · 1.58 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
# ./PongPong/pong/ball.py
import pyglet
import random
from typing import Tuple
from track import update_coverage
class BallObject(pyglet.shapes.Circle):
def __init__(self, *args, **kwargs):
super(BallObject, self).__init__(*args, **kwargs)
self.color = (255, 180, 0)
self.velocity_x, self.velocity_y = 0.0, 0.0
def update(self, win_size: Tuple, border: Tuple, other_object, dt) -> None:
speed = [
2.37,
2.49,
2.54,
2.62,
2.71,
2.85,
2.96,
3.08,
3.17,
3.25,
] # more choices more randomness
rn = random.choice(speed)
newx = self.x + self.velocity_x
newy = self.y + self.velocity_y
if newx < border + self.radius or newx > win_size[0] - border - self.radius:
update_coverage("pong/ball.py/BallObject/update.if")
self.velocity_x = -(self.velocity_x / abs(self.velocity_x)) * rn
elif newy > win_size[1] - border - self.radius:
update_coverage("pong/ball.py/BallObject/update.elif")
self.velocity_y = -(self.velocity_y / abs(self.velocity_y)) * rn
elif (newy - self.radius < other_object.height) and (
other_object.x <= newx <= other_object.rightx
):
update_coverage("pong/ball.py/BallObject/update.elif2")
self.velocity_y = -(self.velocity_y / abs(self.velocity_y)) * rn
else:
update_coverage("pong/ball.py/BallObject/update.else")
self.x = newx
self.y = newy