Skip to content

Instantly share code, notes, and snippets.

@jrheard jrheard/goal_space.diff Secret
Created May 8, 2019

Embed
What would you like to do?
diff --git a/roguelike/game.py b/roguelike/game.py
index ab9501c..fe7e84b 100644
--- a/roguelike/game.py
+++ b/roguelike/game.py
@@ -21,6 +21,14 @@ def run_game():
# Do something else!
roguelib.process_user_input(game, user_input)
+ if (game['player']['x'], game['player']['y']) == game['goal_space']:
+ # The player reached the goal space! They beat the game!
+ roguelib.clear_screen()
+ roguelib.draw_game(game)
+ print("Congratulations, you beat the game!")
+
+ break
+
if __name__ == '__main__':
run_game()
diff --git a/roguelike/level.txt b/roguelike/level.txt
index f0274e6..25f8a19 100644
--- a/roguelike/level.txt
+++ b/roguelike/level.txt
@@ -14,6 +14,6 @@
# #
# #
# #
- # #
+ # ! #
# #
###############
diff --git a/roguelike/roguelib.py b/roguelike/roguelib.py
index 4f225c7..096f1be 100644
--- a/roguelike/roguelib.py
+++ b/roguelike/roguelib.py
@@ -77,6 +77,10 @@ def draw_game(game):
# The player's in this space!
line += '@'
+ elif game['goal_space'] == (x, y):
+ # It's the goal space!
+ line += '!'
+
elif game['level'][x][y] == Space.WALL:
# It's a wall!
line += '#'
@@ -131,6 +135,8 @@ def load_level(filename):
player = None
+ goal_space = None
+
# Find `player` and update `level` with walls.
for (y, line) in enumerate(lines):
for (x, char) in enumerate(line[:-1]):
@@ -160,13 +166,20 @@ def load_level(filename):
assert player is None, "We found an @ sign at {}, {} but we already found a player at {}!".format(x, y, player)
player = make_player(x, y)
+ elif char == '!':
+ assert goal_space is None, "We found an E at {}, {} but we already found a goal space at {}!".format(x, y, goal_space)
+ goal_space = (x, y)
+
else:
raise Exception("{} contains character {}, but the game doesn't yet know what that character means!".format(filename, char))
assert player is not None, "We couldn't find a player! Please mark the player's starting position with an @ character somewhere in {}".format(filename)
+ assert goal_space is not None, "We couldn't find a goal space! Please mark the level's goal space with a ! character somewhere in {}".format(filename)
+
# Hi there! This is what a game dictionary looks like!
return {
+ 'goal_space': goal_space,
'level': level,
'player': player,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.