all repos — Legends-RPG @ 3033ec723dbf8059aad0a4e2b087d5ed2fca6c5f

A fantasy mini-RPG built with Python and Pygame.

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, stats):
 20        self.level = level
 21        self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
 22        self.title_font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 24)
 23        self.title_font.set_underline(True)
 24        self.inventory = inventory
 25        self.stats = stats
 26        self.allow_input = False
 27        self.state = 'topmenu'
 28        self.gold_box = None
 29        self.stat_box = None
 30        self.selection_box = None
 31        self.arrow = SmallArrow()
 32        self.arrow_index = 0
 33        self.arrow_pos = self.make_arrow_pos_list()
 34        self.state_dict = self.make_state_dict()
 35
 36
 37    def make_arrow_pos_list(self):
 38        """Make the list of possible arrow positions"""
 39        pos_list = []
 40
 41        for i in range(4):
 42            pos = (35, 356 + (i * 50))
 43            pos_list.append(pos)
 44
 45        return pos_list
 46
 47
 48    def make_gold_box(self):
 49        """Makes the box that displays gold"""
 50        image = setup.GFX['goldbox2']
 51        rect = image.get_rect(left=10, top=234)
 52
 53        surface = pg.Surface(rect.size)
 54        surface.set_colorkey(c.BLACK)
 55        surface.blit(image, (0, 0))
 56
 57        text = "Gold: " + str(self.inventory['gold'])
 58        text_render = self.font.render(text, True, c.NEAR_BLACK)
 59        text_rect = text_render.get_rect(centerx=130,
 60                                         centery=35)
 61        surface.blit(text_render, text_rect)
 62
 63        sprite = pg.sprite.Sprite()
 64        sprite.image = surface
 65        sprite.rect = rect
 66
 67        return sprite
 68
 69
 70    def make_stat_box(self, title, stat_list):
 71        """Make the box for displaying stats and items"""
 72        image = setup.GFX['playerstatsbox']
 73        rect = image.get_rect(left=285, top=35)
 74        centerx = rect.width / 2
 75
 76        surface = pg.Surface(rect.size)
 77        surface.set_colorkey(c.BLACK)
 78        surface.blit(image, (0,0))
 79
 80        title_image = self.title_font.render(title, True, c.NEAR_BLACK)
 81        title_rect = title_image.get_rect(centerx=centerx, y=30)
 82        surface.blit(title_image, title_rect)
 83
 84        for i, stat in enumerate(stat_list):
 85            if stat == 'Health' or stat == 'Magic Points':
 86                text = (stat + ": " + str(self.stats[stat]['current']) +
 87                        " / " + str(self.stats[stat]['maximum']))
 88            else:
 89                text = stat + ": " + str(self.stats[stat])
 90            text_image = self.font.render(text, True, c.NEAR_BLACK)
 91            text_rect = text_image.get_rect(x=50, y=80+(i*70))
 92            surface.blit(text_image, text_rect)
 93
 94        sprite = pg.sprite.Sprite()
 95        sprite.image = surface
 96        sprite.rect = rect
 97
 98        return sprite
 99
100
101    def make_selection_box(self, choices):
102        """Make the menu with selection options"""
103        image = setup.GFX['selectionbox']
104        rect = image.get_rect(left=10, top=330)
105
106        surface = pg.Surface(rect.size)
107        surface.set_colorkey(c.BLACK)
108        surface.blit(image, (0, 0))
109
110        for i, choice in enumerate(choices):
111            choice_image = self.font.render(choice, True, c.NEAR_BLACK)
112            y_position = 25 + (i * 50)
113            choice_rect = choice_image.get_rect(x=100, y=y_position)
114            surface.blit(choice_image, choice_rect)
115
116        sprite = pg.sprite.Sprite()
117        sprite.image = surface
118        sprite.rect = rect
119
120        return sprite
121
122
123    def make_box_group(self):
124        """Make the sprite group for each GUI box"""
125        return pg.sprite.Group(self.gold_box,
126                               self.stat_box,
127                               self.selection_box)
128
129
130    def make_state_dict(self):
131        """Make the dictionary of all menu states"""
132        state_dict = {'topmenu': self.select_main_options}
133
134        return state_dict
135
136
137    def select_main_options(self, keys):
138        """Allow player to select items, magic, weapons and armor"""
139        choices = ['Equip', 'Items', 'Magic', 'Exit']
140        title = 'PLAYER STATS'
141        stats = ['Level', 'Health', 'Experience to next level',
142                 'Magic Points', 'Attack Points', 'Defense Points' ]
143        self.selection_box = self.make_selection_box(choices)
144        self.gold_box = self.make_gold_box()
145        self.stat_box = self.make_stat_box(title, stats)
146        self.arrow.rect.topleft = self.arrow_pos[self.arrow_index]
147
148        if self.allow_input:
149            if keys[pg.K_DOWN]:
150                if self.arrow_index < len(choices) - 1:
151                    self.arrow_index += 1
152                    self.allow_input = False
153            elif keys[pg.K_UP]:
154                if self.arrow_index > 0:
155                    self.arrow_index -= 1
156                    self.allow_input = False
157            elif keys[pg.K_SPACE]:
158                if self.arrow_index == len(choices) - 1:
159                    self.level.done = True
160            elif keys[pg.K_RETURN]:
161                self.level.done = True
162
163        if (not keys[pg.K_DOWN]
164                and not keys[pg.K_UP]
165                and not keys[pg.K_RETURN]):
166            self.allow_input = True
167
168
169
170    def update(self, keys):
171        state_function = self.state_dict[self.state]
172        state_function(keys)
173
174
175    def draw(self, surface):
176        if self.gold_box and self.stat_box and self.selection_box:
177            surface.blit(self.gold_box.image, self.gold_box.rect)
178            surface.blit(self.stat_box.image, self.stat_box.rect)
179            surface.blit(self.selection_box.image, self.selection_box.rect)
180            surface.blit(self.arrow.image, self.arrow.rect)