Skip to content

Commit

Permalink
Raise an exception when the player is blocked
Browse files Browse the repository at this point in the history
When the player tries to move into a wall, it is a failure of movement,
so an appropriate exception is raised. Since we don't do anything
specific in that case, that exception is later caught and ignored.
  • Loading branch information
Radomir Dopieralski committed Mar 20, 2013
1 parent 9e1be7d commit 72e2627
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions python/SimpleRL.py
Expand Up @@ -25,6 +25,10 @@
}


class BlockedMovement(Exception):
pass


class Game(object):
def __init__(self, screen):
self.screen = screen
Expand All @@ -33,8 +37,9 @@ def __init__(self, screen):

def move_player(self, (dx, dy)):
x, y = self.x + dx, self.y + dy
if MAP[y][x] == ' ':
self.x, self.y = x, y
if MAP[y][x] != ' ':
raise BlockedMovement()
self.x, self.y = x, y

def main(self):
for row in MAP:
Expand All @@ -48,9 +53,11 @@ def main(self):
except KeyError:
pass
else:
self.draw_tile(' ')
self.screen.addstr(self.y, self.x, ' ')
self.move_player(direction)
try:
self.move_player(direction)
except BlockedMovement:
pass


if __name__ == '__main__':
Expand Down

0 comments on commit 72e2627

Please sign in to comment.