Skip to content

Instantly share code, notes, and snippets.

@jrheard jrheard/goblin.diff Secret
Created Apr 25, 2019

Embed
What would you like to do?
diff --git a/roguelike/level.txt b/roguelike/level.txt
index f0274e6..7e0a368 100644
--- a/roguelike/level.txt
+++ b/roguelike/level.txt
@@ -5,8 +5,8 @@
# #
# ###############
# #
-# #
-# #
+# g #
+# g #
# #
# #
# #
diff --git a/roguelike/roguelib.py b/roguelike/roguelib.py
index 4f225c7..25ee640 100644
--- a/roguelike/roguelib.py
+++ b/roguelike/roguelib.py
@@ -11,12 +11,13 @@ class Space(Enum):
WALL = auto()
-def move(direction, level, current_player_x, current_player_y):
+def move(direction, level, monsters, current_player_x, current_player_y):
"""Calculates the player's new position, based on their current position and the direction they'd like to move in.
Arguments:
direction - one of ['up', 'right', 'down', 'left']
level - a 2D list of Spaces
+ monsters - a list of monster dictionaries
current_player_x - a number between 0 and ROOM_WIDTH
current_player_y - a number between 0 and ROOM_HEIGHT
@@ -48,6 +49,11 @@ def move(direction, level, current_player_x, current_player_y):
# There's a wall here, the player can't move into this space.
return current_player_x, current_player_y
+ for monster in monsters:
+ if (new_x, new_y) == (monster['x'], monster['y']):
+ # There's a monster here, the player can't move into this space.
+ return current_player_x, current_player_y
+
# If we've made it this far, the space that the player is trying to move into is clear!
return new_x, new_y
@@ -73,10 +79,23 @@ def draw_game(game):
line = ''
for x in range(len(game['level'])):
+ # See if there's a monster on this space.
+ monster_on_this_space = None
+ for monster in game['monsters']:
+ if (x, y) == (monster['x'], monster['y']):
+ monster_on_this_space = monster
+
if (x, y) == (game['player']['x'], game['player']['y']):
# The player's in this space!
line += '@'
+ elif monster_on_this_space != None:
+ # There's a monster on this space!
+ if monster['type'] == 'goblin':
+ line += 'g'
+ else:
+ raise Exception('Found monster with unknown type {}: {}'.format(monster['type'], monster))
+
elif game['level'][x][y] == Space.WALL:
# It's a wall!
line += '#'
@@ -131,6 +150,8 @@ def load_level(filename):
player = None
+ monsters = []
+
# Find `player` and update `level` with walls.
for (y, line) in enumerate(lines):
for (x, char) in enumerate(line[:-1]):
@@ -160,6 +181,16 @@ 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 == 'g':
+ # We found a goblin!
+ monsters.append({
+ 'type': 'goblin',
+ 'health': 10,
+ 'attack': 2,
+ 'x': x,
+ 'y': y,
+ })
+
else:
raise Exception("{} contains character {}, but the game doesn't yet know what that character means!".format(filename, char))
@@ -168,6 +199,7 @@ def load_level(filename):
# Hi there! This is what a game dictionary looks like!
return {
'level': level,
+ 'monsters': monsters,
'player': player,
}
@@ -211,6 +243,6 @@ def process_user_input(game, command):
if move_direction:
# The player wants to move their character!
- new_player_x, new_player_y = move(move_direction, game['level'], game['player']['x'], game['player']['y'])
+ new_player_x, new_player_y = move(move_direction, game['level'], game['monsters'], game['player']['x'], game['player']['y'])
game['player']['x'] = new_player_x
game['player']['y'] = new_player_y
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.