all repos — Legends-RPG @ 8bc6d878dfc97be6720ebf81a9451d06972ca097

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.animation_lists = self.create_animation_lists()
 13        self.index = 1
 14        self.direction = 'up'
 15        self.image_list = self.animation_lists[self.direction]
 16        self.image = self.image_list[self.index]
 17        self.rect = self.image.get_rect()
 18        self.state_dict = self.create_state_dict()
 19        self.direction_dict = self.create_direction_dict()
 20        self.state = 'resting'
 21        self.x_vel = 0
 22        self.y_vel = 0
 23        self.direction = 'up'
 24        self.timer = 0.0
 25
 26
 27    def create_spritesheet(self):
 28        """Creates the sprite sheet dictionary for player"""
 29        sheet = setup.GFX['player']
 30        image_list = []
 31        for row in range(2):
 32            for column in range(4):
 33                image_list.append(self.get_image(
 34                    self, column*32, row*32, 32, 32, sheet))
 35
 36        image_dict = {'facing up 1': image_list[0],
 37                      'facing up 2': image_list[1],
 38                      'facing down 1': image_list[2],
 39                      'facing down 2': image_list[3],
 40                      'facing left 1': image_list[4],
 41                      'facing left 2': image_list[5],
 42                      'facing right 1': image_list[6],
 43                      'facing right 2': image_list[7]}
 44
 45        return image_dict
 46
 47
 48    def create_animation_lists(self):
 49        """Create the lists for each walking direction"""
 50        image_dict = self.spritesheet_dict
 51
 52        left_list = [image_dict['facing left 1'], image_dict['facing left 2']]
 53        right_list = [image_dict['facing right 1'], image_dict['facing right 2']]
 54        up_list = [image_dict['facing up 1'], image_dict['facing up 2']]
 55        down_list = [image_dict['facing down 1'], image_dict['facing down 2']]
 56
 57        direction_dict = {'left': left_list,
 58                'right': right_list,
 59                'up': up_list,
 60                'down': down_list}
 61
 62        return direction_dict
 63
 64
 65    def create_state_dict(self):
 66        """Creates a dictionary of all the states the player
 67        can be in"""
 68        dict = {'resting': self.resting,
 69                'moving': self.moving}
 70
 71        return dict
 72
 73
 74    def create_direction_dict(self):
 75        """Creates a dictionary of x and y velocities set to
 76        direction keys"""
 77        dict = {'up': (0, -2),
 78                'down': (0, 2),
 79                'left': (-2, 0),
 80                'right': (2, 0)}
 81
 82        return dict
 83
 84
 85    def update(self, keys, current_time):
 86        """Updates player behavior"""
 87        self.keys = keys
 88        self.current_time = current_time
 89        self.check_for_input()
 90        state_function = self.state_dict[self.state]
 91        state_function()
 92
 93
 94    def resting(self):
 95        """When the player is not moving between tiles.
 96        Checks if the player is centered on a tile"""
 97        self.image = self.image_list[self.index]
 98
 99        assert(self.rect.y % 32 == 0), ('Player not centered on tile: '
100                                        + str(self.rect.y))
101        assert(self.rect.x % 32 == 0), ('Player not centered on tile'
102                                        + str(self.rect.x))
103
104
105    def moving(self):
106        """When the player is moving between tiles"""
107        if (self.current_time - self.timer) > 100:
108            if self.index < (len(self.image_list) - 1):
109                self.index += 1
110            else:
111                self.index = 0
112            self.timer = self.current_time
113
114        self.image = self.image_list[self.index]
115
116        assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \
117            'Not centered on tile'
118
119
120    def begin_moving(self, direction):
121        """Transitions the player into the moving state"""
122        self.direction = direction
123        self.image_list = self.animation_lists[direction]
124        self.timer = self.current_time
125        self.state = 'moving'
126
127
128        if self.rect.x % 32 == 0:
129            self.y_vel = self.direction_dict[self.direction][1]
130        if self.rect.y % 32 == 0:
131            self.x_vel = self.direction_dict[self.direction][0]
132
133
134    def begin_resting(self):
135        """Transitions the player into the resting state"""
136        self.state = 'resting'
137        self.index = 1
138        self.x_vel = self.y_vel = 0
139
140
141
142
143    def check_for_input(self):
144        """Checks for player input"""
145        if self.state == 'resting':
146            if self.keys[pg.K_UP]:
147                self.begin_moving('up')
148            elif self.keys[pg.K_DOWN]:
149                self.begin_moving('down')
150            elif self.keys[pg.K_LEFT]:
151                self.begin_moving('left')
152            elif self.keys[pg.K_RIGHT]:
153                self.begin_moving('right')
154
155
156
157