-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
132 lines (119 loc) · 4.4 KB
/
player.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
"""ursina imports"""
from ursina import (Audio,
Sprite,
held_keys)
CAR_ACCEL_SOUND_PATH = "resources/sounds/car_acceleration.wav"
class Player(Sprite): # pylint: disable=too-many-instance-attributes
"""player class"""
def __init__(self, **kwargs):
super().__init__(
collider='box',
)
self.max_speed = .5
self.min_speed = .1
self.acceleration = .01
self.min_turnspeed = .1
self.max_turnspeed = .5
self.road_width = 5.0
self.speed = self.min_speed
self.turnspeed = self.min_turnspeed
self.texture = 'resources/sprites/player.png'
self.model = 'quad'
self.name = "player"
self.score = 0
self.scale = 3
self.x = None
self.y = None
self.life = 3
self.maxlife = 3
self.is_alive = True
self.always_on_top = True
self.enabled = False
self.engine_sound = Audio(CAR_ACCEL_SOUND_PATH,
loop=True, autoplay=False)
for key, value in kwargs.items():
setattr(self, key, value)
def start_engine(self):
"""start engine sound"""
self.engine_sound.play()
def stop_engine(self):
"""stop engine sound"""
self.engine_sound.stop()
def update(self):
"""update method"""
if self.is_alive:
self.move()
self.change_speed()
if self.life <= 0:
self.disable()
self.is_alive = False
hit_info = self.intersects()
if hit_info.hit:
if hit_info.entity.name == "car":
self.take_damage(int(hit_info.entity.health))
hit_info.entity.destroy()
self.add_score(int(hit_info.entity.health * -1))
elif hit_info.entity.name == "health_orb":
self.add_life(hit_info.entity.health)
hit_info.entity.destroy()
elif hit_info.entity.name == "speed_orb":
self.add_speed(hit_info.entity.speed_increase)
hit_info.entity.destroy()
elif hit_info.entity.name == "score_orb":
self.add_score(hit_info.entity.score_increase)
hit_info.entity.destroy()
def move(self):
"""changing player coordinates"""
self.y += self.speed
if self.x > self.road_width:
self.x = self.road_width
if self.x < -self.road_width:
self.x = -self.road_width
else:
self.x += held_keys['d'] * self.turnspeed
self.x += held_keys['right arrow'] * self.turnspeed
self.x -= held_keys['a'] * self.turnspeed
self.x -= held_keys['left arrow'] * self.turnspeed
def change_speed(self):
"""changing speed and turnspeed when the keys are pressed"""
if self.speed < self.max_speed and self.turnspeed < self.max_turnspeed:
if held_keys['w'] or held_keys['up arrow']:
self.speed += self.acceleration
self.turnspeed += self.acceleration
else:
self.speed = self.max_speed
self.turnspeed = self.max_turnspeed
if self.speed > self.min_speed and self.turnspeed > self.min_turnspeed:
if held_keys['s'] or held_keys['down arrow']:
self.speed -= self.acceleration
self.turnspeed -= self.acceleration
if held_keys['space']:
self.speed -= self.acceleration * 2
self.turnspeed -= self.acceleration * 2
if self.speed < self.min_speed:
self.speed = self.min_speed
if self.turnspeed < self.min_turnspeed:
self.turnspeed = self.min_turnspeed
def add_score(self, score):
"""increasing score"""
self.score += score
def take_damage(self, damage):
"""taking damage"""
if self.life - damage > 0:
self.life -= damage
else:
self.life = 0
self.disable()
self.is_alive = False
def add_speed(self, speed):
"""adding speed to player"""
if self.speed < self.max_speed:
self.speed += speed
else:
self.speed = self.max_speed
def add_life(self, life):
"""adding life to player"""
if self.life < self.maxlife:
self.life += life
else:
self.life = self.maxlife