Mercurial > touhou
comparison pytouhou/game/player.py @ 130:11ab06f4c4c6
Introduce characters!
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sat, 10 Sep 2011 22:48:56 +0200 |
parents | 694f25881d0f |
children | c7f0fd9d2145 |
comparison
equal
deleted
inserted
replaced
129:284ac8f97a96 | 130:11ab06f4c4c6 |
---|---|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 ## GNU General Public License for more details. | 12 ## GNU General Public License for more details. |
13 ## | 13 ## |
14 | 14 |
15 | 15 |
16 class Player(object): | 16 from math import cos, sin, atan2, pi |
17 def __init__(self): | 17 |
18 from pytouhou.game.sprite import Sprite | |
19 from pytouhou.vm.anmrunner import ANMRunner | |
20 | |
21 | |
22 SQ2 = 2. ** 0.5 / 2. | |
23 | |
24 | |
25 class PlayerState(object): | |
26 def __init__(self, character=0, score=0, power=0, lives=0, bombs=0): | |
27 self.character = character # ReimuA/ReimuB/MarisaA/MarisaB/... | |
28 | |
29 self.score = score | |
30 self.lives = lives | |
31 self.bombs = bombs | |
32 self.power = power | |
33 | |
34 self.graze = 0 | |
35 self.points = 0 | |
36 | |
18 self.x = 192.0 | 37 self.x = 192.0 |
19 self.y = 384.0 | 38 self.y = 384.0 |
20 self.score = 0 | 39 |
21 self.graze = 0 | 40 |
22 self.power = 0 | 41 class Player(object): |
23 self.lives = 0 | 42 def __init__(self, state, character): |
24 self.bombs = 0 | 43 self._sprite = None |
25 self.character = 0 # ReimuA/ReimuB/MarisaA/MarisaB/... | 44 self._anmrunner = None |
45 | |
46 self.state = state | |
47 self.character = character | |
48 self.anm_wrapper = character.anm_wrapper | |
49 self.direction = None | |
50 | |
51 self.set_anim(0) | |
52 | |
53 | |
54 @property | |
55 def x(self): | |
56 return self.state.x | |
57 | |
58 | |
59 @property | |
60 def y(self): | |
61 return self.state.y | |
62 | |
63 | |
64 def set_anim(self, index): | |
65 self._sprite = Sprite() | |
66 self._anmrunner = ANMRunner(self.anm_wrapper, index, self._sprite) | |
67 self._anmrunner.run_frame() | |
68 | |
69 | |
70 def update(self, keystate): | |
71 try: | |
72 dx, dy = {16: (0.0, -1.0), 32: (0.0, 1.0), 64: (-1.0, 0.0), 128: (1.0, 0.0), | |
73 16|64: (-SQ2, -SQ2), 16|128: (SQ2, -SQ2), | |
74 32|64: (-SQ2, SQ2), 32|128: (SQ2, SQ2)}[keystate & (16|32|64|128)] | |
75 except KeyError: | |
76 speed = 0.0 | |
77 dx, dy = 0.0, 0.0 | |
78 else: | |
79 speed = self.character.focused_speed if keystate & 4 else self.character.speed | |
80 dx, dy = dx * speed, dy * speed | |
81 | |
82 if dx < 0 and self.direction != -1: | |
83 self.set_anim(1) | |
84 self.direction = -1 | |
85 elif dx > 0 and self.direction != +1: | |
86 self.set_anim(3) | |
87 self.direction = +1 | |
88 elif dx == 0 and self.direction is not None: | |
89 self.set_anim({-1: 2, +1: 4}[self.direction]) | |
90 self.direction = None | |
91 | |
92 self.state.x += dx | |
93 self.state.y += dy | |
94 | |
95 self._anmrunner.run_frame() | |
96 |