Mercurial > touhou
annotate pytouhou/game/player.py @ 175:5dc243bcf312
Don’t collect the items you drop while dying…
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 21 Oct 2011 10:45:07 -0700 |
parents | 35d850502d1f |
children | 80a4c7ed43b3 |
rev | line source |
---|---|
52
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
1 # -*- encoding: utf-8 -*- |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
2 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
4 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
5 ## This program is free software; you can redistribute it and/or modify |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
7 ## by the Free Software Foundation; version 3 only. |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
8 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
12 ## GNU General Public License for more details. |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
13 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
14 |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
50
diff
changeset
|
15 |
130 | 16 from pytouhou.game.sprite import Sprite |
17 from pytouhou.vm.anmrunner import ANMRunner | |
18 | |
19 | |
20 SQ2 = 2. ** 0.5 / 2. | |
21 | |
22 | |
23 class PlayerState(object): | |
24 def __init__(self, character=0, score=0, power=0, lives=0, bombs=0): | |
25 self.character = character # ReimuA/ReimuB/MarisaA/MarisaB/... | |
26 | |
27 self.score = score | |
28 self.lives = lives | |
29 self.bombs = bombs | |
30 self.power = power | |
31 | |
32 self.graze = 0 | |
33 self.points = 0 | |
34 | |
50
811cefefb5c8
Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
35 self.x = 192.0 |
811cefefb5c8
Fix a few bugs and add support for a few instructions
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
36 self.y = 384.0 |
130 | 37 |
172
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
38 self.invulnerable_time = 60 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
39 self.touchable = True |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
40 |
130 | 41 |
42 class Player(object): | |
172
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
43 def __init__(self, state, character, game): |
130 | 44 self._sprite = None |
45 self._anmrunner = None | |
172
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
46 self._game = game |
130 | 47 |
142
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
130
diff
changeset
|
48 self.hitbox_half_size = character.hitbox_size / 2. |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
130
diff
changeset
|
49 |
130 | 50 self.state = state |
51 self.character = character | |
52 self.anm_wrapper = character.anm_wrapper | |
53 self.direction = None | |
54 | |
55 self.set_anim(0) | |
56 | |
172
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
57 self.death_time = 0 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
58 |
130 | 59 |
60 @property | |
61 def x(self): | |
62 return self.state.x | |
63 | |
64 | |
65 @property | |
66 def y(self): | |
67 return self.state.y | |
68 | |
69 | |
70 def set_anim(self, index): | |
71 self._sprite = Sprite() | |
72 self._anmrunner = ANMRunner(self.anm_wrapper, index, self._sprite) | |
73 self._anmrunner.run_frame() | |
74 | |
75 | |
156
ebfd328e700c
Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents:
152
diff
changeset
|
76 def collide(self): |
172
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
77 if not self.state.invulnerable_time and not self.death_time and self.state.touchable: # Border Between Life and Death |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
78 self.death_time = self._game.frame |
173
35d850502d1f
Move effects where they should be.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
172
diff
changeset
|
79 self._game.new_effect((self.state.x, self.state.y), 2) |
152
86807b8a63bd
Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
142
diff
changeset
|
80 |
86807b8a63bd
Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
142
diff
changeset
|
81 |
156
ebfd328e700c
Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents:
152
diff
changeset
|
82 def collect(self, item): |
ebfd328e700c
Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents:
152
diff
changeset
|
83 #TODO |
ebfd328e700c
Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents:
152
diff
changeset
|
84 self.state.score += item._item_type.score |
ebfd328e700c
Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents:
152
diff
changeset
|
85 item._removed = True |
ebfd328e700c
Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents:
152
diff
changeset
|
86 |
ebfd328e700c
Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents:
152
diff
changeset
|
87 |
130 | 88 def update(self, keystate): |
172
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
89 if self.death_time == 0 or self._game.frame - self.death_time > 60: |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
90 try: |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
91 dx, dy = {16: (0.0, -1.0), 32: (0.0, 1.0), 64: (-1.0, 0.0), 128: (1.0, 0.0), |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
92 16|64: (-SQ2, -SQ2), 16|128: (SQ2, -SQ2), |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
93 32|64: (-SQ2, SQ2), 32|128: (SQ2, SQ2)}[keystate & (16|32|64|128)] |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
94 except KeyError: |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
95 speed = 0.0 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
96 dx, dy = 0.0, 0.0 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
97 else: |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
98 speed = self.character.focused_speed if keystate & 4 else self.character.speed |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
99 dx, dy = dx * speed, dy * speed |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
100 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
101 if dx < 0 and self.direction != -1: |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
102 self.set_anim(1) |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
103 self.direction = -1 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
104 elif dx > 0 and self.direction != +1: |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
105 self.set_anim(3) |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
106 self.direction = +1 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
107 elif dx == 0 and self.direction is not None: |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
108 self.set_anim({-1: 2, +1: 4}[self.direction]) |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
109 self.direction = None |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
110 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
111 self.state.x += dx |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
112 self.state.y += dy |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
113 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
114 if self.state.invulnerable_time > 0: |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
115 self.state.invulnerable_time -= 1 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
116 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
117 m = self.state.invulnerable_time % 8 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
118 if m == 0: |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
119 self._sprite.color = (255, 255, 255) |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
120 self._sprite._changed = True |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
121 elif m == 2: |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
122 self._sprite.color = (64, 64, 64) |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
123 self._sprite._changed = True |
130 | 124 |
172
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
125 if self.death_time: |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
126 time = self._game.frame - self.death_time |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
127 if time == 6: # too late, you are dead :( |
175
5dc243bcf312
Don’t collect the items you drop while dying…
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
173
diff
changeset
|
128 self.state.touchable = False |
172
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
129 self._game.drop_bonus(self.state.x, self.state.y, 2, end_pos=None) #TODO: find the formula |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
130 for i in range(5): |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
131 self._game.drop_bonus(self.state.x, self.state.y, 0, end_pos=None) #TODO: find the formula |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
132 self.state.lives -= 1 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
133 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
134 elif time == 7: |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
135 self._sprite.mirrored = False |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
136 self._sprite.fade(24, 128, lambda x: x) |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
137 self._sprite.blendfunc = 1 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
138 self._sprite.scale_in(24, 0., 2., lambda x: x) |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
139 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
140 elif time == 31: |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
141 self.state.x = 192.0 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
142 self.state.y = 384.0 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
143 self.direction = None |
130 | 144 |
172
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
145 self._sprite = Sprite() |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
146 self._anmrunner = ANMRunner(self.anm_wrapper, 0, self._sprite) |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
147 self._sprite.alpha = 128 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
148 self._sprite.rescale = 0., 2. |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
149 self._sprite.fade(30, 255, lambda x: x) |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
150 self._sprite.blendfunc = 1 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
151 self._sprite.scale_in(30, 1., 1., lambda x: x) |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
152 self._anmrunner.run_frame() |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
153 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
154 elif time == 60: # respawned |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
155 self.state.touchable = True |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
156 self.state.invulnerable_time = 240 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
157 self._sprite.blendfunc = 0 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
158 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
159 if time > 30: |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
160 for bullet in self._game.bullets: |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
161 bullet.cancel() |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
162 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
163 if time > 90: # start the bullet hell again |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
164 self.death_time = 0 |
ea2ad94c33a0
Implement player death.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
165 |
130 | 166 |
167 self._anmrunner.run_frame() | |
168 |