all repos — Legends-RPG @ 8de0e0ce2d3efaf9269a890cca6875c4bf5c3ec3

A fantasy mini-RPG built with Python and Pygame.

data/components/player.py (view raw)

 1__author__ = 'justinarmstrong'
 2
 3import pygame as pg
 4from .. import setup
 5
 6class Player(pg.sprite.Sprite):
 7    """User controllable character"""
 8    def __init__(self):
 9        super(Player, self).__init__()
10        self.get_image = setup.tools.get_image
11        self.spritesheet_dict = self.create_spritesheet()
12        self.image = self.spritesheet_dict['facing down 1']
13        self.rect = self.image.get_rect()
14        self.state_dict = self.create_state_dict()
15        self.direction_dict = self.create_direction_dict()
16        self.state = 'resting'
17        self.x_vel = 0
18        self.y_vel = 0
19        self.direction = 'up'
20
21
22    def create_spritesheet(self):
23        """Creates the sprite sheet dictionary for player"""
24        sheet = setup.GFX['player']
25        image_list = []
26        for row in range(2):
27            for column in range(4):
28                image_list.append(self.get_image(
29                    self, column*32, row*32, 32, 32, sheet))
30
31        dict = {'facing up 1': image_list[0],
32                'facing up 2': image_list[1],
33                'facing down 1': image_list[2],
34                'facing down 2': image_list[3],
35                'facing left 1': image_list[4],
36                'facing left 2': image_list[5],
37                'facing right 1': image_list[6],
38                'facing right 2': image_list[7]}
39
40        return dict
41
42
43    def create_state_dict(self):
44        """Creates a dictionary of all the states the player
45        can be in"""
46        dict = {'resting': self.resting,
47                'moving': self.moving}
48
49        return dict
50
51
52    def create_direction_dict(self):
53        """Creates a dictionary of directions with truth values
54        corresponding to the player's direction"""
55        dict = {'up': (0, -2),
56                'down': (0, 2),
57                'left': (-2, 0),
58                'right': (2, 0)}
59
60        return dict
61
62
63    def update(self):
64        """Updates player behavior"""
65        state = self.state_dict[self.state]
66        state()
67
68
69    def resting(self):
70        """When the player is not moving between tiles.
71        Checks if the player is centered on a tile"""
72        assert(self.rect.y % 32 == 0), ('Player not centered on tile: '
73                                        + str(self.rect.y))
74        assert(self.rect.x % 32 == 0), ('Player not centered on tile'
75                                        + str(self.rect.x))
76
77
78    def moving(self):
79        """When the player is moving between tiles"""
80        self.rect.x += self.x_vel
81        self.rect.y += self.y_vel
82
83
84    def begin_moving(self, direction):
85        """Transitions the player into the moving state"""
86        self.state = 'moving'
87        self.direction = direction
88        self.x_vel, self.y_vel = self.direction_dict[self.direction]
89
90
91    def begin_resting(self):
92        """Transitions the player into the resting state"""
93        self.state = 'resting'
94        self.x_vel, self.y_vel = 0
95
96
97