Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Waiting in a text adventure
#1
I just started learning Python this week and after doing a few simple scripts, I've started work on a simple text adventure, I'm trying to create different outcomes if the player does not enter the dungeon a certain amount of times, but I can't find a way to do this as setting the value of wait to 0 inside of game() makes it run multiple times (not letting the value change) and putting it outside doesn't give it a value at all. Any other generally better ways to code this would be helpful too, thanks!

import time
import random

def start():

	print("You wake up in a hole")
	time.sleep(1)
	print("You grab a stick nearby and stand up")
	time.sleep(2)
	print("You are at the end of a long hallway made of some kind of stone bricks")
	time.sleep(3)
	print("All of the bricks are covered in moss and dust")
	time.sleep(2)
	print("It's to hard to see very far into the hallway")
	time.sleep(2)
	
	
def game():
	
	enter = str(input("Walk into the cave? [y/n]:\n"))
	if enter.lower() == ["yes","y"]:
		print("You walk into a room with a small, dusty chest infront of you")
		
	else:
		wait = wait + 1
		if(wait<=5):
			print("You sit on the cold stone floor")
			time.sleep(1)
			game()
		elif(wait==6):
			print("Your stomach rumbles as you start to get hungry")
			time.sleep(2)
			game()
		elif(wait<=15):
			print("Your stomach rumbles as become even more hungry")
			time.sleep(2)
			game()
		
start()
game()
Reply
#2
Well, there's two solutions. You could make wait be a parameter of the game function, with a default of one. Then each time you call game, you pass the current value of wait. However, I don't really think recursively calling game like you are doing is a good idea.

The other thing you could do is put it into a loop. Then you wouldn't have to keep calling game over and over again. You could just go through the loop and retain the value of wait.

I don't think that is a great solution either. I would recommend looking at my text adventure tutorial. It explains a better way to handle this sort of game than the if/else structure you seem to be working with.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Waiting for input from serial port, then move on KenHorse 3 1,000 Apr-17-2024, 07:21 AM
Last Post: DeaD_EyE
  pip stops waiting for python walker 6 1,047 Nov-28-2023, 06:55 PM
Last Post: walker
  Waiting for heavy functions question philipbergwerf 14 3,368 Apr-29-2022, 07:31 PM
Last Post: philipbergwerf
  How to create waiting process? samuelbachorik 4 1,971 Sep-02-2021, 05:41 PM
Last Post: bowlofred
  loop adventure game ilikedofs 1 1,699 May-26-2021, 12:43 AM
Last Post: bowlofred
  how to make a hotkey for text adventure game myn2018 2 1,966 Jan-06-2021, 10:39 PM
Last Post: myn2018
  Waiting and listening test 2 2,136 Nov-13-2020, 04:43 PM
Last Post: michael1789
  waiting for barcode scanner output, while main program continues to run lightframe109 3 4,641 Sep-03-2020, 02:19 PM
Last Post: DeaD_EyE
  waiting to connect Skaperen 9 3,532 Aug-17-2020, 05:58 AM
Last Post: Skaperen
  Choose your own adventure game noahc2004 2 2,571 Jun-26-2020, 02:06 PM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020