all repos — Legends-RPG @ b12dd52bf6f08a216fbb7a29e9e81880a7f644a8

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        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 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        dict = {'left': left_list,
 58                'right': right_list,
 59                'up': up_list,
 60                'down': down_list}
 61
 62        return dict
 63
 64
 65
 66
 67    def create_state_dict(self):
 68        """Creates a dictionary of all the states the player
 69        can be in"""
 70        dict = {'resting': self.resting,
 71                'moving': self.moving}
 72
 73        return dict
 74
 75
 76    def create_direction_dict(self):
 77        """Creates a dictionary of directions with truth values
 78        corresponding to the player's direction"""
 79        dict = {'up': (0, -2),
 80                'down': (0, 2),
 81                'left': (-2, 0),
 82                'right': (2, 0)}
 83
 84        return dict
 85
 86
 87    def update(self, keys, current_time):
 88        """Updates player behavior"""
 89        self.keys = keys
 90        self.current_time = current_time
 91        self.check_for_input()
 92        state = self.state_dict[self.state]
 93        state()
 94
 95
 96    def resting(self):
 97        """When the player is not moving between tiles.
 98        Checks if the player is centered on a tile"""
 99        self.image = self.image_list[self.index]
100
101        assert(self.rect.y % 32 == 0), ('Player not centered on tile: '
102                                        + str(self.rect.y))
103        assert(self.rect.x % 32 == 0), ('Player not centered on tile'
104                                        + str(self.rect.x))
105
106
107    def moving(self):
108        """When the player is moving between tiles"""
109        self.rect.x += self.x_vel
110        self.rect.y += self.y_vel
111
112        if (self.current_time - self.timer) > 100:
113            if self.index < (len(self.image_list) - 1):
114                self.index += 1
115            else:
116                self.index = 0
117            self.timer = self.current_time
118
119        self.image = self.image_list[self.index]
120
121        if self.rect.x % 32 == 0 and self.rect.y % 32 == 0:
122            self.begin_resting()
123
124        assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \
125            'Not centered on tile'
126
127
128    def begin_moving(self, direction):
129        """Transitions the player into the moving state"""
130        self.state = 'moving'
131        self.direction = direction
132        self.image_list = self.animation_lists[direction]
133        self.timer = self.current_time
134
135        if self.rect.x % 32 == 0:
136            self.y_vel = self.direction_dict[self.direction][1]
137        if self.rect.y % 32 == 0:
138            self.x_vel = self.direction_dict[self.direction][0]
139
140
141    def begin_resting(self):
142        """Transitions the player into the resting state"""
143        self.state = 'resting'
144        self.index = 1
145        self.x_vel = self.y_vel = 0
146
147
148    def check_for_input(self):
149        """Checks for player input"""
150        if self.state == 'resting':
151            if self.keys[pg.K_UP]:
152                self.begin_moving('up')
153            elif self.keys[pg.K_DOWN]:
154                self.begin_moving('down')
155            elif self.keys[pg.K_LEFT]:
156                self.begin_moving('left')
157            elif self.keys[pg.K_RIGHT]:
158                self.begin_moving('right')
159
160
161
162