all repos — Legends-RPG @ 042b912afd55ebeecab540dc4a44dd070f886b6a

A fantasy mini-RPG built with Python and Pygame.

data/states/player_menu.py (view raw)

 1"""
 2This is the state where the player can look at
 3his inventory, equip items and check stats.
 4"""
 5import pygame as pg
 6from .. import tools, setup, menugui
 7from .. import constants as c
 8
 9
10class Player_Menu(tools._State):
11    def __init__(self):
12        super(Player_Menu, self).__init__()
13        self.get_image = tools.get_image
14
15
16
17    def startup(self, current_time, game_data):
18        """Call when state is switched to"""
19        inventory = game_data['player inventory']
20        self.allow_input = False
21        self.game_data = game_data
22        self.current_time = current_time
23        self.background = self.make_background()
24        self.gui = menugui.MenuGui(inventory)
25
26
27    def make_background(self):
28        """Makes the generic black/blue background"""
29        background = pg.sprite.Sprite()
30        surface = pg.Surface(c.SCREEN_SIZE).convert()
31        surface.fill(c.BLACK_BLUE)
32        background.image = surface
33        background.rect = background.image.get_rect()
34
35        player = self.make_sprite('player', 96, 32)
36
37        background.image.blit(player.image, player.rect)
38
39        return background
40
41
42    def make_sprite(self, key, coordx, coordy, x=40, y=25):
43        """Get the image for the player"""
44        spritesheet = setup.GFX[key]
45        surface = pg.Surface((32, 32))
46        surface.set_colorkey(c.BLACK)
47        image = self.get_image(coordx, coordy, 32, 32, spritesheet)
48        rect = image.get_rect()
49        surface.blit(image, rect)
50
51        surface = pg.transform.scale(surface, (192, 192))
52        rect = surface.get_rect(left=x, top=y)
53        sprite = pg.sprite.Sprite()
54        sprite.image = surface
55        sprite.rect = rect
56
57        return sprite
58
59
60    def update(self, surface, keys, current_time):
61        self.check_for_quit(keys)
62        self.gui.update(keys)
63        self.draw(surface)
64
65
66    def check_for_quit(self, keys):
67        if keys[pg.K_RETURN] and self.allow_input:
68            self.done = True
69            self.next = 'town'
70
71        if not keys[pg.K_RETURN]:
72            self.allow_input = True
73
74
75    def draw(self, surface):
76        surface.blit(self.background.image, self.background.rect)
77        self.gui.draw(surface)
78