all repos — Legends-RPG @ 43c7782a1a3a9749ebf07c8e1897407f742bc3d2

A fantasy mini-RPG built with Python and Pygame.

data/states/shop.py (view raw)

 1"""
 2This class is the parent class of all shop states.
 3This includes weapon, armour, magic and potion shops.
 4It also includes the inn.  These states are scaled
 5twice as big as a level state.
 6"""
 7import copy
 8import pygame as pg
 9from .. import tools, setup
10from .. import constants as c
11from .. components import person, textbox
12
13
14class Shop(tools._State):
15    """Basic shop state"""
16    def __init__(self, name):
17        super(Shop, self).__init__(name)
18        self.map_width = 13
19        self.map_height = 10
20
21    def startup(self, current_time, persist):
22        """Startup state"""
23        self.persist = persist
24        self.current_time = current_time
25        self.state = 'normal'
26        self.get_image = tools.get_image
27        self.level_surface = pg.Surface(c.SCREEN_SIZE).convert()
28        self.level_surface.fill(c.BLACK_BLUE)
29        self.level_rect = self.level_surface.get_rect()
30        self.player = self.make_sprite('player', 96, 32, 100)
31        self.shop_owner = self.make_sprite('man1', 32, 32, 600)
32
33
34        self.dialogue_handler = textbox.DialogueHandler(self.player,
35                                                        self.shop_owner,
36                                                        self)
37
38
39    def make_sprite(self, key, coordx, coordy, x, y=304):
40        """Get the image for the player"""
41        spritesheet = setup.GFX[key]
42        surface = pg.Surface((32, 32))
43        surface.set_colorkey(c.BLACK)
44        image = self.get_image(coordx, coordy, 32, 32, spritesheet)
45        rect = image.get_rect()
46        surface.blit(image, rect)
47
48        surface = pg.transform.scale(surface, (96, 96))
49        rect = surface.get_rect(left=x, centery=y)
50        sprite = pg.sprite.Sprite()
51        sprite.image = surface
52        sprite.rect = rect
53
54        return sprite
55
56
57    def make_shop_owner(self):
58        """Make the shop owner sprite"""
59        return None
60
61
62    def update(self, surface, keys, current_time):
63        """Update level state"""
64        self.draw_level(surface)
65
66
67    def draw_level(self, surface):
68        """Blit graphics to game surface"""
69        surface.blit(self.level_surface, self.level_rect)
70        surface.blit(self.player.image, self.player.rect)
71        surface.blit(self.shop_owner.image, self.shop_owner.rect)