-
Notifications
You must be signed in to change notification settings - Fork 2
/
ship.py
158 lines (127 loc) · 5.04 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
import random
from util.vectorsprites import *
from shooter import *
from math import *
from soundManager import *
class Ship(Shooter):
# Class attributes
acceleration = 0.2
decelaration = -0.005
maxVelocity = 10
turnAngle = 6
bulletVelocity = 13.0
maxBullets = 4
bulletTtl = 35
def __init__(self, stage):
position = Vector2d(stage.width/2, stage.height/2)
heading = Vector2d(0, 0)
self.thrustJet = ThrustJet(stage, self)
self.shipDebrisList = []
self.visible = True
self.inHyperSpace = False
pointlist = [(0, -10), (6, 10), (3, 7), (-3, 7), (-6, 10)]
Shooter.__init__(self, position, heading, pointlist, stage)
def draw(self):
if self.visible:
if not self.inHyperSpace:
VectorSprite.draw(self)
else:
self.hyperSpaceTtl -= 1
if self.hyperSpaceTtl == 0:
self.inHyperSpace = False
self.color = (255, 255, 255)
self.thrustJet.color = (255, 255, 255)
self.position.x = random.randrange(0, self.stage.width)
self.position.y = random.randrange(0, self.stage.height)
position = Vector2d(self.position.x, self.position.y)
self.thrustJet.position = position
return self.transformedPointlist
def rotateLeft(self):
self.angle += self.turnAngle
self.thrustJet.angle += self.turnAngle
def rotateRight(self):
self.angle -= self.turnAngle
self.thrustJet.angle -= self.turnAngle
def increaseThrust(self):
playSoundContinuous("thrust")
if math.hypot(self.heading.x, self.heading.y) > self.maxVelocity:
return
dx = self.acceleration * math.sin(radians(self.angle)) * -1
dy = self.acceleration * math.cos(radians(self.angle)) * -1
self.changeVelocity(dx, dy)
def decreaseThrust(self):
stopSound("thrust")
if (self.heading.x == 0 and self.heading.y == 0):
return
dx = self.heading.x * self.decelaration
dy = self.heading.y * self.decelaration
self.changeVelocity(dx, dy)
def changeVelocity(self, dx, dy):
self.heading.x += dx
self.heading.y += dy
self.thrustJet.heading.x += dx
self.thrustJet.heading.y += dy
def move(self):
VectorSprite.move(self)
self.decreaseThrust()
# Break the shape of the ship down into several lines
# Ship shape - [(0, -10), (6, 10), (3, 7), (-3, 7), (-6, 10)]
def explode(self):
pointlist = [(0, -10), (6, 10)]
self.addShipDebris(pointlist)
pointlist = [(6, 10), (3, 7)]
self.addShipDebris(pointlist)
pointlist = [(3, 7), (-3, 7)]
self.addShipDebris(pointlist)
pointlist = [(-3, 7), (-6, 10)]
self.addShipDebris(pointlist)
pointlist = [(-6, 10), (0, -10)]
self.addShipDebris(pointlist)
# Create a peice of ship debris
def addShipDebris(self, pointlist):
heading = Vector2d(0, 0)
position = Vector2d(self.position.x, self.position.y)
debris = VectorSprite(position, heading, pointlist, self.angle)
# Add debris to the stage
self.stage.addSprite(debris)
# Calc a velocity moving away from the ship's center
centerX = debris.boundingRect.centerx
centerY = debris.boundingRect.centery
# Alter the random values below to change the rate of expansion
debris.heading.x = ((centerX - self.position.x) +
0.1) / random.uniform(20, 40)
debris.heading.y = ((centerY - self.position.y) +
0.1) / random.uniform(20, 40)
self.shipDebrisList.append(debris)
# Set the bullet velocity and create the bullet
def fireBullet(self):
if self.inHyperSpace == False:
vx = self.bulletVelocity * math.sin(radians(self.angle)) * -1
vy = self.bulletVelocity * math.cos(radians(self.angle)) * -1
heading = Vector2d(vx, vy)
Shooter.fireBullet(self, heading, self.bulletTtl,
self.bulletVelocity)
playSound("fire")
#
def enterHyperSpace(self):
if not self.inHyperSpace:
self.inHyperSpace = True
self.hyperSpaceTtl = 100
self.color = (0, 0, 0)
self.thrustJet.color = (0, 0, 0)
# Exhaust jet when ship is accelerating
class ThrustJet(VectorSprite):
pointlist = [(-3, 7), (0, 13), (3, 7)]
def __init__(self, stage, ship):
position = Vector2d(stage.width/2, stage.height/2)
heading = Vector2d(0, 0)
self.accelerating = False
self.ship = ship
VectorSprite.__init__(self, position, heading, self.pointlist)
def draw(self):
if self.accelerating and self.ship.inHyperSpace == False:
self.color = (255, 255, 255)
else:
self.color = (0, 0, 0)
VectorSprite.draw(self)
return self.transformedPointlist