all repos — Legends-RPG @ 04ff553e1d8f2f6db8a7657eabcdfc8a843787ba

A fantasy mini-RPG built with Python and Pygame.

data/components/person.py (view raw)

  1__author__ = 'justinarmstrong'
  2import math, random
  3import pygame as pg
  4from .. import setup
  5
  6
  7class Person(pg.sprite.Sprite):
  8    """Base class for all world characters
  9    controlled by the computer"""
 10
 11    def __init__(self, sheet_key, x, y, direction='down'):
 12        super(Person, self).__init__()
 13        self.get_image = setup.tools.get_image
 14        self.spritesheet_dict = self.create_spritesheet_dict(sheet_key)
 15        self.animation_dict = self.create_animation_dict()
 16        self.index = 0
 17        self.direction = direction
 18        self.image_list = self.animation_dict[self.direction]
 19        self.image = self.image_list[self.index]
 20        self.rect = self.image.get_rect(left=x, top=y)
 21        self.old_rect = self.rect
 22        self.state_dict = self.create_state_dict()
 23        self.vector_dict = self.create_vector_dict()
 24        self.x_vel = 0
 25        self.y_vel = 0
 26        self.timer = 0.0
 27        self.move_timer = 0.0
 28        self.current_time = 0.0
 29        self.state = 'animated resting'
 30        self.blockers = self.set_blockers()
 31        self.location = self.get_tile_location()
 32        self.dialogue = ['Placeholder Dialogue']
 33
 34
 35    def create_spritesheet_dict(self, sheet_key):
 36        """Implemented by inheriting classes"""
 37        image_list = []
 38        image_dict = {}
 39        sheet = setup.GFX[sheet_key]
 40
 41        image_keys = ['facing up 1', 'facing up 2',
 42                      'facing down 1', 'facing down 2',
 43                      'facing left 1', 'facing left 2',
 44                      'facing right 1', 'facing right 2']
 45
 46        for row in range(2):
 47            for column in range(4):
 48                image_list.append(
 49                    self.get_image(column*32, row*32, 32, 32, sheet))
 50
 51        for key, image in zip(image_keys, image_list):
 52            image_dict[key] = image
 53
 54        return image_dict
 55
 56
 57    def create_animation_dict(self):
 58        """Return a dictionary of image lists for animation"""
 59        image_dict = self.spritesheet_dict
 60
 61        left_list = [image_dict['facing left 1'], image_dict['facing left 2']]
 62        right_list = [image_dict['facing right 1'], image_dict['facing right 2']]
 63        up_list = [image_dict['facing up 1'], image_dict['facing up 2']]
 64        down_list = [image_dict['facing down 1'], image_dict['facing down 2']]
 65
 66        direction_dict = {'left': left_list,
 67                          'right': right_list,
 68                          'up': up_list,
 69                          'down': down_list}
 70
 71        return direction_dict
 72
 73
 74    def create_state_dict(self):
 75        """Return a dictionary of all state methods"""
 76        state_dict = {'resting': self.resting,
 77                      'moving': self.moving,
 78                      'animated resting': self.animated_resting,
 79                      'autoresting': self.auto_resting,
 80                      'automoving': self.auto_moving}
 81
 82        return state_dict
 83
 84
 85    def create_vector_dict(self):
 86        """Return a dictionary of x and y velocities set to
 87        direction keys."""
 88        vector_dict = {'up': (0, -1),
 89                       'down': (0, 1),
 90                       'left': (-1, 0),
 91                       'right': (1, 0)}
 92
 93        return vector_dict
 94
 95
 96    def update(self, current_time):
 97        """Implemented by inheriting classes"""
 98        self.blockers = self.set_blockers()
 99        self.current_time = current_time
