all repos — Legends-RPG @ 35c220742eca1ddd56f886e71e2140a27df951af

A fantasy mini-RPG built with Python and Pygame.

data/states/town.py (view raw)

  1__author__ = 'justinarmstrong'
  2
  3import pygame as pg
  4from .. import tools, collision
  5from .. import tilemap as tm
  6from .. components import person, textbox
  7
  8
  9
 10class Town(tools._State):
 11    def __init__(self):
 12        super(Town, self).__init__()
 13
 14
 15    def startup(self, current_time, persist):
 16        """Called when the State object is created"""
 17        self.persist = persist
 18        self.current_time = current_time
 19        self.state = 'normal'
 20        self.town_map = tm.create_town_map()
 21        self.viewport = tm.create_viewport(self.town_map)
 22        self.blockers = tm.create_blockers()
 23        self.level_surface = tm.create_level_surface(self.town_map)
 24        self.level_rect = self.level_surface.get_rect()
 25        self.player = person.Player('up')
 26        self.town_sprites = pg.sprite.Group()
 27        self.start_positions = tm.set_sprite_positions(self.player,
 28                                                       self.town_sprites)
 29        self.set_sprite_dialogue()
 30        self.collision_handler = collision.CollisionHandler(self.player,
 31                                                            self.blockers,
 32                                                            self.town_sprites)
 33        self.dialogue_handler = textbox.DialogueHandler(self.player,
 34                                                        self.town_sprites,
 35                                                        self)
 36        self.state_dict = self.make_state_dict()
 37
 38
 39    def set_sprite_dialogue(self):
 40        """Sets unique dialogue for each sprite"""
 41        for sprite in self.town_sprites:
 42            if sprite.location == (10, 47):
 43                sprite.dialogue = 'Welcome to our town, Mr. Traveller!'
 44            elif sprite.location == (16, 42):
 45                sprite.dialogue = 'You seem tired, why not rest at our Inn?'
 46                sprite.begin_auto_resting()
 47            elif sprite.location == (14, 14):
 48                sprite.dialogue = 'Welcome to the castle, citizen.'
 49            elif sprite.location == (11, 14):
 50                sprite.dialogue = 'I have heard rumours that the King has lost something...'
 51            elif sprite.location == (11, 8):
 52                sprite.dialogue = 'Be careful. There are monsters surrounding our town.'
 53            elif sprite.location == (14, 8):
 54                sprite.dialogue = 'Move along, citizen.'
 55
 56
 57    def make_state_dict(self):
 58        """Make a dictionary of states the level can be in"""
 59        state_dict = {'normal': self.running_normally,
 60                      'dialogue': self.handling_dialogue}
 61
 62        return state_dict
 63
 64
 65    def running_normally(self, surface, keys, current_time):
 66        """Update level normally"""
 67        self.player.update(keys, current_time)
 68        self.town_sprites.update(current_time)
 69        self.collision_handler.update(keys, current_time)
 70        self.dialogue_handler.update(keys, current_time)
 71        self.viewport_update()
 72
 73        self.draw_level(surface)
 74
 75
 76    def handling_dialogue(self, surface, keys, current_time):
 77        """Update only dialogue boxes"""
 78        self.dialogue_handler.update(keys, current_time)
 79        self.draw_level(surface)
 80
 81
 82    def update(self, surface, keys, current_time):
 83        """Updates state"""
 84        state_function = self.state_dict[self.state]
 85        state_function(surface, keys, current_time)
 86
 87
 88    def viewport_update(self):
 89        """Viewport stays centered on character, unless at edge of map"""
 90        self.viewport.center = self.player.rect.center
 91        self.viewport.clamp_ip(self.level_rect)
 92
 93
 94    def draw_level(self, surface):
 95        """Blits all images to screen"""
 96        self.level_surface.blit(self.town_map['surface'], self.viewport, self.viewport)
 97        self.level_surface.blit(self.player.image, self.player.rect)
 98        self.town_sprites.draw(self.level_surface)
 99
100        surface.blit(self.level_surface, (0, 0), self.viewport)
101        self.dialogue_handler.draw(surface)
102
103
104    def get_event(self, event):
105        """Set event to level attribute"""
106        self.event = event
107
108
109
110
111
112
113
114
115
116
117
118
119