-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathship.py
212 lines (183 loc) · 7.05 KB
/
ship.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
import pygame
from bullet import Bullet
from maze import CollideType
from colours import RED, GREEN
from random import randint
# Define some constants
SHIP_ROTATION_SPEED = 0.19
SHIP_THRUST_POWER = 0.0003
BULLET_TTL = 2000
MAX_SHIP_SPEED = 1
MAX_BULLETS = 10
MAZE_BLOCK_SIZE = 47
SHIP_SIZE = 4
MAX_FUEL = 10000
class RotateType:
NONE = 0
LEFT = 1
RIGHT = 2
class Ship:
def __init__(self, screen, x, y):
self.pos = pygame.math.Vector2(x, y)
self.vel = pygame.math.Vector2(0, 0)
self.acc = pygame.math.Vector2(0, 0)
self.angle = 0
self.max_speed = 5
self.max_force = SHIP_THRUST_POWER
self.thrusting = False
self.rotation = RotateType.NONE
self.eating = False
self.bullets = []
self.screen = screen
self.name = "ship"
self.fuel = MAX_FUEL
pygame.mixer.init()
self.fire_sound = pygame.mixer.Sound("fire.mp3")
self.fire_sound.set_volume(0.1)
self.thrust_sound = pygame.mixer.Sound("thruster.mp3")
self.eating_sound = pygame.mixer.Sound("eating.mp3")
def rotate_left(self):
self.angle += SHIP_ROTATION_SPEED
self.rotation = RotateType.LEFT
def rotate_right(self):
self.angle -= SHIP_ROTATION_SPEED
self.rotatiion = RotateType.RIGHT
def stop_rotating(self):
self.rotating = RotateType.NONE
def thrust(self):
if self.fuel <= 0:
return
thrust_force = pygame.math.Vector2(1, 0).rotate(-self.angle)
self.acc += thrust_force * self.max_force
# maximum speed
if self.vel.length() > MAX_SHIP_SPEED:
self.vel.scale_to_length(MAX_SHIP_SPEED)
if self.thrusting == False:
self.thrust_sound.play(99)
self.thrusting = True
def stop_thrust(self):
self.thrusting = False
self.thrust_sound.fadeout(100)
def slow(self, amount=1):
self.vel -= self.vel * (0.0001 * amount)
def collided_with(self, collision_type, obj):
if (obj.name == "block"):
self.eat_block(obj)
return
if (obj.name == "bullet"):
if (collision_type == CollideType.NONE):
return
if collision_type == CollideType.TOP:
self.vel.y = abs(self.vel.y)
elif collision_type == CollideType.BOTTOM:
self.vel.y = -abs(self.vel.y)
elif collision_type == CollideType.LEFT:
self.vel.x = abs(self.vel.x)
elif collision_type == CollideType.RIGHT:
self.vel.x = -abs(self.vel.x)
print("Ship collided with " + str(collision_type))
def handle_collision_with(self, obj):
(collide_type, struck_object) = obj.get_collision(self)
if struck_object:
self.collided_with(collide_type, struck_object)
struck_object.collided_with(collide_type, self)
else:
self.stop_eating()
def handle_bullet_collisions_with(self, obj):
for bullet in self.bullets:
(collide_type, struck_object) = obj.get_collision(bullet)
if struck_object:
bullet.collided_with(collide_type, struck_object)
struck_object.collided_with(collide_type, bullet)
def eat_block(self, block):
if block.hp >= 2:
if self.eating == False:
self.eating = True
self.eating_sound.play()
block.hp -= 1
self.fuel += 10
else:
self.stop_eating()
def stop_eating(self):
self.eating = False
self.eating_sound.fadeout(300)
def bounce(self, collision_type):
if collision_type == CollideType.TOP:
self.vel.y = abs(self.vel.y)
elif collision_type == CollideType.BOTTOM:
self.vel.y = -abs(self.vel.y)
elif collision_type == CollideType.LEFT:
self.vel.x = abs(self.vel.x)
elif collision_type == CollideType.RIGHT:
self.vel.x = -abs(self.vel.x)
def fire(self):
bullet_pos = self.pos + pygame.math.Vector2(20, 0).rotate(-self.angle)
bullet_vel = pygame.math.Vector2(10, 0).rotate(-self.angle) * 0.1
self.bullets.append(Bullet(self.screen, bullet_pos, bullet_vel, BULLET_TTL))
self.firing = True
self.fire_sound.play()
def stop_firing(self):
self.firing = False
def update(self):
self.vel += self.acc
if self.vel.length() > self.max_speed:
self.vel.scale_to_length(self.max_speed)
self.pos += self.vel
self.acc *= 0
if self.pos.x < 0:
self.bounce(CollideType.LEFT)
elif self.pos.x > self.screen.get_width():
self.bounce(CollideType.RIGHT)
if self.pos.y < 0:
self.bounce(CollideType.TOP)
elif self.pos.y > self.screen.get_height():
self.bounce(CollideType.BOTTOM)
self.slow()
for bullet in self.bullets:
bullet.update()
bullet.draw()
if bullet.ttl == 0:
self.bullets.remove(bullet)
def draw(self):
points = [pygame.math.Vector2(SHIP_SIZE * 2, 0).rotate(-self.angle),
pygame.math.Vector2(-SHIP_SIZE, SHIP_SIZE).rotate(-self.angle),
pygame.math.Vector2(-SHIP_SIZE, -SHIP_SIZE).rotate(-self.angle)]
points = [p + self.pos for p in points]
# Ship is duller as it loses fuel
fuel_fraction = min(self.fuel, MAX_FUEL) / MAX_FUEL
#print(fuel_fraction)
if self.eating:
colour = pygame.Color(randint(200,255), randint(200,255), randint(200,255))
else:
colour = pygame.Color(255, int(255 * fuel_fraction), int(120 * fuel_fraction))
pygame.draw.polygon(self.screen, colour, points)
if self.thrusting:
self.fuel -= 1
jet_pos = pygame.math.Vector2(-10, 0).rotate(-self.angle) + self.pos
pygame.draw.circle(self.screen, self.flame_colour(), jet_pos, 5)
jet_pos = pygame.math.Vector2(-13, -5).rotate(-self.angle) + self.pos
pygame.draw.circle(self.screen, self.flame_colour(0.5), jet_pos, 3)
jet_pos = pygame.math.Vector2(-13, 5).rotate(-self.angle) + self.pos
pygame.draw.circle(self.screen, self.flame_colour(0.5), jet_pos, 3)
def flame_colour(self, brightness=1):
r = 255
g = randint(0, 255)
b = randint(0, 255)
r = int(r * brightness)
g = int(g * brightness)
b = int(b * brightness)
return pygame.Color(r, g, b)
def get_state(self):
return {
"rotation": self.rotation,
"pos": self.pos,
"vel": self.vel,
"acc": self.acc,
"angle": self.angle,
"fuel": self.fuel,
"max_speed": self.max_speed,
"thrusting": self.thrusting,
"firing": self.firing,
"eating": self.eating,
#"bullets": [bullet.get_state() for bullet in self.bullets]
}