-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.py
324 lines (232 loc) · 9.92 KB
/
classes.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import GameMaker as gm
from GameMaker.Assets import *
import random
from functions import *
def CircleCollision(player,position,a):
item = a.draw_image
distance = Distance(position, Vector2(item.x + item.w/2,item.y + item.h/2))
if distance < item.w/2:
player.collision = Angle(position, Vector2(item.x + item.w/2,item.y + item.h/2))
def GenerateAsteroids(amount,pos_range):
return AssetList([Asteroid(
Vector2(random.randint(-pos_range,pos_range), random.randint(-pos_range,pos_range)),
size=random.uniform(0.5,1.5),
angle=random.randint(0,360),
velocity=random.uniform(0.03,0.1),
rotation_speed=random.uniform(0.05,0.165),
) for i in range(amount)])
def GenerateStars(amount,pos_range):
return AssetList([Star( Vector2(random.randint(-pos_range,pos_range), random.randint(-pos_range,pos_range)), size=random.uniform(0.5,1.5), angle=random.randint(0,360)) for i in range(amount)])
def BrokenAsteroid():
return AssetList([
Asteroid(
position=self.parent.position,
size=self.parent.size,
angle=self.parent.angle,
velocity=self.parent.velocity,
rotation_speed=self.parent.rotation_speed,
path=f"assets\\draw_asteroid_{i+2}.png"
) for i in range(4)
])
class AssetList():
def __init__(self,items):
self.items = items
try:
items[0].Move
self.should_move = True
except:
self.should_move = False
try:
items[0].collision
self.collision_check = True
except:
self.collision_check = False
def draw(self,window,player,tick):
for item in self.items:
if self.should_move: item.Move(tick)
if self.collision_check: item.Collide(player,window)
item.draw_image.x = window.get_width()/2 + item.position.x - player.camera.x
item.draw_image.y = window.get_height()/2 - item.position.y + player.camera.z
if 0 - item.draw_image.w < item.draw_image.x < window.get_width():
if 0 - item.draw_image.h < item.draw_image.y < window.get_height():
item.draw_image.draw(window)
def dump(self):
return self.items[0].title,[item.dump() for item in self.items]
class Spaceship():
"""docstring for Spaceship"""
def __init__(self,size,friendly=True):
self.attachments = []
if friendly:
self.body = RotatedImage("assets\\draw_spaceship_body.png",[0,0,size,size])
else:
self.body = RotatedImage("assets\\draw_spaceship_enemy.png",[0,0,size,size])
self.fire = RotatedImage("assets\\draw_spaceship_fire.png",[0,0,size,size])
def draw(self,window,rect,angle,speed):
self.body.x,self.body.y,self.body.w,self.body.h = rect.format()
self.fire.x,self.fire.y,self.fire.w,self.fire.h = rect.format()
self.body.rotation = angle
self.fire.rotation = angle
offset = AngleToPosition(90 - angle, speed * 3)
self.fire.x += offset.x
self.fire.y += offset.y
self.fire.draw(window)
self.body.draw(window)
class Attachment():
"""docstring for Attachment"""
def __init__(self,title,base_price,price_scale,description):
self.title = title
self.base_price = base_price
self.price_scale = price_scale
self.description = description
class Currency():
"""Base Class for Currency"""
def __init__(self,title,icon,draw_image,description,value=0,position=[0,0],hide=[0,0]):
self.title = title
self.icon = icon
self.draw_image = draw_image
self.description = description
self.position = position
self.hide = hide
self.value = value
self.text = Text(self.value,self.position,color=(255,255,255),center=[gm.CENTER,gm.CENTER])
def Animate(self,visible=True,smoothing=5):
if visible:
self.icon.x += (self.position[0] - self.icon.x) / smoothing
else:
self.icon.x += ((self.position[0] - (self.icon.w + self.text.w + 5)) - self.icon.x + self.hide[0]) / smoothing
def draw(self,window):
# self.text.update(self.value)
self.text.x = self.icon.x + self.icon.w + self.text.w/2 + 3
self.text.y = self.icon.y + self.text.h/2 + 7
self.icon.draw(window)
self.text.draw(window)
class Asteroid():
"""Base Asteroid Class"""
def __init__(self,position,angle=0,size=1,velocity=0.05,rotation_speed=0.1,path="assets\\draw_asteroid_1.png"):
self.start_position = position
self.position = self.start_position
self.size = size
self.angle = angle
self.draw_image = RotatedImage(path,[position.x,position.y,60*size,60*size],rotation=self.angle)
self.velocity = velocity
self.rotation_speed = rotation_speed
self.title = "Asteroid"
self.collision = None
def draw(self,window,tick):
self.position = self.start_position + AngleToPosition(90 - self.angle,self.velocity * tick)
self.draw_image.x = self.position.x
self.draw_image.y = self.position.y
self.draw_image.w = 60 * self.size
self.draw_image.h = 60 * self.size
self.draw_image.rotation = self.angle + self.rotation_speed * tick
self.draw_image.draw(window)
def Move(self,tick):
self.position = self.start_position + AngleToPosition(90 - self.angle,self.velocity * tick)
self.draw_image.rotation = self.angle + self.rotation_speed * tick
def Collide(self,player,window):
size = player.screen_position(window)
CircleCollision(player,Vector2(size.x + size.w/2, size.y + size.h/2),self)
class Star():
"""Visual Star"""
def __init__(self,position,angle=0,size=1):
self.position = position
self.size = size
self.angle = angle
self.draw_image = RotatedImage("assets\\draw_star_1.png",[position.x,position.y,10*size,10*size],rotation=self.angle)
self.title = "Star"
def draw(self,window,tick):
self.draw_image.x = self.position.x
self.draw_image.y = self.position.y
self.draw_image.w = 10 * self.size
self.draw_image.h = 10 * self.size
self.draw_image.rotation = self.angle
self.draw_image.draw(window)
class Laser():
"""Laser Weapon Class"""
def __init__(self,tick,position,rotation,size=1):
self.start_position = position
self.position = self.start_position
self.rotation = rotation
self.size = size
self.draw_image = RotatedImage("assets\\draw_laser_1.png",[0,0,self.size*5,self.size*5])
self.start_tick = tick
self.velocity = 7
self.lifetime = 150
self.title = "Laser"
def Alive(self,tick):
return (tick - self.start_tick) < self.lifetime
def Move(self,tick):
self.position = self.start_position + AngleToPosition(90 - self.rotation,self.velocity * (tick - self.start_tick))
def draw(self,window,player):
self.draw_image.x = window.get_width()/2 + self.position.x - player.camera.x
self.draw_image.y = window.get_height()/2 - self.position.y + player.camera.z
self.draw_image.w = 5 * self.size
self.draw_image.h = 5 * self.size
self.draw_image.rotation = 180 - self.rotation
self.draw_image.draw(window)
def dump(self):
return [
self.start_tick,
round(self.start_position.x,2),
round(self.start_position.y,2),
round(self.rotation,2),
]
class Mothership():
"""Main Player Base for Upgrades"""
def __init__(self,title,position,rotation=0,friendly=True):
self.position = position
self.rotation = rotation
if friendly:
self.draw_image = RotatedImage("assets\\draw_mothership_body.png",[0,0,400,450])
else:
self.draw_image = RotatedImage("assets\\draw_mothership_enemy.png",[0,0,400,450])
self.center = Vector2(self.position.x + self.draw_image.w/2, self.position.y - self.draw_image.h/2)
self.title = title
self.text = Text(title,[0,0],center=[gm.CENTER,gm.CENTER],color=(255,255,255),font_size=50)
self.health = 100
self.healthbar = ProgressBar([0,0,200,12])
def updateCenter(self):
self.center = Vector2(self.position.x + self.draw_image.w/2, self.position.y - self.draw_image.h/2)
def draw(self,window,player):
self.draw_image.x = window.get_width()/2 + self.position.x - player.camera.x
self.draw_image.y = window.get_height()/2 - self.position.y + player.camera.z
self.draw_image.rotation = self.rotation
self.draw_image.draw(window)
self.text.x = self.draw_image.x + self.draw_image.w/2
self.text.y = self.draw_image.y + self.draw_image.h/2 - 10
self.healthbar.x = self.draw_image.x + self.draw_image.w/2 - self.healthbar.w/2
self.healthbar.y = self.draw_image.y + self.draw_image.h/2 + 20
self.healthbar.update(self.health / 100)
self.text.draw(window)
self.healthbar.draw(window)
class Arrow():
def __init__(self):
self.icon = RotatedImage("assets\\icon_arrow.png",[0,0,60,60])
def draw(self,window,player):
angle,distance = Angle(player.position,player.mothership.center),Distance(player.position,player.mothership.center)
if distance < 200: return
if distance > 630: distance = 630
position = AngleToPosition(angle, distance/2)
self.icon.x, self.icon.y = window.H_WIDTH + position.x, window.H_HEIGHT - position.y
self.icon.rotation = angle + 90
window.draw(self.icon)
class Trail():
def __init__(self,position,rotation = 0):
self.position = RectStruct(*position)
self.rotation = rotation
self.increment = 4.5
self.loffset = AngleToPosition(self.rotation + 35,13)
self.roffset = AngleToPosition(self.rotation - 35,13)
self.left = AlphaRectangle(self.position + self.loffset, [171,219,227,70], 90 - self.rotation)
self.right = AlphaRectangle(self.position - self.roffset, [171,219,227,70], 90 - self.rotation)
def Alive(self):
return self.left.color[3] > self.increment
def draw(self,window,player):
self.left.color[3] -= self.increment
self.right.color[3] -= self.increment
self.left.x = window.get_width()/2 + self.position.x - player.camera.x + self.loffset.x - self.position.w/2
self.left.y = window.get_height()/2 - self.position.y + player.camera.z + self.loffset.y - self.position.h/2
self.right.x = window.get_width()/2 + self.position.x - player.camera.x - self.roffset.x - self.position.w/2
self.right.y = window.get_height()/2 - self.position.y + player.camera.z - self.roffset.y - self.position.h/2
self.left.draw(window)
self.right.draw(window)