data/menugui.py (view raw)
1"""
2This class controls all the GUI for the player
3menu screen.
4"""
5import pygame as pg
6from . import setup
7from . import constants as c
8
9
10class SmallArrow(pg.sprite.Sprite):
11 """Small arrow for menu"""
12 def __init__(self):
13 super(SmallArrow, self).__init__()
14 self.image = setup.GFX['smallarrow']
15 self.rect = self.image.get_rect()
16
17
18class MenuGui(object):
19 def __init__(self, level, inventory):
20 self.level = level
21 self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
22 self.inventory = inventory
23 self.allow_input = False
24 self.state = 'topmenu'
25 self.gold_box = None
26 self.stat_box = None
27 self.selection_box = None
28 self.arrow = SmallArrow()
29 self.arrow_index = 0
30 self.arrow_pos = self.make_arrow_pos_list()
31 self.state_dict = self.make_state_dict()
32
33
34 def make_arrow_pos_list(self):
35 """Make the list of possible arrow positions"""
36 pos_list = []
37
38 for i in range(4):
39 pos = (35, 356 + (i * 50))
40 pos_list.append(pos)
41
42 return pos_list
43
44
45 def make_gold_box(self):
46 """Makes the box that displays gold"""
47 image = setup.GFX['goldbox2']
48 rect = image.get_rect(left=10, top=234)
49
50 surface = pg.Surface(rect.size)
51 surface.set_colorkey(c.BLACK)
52 surface.blit(image, (0, 0))
53
54 text = "Gold: " + str(self.inventory['gold'])
55 text_render = self.font.render(text, True, c.NEAR_BLACK)
56 text_rect = text_render.get_rect(centerx=130,
57 centery=35)
58 surface.blit(text_render, text_rect)
59
60 sprite = pg.sprite.Sprite()
61 sprite.image = surface
62 sprite.rect = rect
63
64 return sprite
65
66
67 def make_stat_box(self):
68 """Make the box for displaying stats and items"""
69 image = setup.GFX['playerstatsbox']
70 rect = image.get_rect(left=285, top=35)
71
72 surface = pg.Surface(rect.size)
73 surface.set_colorkey(c.BLACK)
74 surface.blit(image, (0,0))
75
76 sprite = pg.sprite.Sprite()
77 sprite.image = surface
78 sprite.rect = rect
79
80 return sprite
81
82
83 def make_selection_box(self, choices):
84 """Make the menu with selection options"""
85 image = setup.GFX['selectionbox']
86 rect = image.get_rect(left=10, top=330)
87
88 surface = pg.Surface(rect.size)
89 surface.set_colorkey(c.BLACK)
90 surface.blit(image, (0, 0))
91
92 for i, choice in enumerate(choices):
93 choice_image = self.font.render(choice, True, c.NEAR_BLACK)
94 y_position = 25 + (i * 50)
95 choice_rect = choice_image.get_rect(x=100, y=y_position)
96 surface.blit(choice_image, choice_rect)
97
98 sprite = pg.sprite.Sprite()
99 sprite.image = surface
100 sprite.rect = rect
101
102 return sprite
103
104
105 def make_box_group(self):
106 """Make the sprite group for each GUI box"""
107 return pg.sprite.Group(self.gold_box,
108 self.stat_box,
109 self.selection_box)
110
111
112 def make_state_dict(self):
113 """Make the dictionary of all menu states"""
114 state_dict = {'topmenu': self.select_main_options}
115
116 return state_dict
117
118
119 def select_main_options(self, keys):
120 """Allow player to select items, magic, weapons and armor"""
121 choices = ['Equip', 'Items', 'Magic', 'Exit']
122 self.selection_box = self.make_selection_box(choices)
123 self.gold_box = self.make_gold_box()
124 self.stat_box = self.make_stat_box()
125 self.arrow.rect.topleft = self.arrow_pos[self.arrow_index]
126
127 if self.allow_input:
128 if keys[pg.K_DOWN]:
129 if self.arrow_index < len(choices) - 1:
130 self.arrow_index += 1
131 self.allow_input = False
132 elif keys[pg.K_UP]:
133 if self.arrow_index > 0:
134 self.arrow_index -= 1
135 self.allow_input = False
136 elif keys[pg.K_SPACE]:
137 if self.arrow_index == len(choices) - 1:
138 self.level.done = True
139
140 if not keys[pg.K_DOWN] and not keys[pg.K_UP]:
141 self.allow_input = True
142
143
144
145 def update(self, keys):
146 state_function = self.state_dict[self.state]
147 state_function(keys)
148
149
150 def draw(self, surface):
151 if self.gold_box and self.stat_box and self.selection_box:
152 surface.blit(self.gold_box.image, self.gold_box.rect)
153 surface.blit(self.stat_box.image, self.stat_box.rect)
154 surface.blit(self.selection_box.image, self.selection_box.rect)
155 surface.blit(self.arrow.image, self.arrow.rect)