all repos — Legends-RPG @ 29d6707d48fd9a25b37b8dd03c3031896d11dc36

A fantasy mini-RPG built with Python and Pygame.

data/components/person.py (view raw)

  1from __future__ import division
  2import math, random, copy
  3import pygame as pg
  4from .. import setup
  5from .. import constants as c
  6
  7
  8class Person(pg.sprite.Sprite):
  9    """Base class for all world characters
 10    controlled by the computer"""
 11
 12    def __init__(self, sheet_key, x, y, direction='down', state='resting', index=0):
 13        super(Person, self).__init__()
 14        self.name = sheet_key
 15        self.get_image = setup.tools.get_image
 16        self.spritesheet_dict = self.create_spritesheet_dict(sheet_key)
 17        self.animation_dict = self.create_animation_dict()
 18        self.index = index
 19        self.direction = direction
 20        self.image_list = self.animation_dict[self.direction]
 21        self.image = self.image_list[self.index]
 22        self.rect = self.image.get_rect(left=x, top=y)
 23        self.origin_pos = self.rect.topleft
 24        self.state_dict = self.create_state_dict()
 25        self.vector_dict = self.create_vector_dict()
 26        self.x_vel = 0
 27        self.y_vel = 0
 28        self.timer = 0.0
 29        self.move_timer = 0.0
 30        self.current_time = 0.0
 31        self.state = state
 32        self.blockers = self.set_blockers()
 33        self.location = self.get_tile_location()
 34        self.dialogue = ['Location: ' + str(self.location)]
 35        self.default_direction = direction
 36        self.item = None
 37        self.wander_box = self.make_wander_box()
 38        self.observers = []
 39        self.level = 1
 40        self.health = 0
 41
 42    def create_spritesheet_dict(self, sheet_key):
 43        """Implemented by inheriting classes"""
 44        image_list = []
 45        image_dict = {}
 46        sheet = setup.GFX[sheet_key]
 47
 48        image_keys = ['facing up 1', 'facing up 2',
 49                      'facing down 1', 'facing down 2',
 50                      'facing left 1', 'facing left 2',
 51                      'facing right 1', 'facing right 2']
 52
 53        for row in range(2):
 54            for column in range(4):
 55                image_list.append(
 56                    self.get_image(column*32, row*32, 32, 32, sheet))
 57
 58        for key, image in zip(image_keys, image_list):
 59            image_dict[key] = image
 60
 61        return image_dict
 62
 63    def create_animation_dict(self):
 64        """Return a dictionary of image lists for animation"""
 65        image_dict = self.spritesheet_dict
 66
 67        left_list = [image_dict['facing left 1'], image_dict['facing left 2']]
 68        right_list = [image_dict['facing right 1'], image_dict['facing right 2']]
 69        up_list = [image_dict['facing up 1'], image_dict['facing up 2']]
 70        down_list = [image_dict['facing down 1'], image_dict['facing down 2']]
 71
 72        direction_dict = {'left': left_list,
 73                          'right': right_list,
 74                          'up': up_list,
 75                          'down': down_list}
 76
 77        return direction_dict
 78
 79    def create_state_dict(self):
 80        """Return a dictionary of all state methods"""
 81        state_dict = {'resting': self.resting,
 82                      'moving': self.moving,
 83                      'animated resting': self.animated_resting,
 84                      'autoresting': self.auto_resting,
 85                      'automoving': self.auto_moving,
 86                      'battle resting': self.battle_resting,
 87                      'attack': self.attack,
 88                      'enemy attack': self.enemy_attack,
 89                      c.RUN_AWAY: self.run_away}
 90
 91        return state_dict
 92
 93    def create_vector_dict(self):
 94        """Return a dictionary of x and y velocities set to
 95        direction keys."""
 96        vector_dict = {'up': (0, -1),
 97                       'down': (0, 1),
 98                       'left': (-1, 0),
 99                       'right': (1, 0)}
