-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpaceShip.py
248 lines (185 loc) · 8.22 KB
/
SpaceShip.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
from Screen import Screen
import pygame
import math
from pygame import transform
from Entity import Entity
from Background import load_image
from shared import set_origin, rotate
class SpaceShip(Entity):
class Thruster(Entity):
def __init__(self, ship):
self.ship = ship
self.vec_ = pygame.math.Vector2(self.ship.vec.x, self.ship.vec.y)
# self.sprit = ship.sprits["thruster_idle"]
self.sprit_ = self.sprit
self.origin = (self.vec.x, self.vec.y)
self.sprit_copy_ = self.sprit
@property
def sprit(self):
if self.ship.is_["forward"]: return self.ship.sprits["thruster_power"]
else: return self.ship.sprits["thruster_idle"]
# @sprit.setter
# def sprit(self, value): self.sprit_ = value
@property
def vec(self):
self.vec_.x = self.ship.rect.center[0] - (self.ship.offset("thruster")[0] * math.cos(self.ship.angle_rad) + self.ship.offset("thruster")[1] * math.sin(self.ship.angle_rad))
self.vec_.y = self.ship.rect.center[1] - (self.ship.offset("thruster")[0] * -math.sin(self.ship.angle_rad) + self.ship.offset("thruster")[1] * math.cos(self.ship.angle_rad))
return self.vec_
# @property
# def x(self): return self.ship.rect.center[0] - (self.ship.offset("thruster")[0] * math.cos(self.ship.angle_rad) + self.ship.offset("thruster")[1] * math.sin(self.ship.angle_rad))
# @property
# def y(self): return self.ship.rect.center[1] - (self.ship.offset("thruster")[0] * -math.sin(self.ship.angle_rad) + self.ship.offset("thruster")[1] * math.cos(self.ship.angle_rad))
@property
def rect(self): return self.sprit_copy.get_rect(center=(self.origin))
# @rect.setter
# def rect(self, value): self.rect = value
@property
def sprit_copy(self):
w, h = self.sprit_copy_.get_size()
if self.ship.angle_:
set_origin(self, self.sprit, (self.vec.x, self.vec.y), (w/2, h/2), self.ship.angle)
else:
set_origin(self, self.sprit, (self.vec.x, self.vec.y), (w/2, h/2), 0)
rotate(self, self.ship.screen.screen, self.sprit, self.ship.angle)
return self.sprit_copy_
@sprit_copy.setter
def sprit_copy(self, value): self.sprit_copy_ = value
def draw(self):
self.ship.screen.blit(self.sprit_copy, self.origin)
def action(self):
pass
def __init__(self, name, screen, planet, pilot, position):
super().__init__(name, screen, planet, position)
self.old_x = self.vec.x
self.old_y = self.vec.y
self.normal_speed = 10
self.max_speed = 20
self.accel = 2
self.sensvity = 7
self.angle = 0
self.angle_ = False
self.origin = (self.vec.x, self.vec.y)
self.pilot = pilot
self.poses = [[self.vec.x, self.vec.y]]
self.planet = self.pilot.planet
self.is_ = {"right":0, "left":0, "up":0, "down":0, "forward":0, "backward":0}
aux_controllers = self.pilot.controllers
aux_controllers.update({"crouch": pygame.K_DOWN})
self.controllers = aux_controllers
self.sprits = {
"ship":pygame.transform.scale(load_image(self.name), (128, 128)),
"thruster_idle":pygame.transform.scale(load_image("1.png"), (64, 64)),
"thruster_power":pygame.transform.scale(load_image("2.png"), (64, 64))
}
self.sprit_copy = self.sprits["ship"]
self.offsets = {"thruster":(82, -7)}
self.thruster = self.Thruster(self)
# self.location = self.sprits["ship"].get_rect(center=self.rect.center)
# @property
# def offsets(self):
# offsets = {"thruster":self.thrus}
# return
def offset(self, key):
return self.offsets[key]
@property
def speed(self): return abs(self.vel)
@speed.setter
def speed(self, value): self.speed = value
@property
def pitch(self): return self.is_["forward"] - self.is_["backward"]
@pitch.setter
def pitch(self, value): self.pitch = value
@property
def angle_rad(self): return math.radians(self.angle)
@angle_rad.setter
def angle_rad(self, value): self.angle_rad = value
@property
def rect(self): return self.sprit_copy.get_rect(center=(self.vec.x, self.vec.y))
@rect.setter
def rect(self, value): self.rect = value
def draw(self):
self.screen.blit(self.sprit_copy, self.origin)
self.thruster.draw()
def keyboard_behaviours(self):
#if self.angle >= 360: self.angle = 0
keys = pygame.key.get_pressed()
if keys[self.controllers["jump"]]:
self.is_["backward"] = 0
self.is_["forward"] = 1
if self.speed < self.max_speed:
self.vel += self.accel
if self.speed > self.max_speed:
self.vel = self.max_speed
elif keys[self.controllers["crouch"]]:
self.is_["backward"] = 1
self.is_["forward"] = 0
if self.speed < self.max_speed:
self.vel -= self.accel
if self.speed > self.max_speed:
self.vel = -self.max_speed
else:
self.is_["backward"] = 0
self.is_["forward"] = 0
pass
if keys[self.controllers["right"]]:
self.angle_ = True
self.angle -= self.sensvity
elif keys[self.controllers["left"]]:
self.angle_ = True
self.angle += self.sensvity
else:
self.angle_ = False
def action(self):
self.thruster.action()
self.poses.append([self.vec.x, self.vec.y])
w, h = self.sprits["ship"].get_size()
if self.angle_:
set_origin(self, self.sprits["ship"], (self.vec.x, self.vec.y), (w/2, h/2), self.angle)
rotate(self, self.screen.screen, self.sprits["ship"], self.angle)
else:
set_origin(self, self.sprits["ship"], (self.vec.x, self.vec.y), (w/2, h/2), 0)
if self.pitch == 0:
if self.vel > 0:
self.vel -= self.max_speed * self.planet.air_friction
if self.vel < 0:
self.vel = 0
elif self.vel < 0:
self.vel += self.max_speed * self.planet.air_friction
if self.vel > 0:
self.vel = 0
else:
# self.vel = 0
pass
else:
pass
# if (math.cos(self.angle_rad)) > 0:
# self.is_["right"] = 1
# self.is_["left"] = 0
# elif (math.cos(self.angle_rad)) < 0:
# self.is_["right"] = 0
# self.is_["left"] = 1
# else:
# self.is_["right"] = 0
# self.is_["left"] = 0
# if (math.sin(self.angle_rad)) > 0:
# self.is_["up"] = 1
# self.is_["down"] = 0
# elif (math.sin(self.angle_rad)) < 0:
# self.is_["up"] = 0
# self.is_["down"] = 1
# else:
# self.is_["up"] = 0
# self.is_["down"] = 0
# print(ship.rect.top, ship.rect.bottom)
if (self.rect.left <= 0) or (self.rect.right >= self.screen.width) or (self.rect.top <= 0) or (self.rect.bottom >= self.screen.height):
self.screen.background.scroll(-int(math.cos(self.angle_rad) * self.vel), int(math.sin(self.angle_rad) * self.vel))
if (self.rect.left <= 0):
self.vec.x = 0
if (self.rect.right >= self.screen.width):
self.vec.x = self.screen.width - self.width
# if (self.screen.width - self.rect.right <= self.screen.width / 12) or (self.rect.left <= self.screen.width / 12) or (self.rect.top <= self.screen.height / 12) or (self.screen.height - self.rect.bottom <= self.screen.height / 12):
# self.screen.background.scroll(-int(math.cos(self.angle_rad) * self.vel), int(math.sin(self.angle_rad) * self.vel))
else:
self.vec.x += math.cos(self.angle_rad) * self.vel
self.vec.y -= math.sin(self.angle_rad) * self.vel
self.screen.background.scroll(0, 0)