Mercurial > touhou
annotate pytouhou/game/player.py @ 164:5271789c067d
Implement player bullets rendering and updating.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 16 Oct 2011 08:01:28 -0700 |
parents | 7769ce7be03c |
children | ea2ad94c33a0 |
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 |
38 | |
39 class Player(object): | |
40 def __init__(self, state, character): | |
41 self._sprite = None | |
42 self._anmrunner = None | |
43 | |
142
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
130
diff
changeset
|
44 self.hitbox_half_size = character.hitbox_size / 2. |
c7f0fd9d2145
Simple collision detection
Thibaut Girka <thib@sitedethib.com>
parents:
130
diff
changeset
|
45 |
130 | 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 | |
156
ebfd328e700c
Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents:
152
diff
changeset
|
70 def collide(self): |
152
86807b8a63bd
Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
142
diff
changeset
|
71 self.state.lives -= 1 |
86807b8a63bd
Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
142
diff
changeset
|
72 self.state.x = 192.0 |
86807b8a63bd
Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
142
diff
changeset
|
73 self.state.y = 384.0 |
86807b8a63bd
Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
142
diff
changeset
|
74 #TODO: animation |
86807b8a63bd
Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
142
diff
changeset
|
75 #TODO: set invulnerability. |
86807b8a63bd
Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
142
diff
changeset
|
76 |
86807b8a63bd
Add collisions with enemies and items.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
142
diff
changeset
|
77 |
156
ebfd328e700c
Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents:
152
diff
changeset
|
78 def collect(self, item): |
ebfd328e700c
Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents:
152
diff
changeset
|
79 #TODO |
ebfd328e700c
Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents:
152
diff
changeset
|
80 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
|
81 item._removed = True |
ebfd328e700c
Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents:
152
diff
changeset
|
82 |
ebfd328e700c
Rename a few functions, move a few things around...
Thibaut Girka <thib@sitedethib.com>
parents:
152
diff
changeset
|
83 |
130 | 84 def update(self, keystate): |
85 try: | |
86 dx, dy = {16: (0.0, -1.0), 32: (0.0, 1.0), 64: (-1.0, 0.0), 128: (1.0, 0.0), | |
87 16|64: (-SQ2, -SQ2), 16|128: (SQ2, -SQ2), | |
88 32|64: (-SQ2, SQ2), 32|128: (SQ2, SQ2)}[keystate & (16|32|64|128)] | |
89 except KeyError: | |
90 speed = 0.0 | |
91 dx, dy = 0.0, 0.0 | |
92 else: | |
93 speed = self.character.focused_speed if keystate & 4 else self.character.speed | |
94 dx, dy = dx * speed, dy * speed | |
95 | |
96 if dx < 0 and self.direction != -1: | |
97 self.set_anim(1) | |
98 self.direction = -1 | |
99 elif dx > 0 and self.direction != +1: | |
100 self.set_anim(3) | |
101 self.direction = +1 | |
102 elif dx == 0 and self.direction is not None: | |
103 self.set_anim({-1: 2, +1: 4}[self.direction]) | |
104 self.direction = None | |
105 | |
106 self.state.x += dx | |
107 self.state.y += dy | |
108 | |
109 self._anmrunner.run_frame() | |
110 |