forked from BobMak/SpaceShooter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClasses.py
1429 lines (1097 loc) · 43.2 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pickle
import random, copy
import numpy as np, numpy
import pygame.gfxdraw as gfx
import State
import Funcs
import Scripts
from Assets import *
class Vulnerable:
"""
Inherited by all objects that can be damaged.
"""
def __init__(self, hp):
self.hp = hp
def damage(self, damage):
self.hp += -max(0, damage)
class Moving:
"""
Inherited by all classes that can move by themselves.
"""
def __init__(self):
self.speed = [0.0, 0.0]
self.position = [self.rect.x, self.rect.y]
State.movable.add(self)
def modify_position(self):
"modifyes position with respect to <1 values of acceleration"
self.position[0] += self.speed[0]
self.position[1] += self.speed[1]
self.rect = pygame.Rect(self.position[0], self.position[1],
self.rect.width, self.rect.height)
def slow_down(self):
self.speed[0] += (self.ENV_DEACCELERATION
*abs(np.cos(np.deg2rad(self.look_dir-90.0)))
*-np.sign(self.speed[0]))
self.speed[1] += (self.ENV_DEACCELERATION
*abs(np.sin(np.deg2rad(self.look_dir-90.0)))
*-np.sign(self.speed[1]))
def accelerate(self, temp):
self.speed[0] += temp*np.cos(np.deg2rad(self.look_dir-90.0))
self.speed[1] += temp*np.sin(np.deg2rad(self.look_dir-90.0))
class Colliding(pygame.sprite.Sprite, Moving):
'''
Colliding(width, height, distance, angle, source)
Smaller collision rect container for more complex forms.
should be rotated with source object with "orbit_rotate"
'''
def __init__(self, width, height, distance, angle, source):
pygame.sprite.Sprite.__init__(self)
self.rect = pygame.Rect((source.rect.x
+ (np.deg2rad(np.cos(angle)) * distance)),
(source.rect.y
+ np.deg2rad(np.sin(angle)) * distance),
width, height)
self.speed = source.speed
self.angle = angle
self.source = source
self.distance = distance
self.orbit_ang = angle
Moving.__init__(self)
class FX(pygame.sprite.Sprite, Moving):
def __init__(self, rect, duration):
pygame.sprite.Sprite.__init__(self)
self.timer = duration
self.time_count = 0
self.rect = rect
# Requires rect to already be there
Moving.__init__(self)
State.time_dependent.add(self)
class FX_Glow(FX):
"""
FX_Glow(rect, duration, radius, length, color, speed=(0,0))
"""
def __init__(self, rect, duration, radius, length, color, speed=(0,0)):
FX.__init__(self, rect, duration)
self.radius = radius
self.color = color
self.length = length
self.speed = speed
State.glow.add(self)
#draw
def update(self):
"""
drawing funcition
"""
for x in range(self.length):
pygame.gfxdraw.filled_circle(State.screen, self.rect.centerx,
self.rect.centery, self.radius+x,
self.color)
class FX_Track(FX):
'''
FX_Track(image, rect, duration, fading=None,
enlarging=None, rotating=None)
:duration - in logic ticks (there is LOGIC_PER_SECOND ticks per second).
:fading - [x, y], where x is the rate from 0 (no fade)
to 255 (max fade) with which effect will fade each y frames.
:enlarging - [x, y], x - rate of effect's size deviation
per y frames. 0-1 will shrink the effect, while >1 - enlarge.
:rotating - [x, y], x - the angle (degrees) on which
the effect is rotated per y frames.
:color - set the color for effect image.
:look_dir - initial angle (degrees)
:speed - speed (vector [dx, dy])
Tracks take significantly more computations if y is lower
and duration time is higher.
'''
def __init__(self, image, rect, duration,
fading=None, enlarging=None, rotating=None, color=None,
look_dir=None, speed=None):
'''density - [0-1]'''
FX.__init__(self, rect, duration)
self.updates = []
self.fading_count = 0
self.fading_sum = 0
self.look_dir = 0
self.enlarging_count = 0
self.enlarging_summ = 1
self.image = pygame.transform.scale(image, (rect.width, rect.height))
if color != None:
self.image.fill((color[0], color[1], color[2], color[3]),
None, pygame.BLEND_RGBA_MULT)
if look_dir != None:
self.look_dir = look_dir
self.rotated_image = pygame.transform.rotate(self.image,
-self.look_dir)
self.image = pygame.transform.rotate(self.image,
-self.look_dir)
self.rotated_image_base = pygame.transform.rotate(self.image,
-self.look_dir)
else:
self.rotated_image = copy.copy(self.image)
if fading != None:
self.fading = fading[0]
self.fading_tempo = fading[1]
self.updates.append(self.fade)
if enlarging != None:
self.enlarging = enlarging[0]
self.enlarging_tempo = enlarging[1]
self.updates.append(self.enlarge)
if rotating != None:
self.rotating = rotating[0]
self.rotating_tempo = rotating[1]
self.updates.append(self.rotate)
if speed != None:
self.speed = speed
State.effects.add(self)
def enlarge(self):
self.enlarging_count += 1
if self.enlarging_count > self.enlarging_tempo:
self.enlarging_count = 0
self.enlarging_summ += self.enlarging
if self.enlarging_summ > 250:
self.kill()
return
self.rotated_image = pygame.transform.scale(self.rotated_image,
(self.rect.width+self.enlarging,
self.rect.height+self.enlarging))
def rotate(self):
self.rotating_count += 1
if self.rotating_count > self.rotating_tempo:
self.rotating_count = 0
self.look_dir += self.rotating
self.rotated_image = pygame.transform.rotate(self.image, -self.look_dir)
def fade(self):
self.fading_count += 1
if self.fading_count > self.fading_tempo:
self.fading_count = 0
self.fading_sum += self.fading
if self.fading_sum > 230:
self.kill()
return
self.rotated_image_base.fill((255, 255, 255, 255-self.fading_sum),
None, pygame.BLEND_RGBA_MULT)
def update(self):
self.rotated_image = copy.copy(self.rotated_image_base)
for f in self.updates:
f()
class Object(pygame.sprite.Sprite):
'''Object(image, x, y, width=None, height=None)'''
look_dir = 0
rotated_image = 0
rotated_rect = 0
radius = None
dmg = None
time_count = 0
timer = 0
type = 0
def __init__(self, image, x, y, width=None, height=None):
pygame.sprite.Sprite.__init__(self)
if width != None:
self.image = pygame.transform.scale(image, (width, height))
self.image_alpha = pygame.transform.scale(copy.copy(image),
(width, height))
alpha = 128
self.image_alpha.fill((255, 255, 255, alpha),
None, pygame.BLEND_RGBA_MULT)
else:
self.image = image
self.image_alpha = copy.copy(image)
alpha = 128
self.image_alpha.fill((255, 255, 255, alpha),
None, pygame.BLEND_RGBA_MULT)
self.rotated_image = image
self.rotated_image_alpha = image
# Fetch the rectangle object that has the dimensions of the image
# Update the position of this object by setting the values of rect.x and rect.y
self.rect = self.image.get_rect()
self.rotated_rect = self.rect
self.rect.centerx = x
self.rect.centery = y
self.radius = self.rect.width
# movable.add(self)
def rotate(self, dir):
# Ensure that look_dir is in range [0 - 360)
if self.look_dir > 359:
self.look_dir += dir - 360
elif self.look_dir < 0:
self.look_dir += 360 + dir
else:
self.look_dir += dir
self.rotated_image = pygame.transform.rotate(self.image,
-self.look_dir)
self.rotated_image_alpha = pygame.transform.rotate(self.image_alpha,
-self.look_dir)
def get_aim_dir(self, aim):
"""
Returns the angle specifying direction to 'aim' object
"""
x = None
dx = self.rect.centerx - aim.rect.centerx
dy = self.rect.centery - aim.rect.centery
if dx > 0 and dy < 0:
aim_dir = abs(np.rad2deg(np.arctan(dx/dy)))
elif dx < 0 and dy < 0:
aim_dir = abs(np.rad2deg(np.arctan(dy/dx)))
elif dx > 0 and dy > 0:
aim_dir = abs(np.rad2deg(np.arctan(dy/dx)))
elif dx < 0 and dy > 0:
aim_dir = abs(np.rad2deg(np.arctan(dx/dy)))
else:
aim_dir = 0
if dx < 0 and dy > 0:
pass
elif dx < 0 and dy < 0:
aim_dir += 90
elif dx > 0 and dy < 0:
aim_dir += 180
elif dx > 0 and dy > 0:
aim_dir += 270
return aim_dir
def get_distance(self, obj):
"""returns distance to object x"""
return np.sqrt((self.rect.x - obj.rect.x)**2
+ (self.rect.y - obj.rect.y)**2)
def get_real_distance(self, obj):
"""get_real_distance(obj)
the shortest distance to object 'obj' with regards
to linked bounds of the map, comparing distance
on screen to distances to 8 projections of aim on sides
and corners of map"""
all_directions_distances = []
for x in range(-1, 2):
for y in range(-1, 2):
a = (self.rect.centerx
- (obj.rect.centerx + x*(State.WIDTH+obj.rect.width)))
b = (self.rect.centery
- (obj.rect.centery + y*(State.HEIGHT+obj.rect.height)))
dist = np.sqrt(a**2 + b**2)
all_directions_distances.append(dist)
return min(all_directions_distances)
def get_closest_aim_dir(self, aim):
"""
returns the angle of closest position of aim with respect to looped map.
"""
all_directions_distances = []
for x in range(-1, 2):
for y in range(-1, 2):
a = (self.rect.centerx
- (aim.rect.centerx + x*(State.WIDTH+aim.rect.width)))
b = (self.rect.centery
- (aim.rect.centery + y*(State.HEIGHT+aim.rect.height)))
dist = np.sqrt(a**2 + b**2)
all_directions_distances.append(dist)
best = all_directions_distances.index(min(all_directions_distances))
if best < 3: x = -1
elif best > 5: x = 1
else: x = 0
if (best+1)%3 == 0: y = 1
elif best in [1, 3, 6]: y = 0
else: y = -1
a = aim.rect.centerx + x*(State.WIDTH + aim.rect.width)
b = aim.rect.centery + y*(State.HEIGHT + aim.rect.height)
aim = Object(blanc, a, b)
return self.get_aim_dir(aim)
class Player(Object, Moving, Vulnerable):
'''Player(image, x, y, lives, bolt=0,
complex_sh=-1, width=None, height=None)'''
global score
bolt = 0
arr_input = []
player_hull_group = pygame.sprite.Group()
shields_orbit_group = pygame.sprite.Group()
shields = pygame.sprite.Group()
turrets = pygame.sprite.Group()
orbiting = pygame.sprite.Group()
mounts = []
hull_group_ang = 0
HP = 10
MAX_HP = 10
S_HP = 10
MAX_S_HP = 10
ROTATION = 10
ACCELERATION = 1
DEACCELERATION = 0.5
ENV_DEACCELERATION = 0.25
space_lock = False
special_lock = False
missile_lock = False
shield_lock = False
locks = [space_lock, special_lock, missile_lock, shield_lock]
#time in frames
def __init__(self, image, x, y, lives, bolt=0,
complex_sh=-1, player=True, width=None, height=None):
self.speed = [0,0]
self.lives = lives
super().__init__(image, x, y, width=width, height=height)
Moving.__init__(self)
Vulnerable.__init__(self, State.SHIP_HP[complex_sh])
"""#################FIX HP###################"""
if player == True:
self.add(State.player_group)
for i in range(lives):
r = Object(live,270 + 35*(1+i),20,30, 30)
r.add(State.interface)
State.movable.remove(r)
for x in State.complex_rects[complex_sh]:
b = Colliding(x[0], x[1], x[2], x[3], self)
self.player_hull_group.add(b)
self.bolt = bolt
self.time_count_fire = 0
self.timer_fire = State.prj_cooldown[bolt]
self.time_count_special = 0
self.timer_special = State.spec_cooldown[complex_sh]
self.time_count_missile = 0
self.timer_missile = State.prj_cooldown[State.n_bolts + bolt]
self.time_count_shield = 0
self.timer_shield = 50
self.counts = [self.time_count_fire, self.time_count_special,
self.time_count_missile, self.time_count_shield]
self.timers = [self.timer_fire, self.timer_special,
self.timer_missile, self.timer_shield]
self.distance = 0
self.orbit_ang = 0
self.player = player
def destroy(self):
self.kill()
self.rotate(0)
self.speed = [0,0]
Funcs.FX_explosion(self.rect.centerx, self.rect.centery)
for x in self.shields:
x.down()
if self.player == True:
for x in self.mounts:
x.kill()
for x in self.shields:
x.kill()
for x in self.player_hull_group:
x.kill()
self.lives += -1
if self.lives > -1:
pygame.time.set_timer(pygame.USEREVENT+2, 500)
else:
# graphics thread termination call
pygame.time.set_timer(pygame.USEREVENT+5, 10)
with open('save.pkl', 'wb') as f:
pickle.dump(State.save, f, pickle.HIGHEST_PROTOCOL)
Scripts.death_menu()
def damage(self, dmg):
self.HP += -max(0, dmg)
if self.HP < 0:
self.destroy()
if self.player == True:
return True
def show_HP(self):
gfx.box(State.screen, (10, 10, self.HP*100/self.MAX_HP, 20), (0, 255, 0, 50))
def m_add(self, mounted):
self.mounts.append(mounted)
def sh_add(self, shield):
self.shields.add(shield)
def scan(self):
min_dist = State.asteroids.sprites[0]
for i in State.asteroids:
dist = np.sqrt((self.rect.x - i.rect.x)**2
+ (self.rect.y - i.rect.y)**2)
if dist < min_dist:
min_dist = dist
return min_dist
def fire(self):
if self.locks[0] == False:
self.locks[0] = True
Funcs.shot(self, self.look_dir, self.bolt)
def update(self):
# if self.space_lock:
# self.time_count_fire += 1
# if self.timer_fire < self.time_count_fire:
# self.time_count_fire = 0
# self.space_lock = False
for n in range(len(self.locks)):
if self.locks[n]:
self.counts[n] += 1
if self.timers[n] < self.counts[n]:
self.counts[n] = 0
self.locks[n] = False
class Mounted(Object):
'''
Mounted(image, mounted_on, distance = 20, look_dir = 0,
width = 20, height = 20, restriction = None)
'''
mounted_on = None
aim = None
aim_dir = None
orbit_ang = None
# elliptic orbiting parameters
d_ang = 1 # unmounted orbiting speed
min_dist = 10
max_dist = 5
orbit_coef = 120 # Degrees before changing direction of distance movement
distance = 0
d_dist = 0
d_dist_dir = -1 # 1 or -1 -- is object getting closer or further
def __init__(self, image, mounted_on,
distance = 20, look_dir = 0,
width = 20, height = 20,
restriction = None):
super().__init__(image, width, height,
(mounted_on.rect.x + mounted_on.rect.width//4
+ distance*np.cos(np.deg2rad(mounted_on.look_dir
+ look_dir -90))),
(mounted_on.rect.y + mounted_on.rect.height//4
+ distance*np.sin(np.deg2rad(mounted_on.look_dir
+ look_dir -90)))
)
self.look_dir = mounted_on.look_dir + look_dir
self.restriction = restriction
self.mounted_on = mounted_on
self.distance = distance
self.speed = mounted_on.speed
if look_dir == 0:
self.orbit_ang = mounted_on.look_dir-180
else:
self.orbit_ang = mounted_on.look_dir+look_dir
def aim(self, aim):
x = (self.look_dir - self.get_aim_dir(aim))
if x < 5 and x > -5:
return True
elif abs(x) > 180:
self.rotate(5*np.sign(x))
else:
self.rotate(-5*np.sign(x))
def init_orbit(self, orbit_coef, d_ang, min, max, distance):
self.min_dist = min
self.max_dist = max
self.d_ang = d_ang
self.distance = distance
self.orbit_coef = orbit_coef
self.d_dist = 30*d_ang/orbit_coef
self.mounted_on.orbiting.add(self)
class Turret(Mounted):
"""Turret(image, radius, mounted_on, groups = None,
distance = 20, look_dir = 0,
width = 20, height = 20,
restriction = None, bg = bg_ball)
"""
interesting = [State.asteroids]
in_range = []
def __init__(self, image, radius, mounted_on,
groups = None, distance = 20, look_dir = 0,
width = 20, height = 20, restriction = None, bg = bg_ball):
super().__init__(image, mounted_on, distance, look_dir,
width, height, restriction)
self.radius = radius
self.locked = None
self.bg = pygame.transform.scale(bg, (width-6, height-6))
self.bg_rect = bg.get_rect()
# self.bg_rect = bg.get_rect()
if groups != None:
for i in groups:
self.interesting.append(i)
def set_priorities(self, group):
b = self.interesting.pop(State.interesting.index('group'))
self.interesting.insert(0, b)
def scan(self, group):
a = Object(blanc, self.radius, self.radius,
self.rect.centerx, self.rect.centery)
pygame.gfxdraw.circle(State.screen, self.rect.centerx, self.rect.centery,
self.radius, (0,255,0,50))
for x in group:
if pygame.sprite.collide_circle(a, x):
if x not in self.in_range:
self.in_range.append(x)
a.kill()
def scan_all(self):
self.in_range.clear()
for i in self.interesting:
self.scan(i)
# Is there anything interesting in range?
try:
if not self.locked.alive():
self.locked = None
except:
self.locked = None
if self.in_range:
return True
def lock_on(self):
if self.in_range:
self.locked = self.in_range[0]
return True
else:
return False
def auto_lock_on(self):
# Don't not switch target if it is still in range
if self.locked != None:
pass
else:
self.lock_on()
def aim_locked(self):
if self.locked != None:
self.aim(self.locked)
class T_PreAim(Turret):
mode = 2
def __init__(self, image, radius, mounted_on, bolt_number, cooldown,
groups = None, distance = 20, look_dir = 0,
width = 20, height = 20, restriction = None):
"""
Turret shoting prjoectiles with predictions of aim's
position by its speed.
'prj_speed' defines speed of projectiles.
'cooldown' - in seconds.
'bolt_numer' - index of given bolt in bolts' lists
"""
super().__init__(image, radius, mounted_on, groups, distance,
look_dir, width, height, restriction)
self.bolt_number = bolt_number
self.bolt_img = prj_imgs[bolt_number]
self.prj_speed = State.prj_speeds[bolt_number]
self.predict_pos = Object(ball_img, 1, 1, -50, 1)
self.blocked = False
self.timer = cooldown * State.FPS
self.time_count = 0
self.add(mounted_on.turrets)
mounted_on.m_add(self)
def get_predict_pos(self):
self.predict_pos.rect = copy.copy(self.locked.rect)
length = np.sqrt((self.rect.x - self.locked.rect.x)**2
+ (self.rect.y - self.locked.rect.y)**2)
try:
if (self.prj_speed*np.cos(np.deg2rad(self.look_dir))) != -99:
self.predict_pos.rect.centerx += (round(self.locked.speed[0]
*length/self.prj_speed)
*(1/self.prj_speed + 1))
except:
pass
try:
if (self.prj_speed*np.sin(np.deg2rad(self.look_dir))) != -99:
self.predict_pos.rect.centery += (round(self.locked.speed[1]
*length/self.prj_speed)
*(1/self.prj_speed + 1))
except:
pass
if self.blocked:
self.time_count += 1
if self.time_count > self.timer:
self.time_count = 0
self.blocked = False
def aim_locked(self):
if self.locked != None:
if self.aim(self.predict_pos) and not self.blocked:
self.blocked = True
Funcs.shot(self, self.look_dir, self.bolt_number)
def auto_fire(self):
self.scan_all()
self.auto_lock_on()
try:
self.get_predict_pos()
except:
pass
self.aim_locked()
def closest(self):
if self.scan_all():
self.locked = self.in_range[0]
dist = (abs(self.in_range[0].rect.x - self.mounted_on.rect.x)
+ abs(self.in_range[0].rect.y - self.mounted_on.rect.y))
for x in self.in_range:
t = (abs(x.rect.x - self.rect.x) + abs(x.rect.y - self.rect.y))
if t < dist:
dist = t
self.locked = x
try:
self.get_predict_pos()
except:
pass
self.aim_locked()
def hunt(self):
try:
if self.locked == None or not self.locked.alive():
self.scan_all()
self.lock_on()
else:
pass
except:
pass
self.lock_on()
try:
#self.aim(self.locked)
if self.locked in self.in_range:
self.get_predict_pos()
self.aim_locked()
except:
pass
mods = [auto_fire, closest, hunt]
def switch_aim(self):
if len(self.in_range) > 1:
try:
self.locked = self.in_range[self.in_range.index(self.locked)+1]
except:
self.locked = self.in_range[self.in_range.index(self.locked)-1]
else:
pass
def active(self):
self.mods[self.mode](self)
class D_PreAim(T_PreAim):
def __init__(self, image, radius, mounted_on, bolt_number, cooldown,
orbit_coef, d_ang, min, max, distance,
groups = None, look_dir = 0,
width = 24, height = 24, restriction = None):
super().__init__(image, radius, mounted_on, bolt_number, cooldown,
groups, distance, look_dir,
width, height, restriction)
self.mounted_on.turrets.remove(self)
self.init_orbit(orbit_coef, d_ang, min, max, distance)
class Script_Mob(Player):
close_range = 20
goal = None
to_do_list = []
def __init__(self, image, x, y, picked_ship=0):
super().__init__(image, x, y, lives=1, player=False)
State.script_mob_group.add(self)
self.ROTATION = State.SHIP_CONSTANTS[picked_ship][0]
self.ACCELERATION = State.SHIP_CONSTANTS[picked_ship][1]
self.DEACCELERATION = State.SHIP_CONSTANTS[picked_ship][2]
self.ENV_DEACCELERATION = State.SHIP_CONSTANTS[picked_ship][3]
self.HP = State.SHIP_CONSTANTS[picked_ship][4]
self.S_HP = State.SHIP_CONSTANTS[picked_ship][5]
def assign_goal(self, obj=None, x=None, y=None):
"""
assign_goal(obj=None, x=None, y=None)
interface function.
Assign a goal by passing the object obj (must have rect attribute)
or giving the coordinats of the goal.
"""
if obj == None:
if x == None:
print('No goal given. Both obj and x are None')
self.goal = Object(blanc, x, y)
else:
self.goal = obj
def go(self):
"""
go()
Perform actions to approach the goal
"""
dist = self.get_distance(self.goal)
if dist > self.close_range:
speed_mod = np.sqrt(self.speed[0]**2+self.speed[1]**2)
# If speed is small, turn in the direction of goal,
# otherwise, in the direction allowing greater speed vecror change
if speed_mod < 1:
t = self.look_dir - abs(self.get_aim_dir(self.goal))
else:
ang = np.arctan(self.speed[0]/self.speed[1])
spe = Object(blanc,
int(self.rect.centerx+30*np.sin(ang)
*np.sign(self.speed[1])),
int(self.rect.centery+30*np.cos(ang)
*np.sign(self.speed[1])))
true_ang = self.get_aim_dir(self.goal) - self.get_aim_dir(spe)
spe.kill()
if true_ang < -180 or true_ang > 180:
true_ang = -360*np.sign(true_ang) + true_ang
if true_ang < -90 or true_ang > 90:
t = self.get_aim_dir(self.goal)
else:
t = self.get_aim_dir(self.goal) + true_ang
# true_ang = self.get_aim_dir(self.goal) - true_ang
t = self.look_dir - t
if t > 360 or t < -360:
t += -360*np.sign(t)
if abs(t) > self.ROTATION:
if t < -180 or t > 180:
t = -t
self.rotate(-np.sign(t) * self.ROTATION)
if abs(t) < 90:
if speed_mod < ((self.DEACCELERATION+self.ENV_DEACCELERATION)
*(dist/max(speed_mod,0.001)) + self.ENV_DEACCELERATION):
self.accelerate(self.ACCELERATION)
elif speed_mod>1 and abs(true_ang) < 30:
self.accelerate(-self.DEACCELERATION)
else:
if speed_mod < ((self.DEACCELERATION+self.ENV_DEACCELERATION)
*(dist/speed_mod) + self.ENV_DEACCELERATION):
self.accelerate(-self.DEACCELERATION)
elif speed_mod>1 and true_ang < 30:
self.accelerate(self.ACCELERATION)
else:
self.to_do_list.remove(self.go)
def go_to(self, obj=None, x=None, y=None):
"""
go_to(obj=None, x=None, y=None)
interface function. Stop after reaching the goal
"""
self.assign_goal(obj=obj, x=x, y=y)
self.to_do_list.append(self.go)
def follow(self):
"""
follow()
follow the goal untill met stop_following()
"""
if self.go not in self.to_do_list:
self.to_do_list.append(self.go)
if self.follow not in self.to_do_list:
self.to_do_list.append(self.follow)
def stop_following(self):
"""
stop_following()
Removes follow() and itself from to_do_list
"""
self.to_do_list.remove(self.follow)
self.to_do_list.remove(self.stop_following)
def update(self):
"""Execute all functions in to_do_list if there is any goal"""
# Excecute all todo dunctions if goal is player
if self.goal in State.player_group:
[x() for x in self.to_do_list]
else:
try:
self.goal = State.player_group.sprites()[0]
except:
pass
class Agressor(Script_Mob):
def __init__(self, image, x, y):
super().__init__(image, x, y, 3)
# Assign goal if
try:
self.assign_goal(State.player_group.sprites()[0])
except:
pass
State.asteroids.add(self)
self.look_dir = random.randint(0, 358)
self.speed = [random.uniform(-3, 3), random.uniform(-3, 3)]
def rush(self):
dist = self.get_distance(self.goal)
if dist > self.close_range:
speed_mod = np.sqrt(self.speed[0]**2+self.speed[1]**2)
# If speed is small, turn in the direction of goal,
# otherwise, in the direction allowing greater speed vecror change
if speed_mod < 1:
t = self.look_dir - abs(self.get_aim_dir(self.goal))
else:
ang = np.arctan(self.speed[0]/self.speed[1])
# Direction of motion
spe = Object(blanc,
int(self.rect.centerx+30*np.sin(ang)
*np.sign(self.speed[1])),
int(self.rect.centery+30*np.cos(ang)
*np.sign(self.speed[1])))
true_ang = self.get_aim_dir(self.goal) - self.get_aim_dir(spe)