Mercurial > skaapsteker
annotate skaapsteker/sprites/player.py @ 102:2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Mon, 04 Apr 2011 20:23:14 +0200 |
parents | 3eafceff6293 |
children | aaef228b6358 |
rev | line source |
---|---|
90 | 1 """Class for dealing with the player""" |
2 | |
3 import pygame.transform | |
4 import os | |
5 | |
100
3eafceff6293
Have monsters also collide with players as an interim solution while I relax, eat dinner and think about the best way to use the new collision layers.
Simon Cross <hodgestar@gmail.com>
parents:
97
diff
changeset
|
6 from skaapsteker.sprites.base import TILE_SIZE, PC_LAYER, MONSTER_LAYER |
90 | 7 from skaapsteker.physics import Sprite |
8 from skaapsteker.constants import Layers | |
9 from skaapsteker.data import get_files, load_image | |
10 | |
11 | |
12 class Player(Sprite): | |
13 | |
97
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
90
diff
changeset
|
14 collision_layer = PC_LAYER |
100
3eafceff6293
Have monsters also collide with players as an interim solution while I relax, eat dinner and think about the best way to use the new collision layers.
Simon Cross <hodgestar@gmail.com>
parents:
97
diff
changeset
|
15 collides_with = set([MONSTER_LAYER]) |
97
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
90
diff
changeset
|
16 |
90 | 17 def __init__(self): |
18 Sprite.__init__(self) | |
19 self.image = None | |
20 self._image_dict = {} | |
21 # State flags and such | |
22 self.running = False | |
23 self.jumping = False | |
24 self.flying = False | |
25 self._load_images() | |
26 # We muck with these in load for convience, so ensure they're right | |
27 self.tails = 0 | |
28 self.set_facing('left') | |
29 self.set_image() | |
30 self.set_pos((0, 0)) | |
31 self._layer = Layers.PLAYER | |
32 | |
33 def set_image(self): | |
34 key = self._make_key() | |
35 # TODO: handle animations | |
36 self.image = self._image_dict[key][0] | |
37 | |
38 def set_facing(self, new_facing): | |
39 self.facing = new_facing | |
40 | |
41 def set_pos(self, pos): | |
42 self.starting_tile_pos = pos | |
43 self.rect = self.image.get_rect(topleft=(pos[0]*TILE_SIZE[0], pos[1]*TILE_SIZE[1])) | |
44 | |
102
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
100
diff
changeset
|
45 def action_left(self): |
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
100
diff
changeset
|
46 print "L" |
90 | 47 |
102
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
100
diff
changeset
|
48 def action_right(self): |
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
100
diff
changeset
|
49 print "R" |
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
100
diff
changeset
|
50 |
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
100
diff
changeset
|
51 def action_up(self): |
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
100
diff
changeset
|
52 print "U" |
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
100
diff
changeset
|
53 |
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
100
diff
changeset
|
54 def action_down(self): |
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
100
diff
changeset
|
55 print "D" |
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
100
diff
changeset
|
56 |
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
100
diff
changeset
|
57 def action_fire1(self): |
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
100
diff
changeset
|
58 print "F1" |
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
100
diff
changeset
|
59 |
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
100
diff
changeset
|
60 def action_fire2(self): |
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
100
diff
changeset
|
61 print "F2" |
90 | 62 |
63 def _get_action(self): | |
64 if self.running: | |
65 return 'running' | |
66 if self.jumping: | |
67 return 'jumpin' | |
68 return 'standing' | |
69 | |
70 def _make_key(self, action=None): | |
71 if action is None: | |
72 action = self._get_action() | |
73 tails = self.tails | |
74 if self.tails >= 4: | |
75 tails = 4 | |
76 elif self.tails >= 2: | |
77 tails = 2 | |
78 return '%s %s %d' % (action, self.facing, tails) | |
79 | |
80 def _load_images(self): | |
81 for action in ['standing', 'running', 'jumping']: | |
82 for tails in [0, 1, 2, 4]: | |
83 self.tails = tails | |
84 directory = os.path.join('sprites', | |
85 'kitsune_%s' % action, | |
86 'kitsune_%s_%dtail' % (action, tails)) | |
87 for facing in ['left', 'right']: | |
88 self.facing = facing | |
89 key = self._make_key(action) | |
90 self._image_dict[key] = [] | |
91 for image_file in get_files(directory): | |
92 if image_file.startswith('.'): | |
93 # Skip extra junk for now | |
94 continue | |
95 image = load_image(os.path.join(directory, image_file)) | |
96 if facing == 'right': | |
97 image = pygame.transform.flip(image, True, False) | |
98 self._image_dict[key].append(image) | |
99 | |
100 |