-
Notifications
You must be signed in to change notification settings - Fork 0
/
duck.py
62 lines (51 loc) · 1.66 KB
/
duck.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
from constants import (
ui,
DRAG,
GRAVITY,
MIN_TAKEOFF_VELOCITY,
BOUNCE_ACCELERATION_Y,
BOUNCE_ACCELERATION_X,
)
from utils import degrees_and_ray_to_x_y, get_hypotenuse
class Duck:
scale_on_save = 1
def __init__(self):
self.x = ui.SLING_POINT
self.y = ui.FLOOR_LEVEL + ui.SPRITE_HEIGHT
self.x_velocity = 0
self.y_velocity = 0
self.angle = 10
self.force = 5
self.is_flying = False
def launch(self, scale=1):
current_x_velocity, current_y_velocity = degrees_and_ray_to_x_y(
self.angle, self.force
)
current_takeoff_velocity = get_hypotenuse(
current_x_velocity, current_y_velocity
)
if current_takeoff_velocity < MIN_TAKEOFF_VELOCITY:
return
if not self.is_flying:
self.is_flying = True
x, y = degrees_and_ray_to_x_y(self.angle, self.force)
self.x_velocity = x * scale
self.y_velocity = y * scale
def stop(self):
self.is_flying = False
self.x_velocity = 0
self.y_velocity = 0
def move(self, scale=1):
scaled_gravity = GRAVITY * scale
self.x += min(self.x_velocity, ui.SPRITE_WIDTH)
self.y += min(self.y_velocity, ui.SPRITE_HEIGHT)
self.y_velocity -= scaled_gravity
return self.x, self.y
def bounce_y(self, prev_position_y):
self.y = prev_position_y
self.y_velocity *= BOUNCE_ACCELERATION_Y
self.x_velocity *= DRAG
def bounce_x(self, prev_position_x):
self.x = prev_position_x
self.x_velocity *= BOUNCE_ACCELERATION_X
self.y_velocity *= DRAG