all repos — Legends-RPG @ 0283cc01f0a6676c7ec039186b4b1338718b5f12

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.background = self.make_background()
 28        self.player = None
 29        self.sprites = None
 30        self.text_handler = textbox.TextHandler(self)
 31        self.make_greeting_text()
 32
 33
 34    def make_background(self):
 35        """Make the level surface"""
 36        background = pg.sprite.Sprite()
 37        surface = pg.Surface(c.SCREEN_SIZE).convert()
 38        surface.fill(c.BLACK_BLUE)
 39        background.image = surface
 40        background.rect = background.image.get_rect()
 41
 42        player = self.make_sprite('player', 96, 32, 150)
 43        shop_owner = self.make_sprite('man1', 32, 32, 600)
 44        counter = self.make_counter()
 45
 46        background.image.blit(player.image, player.rect)
 47        background.image.blit(shop_owner.image, shop_owner.rect)
 48        background.image.blit(counter.image, counter.rect)
 49
 50        return background
 51
 52
 53    def make_sprite(self, key, coordx, coordy, x, y=304):
 54        """Get the image for the player"""
 55        spritesheet = setup.GFX[key]
 56        surface = pg.Surface((32, 32))
 57        surface.set_colorkey(c.BLACK)
 58        image = self.get_image(coordx, coordy, 32, 32, spritesheet)
 59        rect = image.get_rect()
 60        surface.blit(image, rect)
 61
 62        surface = pg.transform.scale(surface, (96, 96))
 63        rect = surface.get_rect(left=x, centery=y)
 64        sprite = pg.sprite.Sprite()
 65        sprite.image = surface
 66        sprite.rect = rect
 67
 68        return sprite
 69
 70
 71    def make_greeting_text(self):
 72        """Make the textbox for the shop owner's greeting"""
 73        dialogue = ["Welcome to the " + self.name + "!",
 74                    "Would you like to spend 30 gold on a room tonight?"]
 75        textbox = self.text_handler.make_textbox('dialoguebox', dialogue)
 76
 77        self.text_handler.textbox = textbox
 78
 79
 80    def make_counter(self):
 81        """Make the counter to conduct business"""
 82        sprite_sheet = copy.copy(setup.GFX['house'])
 83        sprite = pg.sprite.Sprite()
 84        sprite.image = self.get_image(102, 64, 26, 82, sprite_sheet)
 85        sprite.image = pg.transform.scale2x(sprite.image)
 86        sprite.rect = sprite.image.get_rect(left=550, top=225)
 87
 88        return sprite
 89
 90
 91    def update(self, surface, keys, current_time):
 92        """Update level state"""
 93        self.text_handler.update_for_shops(keys, current_time)
 94        self.draw_level(surface)
 95
 96
 97    def draw_level(self, surface):
 98        """Blit graphics to game surface"""
 99        surface.blit(self.background.image, self.background.rect)
100        self.text_handler.draw(surface)