all repos — Legends-RPG @ 7c7eafe8cb828455b8babf01082d53a815115ff1

A fantasy mini-RPG built with Python and Pygame.

data/components/person.py (view raw)

  1import math, random
  2import pygame as pg
  3from .. import setup
  4
  5
  6class Person(pg.sprite.Sprite):
  7    """Base class for all world characters
  8    controlled by the computer"""
  9
 10    def __init__(self, sheet_key, x, y, direction='down', state='resting', index=0):
 11        super(Person, self).__init__()
 12        self.name = sheet_key
 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 = index
 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.origin_pos = self.rect.topleft
 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 = state
 30        self.blockers = self.set_blockers()
 31        self.location = self.get_tile_location()
 32        self.dialogue = ['Location: ' + str(self.location)]
 33        self.default_direction = direction
 34        self.item = None
 35        self.wander_box = self.make_wander_box()
 36
 37    def create_spritesheet_dict(self, sheet_key):
 38        """Implemented by inheriting classes"""
 39        image_list = []
 40        image_dict = {}
 41        sheet = setup.GFX[sheet_key]
 42
 43        image_keys = ['facing up 1', 'facing up 2',
 44                      'facing down 1', 'facing down 2',
 45                      'facing left 1', 'facing left 2',
 46                      'facing right 1', 'facing right 2']
 47
 48        for row in range(2):
 49            for column in range(4):
 50                image_list.append(
 51                    self.get_image(column*32, row*32, 32, 32, sheet))
 52
 53        for key, image in zip(image_keys, image_list):
 54            image_dict[key] = image
 55
 56        return image_dict
 57
 58    def create_animation_dict(self):
 59        """Return a dictionary of image lists for animation"""
 60        image_dict = self.spritesheet_dict
 61
 62        left_list = [image_dict['facing left 1'], image_dict['facing left 2']]
 63        right_list = [image_dict['facing right 1'], image_dict['facing right 2']]
 64        up_list = [image_dict['facing up 1'], image_dict['facing up 2']]
 65        down_list = [image_dict['facing down 1'], image_dict['facing down 2']]
 66
 67        direction_dict = {'left': left_list,
 68                          'right': right_list,
 69                          'up': up_list,
 70                          'down': down_list}
 71
 72        return direction_dict
 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                      'battle resting': self.battle_resting,
 82                      'attack': self.attack}
 83
 84        return state_dict
 85
 86    def create_vector_dict(self):
 87        """Return a dictionary of x and y velocities set to
 88        direction keys."""
 89        vector_dict = {'up': (0, -1),
 90                       'down': (0, 1),
 91                       'left': (-1, 0),
 92                       'right': (1, 0)}
 93
 94        return vector_dict
 95
 96    def update(self, current_time, *args):
 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
