comparison demos/shooter/scripts/ships/player.py @ 462:c4f745a566d6

Added player ship animations including flash and explode. Removed the old flashing routine. The player now gets moved to the left side of the screen after dying and is invulnerable for a short period of time.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 12 Apr 2010 19:01:41 +0000
parents f87f686b5b59
children 7a79dc2a0592
comparison
equal deleted inserted replaced
461:f87f686b5b59 462:c4f745a566d6
20 # Free Software Foundation, Inc., 20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # #################################################################### 22 # ####################################################################
23 23
24 from fife import fife 24 from fife import fife
25 from scripts.ships.shipbase import Ship 25 from scripts.ships.shipbase import *
26 from scripts.common.helpers import Rect 26 from scripts.common.helpers import Rect
27 from scripts.weapons import * 27 from scripts.weapons import *
28 28
29 29
30 class PlayerActionListener(ShipActionListener):
31 def __init__(self, ship):
32 super(PlayerActionListener, self).__init__(ship)
33
34 def onInstanceActionFinished(self, instance, action):
35 if action.getId() == 'explode':
36 self._ship.respawn()
37 if action.getId() == 'flash':
38 if self._ship._flashnumber > 0:
39 self._ship.instance.act('flash', self._ship.instance.getFacingLocation())
40 self._ship._flashnumber -= 1
41 else:
42 self._ship._flashing = False
43
44
30 class Player(Ship): 45 class Player(Ship):
31 def __init__(self, scene, playerName): 46 def __init__(self, scene, playerName):
32 super(Player, self).__init__(scene, playerName) 47 super(Player, self).__init__(scene, playerName)
33 48
34 self._score = 0 49 self._score = 0
42 57
43 self._lives = 3 58 self._lives = 3
44 self._invulnerable = False 59 self._invulnerable = False
45 60
46 self._isplayer = True 61 self._isplayer = True
47 62 self._dead = False
63
64 self._actionlistener = PlayerActionListener(self)
65
48 def init(self): 66 def init(self):
49 self._lives = 3 67 self._lives = 3
50 68
51 def _getScore(self): 69 def _getScore(self):
52 return self._score 70 return self._score
53 71
72 def respawn(self):
73 if self._lives >= 0:
74 self._dead = False
75 self.setInvulnerable(20)
76
77 campos = self._scene.camera.getLocation().getExactLayerCoordinates()
78 location = self.location
79 playerpos = location.getExactLayerCoordinates()
80
81 playerpos.x = campos.x - 6.5
82 playerpos.y = campos.y
83 location.setExactLayerCoordinates(playerpos)
84 self.location = location
85
86 else:
87 self._instance.get2dGfxVisual().setVisible(False)
88
89 def setInvulnerable(self, seconds):
90 self._invulnerable = True
91 self.flash(seconds)
92
54 def applyScore(self, sc): 93 def applyScore(self, sc):
55 self._score += sc 94 self._score += sc
56 95
57 def destroy(self): 96 def destroy(self):
58 if not self._flashing: 97 if not self._invulnerable and not self._dead:
59 self._lives -= 1 98 self._instance.act('explode', self._instance.getFacingLocation())
60 99 self._dead = True
61 if self._lives < 0: 100 self._invulnerable = True
62 self._lives = -1 101 self._lives -= 1
63
64 def setInvulnerable(self, seconds):
65 self.flash(20, 20*seconds)
66 self._invulnerable = True
67 102
68 def update(self): 103 def update(self):
69 104
70 key = False 105 key = False
71 106
72 #player is no longer invulnerable 107 #player is no longer invulnerable
73 if not self._flashing and self._invulnerable: 108 if not self._flashing and self._invulnerable and not self._dead:
74 self._invulnerable = False 109 self._invulnerable = False
75 110
76 oldpos = self.location 111 oldpos = self.location
77 112
78 if self._scene.keystate['UP']: 113 if not self._dead:
79 self.applyThrust(fife.DoublePoint(0,-1.5)) 114 if self._scene.keystate['UP']:
80 key = True 115 self.applyThrust(fife.DoublePoint(0,-1.5))
81 if self._scene.keystate['DOWN']: 116 key = True
82 self.applyThrust(fife.DoublePoint(0,1.5)) 117 if self._scene.keystate['DOWN']:
83 key = True 118 self.applyThrust(fife.DoublePoint(0,1.5))
84 if self._scene.keystate['LEFT']: 119 key = True
85 self.applyThrust(fife.DoublePoint(-1.5,0)) 120 if self._scene.keystate['LEFT']:
86 key = True 121 self.applyThrust(fife.DoublePoint(-1.5,0))
87 if self._scene.keystate['RIGHT']: 122 key = True
88 self.applyThrust(fife.DoublePoint(1.5,0)) 123 if self._scene.keystate['RIGHT']:
89 key = True 124 self.applyThrust(fife.DoublePoint(1.5,0))
125 key = True
90 126
91 #fire the currently selected gun 127 #fire the currently selected gun
92 if self._scene.keystate['SPACE']: 128 if self._scene.keystate['SPACE']:
93 self.fire(fife.DoublePoint(1,0)) 129 self.fire(fife.DoublePoint(1,0))
94 130
95 if not key and self._velocity.length() > 0: 131 if not key and self._velocity.length() > 0:
96 self.applyBrake(1.5) 132 self.applyBrake(1.5)
97 133
98 super(Player, self).update() 134 super(Player, self).update()
137 return self._lives 173 return self._lives
138 174
139 def _getInvulnerable(self): 175 def _getInvulnerable(self):
140 return self._invulnerable 176 return self._invulnerable
141 177
178 def _setInvulnerable(self, inv):
179 self._invulnerable = inv
180
142 score = property(_getScore) 181 score = property(_getScore)
143 lives = property(_getLives) 182 lives = property(_getLives)
144 invulnerable = property(_getInvulnerable) 183 invulnerable = property(_getInvulnerable, _setInvulnerable)