all repos — Legends-RPG @ 5d4d09e8ded491a659f167c2e96f001b5a78037e

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