Input in loops
-
Hi everyone, I'm new at programming and I wanted to start a little project which is a text adventure with a very simple combat system, and also a basic UI. The problem is that every time I ask for input (enter your name) in the loop it keeps repeating forever and I have no clue how to make it work. Any advice would be very welcome.
-
Could you provide us your sourcecode? It's much easier that way :D
But I guess that you're asking for the name inside your main loop. If you wan't to ask the player only one time you'll need to ask for the input before you're entering the main game loop.
playerName = input("Would you tell me your name, foreign adventurer?") loop clear() update() repeat
-
Thanks a lot for your answer! Actually the problem comes after, when for example, you have to choose right or left, if you choose right then a monster appears and the battle starts, Can you do that outside of the main loop? Because I'm using a basic UI (a couple of boxes accross the screen, and as far as I know, those have to be inside the main loop). I'll try to post the code.
-
yes you will definitely need to be able to call for inputs throughout the main loop. wouldn't you just use a series of if statements?
have a flag for which "area" you are in and switch that in the if statement maybe?
area = forest //have your inputs here I suppose if area == house and answer == left then "you move left!" area = forestLeft // or wherever endif if area == house and answer == right then "you move right!" area = lake endif
something like that could work, yet I'm sure there are way more efficient and better ways to make a foundation that can be built upon than using this method.
What do you think?
-
Also I know you can use the input() function and that creates a box on screen, but how do you have it just appear on screen like the old console entry days and the keyboard is always up and accepting input?
-
This is what I've got so far:
loop
clear(lightgrey)
ink(black)
box(gwidth()/1.3, 0, 10, gheight(),navy,false) // This is just to separate the stats to the right. My basic UI :)print ("In the middle of the forest you see 2 ways ahead of you") answer = input("Which path do you choose? Right or left?") // Here I have another problem: the screen with the keyboard appears and I can´t read the previous part. Any ideas? if answer == Right then print("You see a dwarf, he attacks you!") // Here the battle starts, but I have to figure out the code for this :) if answer == Left then print ("You chose wisely, continue your adventure") else: print("That's not a valid answer") endif
The problem here is that no matter what my answer is , I keep getting the screen for the input.
Any ideas?
-
in your example as soon as you type in an answer it prints the message super fast and then just loops back to the top of your code and asks for entry again. if you sleep(3) you would at least see the message before it would go back through the loop and clear the message. You will also need a flag to change for the program to continue in a new direction.
loop clear(lightgrey) ink(black) box(gwidth()/1.3, 0, 10, gheight(),navy,false) area = start print ("In the middle of the forest you see 2 ways ahead of you") answer = input("Which path do you choose? Right or left?") if answer == Right then print("You see a dwarf, he attacks you!") sleep(4) // if you sleep, text will at least be able to be seen before being cleared. battleFunction() endif if answer == Left then print ("You chose wisely, continue your adventure") else: print("That's not a valid answer") endif update() repeat function battleFunction() /* you can enter code here for your battle section and when you call "battleFunction()" in the code, the program will jump here and continue back to the loop when completed */ return void
All of my suggestions are pretty basic though since I am still a novice and learning the ways of coding, and I'm sure others can toss in more proper advice!
-
@Elderrick Perhaps a 'press a to continue' before the input()
Something likePrint("Press A to enter your answer.") p1 = Controls(0) While p1.a == 0 Loop // Keep looping until the player presses 'A' button on controller. p1 = Controls(0) Repeat Input("Which path do you choose?")
-
@lawyerlounge You can do this using https://fuzearena.com/help/view/showKeyboard and https://fuzearena.com/help/view/getKeyboardBuffer