Question: I recently started working on a Text based RPG game, and have almost finished. But while playtesting I came across an error where it stops around halfway through without an exit() function.
Repl link: https://replit.com/@LeviRobertson1/TxtbasedRPGtests#main.py
import random
import time
import replit
from colorama import Back, Fore, Style, init
init()
def txt(sentence):
for char in sentence:
print(char, end='', flush=True)
time.sleep(0.05)
print()
def txt1(sentence):
for char in sentence:
print(char, end='', flush=True)
time.sleep(0.5)
print()
def askgrab(item):
txt(f'Do you want to grab the {item}?')
qna = str(input(Fore.BLUE+'y/n\n'))
print(Style.RESET_ALL)
if qna == 'y':
inventory.append(currentitems[0])
txt(f'You have grabbed the {item}')
else:
txt('You decide to not take the item. There was no other opportunity to get out...')
txt('You rot to death in your cell...')
txt('The End')
txt(Fore.RED+'[Ending 1 of 3: The Coward]')
exit()
def unlock(locknum):
txt('You try to pick the lock')
txt('Guess the number from 1 to 100 to unlock')
guess = int(input('> '))
if guess == locknum:
txt('You unlocked the door')
elif guess > locknum:
txt('Too high, try again!')
replit.clear()
unlock(locknum)
elif guess < locknum:
txt('Too low, try again!')
replit.clear()
unlock(locknum)
def attack():
txt('Do you want to attack?')
atkcheck = str(input(Fore.BLUE+'y/n\n'))
print(Style.RESET_ALL)
if atkcheck == 'y':
txt('You attack the guard')
else:
txt('You decide to not attack the guard')
txt('[!]The guard wakes up! You could not sneak past...')
txt('[!]The guard sees you!')
txt('You hit the guard')
txt('The guard slumps over')
def askdirection(direction):
qna = str(input(Fore.BLUE+'y/n\n'))
print(Style.RESET_ALL)
if qna == 'y':
txt('You decide to go north')
direction = 'north'
elif qna == 'n':
direction = 'south'
allrooms = ['cell','hallway','armory1','armory2','secret']
knownrooms = ['cell'
,'hallway'
,'armory1']
currentroom = 'cell'
currentitems = ['hairpen',]
inventory = []
hp = 100
txt('You wake up in a damp room...')
txt('You look around...')
txt1('...')
txt('Suddenly you remember, you were taken here when you were framed for a murder')
txt('You have to escape!')
txt(f'You are in the {currentroom}')
txt(f'You look around and see a {currentitems[0]}')
askgrab(currentitems[0])
txt('You go over to the door.')
txt('To unlock the door you will have to pick it with the hairpin')
locknum = random.randint(1,100)
unlock(locknum)
txt('You walk out and peek around the new room')
txt('[NEW ROOM UNLOCKED: Hallway]')
currentroom = allrooms[1]
time.sleep(6/5)
txt('You hear snoring and look to the sound')
txt('[!]There is a sleeping guard to your left')
attack()
txt('Do you want to go north?')
txt('Yes for north, no for south.')
time.sleep(2)
replit.clear()
direction = ''
askdirection(direction)
if direction == 'north':
txt('You end up in a barracks')
txt("[!]There's a guard watching the room")
txt('Do you wish to attack?')
qna = str(input(Fore.BLUE+'y/n\n'))
while True:
if qna == 'y':
if inventory[1] == 'sword' and inventory[2] == 'shield':
txt(Style.RESET_ALL+'You run at the guard')
txt('The guard swings, but you block with your shield')
txt('You swing at the guard')
txt('The guard blocks you and pulls out a dagger.')
txt('You slice the guard just as he stabs you in the hip')
hp -= 40
txt('[!]You are bleeding, you have lost 40 hp...')
txt('The guard passed out')
txt1('...')
break
else:
txt(Style.RESET_ALL+"You don't have a weapon...")
txt('The guard slits your throat...')
txt('The End...')
txt(Fore.RED+'[Ending 2 of 3: The Unprepared]')
exit()
elif qna == 'n':
txt(Style.RESET_ALL+'You sneak around avoiding the guard')
txt('You grab a sword and shield.')
inventory.append('sword')
inventory.append('shield')
txt('You survived and leave the armory')
txt('You are walking out when something catches your eye...')
txt('A map!')
txt('Do you want to grab it?')
qna = str(input(Fore.BLUE+'y/n\n'))
if qna == 'y':
txt(Style.RESET_ALL+'You grab the map...')
txt('The map is titled "Vents"')
inventory.append('map')
txt('You look over the map and see this:')
print(Back.LIGHTYELLOW_EX)
# unicode: β β β β¦ β β β¬ β£ β β© β β
txt('βA B C D E F')
txt('1ββββββ¦ββ¦βββ')
txt('2βββ¦~~β ββ£? β')
txt('3β βββ#β¬ββ¬β β')
txt('4β~~~~~~~~~β')
txt('5βββββ©ββββββ')
txt(Style.RESET_ALL+'Some of the map is blurred out (~)')
txt('The vent areas are marked by the paths')
txt('The # is the cell, and the ? looks something like a throne...')
else:
txt('You decide against taking the map...')
elif direction == 'south':
# Not done yet
txt('[!]Uncompleted')
txt1('...')
exit()