Mercurial > skaapsteker
annotate skaapsteker/sprites/player.py @ 115:b73724bb93f8
Fix fox facing
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Mon, 04 Apr 2011 21:45:59 +0200 |
parents | a76b8c49f1f9 |
children | 69a97094417a |
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])) | |
113
a76b8c49f1f9
Make fox rect a bit smaller as an experiment.
Simon Cross <hodgestar@gmail.com>
parents:
103
diff
changeset
|
44 self.rect.inflate_ip(-10, -10) |
90 | 45 |
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
|
46 def action_left(self): |
115 | 47 if self.facing != 'left': |
48 self.facing = 'left' | |
49 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
|
50 self.deltav((-100.0, 0.0)) |
90 | 51 |
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
|
52 def action_right(self): |
115 | 53 if self.facing != 'right': |
54 self.facing = 'right' | |
55 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
|
56 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
|
57 |
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 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
|
59 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
|
60 |
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 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
|
62 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
|
63 |
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 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
|
65 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
|
66 |
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 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
|
68 print "F2" |
90 | 69 |
70 def _get_action(self): | |
71 if self.running: | |
72 return 'running' | |
73 if self.jumping: | |
74 return 'jumpin' | |
75 return 'standing' | |
76 | |
77 def _make_key(self, action=None): | |
78 if action is None: | |
79 action = self._get_action() | |
80 tails = self.tails | |
81 if self.tails >= 4: | |
82 tails = 4 | |
83 elif self.tails >= 2: | |
84 tails = 2 | |
85 return '%s %s %d' % (action, self.facing, tails) | |
86 | |
87 def _load_images(self): | |
88 for action in ['standing', 'running', 'jumping']: | |
89 for tails in [0, 1, 2, 4]: | |
90 self.tails = tails | |
91 directory = os.path.join('sprites', | |
92 'kitsune_%s' % action, | |
93 'kitsune_%s_%dtail' % (action, tails)) | |
94 for facing in ['left', 'right']: | |
95 self.facing = facing | |
96 key = self._make_key(action) | |
97 self._image_dict[key] = [] | |
98 for image_file in get_files(directory): | |
99 if image_file.startswith('.'): | |
100 # Skip extra junk for now | |
101 continue | |
102 image = load_image(os.path.join(directory, image_file)) | |
103 if facing == 'right': | |
104 image = pygame.transform.flip(image, True, False) | |
105 self._image_dict[key].append(image) | |
106 | |
107 |