Changeset 244:5bbf90e6a94b


Ignore:
Timestamp:
04/07/11 16:13:51 (12 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
Phase:
public
Message:

Add minimal support for player death

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • data/game.json

    r237 r244  
    33        "item": null,
    44        "tails" : [],
    5         "health" : 80
     5        "cur_health" : 40,
     6        "max_health" : 40
    67    },
    78    "missions": {
  • skaapsteker/engine.py

    r189 r244  
    9595    def post(cls, next_scene):
    9696        super(ChangeScene, cls).post(next_scene=next_scene)
     97
     98class PlayerDied(UserEvent):
     99
     100    utype = "PLAYER_DIED"
     101
     102    @classmethod
     103    def post(cls):
     104        super(PlayerDied, cls).post()
  • skaapsteker/levelscene.py

    r243 r244  
    22
    33from pygame.locals import (KEYDOWN, KEYUP, K_DOWN, K_ESCAPE, K_LEFT, K_RIGHT,
    4                            K_SEMICOLON, K_UP, K_p, K_q, K_x, K_z)
     4                           K_SEMICOLON, K_UP, K_p, K_q, K_x, K_z, K_RETURN)
    55
    66from . import options
     
    1919        self._player = sprites.player.Player(game_state.world)
    2020        self._level = level.Level(leveldef, self._player)
     21        self._leveldef = leveldef
     22        self._player_dead = False
    2123
    2224        self._level_surface = self._level.get_surface()
     
    3537    def _build_action_map(self):
    3638        action = lambda s: getattr(self._player, 'action_%s' % s)
     39
     40        self._quit_keys = set([K_q, K_ESCAPE])
     41        self._restart_keys = set([K_x, K_z, K_RETURN])
    3742
    3843        self._fast_key_map = {
     
    5661            self._slow_key_map[K_q] = self._quit
    5762
    58     def _quit(self):
     63    def _quit(self, pause=True):
    5964        import menuscene # avoid circular import
    60         engine.ChangeScene.post(menuscene.MenuScene(self.game_state, self))
     65        if pause:
     66            engine.ChangeScene.post(menuscene.MenuScene(self.game_state, self))
     67        else:
     68            # FIXME: When starting the game, we shoudl ensure we have sane
     69            # states
     70            if self._player_dead:
     71                self._player.restore()
     72            engine.ChangeScene.post(menuscene.MenuScene(self.game_state, None))
     73
     74    def _restart(self):
     75        if self._player_dead:
     76            self._player.restore()
     77        engine.ChangeScene.post(LevelScene(self.game_state, self._leveldef))
    6178
    6279    def _toggle_pause(self):
     
    102119            paused_text.draw(self._level_surface)
    103120
     121        if self._player_dead:
     122            death_text_pos = self._clip_rect.centerx - 100, self._clip_rect.centery - 100
     123            death_text = Text("You've died.\nPress Escape to exit or Return to restart the level", death_text_pos)
     124            death_text.draw(self._level_surface)
     125
    104126        screen_surface.blit(self._level_surface, (0, 0), self._clip_rect)
    105127
     
    110132        cr.clamp_ip(lr)
    111133
     134
    112135    def dispatch(self, ev):
    113         if ev.type is KEYDOWN:
     136        if engine.PlayerDied.matches(ev):
     137            self._player_dead = True
     138        elif ev.type is KEYDOWN:
     139            if self._player_dead:
     140                if ev.key in self._restart_keys:
     141                    self._restart()
     142                elif ev.key in self._quit_keys:
     143                    self._quit(False)
     144                return
    114145            if ev.key in self._fast_key_map:
    115146                self._fast_keys_down.add(ev.key)
  • skaapsteker/sprites/player.py

    r212 r244  
    88from skaapsteker.constants import Layers
    99from skaapsteker.data import get_files, load_image
     10from skaapsteker.engine import PlayerDied
    1011
    1112
     
    3132        # We muck with these in load for convience, so ensure they're right
    3233        self.the_world = the_world
    33         self.health = the_world.fox.health
    3434        self.set_facing('left')
    3535        self.set_image()
     
    149149        if hasattr(other, 'collided_player'):
    150150            other.collided_player(self)
    151             print 'Health', self.the_world.fox.health
     151            print 'Health', self.the_world.fox.cur_health
    152152
    153153
    154154    def damage(self, damage):
    155         self.the_world.fox.health -= damage
    156         if self.the_world.fox.health < 0:
    157             print 'You should have dided'
    158 
     155        self.the_world.fox.cur_health -= damage
     156        if self.the_world.fox.cur_health <= 0:
     157            PlayerDied.post()
     158
     159    def restore(self):
     160        """Restore player to max health (for restarting levels, etc.)"""
     161        self.the_world.fox.cur_health = self.the_world.fox.max_health
    159162
    160163    def set_pos(self, pos):
Note: See TracChangeset for help on using the changeset viewer.