Skip to content

Instantly share code, notes, and snippets.

@Renddslow
Last active December 13, 2019 19:55
Show Gist options
  • Save Renddslow/0a1dafe0643b293a50a1fe1bad9bcb11 to your computer and use it in GitHub Desktop.
Save Renddslow/0a1dafe0643b293a50a1fe1bad9bcb11 to your computer and use it in GitHub Desktop.
Dungeon Crawler Week 2 (Starter)
from time import sleep
import sys
from lib.clear import clear
from utils.render_hud import render_hud
from utils.cmds import run_cmd
from utils.message import message
from utils.square_exists import square_exists
from utils.dice import d
from data.monsters import get_random_monster
from actions.monster_attack import monster_attack
from actions.player_attack import player_attack
from game_start import game_start
args = sys.argv[1:]
MAX_HP = 24
DIRECTIONS = [
"up",
"down",
"left",
"right"
]
SPEED_UP_TEXT = "--fast" in args
def main():
state = game_start(MAX_HP, SPEED_UP_TEXT)
turns = 0
def render_and_message(msg):
clear()
render_hud(state, MAX_HP)
print(message(msg))
while True:
'''
Anatomy of a Turn (Loop)
1. Clear the screen
2. Re-render the heads up display (HUD)
3. Check to see if we're in combat, if we are, complete the combat
4. If not in combat, let the user know what directions are available
and ask them what they want to do.
5. Check the new "square" to see if there's loot or a monster there
6. If there's loot, pick it up. If it's a monster, enter combat.
'''
clear()
render_hud(state, MAX_HP)
if state["combat"]:
monster = get_random_monster()
# TODO: Combat
# Combat loop
while True:
cmd = input("Do you attack? (Y/n) ")
print(message("Which direction do you go?"))
print(state["coords"])
cmd = run_cmd(input("> "))
if cmd.lower() not in DIRECTIONS:
clear()
render_hud(state, MAX_HP)
print(message("Hmm... that doesn't sound like any direction I've heard of before"))
print("> ")
sleep(1)
continue
if cmd.lower() == "up":
(x, y) = state["coords"]
next_square = (x, y - 1)
elif cmd.lower() == "down":
(x, y) = state["coords"]
next_square = (x, y + 1)
elif cmd.lower() == "left":
(x, y) = state["coords"]
next_square = (x - 1, y)
elif cmd.lower() == "right":
(x, y) = state["coords"]
next_square = (x + 1, y)
if not square_exists(state, next_square):
render_and_message("Where you're trying to go is no place at all.")
print("> ")
sleep(1)
continue
(x, y) = next_square
if state["dungeon"][y][x] == 3:
# TODO: make the victory more enjoyable
print(message("You did it! you made it out alive!"))
break
if state["dungeon"][y][x] == 1:
render_and_message("Looks like you hit a wall there bud. Maybe try a different direction.")
print("> ")
sleep(1)
continue
state["coords"] = next_square
state["visited"].append(state["coords"])
# TODO: check square
if d(1, 20) > 10:
state["combat"] = True
render_and_message("There's something in the dark!")
else:
state["combat"] = False
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
clear()
print("Goodbye!")
# put this in the utils/ folder
def square_exists(state, coords):
(x, y) = coords
dungeon = state["dungeon"]
if x < 0 or y < 0:
return False
try:
dungeon[y][x]
return True
except IndexError:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment