forked from Leemoose/Roguelike-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.py
1754 lines (1507 loc) · 92.4 KB
/
display.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 pygame
import pygame_gui
import items as I
import ui
import objects as O
import monster as M
class Buttons:
def __init__(self):
self.buttons = {}
def add(self, button, name):
self.buttons[name] = button
"""
def render_button(self, key):
font = pygame.font.Font('freesansbold.ttf', 32)
button = self.buttons[key]
text = font.render(key, True, (255, 255, 255))
text_width, text_height = font.size("Enter: Play!")
scr.win.blit(text, (scr.screen_width / 2 - text_width / 2, scr.screen_height * 85 / 100 + button.height / 2 - text_height / 2))
"""
class Button:
# A button is anything in game that you could click
def __init__(self, screen_width, screen_height, asset, modx, mody, action, positionx, positiony):
self.width = screen_width * modx
self.height = screen_height * mody
self.modx = modx #How large in fraction relative to the screen
self.mody = mody #How tall in fraction relative to the screen
self.img = pygame.transform.scale(pygame.image.load("assets/button.png"), (self.width, self.height))
self.action = action #See keyboard for list of actions
self.positionx = positionx #center of button
self.positiony = positiony #center of button
def scale(self, screen_width, screen_height):
#rescaling the button size
self.img = pygame.transform.scale(self.img, (screen_width * self.modx, screen_height * self.mody))
def clicked(self, x, y):
#x,y is position clicked
cornerx, cornery = self.get_position()
return (cornerx < x and x < cornerx + self.width) and (cornery < y and y < cornery + self.height)
def get_position(self):
return self.positionx - self.width // 2, self.positiony + self.height // 2
class Display:
"""
Display is responsible for put images in the screen. Currently have it set that each function will update a
seperate part of the game.
"""
def __init__(self, width, height, textSize, textWidth, textHeight):
pygame.display.set_caption('Tiles')
self.win = pygame.display.set_mode((width, height), pygame.RESIZABLE)
self.screen_width = width
self.screen_height = height
self.textWidth = textWidth
self.textHeight = textHeight
self.textSize = textSize
self.uiManager = pygame_gui.UIManager((width, height), "assets/theme.json")
self.windows = []
self.clock = pygame.time.Clock()
self.buttons = Buttons()
self.colorDict = None
def screen_to_tile(self, player, x, y):
xplayerscreen, yplayerscreen = self.r_x * self.textSize + self.textSize // 2, self.r_y * self.textSize + self.textSize // 2
xdiff = x - xplayerscreen + 10
ydiff = y - yplayerscreen + 10
return (player.x + xdiff // self.textSize, player.y + ydiff//self.textSize)
def update_sizes(self):
self.screen_width, self.screen_height = self.win.get_size()
def create_display(self, loop):
self.uiManager.clear_and_reset()
#FPS counter
#fps_counter = ui.FPSCounter(
# pygame.Rect((0,0),(400,40)),
# self.uiManager
#)
tileDict = loop.tileDict
monsterID = loop.monster_dict
player = loop.player
messages = loop.messages
action_screen_width = self.screen_width * 3 // 4
action_screen_height = self.screen_height * 5 // 6
num_tiles_wide = action_screen_width // self.textSize
num_tiles_height = action_screen_height // self.textSize
r_x = num_tiles_wide // 2
r_y = num_tiles_height // 2
self.x_start = player.x - r_x
self.x_end = player.x + r_x
self.y_start = player.y - r_y
self.y_end = player.y + r_y
stats_offset_from_left = action_screen_width
stats_offset_from_top = 0
stats_width = self.screen_width - stats_offset_from_left
stats_height = self.screen_height // 3
map_tile_size = 8
map_offset_from_left = action_screen_width
map_offset_from_top = stats_height
map_width = self.screen_width - action_screen_width
map_message_width = 80
map_height = self.screen_height // 4
map_message_height = 30
num_map_tiles_wide = map_width // map_tile_size
num_map_tiles_height = map_height // map_tile_size
r_map_x = num_map_tiles_wide // 2
r_map_y = num_map_tiles_height // 2
message_offset_from_left = 0
message_offset_from_top = action_screen_height
message_width = action_screen_width *5 // 12 - 2 * message_offset_from_left
message_height = self.screen_height - action_screen_height
skill_bar_height = self.screen_height - action_screen_height
skill_bar_offset_from_left = message_offset_from_left + message_width
skill_bar_width = stats_offset_from_left - skill_bar_offset_from_left
skill_bar_offset_from_top = action_screen_height
num_skill_buttons = 8
skill_button_width = (action_screen_width - skill_bar_offset_from_left) // (num_skill_buttons + 1)
skill_button_height = (self.screen_height - action_screen_height) * 3 // 4
skill_button_offset_from_top = (self.screen_height - action_screen_height) // 8 + skill_bar_offset_from_top
skill_button_offset_from_each_other_width = (action_screen_width - skill_bar_offset_from_left) // (num_skill_buttons + 1)// (num_skill_buttons + 1)
views_num_buttons_height = 3
views_num_buttons_width = 3
views_offset_from_left = action_screen_width
views_offset_from_top = map_offset_from_top + map_height
views_width = (self.screen_width - action_screen_width)
views_height = (self.screen_height - map_offset_from_top - map_height)
views_button_width = (self.screen_width - action_screen_width) // (views_num_buttons_width + 1)
views_button_height = (self.screen_height - map_offset_from_top - map_height) // (views_num_buttons_height + 1)
views_button_offset_from_each_other_height = (self.screen_height - map_offset_from_top - map_height) // (
views_num_buttons_height + 1) // (views_num_buttons_height + 1)
views_button_offset_from_each_other_width = (self.screen_width -action_screen_width) // (
views_num_buttons_width + 1) // (views_num_buttons_width + 1)
# Writing messages
text_box = ui.MessageBox(
pygame.Rect((message_offset_from_left, message_offset_from_top),
(message_width, message_height)),
manager=self.uiManager,
loop=loop)
#Map box
self.draw_empty_box(map_offset_from_left,
map_offset_from_top,
map_width, map_height)
#Depth
depth_label = ui.DepthDisplay(pygame.Rect((map_offset_from_left, map_offset_from_top),
(map_message_width, map_message_height)),
manager=self.uiManager,
loop=loop)
stat_box = ui.StatBox(
pygame.Rect((stats_offset_from_left, stats_offset_from_top), (stats_width, stats_height)),
self.uiManager,
player
)
button_num_height = 0
button_num_width = 0
self.draw_empty_box(views_offset_from_left,
views_offset_from_top,
views_width, views_height)
button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((
views_offset_from_left + views_button_offset_from_each_other_width+ (views_button_offset_from_each_other_width + views_button_width) * button_num_width,
views_offset_from_top + views_button_offset_from_each_other_height+ (views_button_offset_from_each_other_height + views_button_height) * button_num_height),
(views_button_width, views_button_height)),
text="(I)nventory",
manager=self.uiManager,
starting_height=800)
button.action = "i"
self.buttons.add(button, "i")
button_num_height += 1
button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((
views_offset_from_left + views_button_offset_from_each_other_width+ (views_button_offset_from_each_other_width + views_button_width) * button_num_width,
views_offset_from_top + views_button_offset_from_each_other_height+ (views_button_offset_from_each_other_height + views_button_height) * button_num_height),
(views_button_width, views_button_height)),
text="(E)quip",
manager=self.uiManager,
starting_height=800)
button.action = "e"
self.buttons.add(button, "e")
button_num_height += 1
button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((
views_offset_from_left + views_button_offset_from_each_other_width+ (views_button_offset_from_each_other_width + views_button_width) * button_num_width,
views_offset_from_top + views_button_offset_from_each_other_height+ (views_button_offset_from_each_other_height + views_button_height) * button_num_height),
(views_button_width, views_button_height)),
text="Rest(z)",
manager=self.uiManager,
starting_height=800)
button.action = "z"
self.buttons.add(button, "z")
button_num_height = 0
button_num_width += 1
button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((
views_offset_from_left + views_button_offset_from_each_other_width+ (views_button_offset_from_each_other_width + views_button_width) * button_num_width,
views_offset_from_top + views_button_offset_from_each_other_height+ (views_button_offset_from_each_other_height + views_button_height) * button_num_height),
(views_button_width, views_button_height)),
text="(Q)uaff",
manager=self.uiManager,
starting_height=800)
button.action = "q"
self.buttons.add(button, "q")
button_num_height += 1
button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((
views_offset_from_left + views_button_offset_from_each_other_width+ (views_button_offset_from_each_other_width + views_button_width) * button_num_width,
views_offset_from_top + views_button_offset_from_each_other_height+ (views_button_offset_from_each_other_height + views_button_height) * button_num_height),
(views_button_width, views_button_height)),
text="(R)ead",
manager=self.uiManager,
starting_height=800)
button.action = "r"
self.buttons.add(button, "r")
button_num_height += 1
button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((
views_offset_from_left + views_button_offset_from_each_other_width+ (views_button_offset_from_each_other_width + views_button_width) * button_num_width,
views_offset_from_top + views_button_offset_from_each_other_height+ (views_button_offset_from_each_other_height + views_button_height) * button_num_height),
(views_button_width, views_button_height)),
text="Aut(o)explore",
manager=self.uiManager,
starting_height=800)
button.action = "o"
self.buttons.add(button, "o")
button_num_height = 0
button_num_width += 1
button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((
views_offset_from_left + views_button_offset_from_each_other_width+ (views_button_offset_from_each_other_width + views_button_width) * button_num_width,
views_offset_from_top + views_button_offset_from_each_other_height+ (views_button_offset_from_each_other_height + views_button_height) * button_num_height),
(views_button_width, views_button_height)),
text="Pause(esc)",
manager=self.uiManager,
starting_height=800)
button.action = "esc"
self.buttons.add(button, "esc")
button_num_height += 1
button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((
views_offset_from_left + views_button_offset_from_each_other_width+ (views_button_offset_from_each_other_width + views_button_width) * button_num_width,
views_offset_from_top + views_button_offset_from_each_other_height+ (views_button_offset_from_each_other_height + views_button_height) * button_num_height),
(views_button_width, views_button_height)),
text="(S)tairs",
manager=self.uiManager,
starting_height=800)
button.action = "s"
self.buttons.add(button, "s")
button_num_height += 1
button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((
views_offset_from_left + views_button_offset_from_each_other_width+ (views_button_offset_from_each_other_width + views_button_width) * button_num_width,
views_offset_from_top + views_button_offset_from_each_other_height+ (views_button_offset_from_each_other_height + views_button_height) * button_num_height),
(views_button_width, views_button_height)),
text="(G)rab",
manager=self.uiManager,
starting_height=800)
button.action = "g"
self.buttons.add(button, "g")
#if target_to_display != None:
# clear_target = self.draw_examine_window(target_to_display, tileDict, floormap, monster_map, monsterID, item_ID, player)
# if clear_target:
# target_to_display = None
num_skill = len(player.character.skills)
if num_skill == 0:
self.draw_empty_box(skill_bar_offset_from_left,
skill_bar_offset_from_top,
skill_bar_width, skill_bar_height)
else:
self.draw_empty_box(skill_bar_offset_from_left,
skill_bar_offset_from_top,
skill_bar_width, skill_bar_height)
for i, skill in enumerate(player.character.skills):
img1 = pygame.transform.scale(tileDict.tiles[skill.render_tag],
(skill_button_width, skill_button_height))
img2 = pygame.transform.scale(tileDict.tiles[-skill.render_tag],
(skill_button_width, skill_button_height))
button = ui.SkillButton(
rect=pygame.Rect((
skill_bar_offset_from_left + skill_button_offset_from_each_other_width +(skill_button_offset_from_each_other_width + skill_button_width) * i,
skill_button_offset_from_top),
(skill_button_width, skill_button_height)),
manager=self.uiManager,
player=player,
index=i,
img1=img1,
img2=img2,
loop=loop,
object_id='#skill_button')
button.action = chr(ord("1") + i)
# self.draw_on_button(button, img, chr(ord("1") + i), (skill_button_width, skill_button_height), shrink=True,
# offset_factor=10, text_offset=(12, (0.6)))
self.buttons.add(button, chr(ord("1") + i))
healthBar = ui.HealthBar(pygame.Rect((stats_offset_from_left + 70, stats_offset_from_top + 12), (stats_width//3, stats_height//12)), self.uiManager, player)
manaBar = ui.ManaBar(pygame.Rect((stats_offset_from_left + 70, stats_offset_from_top + 38), (stats_width//3, stats_height//12)), self.uiManager, player)
def update_display(self, loop):
self.win.fill((0,0,0))
floormap = loop.generator.tile_map
tileDict = loop.tileDict
monsterID = loop.monster_dict
item_ID = loop.item_dict
monster_map = loop.monster_map
player = loop.player
messages = loop.messages
target_to_display = loop.screen_focus
action_screen_offset_from_left = 0
action_screen_offset_from_top = 0
action_screen_width = self.screen_width * 3 // 4
action_screen_height = self.screen_height * 5 // 6
num_tiles_wide = action_screen_width // self.textSize
num_tiles_height = action_screen_height // self.textSize
self.r_x = num_tiles_wide // 2
self.r_y = num_tiles_height // 2
self.x_start = player.x - self.r_x
self.x_end = player.x + self.r_x
self.y_start = player.y - self.r_y
self.y_end = player.y + self.r_y
stats_offset_from_left = action_screen_width
stats_offset_from_top = 0
stats_width = self.screen_width - stats_offset_from_left
stats_height = self.screen_height // 3
map_tile_size = 5
map_offset_from_left = action_screen_width
map_offset_from_top = stats_height
map_width = self.screen_width - action_screen_width
map_message_width = 30
map_height = self.screen_height // 4
map_message_height = 15
num_map_tiles_wide = map_width // map_tile_size
num_map_tiles_height = map_height // map_tile_size
r_map_x = num_map_tiles_wide // 2
r_map_y = num_map_tiles_height // 2
x_map_start = player.x - r_map_x
x_map_end = player.x + r_map_x
y_map_start = player.y - r_map_y
y_map_end = player.y + r_map_y
message_offset_from_left = 0
message_offset_from_top = action_screen_height
message_width = action_screen_width // 2 - 2 * message_offset_from_left
message_height = self.screen_height - action_screen_height
skill_bar_height = self.screen_height - action_screen_height
skill_bar_offset_from_left = message_offset_from_left + message_width
skill_bar_width = stats_offset_from_left - skill_bar_offset_from_left
skill_bar_offset_from_top = action_screen_height
num_skill_buttons = 6
skill_button_width = (action_screen_width - skill_bar_offset_from_left) // (num_skill_buttons + 1)
skill_button_height = (self.screen_height - action_screen_height) * 3 // 4
skill_button_offset_from_top = (self.screen_height - action_screen_height) // 8 + skill_bar_offset_from_top
skill_button_offset_from_each_other_width = (action_screen_width - skill_bar_offset_from_left) // (num_skill_buttons + 1)// (num_skill_buttons + 1)
views_num_buttons_height = 3
views_num_buttons_width = 2
views_width = (self.screen_width - action_screen_width)
views_height = (self.screen_height - map_offset_from_top - map_height)
views_offset_from_left = action_screen_width
views_offset_from_top = map_offset_from_top + map_height
views_button_width = (self.screen_width - action_screen_width) // (views_num_buttons_width + 1)
views_button_height = (self.screen_height - map_offset_from_top - map_height) // (views_num_buttons_height + 1)
views_button_offset_from_each_other_height = (self.screen_height - map_offset_from_top - map_height) // (
views_num_buttons_height + 1) // (views_num_buttons_height + 1)
views_button_offset_from_each_other_width = (self.screen_width -action_screen_width) // (
views_num_buttons_width + 1) // (views_num_buttons_width + 1)
#Making all the tiles
for x in range(self.x_start, self.x_end):
for y in range(self.y_start, self.y_end):
self.draw_single_tile(x, y, floormap, tileDict)
for key in item_ID.subjects:
item = item_ID.get_subject(key)
if (item.x >= self.x_start and item.x < self.x_end and item.y >= self.y_start and item.y < self.y_end):
if floormap.track_map[item.x][item.y].visible:
item_tile = tileDict.tile_string(item.render_tag)
self.win.blit(item_tile, (self.textSize * (item.x - self.x_start), self.textSize * (item.y - self.y_start)))
for key in monsterID.subjects:
monster = monsterID.get_subject(key)
if (monster.x >= self.x_start and monster.x < self.x_end and monster.y >= self.y_start and monster.y < self.y_end):
if floormap.track_map[monster.x][monster.y].visible:
monster_tile = tileDict.tile_string(monster.render_tag)
self.win.blit(monster_tile, (self.textSize*(monster.x - self.x_start), self.textSize*(monster.y - self.y_start)))
#Draw base character depending on armor state
if (player.character.main_armor == None):
self.win.blit(tileDict.tile_string(200), (self.r_x * self.textSize, self.r_y * self.textSize)) # DONG MODE ENGAGED
else:
self.win.blit(tileDict.tile_string(-200), (self.r_x * self.textSize, self.r_y * self.textSize))
#Draw items on top
if player.character.boots != None:
self.win.blit(tileDict.tile_string(201), (self.r_x * self.textSize, self.r_y * self.textSize))
if player.character.gloves != None:
self.win.blit(tileDict.tile_string(202), (self.r_x * self.textSize, self.r_y * self.textSize))
if player.character.helmet != None:
self.win.blit(tileDict.tile_string(203), (self.r_x * self.textSize, self.r_y * self.textSize))
if player.character.main_armor != None:
self.win.blit(tileDict.tile_string(204), (self.r_x * self.textSize, self.r_y * self.textSize))
self.uiManager.draw_ui(self.win)
#Making all map tiles
item_map = loop.generator.item_map
for x in range(x_map_start, x_map_end):
for y in range(y_map_start, y_map_end):
if (x < 0 or x >= floormap.width or y < 0 or y >= floormap.height):
pass
elif floormap.track_map[x][y].seen == False:
pass
else:
if floormap.track_map[x][y].passable:
if floormap.track_map[x][y].visible and not monster_map.get_passable(x,y):
pygame.draw.rect(self.win, (200, 0, 0),
pygame.Rect(map_offset_from_left + map_tile_size * (x - x_map_start),
map_offset_from_top + map_tile_size * (y - y_map_start),
map_tile_size, map_tile_size))
elif not item_map.get_passable(x,y):
pygame.draw.rect(self.win, (0, 200, 0),
pygame.Rect(map_offset_from_left + map_tile_size * (x - x_map_start),
map_offset_from_top + map_tile_size * (y - y_map_start),
map_tile_size, map_tile_size))
elif isinstance(floormap.track_map[x][y], O.Stairs):
pygame.draw.rect(self.win, (0, 0, 200),
pygame.Rect(map_offset_from_left + map_tile_size * (x - x_map_start),
map_offset_from_top + map_tile_size * (y - y_map_start),
map_tile_size, map_tile_size))
else:
pygame.draw.rect(self.win, (200, 200, 200),
pygame.Rect(map_offset_from_left + map_tile_size * (x - x_map_start),
map_offset_from_top + map_tile_size * (y - y_map_start),
map_tile_size, map_tile_size))
else:
pygame.draw.rect(self.win, (100, 100, 100),
pygame.Rect(map_offset_from_left + map_tile_size * (x - x_map_start),
map_offset_from_top + map_tile_size * (y - y_map_start),
map_tile_size, map_tile_size))
pygame.draw.rect(self.win, (150, 100, 50),
pygame.Rect(map_offset_from_left + r_map_x * map_tile_size,
map_offset_from_top + r_map_y * map_tile_size,
map_tile_size, map_tile_size))
pygame.draw.rect(self.win, (150, 100, 50),
pygame.Rect(map_offset_from_left + (num_map_tiles_wide - num_tiles_wide)* map_tile_size // 2,
map_offset_from_top + (num_map_tiles_height - num_tiles_height)* map_tile_size //2,
num_tiles_wide * map_tile_size, num_tiles_height* map_tile_size), 1)
#HORRIBLE HACK - THIS IS ALSO DEFINED IN UI.PY - KEEP THEM SYNCED!
def get_status_text(self, entity):
status = "Healthy"
if entity.character.health < entity.character.max_health // 3 * 2:
status = "Wounded"
effects = entity.character.status_effects
for effect in effects:
status += ", " + effect.description()
return status
def write_messages(self, messages):
font = pygame.font.Font('freesansbold.ttf', 12)
for i, message in enumerate(messages):
text = font.render(message, True, (255, 255, 255))
self.win.blit(text, (self.screen_width // 100 * 12, self.screen_height // 100 * (85 + i * 3)))
def draw_examine_window(self, target, tileDict, floormap, monster_map, monster_dict, item_dict, player):
examine_offset_from_left = self.screen_width // 30
examine_offset_from_top = self.screen_height // 20
try:
x, y = target
except:
return False
if not floormap.track_map[x][y].visible:
return
black_screen = pygame.transform.scale(pygame.image.load("assets/black_screen.png"), (self.screen_width // 5, self.screen_height // 5))
self.win.blit(black_screen, (0, 0))
any_item_found = False
nothing_at_target = True
# find monster at target
if not monster_map.get_passable(x,y):
monster = monster_dict.get_subject(monster_map.locate(x,y))
if monster == None:
return
# draw monster
to_draw = monster.render_tag
tag = tileDict.tile_string(to_draw)
self.win.blit(tag, (examine_offset_from_left, examine_offset_from_top))
font = pygame.font.Font('freesansbold.ttf', 12)
# name
text = font.render(monster.name, True, (255, 255, 255))
self.win.blit(text, (examine_offset_from_left, examine_offset_from_top + 50))
# level
text = font.render("Level: " + str(monster.character.level), True, (255, 255, 255))
self.win.blit(text, (examine_offset_from_left, examine_offset_from_top + 65))
# health
text = font.render("Health: " + str(monster.character.health) + "/" + str(monster.character.max_health), True, (255, 255, 255))
self.win.blit(text, (examine_offset_from_left, examine_offset_from_top + 80))
# status
status = self.get_status_text(monster)
text = font.render("Status: " + status, True, (255, 255, 255))
self.win.blit(text, (examine_offset_from_left, examine_offset_from_top + 95))
# description
description = monster.description
text = font.render(description, True, (255, 255, 255))
self.win.blit(text, (examine_offset_from_left, examine_offset_from_top + 110))
nothing_at_target = False
else:
# find item at target
for key in item_dict.subjects:
item = item_dict.get_subject(key)
count = 0
if item.x == x and item.y == y:
# draw item
to_draw = item.render_tag
tag = tileDict.tile_string(to_draw)
self.win.blit(tag, (examine_offset_from_left, examine_offset_from_top))
font = pygame.font.Font('freesansbold.ttf', 12)
# name
text = font.render(item.name, True, (255, 255, 255))
self.win.blit(text, (examine_offset_from_left, examine_offset_from_top + 50))
# description item descriptions are too long
# text = font.render(item.description, True, (255, 255, 255))
# self.win.blit(text, (examine_offset_from_left, examine_offset_from_top + 65))
# nothing_at_target = False
text = font.render(item.equipment_type, True, (255, 255, 255))
self.win.blit(text, (examine_offset_from_left, examine_offset_from_top + 65))
nothing_at_target = False
# stats (if present)
next_text = 80
if hasattr(item, "damage_min"):
text = font.render("Damage: " + str(item.damage_min) + " - " + str(item.damage_max), True, (255, 255, 255))
self.win.blit(text, (examine_offset_from_left, examine_offset_from_top + next_text))
next_text += 15
if hasattr(item, "defense"):
text = font.render("Defense: " + str(item.defense), True, (255, 255, 255))
self.win.blit(text, (examine_offset_from_left, examine_offset_from_top + next_text))
next_text += 15
if hasattr(item, "consumable"):
text = font.render("Consumable", True, (255, 255, 255))
self.win.blit(text, (examine_offset_from_left, examine_offset_from_top + next_text))
next_text += 15
examine_offset_from_top = examine_offset_from_top + next_text + 30
count += 1
if count > 6:
break
# find player at target
if nothing_at_target:
if player.x == x and player.y == y:
nothing_at_target = False
to_draw = player.render_tag
tag = tileDict.tile_string(to_draw)
self.win.blit(tag, (self.screen_width // 10, examine_offset_from_top))
font = pygame.font.Font('freesansbold.ttf', 12)
# random flavor text since detailed player info is elsewhere
text = font.render("You", True, (255, 255, 255))
self.win.blit(text, (examine_offset_from_left, examine_offset_from_top + 50))
text = font.render("You are here", True, (255, 255, 255))
self.win.blit(text, (examine_offset_from_left, examine_offset_from_top + 65))
return nothing_at_target
def create_inventory(self, player, equipment_type=None):
self.uiManager.clear_and_reset()
self.win.fill((0,0,0))
inventory_screen_width = self.screen_width // 2
inventory_screen_height = self.screen_height
inventory_offset_from_left = self.screen_width // 4
inventory_offset_from_top = 0
inventory_message_width = self.screen_width // 2
inventory_message_height = self.screen_height // 10
inventory_message_offset_from_left = self.screen_width // 4
inventory_message_offset_from_top = self.screen_height // 30
inventory_button_width = self.screen_width // 5
inventory_button_height = self.screen_height // 30
inventory_button_offset_from_left = self.screen_width *2 //5
inventory_button_offset_from_top = self.screen_height // 10 + self.screen_height // 30+ self.screen_height // 30
inventory_button_offset_from_each_other = self.screen_height // 100
self.uiManager.clear_and_reset()
pygame.draw.rect(self.win, (0,0,0), pygame.Rect(inventory_offset_from_left, inventory_offset_from_top, inventory_screen_width, inventory_screen_height))
self.draw_escape_button(inventory_button_offset_from_left, inventory_button_offset_from_top, inventory_screen_width, inventory_screen_height)
pygame_gui.elements.UILabel(relative_rect=pygame.Rect((inventory_message_offset_from_left, inventory_message_offset_from_top),
(inventory_message_width, inventory_message_height)),
text="Inventory",
manager=self.uiManager,
object_id='#title_label')
if equipment_type == "Enchantable":
enchantable = player.character.get_enchantable()
for i, item in enumerate(player.character.inventory):
if equipment_type == "Enchantable" and item not in enchantable:
continue
elif equipment_type != None and equipment_type != "Enchantable" and item.equipment_type != equipment_type:
continue
if item.stackable:
item_name = item.name + " (x" + str(item.stacks) + ")"
else:
item_name = item.name
if item.can_be_levelled:
item_level = item.level
if item_level > 1:
item_name = item_name + " (+" + str(item_level - 1) + ")"
if item.equipped:
item_name = item_name + " (equipped)"
button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((inventory_button_offset_from_left, inventory_button_offset_from_top + inventory_button_offset_from_each_other * i + inventory_button_height * i),
(inventory_button_width, inventory_button_height)),
text= chr(ord("a") + i) + ". " + item_name,
manager=self.uiManager)
button.action = chr(ord("a") + i)
self.buttons.add(button, chr(ord("a") + i))
self.uiManager.draw_ui(self.win)
return self.buttons
def update_inventory(self, player, equipment_type=None):
self.win.fill((0,0,0))
self.uiManager.draw_ui(self.win)
def draw_on_button(self, button, img, letter="", button_size=None, shrink=False, offset_factor = 10, text_offset = (15, 0.8)):
offset = (0, 0)
if shrink:# shrink weapon image a bit
img = pygame.transform.scale(img, (button_size[0] // 5 * 4, button_size[1] // 5 * 4))
offset = (button_size[0] // offset_factor, button_size[1] // offset_factor)
button.drawable_shape.states['normal'].surface.blit(img, offset)
button.drawable_shape.states['hovered'].surface.blit(img, offset)
button.drawable_shape.states['disabled'].surface.blit(img, offset)
button.drawable_shape.states['selected'].surface.blit(img, offset)
button.drawable_shape.states['active'].surface.blit(img, offset)
if button_size:
font_size = 20
font = pygame.font.Font('freesansbold.ttf', font_size)
text = font.render(letter, True, (255, 255, 255))
button.drawable_shape.states['normal'].surface.blit(text, (button_size[0] // text_offset[0], button_size[1] * text_offset[1]))
button.drawable_shape.states['hovered'].surface.blit(text, (button_size[0] // text_offset[0], button_size[1] * text_offset[1]))
button.drawable_shape.states['disabled'].surface.blit(text, (button_size[0] // text_offset[0], button_size[1] * text_offset[1]))
button.drawable_shape.states['selected'].surface.blit(text, (button_size[0] // text_offset[0], button_size[1] * text_offset[1]))
button.drawable_shape.states['active'].surface.blit(text, (button_size[0] // text_offset[0], button_size[1] * text_offset[1]))
button.drawable_shape.active_state.has_fresh_surface = True
# button.drawable_shape.redraw_all_states()
def create_equipment(self, player, tileMap):
self.uiManager.clear_and_reset()
self.win.fill((0,0,0))
equipment_screen_width = self.screen_width // 3 * 2
equipment_screen_height = self.screen_height
equipment_offset_from_left = self.screen_width // 3
equipment_offset_from_top = 0
equipment_message_width = self.screen_width // 2
equipment_message_height = self.screen_height // 10
equipment_message_offset_from_left = self.screen_width // 4
equipment_message_offset_from_top = self.screen_height // 30
# 8 buttons, for helmet, armor, gloves, boots, ring 1, ring 2, weapon, shield
# equipment is across three columns
medium_button_width = self.screen_width // 8
medium_button_height = self.screen_height // 5
first_col_offset_from_left = self.screen_width // 4
outer_cols_offset_from_top = self.screen_height // 5 * 2
middle_col_offset_from_top = self.screen_height // 5
small_button_width = self.screen_width // 16 - self.screen_width // 120
small_button_height = self.screen_height // 8
margin_between_buttons_height = self.screen_height // 30
small_margin_between_buttons_width = self.screen_width // 60
margin_between_buttons_width = self.screen_width // 30
self.uiManager.clear_and_reset()
pygame.draw.rect(self.win, (0,0,0), pygame.Rect(equipment_offset_from_left, equipment_offset_from_top, equipment_screen_width, equipment_screen_height))
self.draw_escape_button(equipment_message_offset_from_left, equipment_message_offset_from_top, equipment_screen_width, equipment_screen_height)
pygame_gui.elements.UILabel(relative_rect=pygame.Rect((equipment_message_offset_from_left, equipment_message_offset_from_top),
(equipment_message_width, equipment_message_height)),
text="Equipment",
manager=self.uiManager,
object_id='#title_label')
# equipment_slots = ["shield", "ring", "ring", "helmet", "armor", "boots", "weapon", "gloves"]:
if player.character.main_shield == None:
available_slot = False
for item in player.character.inventory:
if item.equipment_type == "Shield":
available_slot = True
break
if available_slot == True:
pre_text = "change "
img = pygame.transform.scale(tileMap.tiles[816],
(medium_button_width, medium_button_height))
else:
pre_text = "no options "
img = pygame.transform.scale(tileMap.tiles[806], (medium_button_width, medium_button_height))
else:
pre_text = "change "
img = pygame.transform.scale(tileMap.tiles[player.character.main_shield.render_tag], (medium_button_width, medium_button_height))
button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((first_col_offset_from_left,
outer_cols_offset_from_top),
(medium_button_width, medium_button_height)),
text = pre_text + "shield",
manager=self.uiManager,
object_id='#equipment_button')
self.draw_on_button(button, img, "q", (medium_button_width, medium_button_height))
button.action = 'q'
if player.character.ring_1 == None:
available_slot = False
for item in player.character.inventory:
if item.equipment_type == "Ring" and (not item.equipped):
available_slot = True
break
if available_slot == True:
pre_text = "change "
img = pygame.transform.scale(tileMap.tiles[817],
(small_button_width, small_button_height))
else:
pre_text = "no options "
img = pygame.transform.scale(tileMap.tiles[807], (small_button_width, small_button_height))
else:
pre_text = "change "
img = pygame.transform.scale(tileMap.tiles[player.character.ring_1.render_tag], (small_button_width, small_button_height))
button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((first_col_offset_from_left,
outer_cols_offset_from_top + (medium_button_height + margin_between_buttons_height)),
(small_button_width, small_button_height)),
text = "ring 1",
manager=self.uiManager,
object_id='#equipment_button')
self.draw_on_button(button, img, "a", (small_button_width, small_button_height))
button.action = 'a'
if player.character.ring_2 == None:
available_slot = False
for item in player.character.inventory:
if item.equipment_type == "Ring" and (not item.equipped):
available_slot = True
break
if available_slot == True:
pre_text = "change "
img = pygame.transform.scale(tileMap.tiles[817],
(small_button_width, small_button_height))
else:
pre_text = "no options "
img = pygame.transform.scale(tileMap.tiles[807], (small_button_width, small_button_height))
else:
pre_text = "change "
img = pygame.transform.scale(tileMap.tiles[player.character.ring_2.render_tag], (small_button_width, small_button_height))
button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((first_col_offset_from_left + small_button_width + small_margin_between_buttons_width,
outer_cols_offset_from_top + (medium_button_height + margin_between_buttons_height)),
(small_button_width, small_button_height)),
text = "ring 2",
manager=self.uiManager,
object_id='#equipment_button')
self.draw_on_button(button, img, "z", (small_button_width, small_button_height))
button.action = 'z'
if player.character.helmet == None:
available_slot = False
for item in player.character.inventory:
if item.equipment_type == "Helmet":
available_slot = True
break
if available_slot == True:
pre_text = "change "
img = pygame.transform.scale(tileMap.tiles[814],
(medium_button_width, medium_button_height))
else:
pre_text = "no options "
img = pygame.transform.scale(tileMap.tiles[804], (medium_button_width, medium_button_height))
else:
pre_text = "change "
img = pygame.transform.scale(tileMap.tiles[player.character.helmet.render_tag], (medium_button_width, medium_button_height))
button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((first_col_offset_from_left + medium_button_width + margin_between_buttons_width,
middle_col_offset_from_top),
(medium_button_width, medium_button_height)),
text = pre_text + "helmet",
manager=self.uiManager,
object_id='#equipment_button')
self.draw_on_button(button, img, "w", (medium_button_width, medium_button_height))
button.action = 'w'
if player.character.main_armor == None:
available_slot = False
for item in player.character.inventory:
if item.equipment_type == "Body Armor":
available_slot = True
break
if available_slot == True:
pre_text = "change "
img = pygame.transform.scale(tileMap.tiles[811],
(medium_button_width, medium_button_height))
else:
pre_text = "no options "
img = pygame.transform.scale(tileMap.tiles[801], (medium_button_width, medium_button_height))
else:
pre_text = "change "
img = pygame.transform.scale(tileMap.tiles[player.character.main_armor.render_tag], (medium_button_width, medium_button_height))
button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((first_col_offset_from_left + medium_button_width + margin_between_buttons_width,
middle_col_offset_from_top + (medium_button_height + margin_between_buttons_height)),
(medium_button_width, medium_button_height)),
text = pre_text + "armor",
manager=self.uiManager,
object_id='#equipment_button')
self.draw_on_button(button, img, "s", (medium_button_width, medium_button_height))
button.action = 's'
if player.character.boots == None:
available_slot = False
for item in player.character.inventory:
if item.equipment_type == "Boots":
available_slot = True
break
if available_slot == True:
pre_text = "change "
img = pygame.transform.scale(tileMap.tiles[812],
(medium_button_width, medium_button_height))
else:
pre_text = "no options "
img = pygame.transform.scale(tileMap.tiles[802], (medium_button_width, medium_button_height))
else:
pre_text = "change "
img = pygame.transform.scale(tileMap.tiles[player.character.boots.render_tag], (medium_button_width, medium_button_height))
button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((first_col_offset_from_left + medium_button_width + margin_between_buttons_width,
middle_col_offset_from_top + 2 * (medium_button_height + margin_between_buttons_height)),
(medium_button_width, medium_button_height)),
text = pre_text + "boots",
manager=self.uiManager,
object_id='#equipment_button')
self.draw_on_button(button, img, "x", (medium_button_width, medium_button_height))
button.action = 'x'
if player.character.main_weapon == None:
available_slot = False
for item in player.character.inventory:
if item.equipment_type== "Weapon":
available_slot = True
break
if available_slot == True:
pre_text = "change "
img = pygame.transform.scale(tileMap.tiles[815],
(medium_button_width, medium_button_height))
else:
pre_text = "no options "
img = pygame.transform.scale(tileMap.tiles[805], (medium_button_width, medium_button_height))
else:
pre_text = "change "
img = pygame.transform.scale(tileMap.tiles[player.character.main_weapon.render_tag], (medium_button_width, medium_button_height))
button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((first_col_offset_from_left + 2 * (medium_button_width + margin_between_buttons_width),
outer_cols_offset_from_top),
(medium_button_width, medium_button_height)),
text = pre_text + "weapon",
manager=self.uiManager,
object_id='#equipment_button')
self.draw_on_button(button, img, "d", (medium_button_width, medium_button_height), shrink=True)
button.action = 'd'
if player.character.gloves == None:
available_slot = False
for item in player.character.inventory:
if item.equipment_type == "Gloves":
available_slot = True
break
if available_slot == True:
pre_text = "change "
img = pygame.transform.scale(tileMap.tiles[813],
(medium_button_width, medium_button_height))
else:
pre_text = "no options "
img = pygame.transform.scale(tileMap.tiles[803], (medium_button_width, medium_button_height))
else:
pre_text = "change "
img = pygame.transform.scale(tileMap.tiles[player.character.gloves.render_tag], (medium_button_width, medium_button_height))
button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((first_col_offset_from_left + 2 * (medium_button_width + margin_between_buttons_width),
outer_cols_offset_from_top + (medium_button_height + margin_between_buttons_height)),
(medium_button_width, medium_button_height)),
text = pre_text + "gloves",
manager=self.uiManager,
object_id='#equipment_button')
self.draw_on_button(button, img, "c", (medium_button_width, medium_button_height))
button.action = 'c'
self.draw_character_stats(player,
margin_from_left = first_col_offset_from_left + 3 * (medium_button_width + margin_between_buttons_width),
margin_from_top = middle_col_offset_from_top,
width = medium_button_width * 2,
height = medium_button_height * 3 + margin_between_buttons_height * 2)
self.uiManager.draw_ui(self.win)
def update_equipment(self, player, tileMap):
self.win.fill((0,0,0))
self.uiManager.draw_ui(self.win)
def update_victory_screen(self):
self.win.fill((0,0,0))
self.uiManager.draw_ui(self.win)
def refresh_screen(self):
self.uiManager.clear_and_reset()
def create_pause_screen(self):
self.uiManager.clear_and_reset()
pause_screen_width = self.screen_width // 3
pause_screen_height = self.screen_height // 2
pause_offset_from_left = (self.screen_width - pause_screen_width) // 2
pause_offset_from_top = self.screen_height // 4
pause_num_buttons_height = 4
pause_button_width = pause_screen_width * 9 // 10
pause_button_height = (pause_screen_height) // (pause_num_buttons_height + 1)
pause_button_offset_from_each_other_height = (pause_screen_height) // (
pause_num_buttons_height + 1) // (pause_num_buttons_height + 1)
pause_button_offset_from_top = pause_offset_from_top
pause_button_offset_from_left = pause_offset_from_left + (pause_screen_width- pause_button_width) //2
self.draw_empty_box(pause_offset_from_left, pause_offset_from_top, pause_screen_width,
pause_screen_height)
button_num = 0
unpause = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((pause_button_offset_from_left, pause_button_offset_from_top + pause_button_offset_from_each_other_height+
(pause_button_height + pause_button_offset_from_each_other_height) * button_num,