Mercurial > skaapsteker
annotate skaapsteker/sprites/player.py @ 122:51bcc909873d
Saner, buggier collision rectangles
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Mon, 04 Apr 2011 22:39:42 +0200 |
parents | 9b08afeadf06 |
children | 6b488e1351a5 |
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: |
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
42 cur_pos = self.rect.topleft |
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) |
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
48 self.rect_offset = self.collide_rect.left - self.rect.left, self.collide_rect.top - self.rect.top |
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
49 self.rect.topleft = cur_pos |
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
50 self.collide_rect.topleft = cur_pos[0] + self.rect_offset[0], cur_pos[1] + self.rect_offset[1] |
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
51 |
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 | |
56 if v_x == 0: | |
57 self.running = False | |
58 else: | |
59 self.running = True | |
60 self.set_image() | |
116
69a97094417a
Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents:
115
diff
changeset
|
61 |
90 | 62 def set_facing(self, new_facing): |
63 self.facing = new_facing | |
64 | |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
65 |
90 | 66 def set_pos(self, pos): |
67 self.starting_tile_pos = pos | |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
68 self.collide_rect.topleft = pos[0] * TILE_SIZE[0] + self.rect_offset[0], pos[1] * TILE_SIZE[1] + self.rect_offset[1] |
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
69 self.rect.topleft = pos[0] * TILE_SIZE[0], pos[1] * TILE_SIZE[1] |
90 | 70 |
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
|
71 def action_left(self): |
115 | 72 if self.facing != 'left': |
73 self.facing = 'left' | |
74 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
|
75 self.deltav((-100.0, 0.0)) |
90 | 76 |
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
|
77 def action_right(self): |
115 | 78 if self.facing != 'right': |
79 self.facing = 'right' | |
80 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
|
81 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
|
82 |
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
|
83 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
|
84 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
|
85 |
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
|
86 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
|
87 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
|
88 |
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 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
|
90 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
|
91 |
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 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
|
93 print "F2" |
90 | 94 |
95 def _get_action(self): | |
96 if self.running: | |
97 return 'running' | |
98 if self.jumping: | |
99 return 'jumpin' | |
100 return 'standing' | |
101 | |
102 def _make_key(self, action=None): | |
103 if action is None: | |
104 action = self._get_action() | |
105 tails = self.tails | |
106 if self.tails >= 4: | |
107 tails = 4 | |
108 elif self.tails >= 2: | |
109 tails = 2 | |
110 return '%s %s %d' % (action, self.facing, tails) | |
111 | |
112 def _load_images(self): | |
113 for action in ['standing', 'running', 'jumping']: | |
114 for tails in [0, 1, 2, 4]: | |
115 self.tails = tails | |
116 directory = os.path.join('sprites', | |
117 'kitsune_%s' % action, | |
118 'kitsune_%s_%dtail' % (action, tails)) | |
119 for facing in ['left', 'right']: | |
120 self.facing = facing | |
121 key = self._make_key(action) | |
122 self._image_dict[key] = [] | |
123 for image_file in get_files(directory): | |
124 if image_file.startswith('.'): | |
125 # Skip extra junk for now | |
126 continue | |
127 image = load_image(os.path.join(directory, image_file)) | |
128 if facing == 'right': | |
129 image = pygame.transform.flip(image, True, False) | |
130 self._image_dict[key].append(image) |