all repos — Legends-RPG @ 7a6a78187d162fec0124e97223e27e5ce24245bd

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