Mercurial > mamba
changeset 392:a48c7b4cf65a
Die if you leave the arena
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sat, 17 Sep 2011 01:53:44 +0200 |
parents | 2aff7e73cdf4 |
children | e274ccc758a6 |
files | mamba/snake.py |
diffstat | 1 files changed, 9 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/mamba/snake.py Sat Sep 17 01:52:54 2011 +0200 +++ b/mamba/snake.py Sat Sep 17 01:53:44 2011 +0200 @@ -2,6 +2,7 @@ import random +import pygame from pygame.sprite import Group, spritecollide from mamba.constants import TILE_SIZE, UP, DOWN, LEFT, RIGHT @@ -92,6 +93,7 @@ self.shiftup_segments(1, tile_state) self.check_self_crash() + self.check_on_screen(world) for segment in self.segments: segment.shift_pixels(ds) world.interact(segment) @@ -124,6 +126,13 @@ if [s for s in collides if s not in self.segments[:2]]: self.crash('You hit yourself!') + def check_on_screen(self, world): + if self.coiled: + return + world_rect = pygame.Rect((0, 0), world.level.get_size()) + if not world_rect.contains(self.head.rect): + self.crash('You left the arena!') + def crash(self, reason='You hit something'): play_sound('crash') SnakeDiedEvent.post(reason)