-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathship.py
172 lines (141 loc) · 6.49 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
"""This file is for the Ship class, which creates and tracks the ship within the game."""
from flying_objects import Flying_Objects
from bullet import Bullet
import arcade
import constants
import math
class Ship(Flying_Objects):
"""A class for a ship, which is a flying object."""
def __init__(self):
"""Calls super; accepts radius, turn_amount,
and thrust_amount to initialize aspects of the ship.
Also initializes information to keep track of the ship's lives."""
super().__init__()
self._center.x = constants.SCREEN_WIDTH / 2
self._center.y = constants.SCREEN_HEIGHT / 2
self._radius = constants.SHIP_RADIUS
self._spin = constants.SHIP_TURN_AMOUNT
self._speed = constants.SHIP_THRUST_AMOUNT
self._firing_cooldown = constants.FIRING_COOLDOWN
self._lives = constants.SHIP_LIVES
self._thrusters_on = False
self._thrusters_direction = "forward"
self._texture = self.load_texture(":resources:images/space_shooter/playerShip1_orange.png")
self._thrusters_texture = self.load_texture(":resources:images/tiles/torch1.png")
def draw(self):
"""Draws a ship and thrusters from image files."""
# Draws thrusters
width2, height2, alpha2, texture2 = self._thrusters_texture
width2 = texture2.width - self._radius
height2 = texture2.height - self._radius
alpha2 = 1
if self._thrusters_on and self._alive:
alpha2 = 255
# Determines whether to aim the thrusters forward or backward
if self._thrusters_direction == "forward":
angle2 = self._angle + 180
x2 = self._center.x - ((math.cos(math.radians(self._angle + 90))) * self._radius)
y2 = self._center.y - ((math.sin(math.radians(self._angle + 90))) * self._radius)
elif self._thrusters_direction == "backward":
angle2 = self._angle
# Adjusts the position of the thrusters slightly, to be placed at the correct position
x2 = self._center.x + ((math.cos(math.radians(self._angle + 90))) * (self._radius - 10))
y2 = self._center.y + ((math.sin(math.radians(self._angle + 90))) * (self._radius - 10))
arcade.draw_texture_rectangle(x2, y2, width2, height2, texture2, angle2, alpha2)
# Draws ship
width, height, alpha, texture = self._texture
if not self._alive:
alpha = 1
arcade.draw_texture_rectangle(self._center.x, self._center.y, width, height, texture, self._angle, alpha)
def advance(self):
"""Calls super, and increments the firing cooldown."""
super().advance()
self._firing_cooldown += 1
def turn(self, direction):
"""A method for changing the angle of the ship."""
self._angle += direction * self._spin
def thrust(self, direction):
"""A method that handles the thrust of the ship.
Also instantiates and returns a thrusters object."""
# Sets the thrusters position to forward or backward, depending on direction given
if direction > 0:
self._thrusters_direction = "forward"
elif direction < 0:
self._thrusters_direction = "backward"
# Changes velocity for ship, based on direction and speed
self._velocity.dx += (math.cos(math.radians(self._angle + 90))) * self._speed * direction
self._velocity.dy += (math.sin(math.radians(self._angle + 90))) * self._speed * direction
# Allows thrusters to be visible
self._thrusters_on = True
def fire(self):
"""If firing cooldown is cleared, resets firing cooldown.
Fires a bullet from the bullet class.
Sets bullet attributes to ship's attributes, and returns bullet."""
self._firing_cooldown = 0
bullet = Bullet()
# Centers bullet slightly in front of ship
bullet.center.x = self._center.x + ((math.cos(math.radians(self._angle + 90))) * ((self._radius + bullet.radius) / 2))
bullet.center.y = self._center.y + ((math.sin(math.radians(self._angle + 90))) * ((self._radius + bullet.radius) / 2))
# Sets bullet attributes based on ship's attributes
bullet.angle = self._angle + 90
bullet.on_fire(self._velocity.dx, self._velocity.dy)
return bullet
def hit(self):
"""A method that keeps track of the ship's lives.
Removes the ship from the screen momentarily so it can be properly reset"""
self._center.x = constants.SCREEN_WIDTH * 2
self._center.y = constants.SCREEN_HEIGHT * 2
self._lives -= 1
self._alive = False
def reset(self):
"""Resets the ship when destroyed, if it has life left."""
if self._lives > 0:
self._center.x = constants.SCREEN_WIDTH / 2
self._center.y = constants.SCREEN_HEIGHT / 2
self._velocity.dx = 0
self._velocity.dy = 0
self._angle = math.degrees(0)
self._alive = True
# Getter and setter properties are listed below
@property
def radius(self):
return self._radius
@radius.setter
def radius(self, radius):
self._radius = radius
@property
def speed(self):
return self._speed
@speed.setter
def speed(self, speed):
self._speed = speed
@property
def spin(self):
return self._spin
@spin.setter
def spin(self, spin):
self._spin = spin
@property
def thrusters_direction(self):
return self._thrusters_direction
@thrusters_direction.setter
def thrusters_direction(self, thrusters_direction):
self._thrusters_direction = thrusters_direction
@property
def thrusters_on(self):
return self._thrusters_on
@thrusters_on.setter
def thrusters_on(self, thrusters_on):
self._thrusters_on = thrusters_on
@property
def lives(self):
return self._lives
@lives.setter
def lives(self, lives):
self._lives = lives
@property
def firing_cooldown(self):
return self._firing_cooldown
@firing_cooldown.setter
def firing_cooldown(self, firing_cooldown):
self._firing_cooldown = firing_cooldown