Mercurial > skaapsteker
annotate skaapsteker/sprites/player.py @ 116:69a97094417a
Hook up per-tick sprite animations.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Mon, 04 Apr 2011 21:54:42 +0200 |
parents | b73724bb93f8 |
children | 9b08afeadf06 |
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]) |
116
69a97094417a
Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents:
115
diff
changeset
|
16 wants_updates = True |
97
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
90
diff
changeset
|
17 |
90 | 18 def __init__(self): |
19 Sprite.__init__(self) | |
20 self.image = None | |
21 self._image_dict = {} | |
22 # State flags and such | |
23 self.running = False | |
24 self.jumping = False | |
25 self.flying = False | |
26 self._load_images() | |
27 # We muck with these in load for convience, so ensure they're right | |
28 self.tails = 0 | |
29 self.set_facing('left') | |
30 self.set_image() | |
31 self.set_pos((0, 0)) | |
32 self._layer = Layers.PLAYER | |
33 | |
34 def set_image(self): | |
35 key = self._make_key() | |
36 # TODO: handle animations | |
37 self.image = self._image_dict[key][0] | |
38 | |
116
69a97094417a
Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents:
115
diff
changeset
|
39 def update(self): |
69a97094417a
Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents:
115
diff
changeset
|
40 pass # animate tail here |
69a97094417a
Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents:
115
diff
changeset
|
41 |
90 | 42 def set_facing(self, new_facing): |
43 self.facing = new_facing | |
44 | |
45 def set_pos(self, pos): | |
46 self.starting_tile_pos = pos | |
47 self.rect = self.image.get_rect(topleft=(pos[0]*TILE_SIZE[0], pos[1]*TILE_SIZE[1])) | |
113
a76b8c49f1f9
Make fox rect a bit smaller as an experiment.
Simon Cross <hodgestar@gmail.com>
parents:
103
diff
changeset
|
48 self.rect.inflate_ip(-10, -10) |
90 | 49 |
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
|
50 def action_left(self): |
115 | 51 if self.facing != 'left': |
52 self.facing = 'left' | |
53 self.set_image() | |
103
aaef228b6358
Run kitsune, run! And by the running of the kitsune gravity is restored to normal.
Simon Cross <hodgestar@gmail.com>
parents:
102
diff
changeset
|
54 self.deltav((-100.0, 0.0)) |
90 | 55 |
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
|
56 def action_right(self): |
115 | 57 if self.facing != 'right': |
58 self.facing = 'right' | |
59 self.set_image() | |
103
aaef228b6358
Run kitsune, run! And by the running of the kitsune gravity is restored to normal.
Simon Cross <hodgestar@gmail.com>
parents:
102
diff
changeset
|
60 self.deltav((100.0, 0.0)) |
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
|
61 |
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
|
62 def action_up(self): |
103
aaef228b6358
Run kitsune, run! And by the running of the kitsune gravity is restored to normal.
Simon Cross <hodgestar@gmail.com>
parents:
102
diff
changeset
|
63 self.deltav((0.0, -100.0)) |
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
|
64 |
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
|
65 def action_down(self): |
103
aaef228b6358
Run kitsune, run! And by the running of the kitsune gravity is restored to normal.
Simon Cross <hodgestar@gmail.com>
parents:
102
diff
changeset
|
66 self.deltav((0.0, 100.0)) |
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
|
67 |
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
|
68 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
|
69 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
|
70 |
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
|
71 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
|
72 print "F2" |
90 | 73 |
74 def _get_action(self): | |
75 if self.running: | |
76 return 'running' | |
77 if self.jumping: | |
78 return 'jumpin' | |
79 return 'standing' | |
80 | |
81 def _make_key(self, action=None): | |
82 if action is None: | |
83 action = self._get_action() | |
84 tails = self.tails | |
85 if self.tails >= 4: | |
86 tails = 4 | |
87 elif self.tails >= 2: | |
88 tails = 2 | |
89 return '%s %s %d' % (action, self.facing, tails) | |
90 | |
91 def _load_images(self): | |
92 for action in ['standing', 'running', 'jumping']: | |
93 for tails in [0, 1, 2, 4]: | |
94 self.tails = tails | |
95 directory = os.path.join('sprites', | |
96 'kitsune_%s' % action, | |
97 'kitsune_%s_%dtail' % (action, tails)) | |
98 for facing in ['left', 'right']: | |
99 self.facing = facing | |
100 key = self._make_key(action) | |
101 self._image_dict[key] = [] | |
102 for image_file in get_files(directory): | |
103 if image_file.startswith('.'): | |
104 # Skip extra junk for now | |
105 continue | |
106 image = load_image(os.path.join(directory, image_file)) | |
107 if facing == 'right': | |
108 image = pygame.transform.flip(image, True, False) | |
109 self._image_dict[key].append(image) | |
110 | |
111 |