Mercurial > skaapsteker
annotate skaapsteker/sprites/player.py @ 152:60138b935bc0
Make enemies block by default, so we can jump off them
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Tue, 05 Apr 2011 21:25:44 +0200 |
parents | 06be025c821c |
children | 794565c8f9f4 |
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 | |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
21 self.rect = None |
90 | 22 self._image_dict = {} |
120 | 23 self._animation_frame = 0.0 |
149
5b4d73d301a1
Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents:
148
diff
changeset
|
24 self._recent_collisions = [] |
90 | 25 # State flags and such |
26 self.running = False | |
27 self.jumping = False | |
28 self.flying = False | |
29 self._load_images() | |
30 # We muck with these in load for convience, so ensure they're right | |
31 self.tails = 0 | |
32 self.set_facing('left') | |
33 self.set_image() | |
34 self.set_pos((0, 0)) | |
35 self._layer = Layers.PLAYER | |
36 | |
37 def set_image(self): | |
38 key = self._make_key() | |
120 | 39 images = self._image_dict[key] |
40 if self._animation_frame >= len(images): | |
41 self._animation_frame = 0.0 | |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
42 if self.rect: |
151
06be025c821c
Use midbottom, rather than bottomleft, when changing fox frames
Neil Muller <drnlmuller@gmail.com>
parents:
149
diff
changeset
|
43 cur_pos = self.collide_rect.midbottom |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
44 else: |
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
45 cur_pos = (0, 0) |
149
5b4d73d301a1
Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents:
148
diff
changeset
|
46 cand_image = images[int(self._animation_frame)] |
5b4d73d301a1
Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents:
148
diff
changeset
|
47 cand_collide_rect = cand_image.get_bounding_rect(1) |
151
06be025c821c
Use midbottom, rather than bottomleft, when changing fox frames
Neil Muller <drnlmuller@gmail.com>
parents:
149
diff
changeset
|
48 cand_collide_rect.midbottom = cur_pos |
149
5b4d73d301a1
Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents:
148
diff
changeset
|
49 if cand_collide_rect.collidelist(self._recent_collisions) != -1 \ |
5b4d73d301a1
Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents:
148
diff
changeset
|
50 and self.collide_rect.collidelist(self._recent_collisions) == -1: |
5b4d73d301a1
Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents:
148
diff
changeset
|
51 # We introduce a new collision, so don't update the image |
5b4d73d301a1
Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents:
148
diff
changeset
|
52 return |
120 | 53 self.image = images[int(self._animation_frame)] |
149
5b4d73d301a1
Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents:
148
diff
changeset
|
54 self.collide_rect = self.image.get_bounding_rect(1) |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
55 self.rect = self.image.get_rect() |
151
06be025c821c
Use midbottom, rather than bottomleft, when changing fox frames
Neil Muller <drnlmuller@gmail.com>
parents:
149
diff
changeset
|
56 self.rect_offset = self.rect.centerx - self.collide_rect.centerx, self.rect.bottom - self.collide_rect.bottom |
06be025c821c
Use midbottom, rather than bottomleft, when changing fox frames
Neil Muller <drnlmuller@gmail.com>
parents:
149
diff
changeset
|
57 self.collide_rect.midbottom = cur_pos |
06be025c821c
Use midbottom, rather than bottomleft, when changing fox frames
Neil Muller <drnlmuller@gmail.com>
parents:
149
diff
changeset
|
58 self.rect.midbottom = cur_pos[0] + self.rect_offset[0], cur_pos[1] + self.rect_offset[1] |
144
6b488e1351a5
Buggy ground implementation. Make the world less bouncy
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
59 self.init_pos() |
90 | 60 |
116
69a97094417a
Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents:
115
diff
changeset
|
61 def update(self): |
120 | 62 v_x, v_y = self.velocity |
63 self._animation_frame += abs(v_x) / 3000.0 | |
148
0474df61d5b1
Clamp very slow horizontal motion
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
64 if abs(v_x) < 10: |
0474df61d5b1
Clamp very slow horizontal motion
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
65 # Clamp when we're not moving at least 5 pixel / s |
0474df61d5b1
Clamp very slow horizontal motion
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
66 self.velocity = (0, v_y) |
120 | 67 self.running = False |
68 else: | |
69 self.running = True | |
70 self.set_image() | |
116
69a97094417a
Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents:
115
diff
changeset
|
71 |
90 | 72 def set_facing(self, new_facing): |
73 self.facing = new_facing | |
74 | |
149
5b4d73d301a1
Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents:
148
diff
changeset
|
75 def collided(self, other): |
5b4d73d301a1
Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents:
148
diff
changeset
|
76 self._recent_collisions.append(other.collide_rect) |
5b4d73d301a1
Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents:
148
diff
changeset
|
77 while len(self._recent_collisions) > 10: |
5b4d73d301a1
Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents:
148
diff
changeset
|
78 self._recent_collisions.pop(0) |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
79 |
90 | 80 def set_pos(self, pos): |
81 self.starting_tile_pos = pos | |
152
60138b935bc0
Make enemies block by default, so we can jump off them
Neil Muller <drnlmuller@gmail.com>
parents:
151
diff
changeset
|
82 self.rect.midbottom = pos[0] * TILE_SIZE[0] + self.rect_offset[0], pos[1] * TILE_SIZE[1] + self.rect_offset[1] |
60138b935bc0
Make enemies block by default, so we can jump off them
Neil Muller <drnlmuller@gmail.com>
parents:
151
diff
changeset
|
83 self.collide_rect.midbottom = pos[0] * TILE_SIZE[0], pos[1] * TILE_SIZE[1] |
90 | 84 |
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
|
85 def action_left(self): |
115 | 86 if self.facing != 'left': |
87 self.facing = 'left' | |
88 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
|
89 self.deltav((-100.0, 0.0)) |
90 | 90 |
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
|
91 def action_right(self): |
115 | 92 if self.facing != 'right': |
93 self.facing = 'right' | |
94 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
|
95 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
|
96 |
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
|
97 def action_up(self): |
152
60138b935bc0
Make enemies block by default, so we can jump off them
Neil Muller <drnlmuller@gmail.com>
parents:
151
diff
changeset
|
98 if self.on_solid: |
144
6b488e1351a5
Buggy ground implementation. Make the world less bouncy
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
99 self.deltav((0.0, -350.0)) |
152
60138b935bc0
Make enemies block by default, so we can jump off them
Neil Muller <drnlmuller@gmail.com>
parents:
151
diff
changeset
|
100 self.on_solid = False |
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
|
101 |
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
|
102 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
|
103 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
|
104 |
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
|
105 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
|
106 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
|
107 |
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
|
108 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
|
109 print "F2" |
90 | 110 |
111 def _get_action(self): | |
112 if self.running: | |
113 return 'running' | |
114 if self.jumping: | |
115 return 'jumpin' | |
116 return 'standing' | |
117 | |
118 def _make_key(self, action=None): | |
119 if action is None: | |
120 action = self._get_action() | |
121 tails = self.tails | |
122 if self.tails >= 4: | |
123 tails = 4 | |
124 elif self.tails >= 2: | |
125 tails = 2 | |
126 return '%s %s %d' % (action, self.facing, tails) | |
127 | |
128 def _load_images(self): | |
129 for action in ['standing', 'running', 'jumping']: | |
130 for tails in [0, 1, 2, 4]: | |
131 self.tails = tails | |
132 directory = os.path.join('sprites', | |
133 'kitsune_%s' % action, | |
134 'kitsune_%s_%dtail' % (action, tails)) | |
135 for facing in ['left', 'right']: | |
136 self.facing = facing | |
137 key = self._make_key(action) | |
138 self._image_dict[key] = [] | |
139 for image_file in get_files(directory): | |
140 if image_file.startswith('.'): | |
141 # Skip extra junk for now | |
142 continue | |
143 image = load_image(os.path.join(directory, image_file)) | |
144 if facing == 'right': | |
145 image = pygame.transform.flip(image, True, False) | |
146 self._image_dict[key].append(image) |