100        self.image_list = self.animation_dict[self.direction]
101        state_function = self.state_dict[self.state]
102        state_function()
103        self.location = self.get_tile_location()
104
105
106    def set_blockers(self):
107        """Sets blockers to prevent collision with other sprites"""
108        blockers = []
109
110        if self.state == 'animated resting' or self.state == 'autoresting':
111            blockers.append(pg.Rect(self.rect.x, self.rect.y, 32, 32))
112
113        elif self.state == 'moving' or self.state == 'automoving':
114            if self.rect.x % 32 == 0:
115                tile_float = self.rect.y / float(32)
116                tile1 = (self.rect.x, math.ceil(tile_float)*32)
117                tile2 = (self.rect.x, math.floor(tile_float)*32)
118                tile_rect1 = pg.Rect(tile1[0], tile1[1], 32, 32)
119                tile_rect2 = pg.Rect(tile2[0], tile2[1], 32, 32)
120                blockers.extend([tile_rect1, tile_rect2])
121
122            elif self.rect.y % 32 == 0:
123                tile_float = self.rect.x / float(32)
124                tile1 = (math.ceil(tile_float)*32, self.rect.y)
125                tile2 = (math.floor(tile_float)*32, self.rect.y)
126                tile_rect1 = pg.Rect(tile1[0], tile1[1], 32, 32)
127                tile_rect2 = pg.Rect(tile2[0], tile2[1], 32, 32)
128                blockers.extend([tile_rect1, tile_rect2])
129
130        return blockers
131
132
133
134    def get_tile_location(self):
135        """Converts pygame coordinates into tile coordinates"""
136        if self.rect.x == 0:
137            tile_x = 1
138        elif self.rect.x % 32 == 0:
139            tile_x = (self.rect.x / 32) + 1
140        else:
141            tile_x = 0
142
143        if self.rect.y == 0:
144            tile_y = 1
145        elif self.rect.y % 32 == 0:
146            tile_y = (self.rect.y / 32) + 1
147        else:
148            tile_y = 0
149
150        return (tile_x, tile_y)
151
152
153    def resting(self):
154        """
155        When the Person is not moving between tiles.
156        Checks if the player is centered on a tile.
157        """
158        self.image = self.image_list[self.index]
159
160        assert(self.rect.y % 32 == 0), ('Player not centered on tile: '
161                                        + str(self.rect.y))
162        assert(self.rect.x % 32 == 0), ('Player not centered on tile'
163                                        + str(self.rect.x))
164
165
166    def moving(self):
167        """Increment index and set self.image for animation."""
168        self.animation()
169
170        assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \
171            'Not centered on tile'
172
173
174    def animated_resting(self):
175        self.animation(500)
176
177
178    def animation(self, freq=100):
179        """Adjust sprite image frame based on timer"""
180        if (self.current_time - self.timer) > freq:
181            if self.index < (len(self.image_list) - 1):
182                self.index += 1
183            else:
184                self.index = 0
185            self.timer = self.current_time
186
187        self.image = self.image_list[self.index]
188
189
190
191    def begin_moving(self, direction):
192        """Transition the player into the 'moving' state."""
193        self.direction = direction
194        self.image_list = self.animation_dict[direction]
195        self.timer = self.current_time
196        self.move_timer = self.current_time
197        self.state = 'moving'
198
199        if self.rect.x % 32 == 0:
200            self.y_vel = self.vector_dict[self.direction][1]
201        if self.rect.y % 32 == 0:
202            self.x_vel = self.vector_dict[self.direction][0]
203
204
205    def begin_resting(self):
206        """Transition the player into the 'resting' state."""
207        self.state = 'resting'
208        self.index = 1
209        self.x_vel = self.y_vel = 0
210
211
212    def begin_auto_moving(self, direction):
213        """Transition sprite to a automatic moving state"""
214        self.direction = direction
215        self.image_list = self.animation_dict[direction]
216        self.state = 'automoving'
217        self.x_vel = self.vector_dict[direction][0]
218        self.y_vel = self.vector_dict[direction][1]
219        self.move_timer = self.current_time
220
221
222    def begin_auto_resting(self):
223        """Transition sprite to an automatic resting state"""
224        self.state = 'autoresting'
225        self.index = 0
226        self.x_vel = self.y_vel = 0
227        self.move_timer = self.current_time
228
229
230    def auto_resting(self):
231        """
232        Determine when to move a sprite from resting to moving in a random
233        direction.
234        """
235        #self.image = self.image_list[self.index]
236        self.animation(700)
237
238        assert(self.rect.y % 32 == 0), ('Player not centered on tile: '
239                                        + str(self.rect.y))
240        assert(self.rect.x % 32 == 0), ('Player not centered on tile'
241                                        + str(self.rect.x))
242
243        if (self.current_time - self.move_timer) > 2000:
244            direction_list = ['up', 'down', 'left', 'right']
245            random.shuffle(direction_list)
246            direction = direction_list[0]
247            self.begin_auto_moving(direction)
248            self.move_timer = self.current_time
249
250
251
252    def auto_moving(self):
253        """Animate sprite and check to stop"""
254        self.animation()
255
256        assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \
257            'Not centered on tile'
258
259
260
261class Player(Person):
262    """User controlled character"""
263
264    def __init__(self, direction):
265        super(Player, self).__init__('player', 0, 0, direction)
266
267
268    def create_vector_dict(self):
269        """Return a dictionary of x and y velocities set to
270        direction keys."""
271        vector_dict = {'up': (0, -2),
272                       'down': (0, 2),
273                       'left': (-2, 0),
274                       'right': (2, 0)}
275
276        return vector_dict
277
278
279    def update(self, keys, current_time):
280        """Updates player behavior"""
281        self.blockers = self.set_blockers()
282        self.keys = keys
283        self.current_time = current_time
284        self.check_for_input()
285        state_function = self.state_dict[self.state]
286        state_function()
287        self.location = self.get_tile_location()
288
289
290    def check_for_input(self):
291        """Checks for player input"""
292        if self.state == 'resting':
293            if self.keys[pg.K_UP]:
294                self.begin_moving('up')
295            elif self.keys[pg.K_DOWN]:
296                self.begin_moving('down')
297            elif self.keys[pg.K_LEFT]:
298                self.begin_moving('left')
299            elif self.keys[pg.K_RIGHT]:
300                self.begin_moving('right')
301
302
303
304class Soldier(Person):
305    """Soldier for the castle"""
306
307    def __init__(self, x, y):
308        super(Soldier, self).__init__('soldier', x, y)
309        self.state = 'animated resting'
310
311
312
313class FemaleVillager(Person):
314    """Female Person for town"""
315
316    def __init__(self, x, y):
317        super(FemaleVillager, self).__init__('femalevillager', x, y)
318
319
320class MaleVillager(Person):
321    """Male Person for town"""
322
323    def __init__(self):
324        super(MaleVillager, self).__init__('male villager', x, y)
325