all repos — Legends-RPG @ c5d70de66c1b6acc4456bef4e66bb278f0375c37

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