100
101        return vector_dict
102
103    def update(self, current_time, *args):
104        """Implemented by inheriting classes"""
105        self.blockers = self.set_blockers()
106        self.current_time = current_time
107        self.image_list = self.animation_dict[self.direction]
108        state_function = self.state_dict[self.state]
109        state_function()
110        self.location = self.get_tile_location()
111
112
113
114    def set_blockers(self):
115        """Sets blockers to prevent collision with other sprites"""
116        blockers = []
117
118        if self.state == 'resting' or self.state == 'autoresting':
119            blockers.append(pg.Rect(self.rect.x, self.rect.y, 32, 32))
120
121        elif self.state == 'moving' or self.state == 'automoving':
122            if self.rect.x % 32 == 0:
123                tile_float = self.rect.y / float(32)
124                tile1 = (self.rect.x, math.ceil(tile_float)*32)
125                tile2 = (self.rect.x, math.floor(tile_float)*32)
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            elif self.rect.y % 32 == 0:
131                tile_float = self.rect.x / float(32)
132                tile1 = (math.ceil(tile_float)*32, self.rect.y)
133                tile2 = (math.floor(tile_float)*32, self.rect.y)
134                tile_rect1 = pg.Rect(tile1[0], tile1[1], 32, 32)
135                tile_rect2 = pg.Rect(tile2[0], tile2[1], 32, 32)
136                blockers.extend([tile_rect1, tile_rect2])
137
138        return blockers
139
140    def get_tile_location(self):
141        """
142        Convert pygame coordinates into tile coordinates.
143        """
144        if self.rect.x == 0:
145            tile_x = 0
146        elif self.rect.x % 32 == 0:
147            tile_x = (self.rect.x / 32)
148        else:
149            tile_x = 0
150
151        if self.rect.y == 0:
152            tile_y = 0
153        elif self.rect.y % 32 == 0:
154            tile_y = (self.rect.y / 32)
155        else:
156            tile_y = 0
157
158        return [tile_x, tile_y]
159
160
161    def make_wander_box(self):
162        """
163        Make a list of rects that surround the initial location
164        of a sprite to limit his/her wandering.
165        """
166        x = int(self.location[0])
167        y = int(self.location[1])
168        box_list = []
169        box_rects = []
170
171        for i in range(x-3, x+4):
172            box_list.append([i, y-3])
173            box_list.append([i, y+3])
174
175        for i in range(y-2, y+3):
176            box_list.append([x-3, i])
177            box_list.append([x+3, i])
178
179        for box in box_list:
180            left = box[0]*32
181            top = box[1]*32
182            box_rects.append(pg.Rect(left, top, 32, 32))
183
184        return box_rects
185
186
187    def resting(self):
188        """
189        When the Person is not moving between tiles.
190        Checks if the player is centered on a tile.
191        """
192        self.image = self.image_list[self.index]
193
194        assert(self.rect.y % 32 == 0), ('Player not centered on tile: '
195                                        + str(self.rect.y) + " : " + str(self.name))
196        assert(self.rect.x % 32 == 0), ('Player not centered on tile'
197                                        + str(self.rect.x))
198
199    def moving(self):
200        """
201        Increment index and set self.image for animation.
202        """
203        self.animation()
204        assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \
205            'Not centered on tile'
206
207    def animated_resting(self):
208        self.animation(500)
209
210    def animation(self, freq=100):
211        """
212        Adjust sprite image frame based on timer.
213        """
214        if (self.current_time - self.timer) > freq:
215            if self.index < (len(self.image_list) - 1):
216                self.index += 1
217            else:
218                self.index = 0
219            self.timer = self.current_time
220
221        self.image = self.image_list[self.index]
222
223    def begin_moving(self, direction):
224        """
225        Transition the player into the 'moving' state.
226        """
227        self.direction = direction
228        self.image_list = self.animation_dict[direction]
229        self.timer = self.current_time
230        self.move_timer = self.current_time
231        self.state = 'moving'
232
233        if self.rect.x % 32 == 0:
234            self.y_vel = self.vector_dict[self.direction][1]
235        if self.rect.y % 32 == 0:
236            self.x_vel = self.vector_dict[self.direction][0]
237
238
239    def begin_resting(self):
240        """
241        Transition the player into the 'resting' state.
242        """
243        self.state = 'resting'
244        self.index = 1
245        self.x_vel = self.y_vel = 0
246
247    def begin_auto_moving(self, direction):
248        """
249        Transition sprite to a automatic moving state.
250        """
251        self.direction = direction
252        self.image_list = self.animation_dict[direction]
253        self.state = 'automoving'
254        self.x_vel = self.vector_dict[direction][0]
255        self.y_vel = self.vector_dict[direction][1]
256        self.move_timer = self.current_time
257
258    def begin_auto_resting(self):
259        """
260        Transition sprite to an automatic resting state.
261        """
262        self.state = 'autoresting'
263        self.index = 1
264        self.x_vel = self.y_vel = 0
265        self.move_timer = self.current_time
266
267
268    def auto_resting(self):
269        """
270        Determine when to move a sprite from resting to moving in a random
271        direction.
272        """
273        #self.image = self.image_list[self.index]
274        self.image_list = self.animation_dict[self.direction]
275        self.image = self.image_list[self.index]
276
277        assert(self.rect.y % 32 == 0), ('Player not centered on tile: '
278                                        + str(self.rect.y))
279        assert(self.rect.x % 32 == 0), ('Player not centered on tile'
280                                        + str(self.rect.x))
281
282        if (self.current_time - self.move_timer) > 2000:
283            direction_list = ['up', 'down', 'left', 'right']
284            random.shuffle(direction_list)
285            direction = direction_list[0]
286            self.begin_auto_moving(direction)
287            self.move_timer = self.current_time
288
289    def battle_resting(self):
290        """
291        Player stays still during battle state unless he attacks.
292        """
293        pass
294
295    def enter_attack_state(self, enemy):
296        """
297        Set values for attack state.
298        """
299        self.attacked_enemy = enemy
300        self.x_vel = -5
301        self.state = 'attack'
302
303
304    def attack(self):
305        """
306        Player does an attack animation.
307        """
308        FAST_FORWARD = -5
309        FAST_BACK = 5
310
311        self.rect.x += self.x_vel
312
313        if self.x_vel == FAST_FORWARD:
314            self.image = self.spritesheet_dict['facing left 1']
315            self.image = pg.transform.scale2x(self.image)
316            if self.rect.x <= self.origin_pos[0] - 110:
317                self.x_vel = FAST_BACK
318                self.notify('attack animation')
319        else:
320            if self.rect.x >= self.origin_pos[0]:
321                self.rect.x = self.origin_pos[0]
322                self.x_vel = 0
323                self.state = 'battle resting'
324                self.image = self.spritesheet_dict['facing left 2']
325                self.image = pg.transform.scale2x(self.image)
326                self.notify(c.PLAYER_FINISHED_ATTACK)
327
328    def enter_enemy_attack_state(self):
329        """
330        Set values for enemy attack state.
331        """
332        self.x_vel = -5
333        self.state = 'enemy attack'
334        self.origin_pos = self.rect.topleft
335        self.move_counter = 0
336
337    def enemy_attack(self):
338        """
339        Enemy does an attack animation.
340        """
341        FAST_LEFT = -5
342        FAST_RIGHT = 5
343        STARTX = self.origin_pos[0]
344
345        self.rect.x += self.x_vel
346
347        if self.move_counter == 3:
348            self.x_vel = 0
349            self.state = 'battle resting'
350            self.rect.x = STARTX
351            self.notify(c.ENEMY_ATTACK_DAMAGE)
352
353        elif self.x_vel == FAST_LEFT:
354            if self.rect.x <= (STARTX - 15):
355                self.x_vel = FAST_RIGHT
356        elif self.x_vel == FAST_RIGHT:
357            if self.rect.x >= (STARTX + 15):
358                self.move_counter += 1
359                self.x_vel = FAST_LEFT
360
361    def auto_moving(self):
362        """
363        Animate sprite and check to stop.
364        """
365        self.animation()
366
367        assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \
368            'Not centered on tile'
369
370    def notify(self, event):
371        """
372        Notify all observers of events.
373        """
374        for observer in self.observers:
375            observer.on_notify(event)
376
377    def calculate_hit(self):
378        """
379        Calculate hit strength based on attack stats.
380        """
381        max_strength = self.level * 5
382        min_strength = 0
383        return random.randint(min_strength, max_strength)
384
385    def run_away(self):
386        """
387        Run away from battle state.
388        """
389        X_VEL = 5
390        self.rect.x += X_VEL
391        self.direction = 'right'
392        self.small_image_list = self.animation_dict[self.direction]
393        self.image_list = []
394        for image in self.small_image_list:
395            self.image_list.append(pg.transform.scale2x(image))
396        self.animation()
397
398
399class Player(Person):
400    """
401    User controlled character.
402    """
403
404    def __init__(self, direction, x=0, y=0, state='resting', index=0):
405        super(Player, self).__init__('player', x, y, direction, state, index)
406        self.damaged = False
407        self.healing = False
408        self.damage_alpha = 0
409        self.healing_alpha = 0
410        self.fade_in = True
411
412    def create_vector_dict(self):
413        """Return a dictionary of x and y velocities set to
414        direction keys."""
415        vector_dict = {'up': (0, -2),
416                       'down': (0, 2),
417                       'left': (-2, 0),
418                       'right': (2, 0)}
419
420        return vector_dict
421
422    def update(self, keys, current_time):
423        """Updates player behavior"""
424        self.current_time = current_time
425        self.damage_animation()
426        self.healing_animation()
427        self.blockers = self.set_blockers()
428        self.keys = keys
429        self.check_for_input()
430        state_function = self.state_dict[self.state]
431        state_function()
432        self.location = self.get_tile_location()
433
434    def damage_animation(self):
435        """
436        Put a red overlay over sprite to indicate damage.
437        """
438        if self.damaged:
439            self.image = copy.copy(self.spritesheet_dict['facing left 2'])
440            self.image = pg.transform.scale2x(self.image).convert_alpha()
441            damage_image = copy.copy(self.image).convert_alpha()
442            damage_image.fill((255, 0, 0, self.damage_alpha), special_flags=pg.BLEND_RGBA_MULT)
443            self.image.blit(damage_image, (0, 0))
444            if self.fade_in:
445                self.damage_alpha += 25
446                if self.damage_alpha >= 255:
447                    self.fade_in = False
448                    self.damage_alpha = 255
449            elif not self.fade_in:
450                self.damage_alpha -= 25
451                if self.damage_alpha <= 0:
452                    self.damage_alpha = 0
453                    self.damaged = False
454                    self.fade_in = True
455                    self.image = self.spritesheet_dict['facing left 2']
456                    self.image = pg.transform.scale2x(self.image)
457
458    def healing_animation(self):
459        """
460        Put a green overlay over sprite to indicate healing.
461        """
462        if self.healing:
463            self.image = copy.copy(self.spritesheet_dict['facing left 2'])
464            self.image = pg.transform.scale2x(self.image).convert_alpha()
465            healing_image = copy.copy(self.image).convert_alpha()
466            healing_image.fill((0, 255, 0, self.healing_alpha), special_flags=pg.BLEND_RGBA_MULT)
467            self.image.blit(healing_image, (0, 0))
468            if self.fade_in:
469                self.healing_alpha += 25
470                if self.healing_alpha >= 255:
471                    self.fade_in = False
472                    self.healing_alpha = 255
473            elif not self.fade_in:
474                self.healing_alpha -= 25
475                if self.healing_alpha <= 0:
476                    self.healing_alpha = 0
477                    self.healing = False
478                    self.fade_in = True
479                    self.image = self.spritesheet_dict['facing left 2']
480                    self.image = pg.transform.scale2x(self.image)
481
482
483
484    def check_for_input(self):
485        """Checks for player input"""
486        if self.state == 'resting':
487            if self.keys[pg.K_UP]:
488                self.begin_moving('up')
489            elif self.keys[pg.K_DOWN]:
490                self.begin_moving('down')
491            elif self.keys[pg.K_LEFT]:
492                self.begin_moving('left')
493            elif self.keys[pg.K_RIGHT]:
494                self.begin_moving('right')
495
496    def calculate_hit(self):
497        """
498        Calculate hit strength based on attack stats.
499        """
500        max_strength = 5 + (self.level * 5)
501        min_strength = max_strength // 2
502        return random.randint(min_strength, max_strength)
503
504
505
506
507class Well(pg.sprite.Sprite):
508    """Talking well"""
509    def __init__(self, x, y):
510        super(Well, self).__init__()
511        self.image = pg.Surface((32, 32))
512        self.image.set_colorkey((0,0,0))
513        self.rect = self.image.get_rect(left=x, top=y)
514        self.location = self.get_location()
515        self.dialogue = ["I'm a well!"]
516        self.blockers = [self.rect]
517        self.x_vel = self.y_vel = 0
518        self.state = 'resting'
519        self.direction = 'down'
520        self.default_direction = self.direction
521        self.item = None
522        self.wander_box = []
523
524    def get_location(self):
525        """Get tile location"""
526        x = self.rect.x / 32
527        y = self.rect.y / 32
528
529        return [x, y]
530
531    def begin_auto_resting(self):
532        """Placeholder"""
533        pass
534
535