-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProfileMenu.py
891 lines (693 loc) · 34.4 KB
/
ProfileMenu.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
'''
Created on Aug 20, 2012
@author: Jami
'''
from consts import MIN_WINDOW_HEIGHT, MIN_WINDOW_WIDTH
from gui.basicmenu import BasicTextInput, BasicTextButton, PagedMenu, Label, \
DropdownSelector, ImageLabel, BasicMenu
from gui.gui import Frame, Element
from profile import Profile
import Utils
import consts
import math
import profile
import pygame
class ProfileMenu(Frame):
'''handles the display and user manipulations for the profile list and currently selected profile'''
current_profile = None
profile_list = None
set_profile_fxn = None
save_profiles_fxn = None
profile_view = None
profile_edit = None
profile_add = None
profile_delete = None
profile_shipselect = None
profile_shipbuy = None
profile_shipupgrade = None
ship_list = None
weapon_list = None
upgrade_list = None
def __init__(self, parent, profile, profiles, set_profile, save_profiles, **kwargs):
'''Constructor'''
super(ProfileMenu, self).__init__(parent, **kwargs)
self.current_profile = profile
self.profile_list = profiles
self.set_profile_fxn = set_profile
self.save_profiles_fxn = save_profiles
self.ship_list = kwargs.get('shiplist', [])
self.weapon_list = kwargs.get('weaponlist', [])
self.upgrade_list = kwargs.get('upgradelist', [])
self.set_profile_view(self.current_profile)
def update(self, event):
'''updates the menu based on the event'''
returnVal = super(ProfileMenu, self).update(event)
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
self.parent.main_menu_click()
return returnVal
def draw(self):
super(ProfileMenu, self).draw()
def set_profile_view(self, profile = None):
if profile and profile is not self.current_profile and profile in self.profile_list:
self.current_profile = profile
self.set_profile_fxn(self.current_profile)
if not self.profile_view:
# create the menu anew:
self.profile_view = ProfileView(self, self.current_profile)
else:
# update the current view menu
self.profile_view.set_profile(self.current_profile)
if not self.profile_view in self.children:
self.add_child(self.profile_view)
for child in self.children:
child.set_active(False)
self.profile_view.set_active(True)
def set_profile_edit(self, profile):
if profile not in self.profile_list:
self.profile_list.append(profile)
self.current_profile = profile
self.set_profile_fxn(self.current_profile)
if not self.profile_edit:
# create the edit menu
self.profile_edit = ProfileEdit(self, self.current_profile, self.save_profiles_fxn)
else:
# update the edit menu
self.profile_edit.set_profile(self.current_profile)
if not self.profile_edit in self.children:
self.add_child(self.profile_edit)
for child in self.children:
child.set_active(False)
self.profile_edit.set_active(True)
def set_profile_delete(self, profile):
if not self.profile_delete:
# create the delete menu
self.profile_delete = ProfileDelete(self, self.current_profile)
else:
# update
self.profile_delete.set_profile(self.current_profile)
if not self.profile_delete in self.children:
self.add_child(self.profile_delete)
for child in self.children:
child.set_active(False)
self.profile_delete.set_active(True)
def set_profile_shipselect(self, profile):
if not self.profile_shipselect:
# create the ship selection menu
self.profile_shipselect = ShipSelectMenu(self, self.ship_list, self.current_profile)
else:
self.profile_shipselect.set_profile(self.current_profile)
if not self.profile_shipselect in self.children:
self.add_child(self.profile_shipselect)
for child in self.children:
child.set_active(False)
self.profile_shipselect.set_active(True)
def set_profile_shipbuy(self, profile):
if self.profile_shipbuy:
self.children.remove(self.profile_shipbuy)
self.profile_shipbuy = ShipBuyMenu(self, self.ship_list, self.weapon_list, self.current_profile)
if not self.profile_shipbuy in self.children:
self.add_child(self.profile_shipbuy)
for child in self.children:
child.set_active(False)
self.profile_shipbuy.set_active(True)
def set_profile_shipupgrade(self, profile):
if self.profile_shipupgrade:
self.children.remove(self.profile_shipupgrade)
self.profile_shipupgrade = ShipUpgradeMenu(self, self.current_profile, self.upgrade_list, self.ship_list, self.weapon_list)
if not self.profile_shipupgrade in self.children:
self.add_child(self.profile_shipupgrade)
for child in self.children:
child.set_active(False)
self.profile_shipupgrade.set_active(True)
def delete_profile(self, pf):
if pf in self.profile_list:
self.profile_list.remove(pf)
if self.current_profile is pf:
if len(self.profile_list) > 0:
self.current_profile = self.profile_list[0]
else:
self.current_profile = profile.create_fresh_profile(profiles = self.profile_list)
self.profile_list.append(self.current_profile)
self.set_profile_fxn(self.current_profile)
self.save_profiles_fxn()
self.set_profile_view(self.current_profile)
def get_ship_image(self, profile):
if not profile:
profile = self.current_profile
if 'ship' in profile and self.ship_list and int(profile['ship']) >= 0 and int(profile['ship']) < len(self.ship_list):
file = self.ship_list[int(profile['ship'])].file
image = Utils.load_image(file, -1)[0]
return image
return None
class ProfileView(Frame):
'''handles the display of the current profile'''
profile = None
v_pad = 0
def __init__(self, parent, profile, **kwargs):
'''Constructor'''
super(ProfileView, self).__init__(parent, **kwargs)
self.profile = profile
self.v_pad = kwargs.get('v_pad', 5)
self.init()
def init(self):
'''initializes the view'''
width = 0
x = 0
y = 0
self.children = []
lb = Label(self, "Callsign: %s" % self.profile['name'])
lb.rect.topleft = (x, y)
y += lb.rect.height + self.v_pad
if lb.rect.width > width:
width = lb.rect.width
if not 'shots-fired' in self.profile:
self.profile['shots-fired'] = 0
if not 'shots-hit' in self.profile:
self.profile['shots-hit'] = 0
if not 'kills' in self.profile:
self.profile['kills'] = 0
if not 'deaths' in self.profile:
self.profile['deaths'] = 0
if not 'damage-dealt' in self.profile:
self.profile['damage-dealt'] = 0
if not 'damage-taken' in self.profile:
self.profile['damage-taken'] = 0
if int(self.profile['deaths']) > 0:
ratio = float(self.profile['kills']) / float(self.profile['deaths'])
else:
ratio = float(self.profile['kills'])
ratio_dec = int((ratio - int(ratio)) * 10)
lb = Label(self, "Kills: %i" % int(self.profile['kills']))
lb.rect.topleft = (x, y)
y += lb.rect.height + self.v_pad
if lb.rect.width > width:
width = lb.rect.width
lb = Label(self, "Deaths: %i" % int(self.profile['deaths']))
lb.rect.topleft = (x, y)
y += lb.rect.height + self.v_pad
if lb.rect.width > width:
width = lb.rect.width
lb = Label(self, "K/D Ratio: %i.%i" % (int(ratio), int(ratio_dec)))
lb.rect.topleft = (x, y)
y += lb.rect.height + self.v_pad
if lb.rect.width > width:
width = lb.rect.width
if float(self.profile['shots-fired']) > 0:
accuracy = float(self.profile['shots-hit']) / float(self.profile['shots-fired']) * 100
else:
accuracy = 0.0
accuracy_dec = int((accuracy - int(accuracy)) * 10)
lb = Label(self, "Shots Hit: %i" % int(self.profile['shots-hit']))
lb.rect.topleft = (x, y)
y += lb.rect.height + self.v_pad
if lb.rect.width > width:
width = lb.rect.width
lb = Label(self, "Shots Fired: %i" % int(self.profile['shots-fired']))
lb.rect.topleft = (x, y)
y += lb.rect.height + self.v_pad
if lb.rect.width > width:
width = lb.rect.width
lb = Label(self, "Accuracy: %i.%i" % (int(accuracy), accuracy_dec))
lb.rect.topleft = (x, y)
y += lb.rect.height + self.v_pad
if lb.rect.width > width:
width = lb.rect.width
lb = Label(self, "Damage Dealt: %i" % int(self.profile['damage-dealt']))
lb.rect.topleft = (x, y)
y += lb.rect.height + self.v_pad
if lb.rect.width > width:
width = lb.rect.width
lb = Label(self, "Damage Taken: %i" % int(self.profile['damage-taken']))
lb.rect.topleft = (x, y)
y += lb.rect.height + self.v_pad
if lb.rect.width > width:
width = lb.rect.width
items = []
sel_item = None
for profile in self.parent.profile_list:
item = (profile['name'], profile)
items.append(item)
if profile is self.profile:
sel_item = item
items.append(("New profile...", None))
btn = BasicTextButton(self, text = "Edit Profile", font = pygame.font.Font(None, 24), callback = self.set_edit_profile)
btn.rect.topleft = (400, 25)
btn = BasicTextButton(self, text = "Delete Profile", font = pygame.font.Font(None, 24), callback = self.set_delete_profile)
btn.rect.topleft = (400, 50)
btn = BasicTextButton(self, text= 'Select & Return to Main Menu', callback = self.parent.parent.main_menu_click, font = pygame.font.Font(None, 24))
#btn.rect.topleft = (0, -btn.rect.height - self.v_pad)
ds = DropdownSelector(self, items, sel_item, on_select = self.select_profile)
ds.rect.topleft = (400, 0)
screen = pygame.display.get_surface()
draw_rect = pygame.rect.Rect(0, 0, 0, 0)
for child in self.children:
if child.is_active():
if child.rect.left + child.rect.width > draw_rect.width:
draw_rect.width = child.rect.left + child.rect.width
if child.rect.top + child.rect.height > draw_rect.height:
draw_rect.height = child.rect.top + child.rect.height
# display the player's ship
image = self.parent.get_ship_image(self.profile)
if image:
lb = ImageLabel(self, image, rotate = True, angle = 90)
lb.rect.center = draw_rect.center
btn.rect.bottomright = draw_rect.bottomright
offset_x = (screen.get_rect().width - draw_rect.width) * 0.5
offset_y = (screen.get_rect().height - draw_rect.height) * 0.5
for child in self.children:
child.rect.topleft = (child.rect.left + offset_x, child.rect.top + offset_y)
def select_profile(self, **kwargs):
pf = kwargs.get('value', None)
if pf and pf in self.parent.profile_list:
self.parent.set_profile_view(pf)
if not pf:
# set up a new profile
self.parent.set_profile_edit(profile.create_fresh_profile(profiles = self.parent.profile_list))
def set_edit_profile(self):
self.parent.set_profile_edit(self.profile)
def set_delete_profile(self):
self.parent.set_profile_delete(self.profile)
def set_profile(self, profile):
self.profile = profile
self.init()
def get_profile(self):
return self.profile
def draw(self):
screen = pygame.display.get_surface()
draw_rect = None #pygame.rect.Rect(0, 0, 0, 0)
for child in self.children:
if child.is_active():
if not draw_rect:
draw_rect = child.rect.copy()
if child.rect.left < draw_rect.left:
draw_rect.width += draw_rect.left - child.rect.left
draw_rect.left = child.rect.left
if child.rect.top < draw_rect.top:
draw_rect.height += draw_rect.top - child.rect.top
draw_rect.top = child.rect.top
if child.rect.left + child.rect.width > draw_rect.left + draw_rect.width:
draw_rect.width = (child.rect.left + child.rect.width) - draw_rect.left
if child.rect.top + child.rect.height > draw_rect.top + draw_rect.height:
draw_rect.height = (child.rect.top + child.rect.height) - draw_rect.top
offset_x = (screen.get_rect().width - draw_rect.width) * 0.5
offset_y = (screen.get_rect().height - draw_rect.height) * 0.5
'''for child in self.children:
if child.is_active():
save_rect = child.rect.copy()
child.rect.topleft = (child.rect.left + offset_x, child.rect.top + offset_y)
child.draw()
child.rect.center = save_rect.center'''
for child in self.children:
if child.is_active():
child.draw()
#pygame.gfxdraw.rectangle(screen, pygame.rect.Rect(draw_rect.left - 5, draw_rect.top - 5, draw_rect.width + 10, draw_rect.height + 10), (51, 102, 255))
class ProfileEdit(Frame):
profile = None
v_pad = 0
callsign_input = None
save_profiles_fxn = None
def __init__(self, parent, profile, save_profiles, **kwargs):
'''Constructor'''
super(ProfileEdit, self).__init__(parent, **kwargs)
self.save_profiles_fxn = save_profiles
self.profile = profile
self.v_pad = kwargs.get('v_pad', 5)
self.init()
def init(self):
'''initializes the view'''
self.children = []
width = 0
x = 0
y = 0
# callsign edit
callsign = "newbie"
if 'name' in self.profile:
callsign = self.profile['name']
cl = BasicTextInput(self, label = "Callsign", value = callsign, font = pygame.font.Font(None, 24))
cl.rect.topleft = (0, 0)
self.callsign_input = cl
y = cl.rect.top + cl.rect.height + self.v_pad
# display the player's ship
image = self.parent.get_ship_image(self.profile)
if image:
lb = ImageLabel(self, image, rotate = True, angle = 90)
lb.rect.left = cl.rect.left + (cl.rect.width - lb.rect.width) * 0.5
dim = lb.rect.height
if lb.rect.width > dim: dim = lb.rect.width
lb.rect.top = cl.rect.top + cl.rect.height + self.v_pad + dim - lb.rect.height
y = lb.rect.top + lb.rect.height + self.v_pad + dim - lb.rect.height
# ship selector
bn = BasicTextButton(self, text = "Select Ship...", font = pygame.font.Font(None, 24), callback = self.set_shipselect)
bn.rect.center = cl.rect.center
bn.rect.top = y
y += bn.rect.height + self.v_pad
# buy ship
bn = BasicTextButton(self, text = "Buy Ship...", font = pygame.font.Font(None, 24), callback = self.set_shipbuy)
bn.rect.center = cl.rect.center
bn.rect.top = y
y += bn.rect.height + self.v_pad
# upgrade ship
bn = BasicTextButton(self, text = "Upgrade Ship...", font = pygame.font.Font(None, 24), callback = self.set_shipupgrade)
bn.rect.center = cl.rect.center
bn.rect.top = y
y += bn.rect.height + self.v_pad
# save
bn = BasicTextButton(self, text = "Save Changes", font = pygame.font.Font(None, 24), callback = self.save_profile)
bn.rect.center = cl.rect.center
bn.rect.top = y
y += bn.rect.height + self.v_pad
# cancel
bn = BasicTextButton(self, text = "Cancel", font = pygame.font.Font(None, 24), callback = self.parent.set_profile_view)
bn.rect.center = cl.rect.center
bn.rect.top = y
y += bn.rect.height + self.v_pad
screen = pygame.display.get_surface()
draw_rect = pygame.rect.Rect(0, 0, 0, 0)
for child in self.children:
if child.is_active():
if child.rect.left + child.rect.width > draw_rect.width:
draw_rect.width = child.rect.left + child.rect.width
if child.rect.top + child.rect.height > draw_rect.height:
draw_rect.height = child.rect.top + child.rect.height
offset_x = (screen.get_rect().width - draw_rect.width) * 0.5
offset_y = (screen.get_rect().height - draw_rect.height) * 0.5
for child in self.children:
child.rect.topleft = (child.rect.left + offset_x, child.rect.top + offset_y)
def set_profile(self, profile):
self.profile = profile
self.init()
def save_profile(self):
self.profile['name'] = self.callsign_input.value
self.save_profiles_fxn()
self.parent.set_profile_view()
def set_shipselect(self):
self.profile['name'] = self.callsign_input.value
self.save_profiles_fxn()
self.parent.set_profile_shipselect(self.profile)
def set_shipbuy(self):
self.profile['name'] = self.callsign_input.value
self.save_profiles_fxn()
self.parent.set_profile_shipbuy(self.profile)
def set_shipupgrade(self):
self.profile['name'] = self.callsign_input.value
self.save_profiles_fxn()
self.parent.set_profile_shipupgrade(self.profile)
class ProfileDelete(Frame):
profile = None
v_pad = 0
def __init__(self, parent, profile, **kwargs):
super(ProfileDelete, self).__init__(parent, **kwargs)
self.profile = profile
self.v_pad = kwargs.get('v_pad', 5)
self.init()
def init(self):
self.children = []
y = 0
lb = Label(self, text = "Do you really want to delete profile %s?" % self.profile['name'])
y += lb.rect.height + self.v_pad
bn = BasicTextButton(self, text = "Yes", callback = self.parent.delete_profile, callback_kwargs = {'pf': self.profile})
bn.rect.centerx = lb.rect.centerx
bn.rect.top = y
y += bn.rect.height + self.v_pad
bn = BasicTextButton(self, text = "Cancel", callback = self.parent.set_profile_view)
bn.rect.centerx = lb.rect.centerx
bn.rect.top = y
y += bn.rect.height + self.v_pad
screen = pygame.display.get_surface()
draw_rect = pygame.rect.Rect(0, 0, 0, 0)
for child in self.children:
if child.is_active():
if child.rect.left + child.rect.width > draw_rect.width:
draw_rect.width = child.rect.left + child.rect.width
if child.rect.top + child.rect.height > draw_rect.height:
draw_rect.height = child.rect.top + child.rect.height
offset_x = (screen.get_rect().width - draw_rect.width) * 0.5
offset_y = (screen.get_rect().height - draw_rect.height) * 0.5
for child in self.children:
child.rect.topleft = (child.rect.left + offset_x, child.rect.top + offset_y)
def set_profile(self, profile):
self.profile = profile
self.init()
class ShipSelectMenu(PagedMenu):
shiplist = None
profile = None
def __init__(self, parent, shiplist, profile, **kwargs):
if not 'back_btn_text' in kwargs: kwargs['back_btn_text'] = "< Back"
if not 'back_btn_callback' in kwargs: kwargs['back_btn_callback'] = self.back_click
if not 'item_callback' in kwargs: kwargs['item_callback'] = self.ship_click
self.shiplist = shiplist
if self.shiplist:
items = []
for ship in self.shiplist:
if ship.player_flyable and str(ship.id) in profile.shiplist: items.append((Utils.load_image(ship.file, -1)[0], ship.id))
kwargs['items'] = items
super(ShipSelectMenu, self).__init__(parent, **kwargs)
self.profile = profile
def ship_click(self, **kwargs):
id = kwargs.get('value', 0)
self.profile['ship'] = id
self.parent.set_profile_edit(self.profile)
def back_click(self, **kwargs):
self.parent.set_profile_edit(self.profile)
def set_profile(self, pf):
self.profile = pf
class ShipBuyMenu(PagedMenu):
shiplist = None
weaponlist = None
profile = None
selected_ship = None
title_color = None
desc_color = None
desc_max_width = 1024
smallfont = None
def __init__(self, parent, shiplist, weaponlist, profile, **kwargs):
if not 'back_btn_text' in kwargs: kwargs['back_btn_text'] = "< Back"
if not 'back_btn_callback' in kwargs: kwargs['back_btn_callback'] = self.back_click
if not 'item_callback' in kwargs: kwargs['item_callback'] = self.ship_click
self.smallfont = kwargs.get('smallfont', pygame.font.Font(None, 16))
self.shiplist = shiplist
self.weaponlist = weaponlist
if self.shiplist:
items = []
for ship in self.shiplist:
if ship.player_flyable and str(ship.id) not in profile.shiplist :
items.append((Utils.load_image(ship.file, -1)[0], ship.id))
kwargs['items'] = items
self.profile = profile
super(ShipBuyMenu, self).__init__(parent, **kwargs)
self.title_color = kwargs.get('title_color', (255, 255, 100))
self.desc_color = kwargs.get('desc_color', (200, 200, 200))
self.desc_max_width = int(kwargs.get('desc_width', 1024))
def draw(self):
'''draws the ship selection'''
draw_rect = super(ShipBuyMenu, self).draw()
screen = pygame.display.get_surface()
# draw the credit label
cr_text = "Credit Balance: $%s" % self.profile['credits']
size = self.font.size(cr_text)
color = (255, 255, 255)
if self.selected_ship and int(self.selected_ship.cost) > int(self.profile['credits']):
color = (255, 0, 0)
screen.blit(self.font.render(cr_text, 1, color), (draw_rect.right - size[0], draw_rect.top - size[1]))
if self.selected_ship:
y = draw_rect.bottom + self.v_pad * 3
size = self.font.size(self.selected_ship.name)
x = (screen.get_width() - size[0]) * 0.5
screen.blit(self.font.render("%s ($%i)" % (self.selected_ship.name, self.selected_ship.cost), 1, self.title_color), (x, y))
if x < draw_rect.left: draw_rect.left = x
y += size[1] + self.v_pad
stats = ["Health %i (%.1f)" % (self.selected_ship.health, self.selected_ship.hregen),
"Shields %i (%.1f)" % (self.selected_ship.shields, self.selected_ship.sregen),
"Armor %i" % self.selected_ship.armor,
"Speed %i" % self.selected_ship.speed,
"Turn %i" % self.selected_ship.turn]
s_max_x = 0
for s in stats:
size = self.font.size(s)
if size[0] > s_max_x: s_max_x = size[0]
weapons = []
i = 0
for wpdict in self.selected_ship.weapons:
i += 1
wp = self.weaponlist[int(wpdict['id'])]
weapons.append("(%i) %s" % (i, wp.name))
weapons.append(" Ammo %i (%.1f)" % (wp.max_ammo, wp.ammo_regen))
weapons.append(" Damage (Fire Rate) %i (%i)" % (wp.base_damage, wp.fire_rate))
weapons.append(" Range (Speed) %i (%.1f)" % ((wp.bullet_speed * wp.bullet_ticks), wp.bullet_speed))
x = screen.get_width() * 0.5 - s_max_x - self.h_pad
y_start = y
if x < draw_rect.left: draw_rect.left = x
for s in stats:
screen.blit(self.font.render(s, 1, self.desc_color), (x, y))
y += self.v_pad + self.font.size(s)[1]
x = screen.get_width() * 0.5 + self.h_pad
y = y_start
screen.blit(self.font.render("Weapons:", 1, self.desc_color), (x, y))
y += self.v_pad + self.font.size("Weapons:")[1]
y_start = y
i = 0
max_x = 0
x -= self.h_pad
for w in weapons:
if i % (len(weapons) / len(self.selected_ship.weapons)) == 0:
x += max_x + self.h_pad
y = y_start
screen.blit(self.smallfont.render(w, 1, self.desc_color), (x, y))
size = self.smallfont.size(w)
y += self.v_pad + size[1]
if x + size[0] > draw_rect.right:
draw_rect.width = x + size[0] - draw_rect.left
if size[0] > max_x: max_x = size[0]
i += 1
draw_rect.height = y - draw_rect.top
return draw_rect
def ship_click(self, **kwargs):
id = kwargs.get('value', 0)
if int(self.profile['credits']) >= int(self.shiplist[id].cost):
self.profile['credits'] = int(self.profile['credits']) - int(self.shiplist[id].cost)
self.profile.shiplist[str(id)] = {'id': id, 'upgrades': ''}
self.parent.set_profile_edit(self.profile)
def back_click(self, **kwargs):
self.parent.set_profile_edit(self.profile)
def set_profile(self, pf):
self.profile = pf
def mouse_over_callback(self, child):
if self.selected_item and self.selected_item is not child:
self.selected_item.on_mouse_off()
self.selected_ship = self.find_ship(child.callback_kwargs.get('value', None))
self.selected_item = child
def find_ship(self, id):
if id is None: return None
for ship in self.shiplist:
if ship.id == id:
return ship
return None
class ShipUpgradeMenu(Frame):
upgradelist = None
shiplist = None
weaponlist = None
profile = None
healthLabel = None
shieldLabel = None
armorLabel = None
speedLabel = None
weaponLabel = None
healthButton = None
shieldButton = None
armorButton = None
speedButton = None
weaponButton = None
backButton = None
creditLabel = None
shipImage = None
width = 600
height = 300
def __init__(self, parent, profile, upgradelist, shiplist, weaponlist, **kwargs):
super(ShipUpgradeMenu, self).__init__(parent, **kwargs)
self.profile = profile
self.shiplist = shiplist
self.upgradelist = upgradelist
self.weaponlist = weaponlist
self.backButton = BasicTextButton(self, text = '< Back', callback = self.back_click)
self.backButton.rect.topleft = (0, 0)
self.creditLabel = Label(self, "Credits $%i" % int(self.profile['credits']))
self.creditLabel.rect.topright = (self.width, 0)
self.shipImage = ImageLabel(self, self.shiplist[int(profile['ship'])].image, rotate = True)
max_d = self.shipImage.image.get_width()
if self.shipImage.image.get_height() > max_d:
max_d = self.shipImage.image.get_height()
self.shipImage.rect.center = (self.width * 0.5, max_d * 0.5)
y = max_d + 5
self.healthLabel = Label(self, "Health %i (%.1f)")
self.healthLabel.rect.topleft = (0, y)
self.healthButton = BasicTextButton(self, text = "+", callback = self.upgrade_click, callback_kwargs = {})
self.healthButton.rect.topleft = (self.width * 0.5, y)
y = self.healthButton.rect.bottom + 5
self.shieldLabel = Label(self, "Shield %i (%.1f)")
self.shieldLabel.rect.topleft = (0, y)
self.shieldButton = BasicTextButton(self, text = "+", callback = self.upgrade_click, callback_kwargs = {})
self.shieldButton.rect.topleft = (self.width * 0.5, y)
y = self.shieldButton.rect.bottom + 5
self.armorLabel = Label(self, "Armor %i")
self.armorLabel.rect.topleft = (0, y)
self.armorButton = BasicTextButton(self, text = "+", callback = self.upgrade_click, callback_kwargs = {})
self.armorButton.rect.topleft = (self.width * 0.5, y)
y = self.armorButton.rect.bottom + 5
self.speedLabel = Label(self, "Speed (Turn) %i (%.1f)")
self.speedLabel.rect.topleft = (0, y)
self.speedButton = BasicTextButton(self, text = "+", callback = self.upgrade_click, callback_kwargs = {})
self.speedButton.rect.topleft = (self.width * 0.5, y)
y = self.speedButton.rect.bottom + 5
self.weaponLabel = Label(self, "Damage %i / %i")
self.weaponLabel.rect.topleft = (0, y)
#self.weaponButton = BasicTextButton(self, text = "+")
#self.weaponButton.rect.topleft = (self.width * 0.5, y)
#y = self.weaponButton.rect.bottom + 5
offset = ((pygame.display.get_surface().get_width() - self.width) * 0.5, (pygame.display.get_surface().get_height() - self.height) * 0.5)
for c in self.children:
c.rect.topleft = (c.rect.left + offset[0], c.rect.top + offset[1])
self.update_elements()
def update_elements(self):
ship = self.shiplist[int(self.profile['ship'])]
upgrades = Utils.parse_intlist(self.profile.shiplist[str(self.profile['ship'])]['upgrades'])
wp_dmg = []
for w in ship.weapons:
wp_dmg.append(self.weaponlist[int(w['id'])].base_damage)
for up in upgrades:
upgrade = self.upgradelist[up]
ship.health += upgrade.health
ship.hregen += upgrade.hregen
ship.shields += upgrade.shields
ship.sregen += upgrade.sregen
ship.armor += upgrade.armor
ship.speed += upgrade.speed
ship.turn += upgrade.turn
for w in wp_dmg:
w += upgrade.damage
if len(wp_dmg) < 2:
wp_dmg.append(-1)
if len(wp_dmg) < 2:
wp_dmg.append(-1)
self.healthLabel.set_text(self.healthLabel.text % (ship.health, ship.hregen))
self.shieldLabel.set_text(self.shieldLabel.text % (ship.shields, ship.sregen))
self.armorLabel.set_text(self.armorLabel.text % ship.armor)
self.speedLabel.set_text(self.speedLabel.text % (ship.speed, ship.turn))
self.weaponLabel.set_text(self.weaponLabel.text % (wp_dmg[0], wp_dmg[1]))
h_upgrade = None
s_upgrade = None
a_upgrade = None
p_upgrade = None
# figure out what options to allow the user to buy
for up in self.upgradelist:
if not int(self.profile['ship']) in up.fits:
# this doesnt fit, skip it
continue
if not h_upgrade and up.type.count('health') > 0:
h_upgrade = up
if not s_upgrade and up.type.count('shield') > 0:
s_upgrade = up
if not a_upgrade and up.type.count('armor') > 0:
a_upgrade = up
if not p_upgrade and up.type.count('speed') > 0:
p_upgrade = up
if h_upgrade:
self.healthButton.callback_kwargs['value'] = h_upgrade
self.healthButton.set_text("$%i: +%i (+%.1f)" % (h_upgrade.cost, h_upgrade.health, h_upgrade.hregen))
if s_upgrade:
self.shieldButton.callback_kwargs['value'] = s_upgrade
self.shieldButton.set_text("$%i: +%i (+%.1f)" % (s_upgrade.cost, s_upgrade.shields, s_upgrade.sregen))
if a_upgrade:
self.armorButton.callback_kwargs['value'] = a_upgrade
self.armorButton.set_text("$%i: +%i" % (a_upgrade.cost, a_upgrade.armor))
if p_upgrade:
self.speedButton.callback_kwargs['value'] = p_upgrade
self.speedButton.set_text("$%i: +%i (+%.1f)" % (p_upgrade.cost, p_upgrade.speed, p_upgrade.turn))
def draw(self):
super(ShipUpgradeMenu, self).draw()
def back_click(self, **kwargs):
self.parent.set_profile_edit(self.profile)
def upgrade_click(self, **kwargs):
pass
# .
# .
# . . .