Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
python3 textrogue2: extend version of textrogue with statues as foes and hitpoints for the player
# legend: #=rock .=floor f=food T=treasure
dungeon = """
###############################################
#.....fff.#..............#f#.T#.....#.......#T#
#..S......#..............###....###.#.....#.#.#
#..SS.....f.....T...f.........##T......f..#...#
###############################################
""" # add more line to the dungeon!
player, msg = "@", "welcome @, move with w,a,s,d"
px, py, dx, dy, php = 1, 1, 0, 0, 10 # position, movement
lines = dungeon.split()
length = len(lines[1])
hunger, treasure, food = 0, 0, 7
prompt = "Type your command or ? and press Enter:"
while hunger < 100 and php >0:
for y, line in enumerate(lines):
# y is the line number starting with 0
if y == py:
print(line[0:px]+player+line[px+1:])
else:
print(line)
s = "hungry:{} food:{} treasure:{} hitpoints:{}\n".format(
hunger, food, treasure, php)
command = input(msg+"\n"+s+prompt)
dx, dy, msg = 0, 0, ""
if command == "help" or command == "?":
msg = "movement: w,a,s,d\n"
msg += "eat: e\nquit: exit or quit or q"
if command in ["quit", "exit", "q"]:
break # exit the game
if command == "e": # eat
if food > 0:
msg = "you eat food"
food -=1
hunger -= 11
else:
msg = "You have no food!"
hunger += 1 # getting more hungry
if command == "a":
dx = -1 # go left
elif command == "d":
dx = 1 # go right
elif command == "w":
dy = -1 # go up
elif command == "s":
dy = 1 # go down
target = lines[py+dy][px+dx]
if target == "#":
dx, dy = 0, 0 # running into wall
msg = "ouch, you hit the wall"
if target in ["f","T","S"]: # food or treasure
lines[py+dy] = lines[py+dy][:px+dx]+ \
"."+lines[py+dy][px+dx+1:]
if target == "f":
msg = "you found food!"
food += 1
if target == "T":
msg = "you found treasure!"
treasure += 1
if target == "S":
msg = "you destroy a statue and loose hitpoints"
php -= 5
px += dx # movement x
py += dy # movement y
print("Game Over")
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.