all repos — Legends-RPG @ 90efb3275223987ac539b64b19322402ed7bfac6

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.town_map = tm.create_town_map()
20        self.viewport = tm.create_viewport(self.town_map)
21        self.blockers = tm.create_blockers()
22        self.level_surface = tm.create_level_surface(self.town_map)
23        self.level_rect = self.level_surface.get_rect()
24        self.player = person.Player('up')
25        self.town_sprites = pg.sprite.Group()
26        self.start_positions = tm.set_sprite_positions(self.player,
27                                                       self.town_sprites)
28        self.set_sprite_dialogue()
29        self.collision_handler = collision.CollisionHandler(self.player,
30                                                            self.blockers,
31                                                            self.town_sprites)
32        self.dialogue_handler = textbox.DialogueHandler(self.player,
33                                                        self.town_sprites)
34
35
36    def set_sprite_dialogue(self):
37        """Sets unique dialogue for each sprite"""
38        for sprite in self.town_sprites:
39            if sprite.location == (9, 49):
40                sprite.dialogue = 'Welcome to our town, Mr. Traveller!'
41            elif sprite.location == (16, 42):
42                sprite.dialogue = 'You seem tired, why not rest at our Inn?'
43            elif sprite.location == (14, 14):
44                sprite.dialogue = 'Welcome to the castle, citizen.'
45            elif sprite.location == (11, 14):
46                sprite.dialogue = 'I have heard rumours that the King has lost something...'
47            elif sprite.location == (11, 8):
48                sprite.dialogue = 'Be careful. There are monsters surrounding our town.'
49            elif sprite.location == (14, 8):
50                sprite.dialogue = 'Move along, citizen.'
51
52
53    def update(self, surface, keys, current_time):
54        """Updates state"""
55        self.player.update(keys, current_time)
56        self.town_sprites.update(current_time)
57        self.collision_handler.update(keys, current_time)
58        self.dialogue_handler.update(keys, current_time)
59        self.viewport_update()
60
61        self.draw_level(surface)
62
63
64    def viewport_update(self):
65        """Viewport stays centered on character, unless at edge of map"""
66        self.viewport.center = self.player.rect.center
67        self.viewport.clamp_ip(self.level_rect)
68
69
70    def draw_level(self, surface):
71        """Blits all images to screen"""
72        self.level_surface.blit(self.town_map['surface'], self.viewport, self.viewport)
73        self.level_surface.blit(self.player.image, self.player.rect)
74        self.town_sprites.draw(self.level_surface)
75
76        surface.blit(self.level_surface, (0, 0), self.viewport)
77        self.dialogue_handler.draw(surface)
78
79
80
81
82
83
84
85
86
87
88
89
90