Mercurial > skaapsteker
annotate skaapsteker/sprites/player.py @ 148:0474df61d5b1
Clamp very slow horizontal motion
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Tue, 05 Apr 2011 17:13:32 +0200 |
parents | 887d69907b22 |
children | 5b4d73d301a1 |
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 |
90 | 24 # State flags and such |
25 self.running = False | |
26 self.jumping = False | |
27 self.flying = False | |
28 self._load_images() | |
29 # We muck with these in load for convience, so ensure they're right | |
30 self.tails = 0 | |
31 self.set_facing('left') | |
32 self.set_image() | |
33 self.set_pos((0, 0)) | |
34 self._layer = Layers.PLAYER | |
35 | |
36 def set_image(self): | |
37 key = self._make_key() | |
120 | 38 images = self._image_dict[key] |
39 if self._animation_frame >= len(images): | |
40 self._animation_frame = 0.0 | |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
41 if self.rect: |
147
887d69907b22
Use bottomleft, so we stay above the floor as we change frames
Neil Muller <drnlmuller@gmail.com>
parents:
144
diff
changeset
|
42 cur_pos = self.collide_rect.bottomleft |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
43 else: |
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
44 cur_pos = (0, 0) |
120 | 45 self.image = images[int(self._animation_frame)] |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
46 self.rect = self.image.get_rect() |
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
47 self.collide_rect = self.image.get_bounding_rect(1) |
147
887d69907b22
Use bottomleft, so we stay above the floor as we change frames
Neil Muller <drnlmuller@gmail.com>
parents:
144
diff
changeset
|
48 self.rect_offset = self.rect.left - self.collide_rect.left, self.rect.bottom - self.collide_rect.bottom |
887d69907b22
Use bottomleft, so we stay above the floor as we change frames
Neil Muller <drnlmuller@gmail.com>
parents:
144
diff
changeset
|
49 self.collide_rect.bottomleft = cur_pos |
887d69907b22
Use bottomleft, so we stay above the floor as we change frames
Neil Muller <drnlmuller@gmail.com>
parents:
144
diff
changeset
|
50 self.rect.bottomleft = 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
|
51 self.init_pos() |
90 | 52 |
116
69a97094417a
Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents:
115
diff
changeset
|
53 def update(self): |
120 | 54 v_x, v_y = self.velocity |
55 self._animation_frame += abs(v_x) / 3000.0 | |
148
0474df61d5b1
Clamp very slow horizontal motion
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
56 if abs(v_x) < 10: |
0474df61d5b1
Clamp very slow horizontal motion
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
57 # 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
|
58 self.velocity = (0, v_y) |
120 | 59 self.running = False |
60 else: | |
61 self.running = True | |
62 self.set_image() | |
116
69a97094417a
Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents:
115
diff
changeset
|
63 |
90 | 64 def set_facing(self, new_facing): |
65 self.facing = new_facing | |
66 | |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
67 |
90 | 68 def set_pos(self, pos): |
69 self.starting_tile_pos = pos | |
144
6b488e1351a5
Buggy ground implementation. Make the world less bouncy
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
70 self.rect.topleft = pos[0] * TILE_SIZE[0] + self.rect_offset[0], pos[1] * TILE_SIZE[1] + self.rect_offset[1] |
6b488e1351a5
Buggy ground implementation. Make the world less bouncy
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
71 self.collide_rect.topleft = pos[0] * TILE_SIZE[0], pos[1] * TILE_SIZE[1] |
90 | 72 |
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
|
73 def action_left(self): |
115 | 74 if self.facing != 'left': |
75 self.facing = 'left' | |
76 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
|
77 self.deltav((-100.0, 0.0)) |
90 | 78 |
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
|
79 def action_right(self): |
115 | 80 if self.facing != 'right': |
81 self.facing = 'right' | |
82 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
|
83 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
|
84 |
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_up(self): |
144
6b488e1351a5
Buggy ground implementation. Make the world less bouncy
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
86 if self.on_ground: |
6b488e1351a5
Buggy ground implementation. Make the world less bouncy
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
87 self.deltav((0.0, -350.0)) |
6b488e1351a5
Buggy ground implementation. Make the world less bouncy
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
88 self.on_ground = 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
|
89 |
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
|
90 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
|
91 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
|
92 |
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
|
93 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
|
94 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
|
95 |
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 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
|
97 print "F2" |
90 | 98 |
99 def _get_action(self): | |
100 if self.running: | |
101 return 'running' | |
102 if self.jumping: | |
103 return 'jumpin' | |
104 return 'standing' | |
105 | |
106 def _make_key(self, action=None): | |
107 if action is None: | |
108 action = self._get_action() | |
109 tails = self.tails | |
110 if self.tails >= 4: | |
111 tails = 4 | |
112 elif self.tails >= 2: | |
113 tails = 2 | |
114 return '%s %s %d' % (action, self.facing, tails) | |
115 | |
116 def _load_images(self): | |
117 for action in ['standing', 'running', 'jumping']: | |
118 for tails in [0, 1, 2, 4]: | |
119 self.tails = tails | |
120 directory = os.path.join('sprites', | |
121 'kitsune_%s' % action, | |
122 'kitsune_%s_%dtail' % (action, tails)) | |
123 for facing in ['left', 'right']: | |
124 self.facing = facing | |
125 key = self._make_key(action) | |
126 self._image_dict[key] = [] | |
127 for image_file in get_files(directory): | |
128 if image_file.startswith('.'): | |
129 # Skip extra junk for now | |
130 continue | |
131 image = load_image(os.path.join(directory, image_file)) | |
132 if facing == 'right': | |
133 image = pygame.transform.flip(image, True, False) | |
134 self._image_dict[key].append(image) |