107    def set_blockers(self):
108        """Sets blockers to prevent collision with other sprites"""
109        blockers = []
110
111        if self.state == 'resting' or self.state == 'autoresting':
112            blockers.append(pg.Rect(self.rect.x, self.rect.y, 32, 32))
113
114        elif self.state == 'moving' or self.state == 'automoving':
115            if self.rect.x % 32 == 0:
116                tile_float = self.rect.y / float(32)
117                tile1 = (self.rect.x, math.ceil(tile_float)*32)
118                tile2 = (self.rect.x, math.floor(tile_float)*32)
119                tile_rect1 = pg.Rect(tile1[0], tile1[1], 32, 32)
120                tile_rect2 = pg.Rect(tile2[0], tile2[1], 32, 32)
121                blockers.extend([tile_rect1, tile_rect2])
122
123            elif self.rect.y % 32 == 0:
124                tile_float = self.rect.x / float(32)
125                tile1 = (math.ceil(tile_float)*32, self.rect.y)
126                tile2 = (math.floor(tile_float)*32, self.rect.y)
127                tile_rect1 = pg.Rect(tile1[0], tile1[1], 32, 32)
128                tile_rect2 = pg.Rect(tile2[0], tile2[1], 32, 32)
129                blockers.extend([tile_rect1, tile_rect2])
130
131        return blockers
132
133    def get_tile_location(self):
134        """
135        Convert pygame coordinates into tile coordinates.
136        """
137        if self.rect.x == 0:
138            tile_x = 0
139        elif self.rect.x % 32 == 0:
140            tile_x = (self.rect.x / 32)
141        else:
142            tile_x = 0
143
144        if self.rect.y == 0:
145            tile_y = 0
146        elif self.rect.y % 32 == 0:
147            tile_y = (self.rect.y / 32)
148        else:
149            tile_y = 0
150
151        return [tile_x, tile_y]
152
153
154    def make_wander_box(self):
155        """
156        Make a list of rects that surround the initial location
157        of a sprite to limit his/her wandering.
158        """
159        x = int(self.location[0])
160        y = int(self.location[1])
161        box_list = []
162        box_rects = []
163
164        for i in range(x-3, x+4):
165            box_list.append([i, y-3])
166            box_list.append([i, y+3])
167
168        for i in range(y-2, y+3):
169            box_list.append([x-3, i])
170            box_list.append([x+3, i])
171
172        for box in box_list:
173            left = box[0]*32
174            top = box[1]*32
175            box_rects.append(pg.Rect(left, top, 32, 32))
176
177        return box_rects
178
179
180    def resting(self):
181        """
182        When the Person is not moving between tiles.
183        Checks if the player is centered on a tile.
184        """
185        self.image = self.image_list[self.index]
186
187        assert(self.rect.y % 32 == 0), ('Player not centered on tile: '
188                                        + str(self.rect.y) + " : " + str(self.name))
189        assert(self.rect.x % 32 == 0), ('Player not centered on tile'
190                                        + str(self.rect.x))
191
192    def moving(self):
193        """
194        Increment index and set self.image for animation.
195        """
196        self.animation()
197        assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \
198            'Not centered on tile'
199
200    def animated_resting(self):
201        self.animation(500)
202
203    def animation(self, freq=100):
204        """
205        Adjust sprite image frame based on timer.
206        """
207        if (self.current_time - self.timer) > freq:
208            if self.index < (len(self.image_list) - 1):
209                self.index += 1
210            else:
211                self.index = 0
212            self.timer = self.current_time
213
214        self.image = self.image_list[self.index]
215
216    def begin_moving(self, direction):
217        """
218        Transition the player into the 'moving' state.
219        """
220        self.direction = direction
221        self.image_list = self.animation_dict[direction]
222        self.timer = self.current_time
223        self.move_timer = self.current_time
224        self.state = 'moving'
225
226        if self.rect.x % 32 == 0:
227            self.y_vel = self.vector_dict[self.direction][1]
228        if self.rect.y % 32 == 0:
229            self.x_vel = self.vector_dict[self.direction][0]
230
231
232    def begin_resting(self):
233        """
234        Transition the player into the 'resting' state.
235        """
236        self.state = 'resting'
237        self.index = 1
238        self.x_vel = self.y_vel = 0
239
240    def begin_auto_moving(self, direction):
241        """
242        Transition sprite to a automatic moving state.
243        """
244        self.direction = direction
245        self.image_list = self.animation_dict[direction]
246        self.state = 'automoving'
247        self.x_vel = self.vector_dict[direction][0]
248        self.y_vel = self.vector_dict[direction][1]
249        self.move_timer = self.current_time
250
251    def begin_auto_resting(self):
252        """
253        Transition sprite to an automatic resting state.
254        """
255        self.state = 'autoresting'
256        self.index = 1
257        self.x_vel = self.y_vel = 0
258        self.move_timer = self.current_time
259
260
261    def auto_resting(self):
262        """
263        Determine when to move a sprite from resting to moving in a random
264        direction.
265        """
266        #self.image = self.image_list[self.index]
267        self.image_list = self.animation_dict[self.direction]
268        self.image = self.image_list[self.index]
269
270        assert(self.rect.y % 32 == 0), ('Player not centered on tile: '
271                                        + str(self.rect.y))
272        assert(self.rect.x % 32 == 0), ('Player not centered on tile'
273                                        + str(self.rect.x))
274
275        if (self.current_time - self.move_timer) > 2000:
276            direction_list = ['up', 'down', 'left', 'right']
277            random.shuffle(direction_list)
278            direction = direction_list[0]
279            self.begin_auto_moving(direction)
280            self.move_timer = self.current_time
281
282    def battle_resting(self):
283        """
284        Player stays still during battle state unless he attacks.
285        """
286        pass
287
288    def enter_attack_state(self, enemy):
289        """
290        Set values for attack state.
291        """
292        self.x_vel = 1
293        self.state = 'attack'
294
295    def attack(self):
296        """
297        Player does an attack animation.
298        """
299        SLOW_BACK = 1
300        FAST_FORWARD = -5
301        FAST_BACK = 5
302
303        self.rect.x += self.x_vel
304
305        if self.x_vel == SLOW_BACK:
306            if self.rect.x >= self.origin_pos[0] + 10:
307                self.x_vel = FAST_FORWARD
308        elif self.x_vel == FAST_FORWARD:
309            if self.rect.topleft >= self.origin_pos:
310                self.image = self.spritesheet_dict['facing left 1']
311                self.image = pg.transform.scale2x(self.image)
312            elif self.rect.x <= self.origin_pos[0] - 100:
313                self.x_vel = FAST_BACK
314        else:
315            if self.rect.x >= self.origin_pos[0]:
316                self.rect.x = self.origin_pos[0]
317                self.x_vel = 0
318                self.state = 'battle resting'
319                self.image = self.spritesheet_dict['facing left 2']
320                self.image = pg.transform.scale2x(self.image)
321                self.observer.on_notify('select action')
322
323
324
325    def auto_moving(self):
326        """
327        Animate sprite and check to stop.
328        """
329        self.animation()
330
331        assert(self.rect.x % 32 == 0 or self.rect.y % 32 == 0), \
332            'Not centered on tile'
333
334
335class Player(Person):
336    """
337    User controlled character.
338    """
339
340    def __init__(self, direction, x=0, y=0, state='resting', index=0):
341        super(Player, self).__init__('player', x, y, direction, state, index)
342
343    def create_vector_dict(self):
344        """Return a dictionary of x and y velocities set to
345        direction keys."""
346        vector_dict = {'up': (0, -2),
347                       'down': (0, 2),
348                       'left': (-2, 0),
349                       'right': (2, 0)}
350
351        return vector_dict
352
353    def update(self, keys, current_time):
354        """Updates player behavior"""
355        self.blockers = self.set_blockers()
356        self.keys = keys
357        self.current_time = current_time
358        self.check_for_input()
359        state_function = self.state_dict[self.state]
360        state_function()
361        self.location = self.get_tile_location()
362
363    def check_for_input(self):
364        """Checks for player input"""
365        if self.state == 'resting':
366            if self.keys[pg.K_UP]:
367                self.begin_moving('up')
368            elif self.keys[pg.K_DOWN]:
369                self.begin_moving('down')
370            elif self.keys[pg.K_LEFT]:
371                self.begin_moving('left')
372            elif self.keys[pg.K_RIGHT]:
373                self.begin_moving('right')
374
375
376
377
378class Well(pg.sprite.Sprite):
379    """Talking well"""
380    def __init__(self, x, y):
381        super(Well, self).__init__()
382        self.image = pg.Surface((32, 32))
383        self.image.set_colorkey((0,0,0))
384        self.rect = self.image.get_rect(left=x, top=y)
385        self.location = self.get_location()
386        self.dialogue = ["I'm a well!"]
387        self.blockers = [self.rect]
388        self.x_vel = self.y_vel = 0
389        self.state = 'resting'
390        self.direction = 'down'
391        self.default_direction = self.direction
392        self.item = None
393        self.wander_box = []
394
395    def get_location(self):
396        """Get tile location"""
397        x = self.rect.x / 32
398        y = self.rect.y / 32
399
400        return [x, y]
401
402    def begin_auto_resting(self):
403        """Placeholder"""
404        pass
405
406