all repos — Legends-RPG @ 107d1b4f334c643b35bd44f6d8865a9e9a42e3b7

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
 9class Town(tools._State):
10    def __init__(self):
11        super(Town, self).__init__()
12
13
14    def startup(self, current_time, persist):
15        """Called when the State object is created"""
16        self.persist = persist
17        self.current_time = current_time
18        self.town_map = tm.create_town_map()
19        self.viewport = tm.create_viewport(self.town_map)
20        self.blockers = tm.create_blockers()
21        self.level_surface = tm.create_level_surface(self.town_map)
22        self.level_rect = self.level_surface.get_rect()
23        self.player = person.Player('up')
24        self.town_sprites = pg.sprite.Group()
25        self.start_positions = tm.set_sprite_positions(self.player,
26                                                       self.town_sprites)
27        self.collision_handler = collision.CollisionHandler(self.player,
28                                                            self.blockers,
29                                                            self.town_sprites)
30        self.textbox_group = pg.sprite.Group()
31        self.dialogue_handler = textbox.DialogueHandler(self.player,
32                                                        self.town_sprites,
33                                                        self.textbox_group)
34
35
36    def update(self, surface, keys, current_time):
37        """Updates state"""
38        self.keys = keys
39        self.current_time = current_time
40        self.player.update(keys, current_time)
41        self.collision_handler.update()
42        self.update_viewport()
43        self.dialogue_handler.update(keys, current_time)
44
45        self.draw_level(surface)
46
47
48    def draw_level(self, surface):
49        """Blits all images to screen"""
50        self.level_surface.blit(self.town_map['surface'], self.viewport, self.viewport)
51        self.level_surface.blit(self.player.image, self.player.rect)
52        self.town_sprites.draw(self.level_surface)
53
54        surface.blit(self.level_surface, (0, 0), self.viewport)
55        self.textbox_group.draw(surface)
56
57
58    def update_viewport(self):
59        """Viewport stays centered on character, unless at edge of map"""
60        self.viewport.center = self.player.rect.center
61        self.viewport.clamp_ip(self.level_rect)
62
63
64
65
66
67
68
69
70
71