all repos — Legends-RPG @ cec3b2e4079061cbf05e7418b69b276ba06fe6f4

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                                   'The King is loved by all!',
 45                                   'You should go visit him in his castle.']
 46            elif sprite.location == (16, 42):
 47                sprite.dialogue = ['You seem tired, why not rest at our Inn?']
 48                sprite.begin_auto_resting()
 49            elif sprite.location == (14, 14):
 50                sprite.dialogue = ['Be careful. There are monsters surrounding our town.',
 51                                   'Make sure to equip sufficient armour and weapons.',
 52                                   'Spells and potions are useful too.']
 53            elif sprite.location == (11, 14):
 54                sprite.dialogue = ['I have heard rumours that the King has lost something...',
 55                                   'Perhaps you should pay him a visit.']
 56            elif sprite.location == (11, 8):
 57                sprite.dialogue = ['Welcome to the castle, citizen.']
 58            elif sprite.location == (14, 8):
 59                sprite.dialogue = ['Move along.']
 60
 61
 62    def make_state_dict(self):
 63        """Make a dictionary of states the level can be in"""
 64        state_dict = {'normal': self.running_normally,
 65                      'dialogue': self.handling_dialogue}
 66
 67        return state_dict
 68
 69
 70    def running_normally(self, surface, keys, current_time):
 71        """Update level normally"""
 72        self.check_for_dialogue()
 73        self.player.update(keys, current_time)
 74        self.town_sprites.update(current_time)
 75        self.collision_handler.update(keys, current_time)
 76        self.dialogue_handler.update(keys, current_time)
 77        self.viewport_update()
 78
 79        self.draw_level(surface)
 80
 81
 82    def handling_dialogue(self, surface, keys, current_time):
 83        """Update only dialogue boxes"""
 84        self.dialogue_handler.update(keys, current_time)
 85        self.draw_level(surface)
 86
 87
 88    def check_for_dialogue(self):
 89        """Check if the level needs to freeze"""
 90        if self.dialogue_handler.textbox:
 91            self.state = 'dialogue'
 92
 93
 94    def update(self, surface, keys, current_time):
 95        """Updates state"""
 96        state_function = self.state_dict[self.state]
 97        state_function(surface, keys, current_time)
 98
 99
100    def viewport_update(self):
101        """Viewport stays centered on character, unless at edge of map"""
102        self.viewport.center = self.player.rect.center
103        self.viewport.clamp_ip(self.level_rect)
104
105
106    def draw_level(self, surface):
107        """Blits all images to screen"""
108        self.level_surface.blit(self.town_map['surface'], self.viewport, self.viewport)
109        self.level_surface.blit(self.player.image, self.player.rect)
110        self.town_sprites.draw(self.level_surface)
111
112        surface.blit(self.level_surface, (0, 0), self.viewport)
113        self.dialogue_handler.draw(surface)
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128