annotate pytouhou/game/player.py @ 167:e483b5a7e84b

Fix default death animation.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 18 Oct 2011 13:37:55 -0700
parents 7769ce7be03c
children ea2ad94c33a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
16 from pytouhou.game.sprite import Sprite
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
17 from pytouhou.vm.anmrunner import ANMRunner
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
18
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
19
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
20 SQ2 = 2. ** 0.5 / 2.
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
21
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
22
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
23 class PlayerState(object):
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
24 def __init__(self, character=0, score=0, power=0, lives=0, bombs=0):
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
25 self.character = character # ReimuA/ReimuB/MarisaA/MarisaB/...
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
26
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
27 self.score = score
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
28 self.lives = lives
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
29 self.bombs = bombs
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
30 self.power = power
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
31
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
32 self.graze = 0
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
33 self.points = 0
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
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
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
37
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
38
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
39 class Player(object):
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
40 def __init__(self, state, character):
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
41 self._sprite = None
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
42 self._anmrunner = None
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
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
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
46 self.state = state
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
47 self.character = character
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
48 self.anm_wrapper = character.anm_wrapper
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
49 self.direction = None
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
50
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
51 self.set_anim(0)
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
52
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
53
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
54 @property
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
55 def x(self):
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
56 return self.state.x
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
57
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
58
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
59 @property
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
60 def y(self):
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
61 return self.state.y
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
62
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
63
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
64 def set_anim(self, index):
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
65 self._sprite = Sprite()
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
66 self._anmrunner = ANMRunner(self.anm_wrapper, index, self._sprite)
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
67 self._anmrunner.run_frame()
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
68
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
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
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
84 def update(self, keystate):
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
85 try:
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
86 dx, dy = {16: (0.0, -1.0), 32: (0.0, 1.0), 64: (-1.0, 0.0), 128: (1.0, 0.0),
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
87 16|64: (-SQ2, -SQ2), 16|128: (SQ2, -SQ2),
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
88 32|64: (-SQ2, SQ2), 32|128: (SQ2, SQ2)}[keystate & (16|32|64|128)]
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
89 except KeyError:
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
90 speed = 0.0
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
91 dx, dy = 0.0, 0.0
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
92 else:
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
93 speed = self.character.focused_speed if keystate & 4 else self.character.speed
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
94 dx, dy = dx * speed, dy * speed
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
95
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
96 if dx < 0 and self.direction != -1:
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
97 self.set_anim(1)
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
98 self.direction = -1
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
99 elif dx > 0 and self.direction != +1:
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
100 self.set_anim(3)
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
101 self.direction = +1
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
102 elif dx == 0 and self.direction is not None:
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
103 self.set_anim({-1: 2, +1: 4}[self.direction])
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
104 self.direction = None
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
105
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
106 self.state.x += dx
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
107 self.state.y += dy
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
108
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
109 self._anmrunner.run_frame()
11ab06f4c4c6 Introduce characters!
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
110