Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Input in loops

    Help
    5
    9
    303
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • E
      Elderrick last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • B
        Bl4ckM4ch1n3 last edited by Bl4ckM4ch1n3

        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
        
        1 Reply Last reply Reply Quote 0
        • E
          Elderrick last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • lawyerlounge
            lawyerlounge last edited by

            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?

            1 Reply Last reply Reply Quote 0
            • lawyerlounge
              lawyerlounge last edited by

              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?

              pianofire 1 Reply Last reply Reply Quote 0
              • E
                Elderrick last edited by

                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?

                Mincus 1 Reply Last reply Reply Quote 0
                • lawyerlounge
                  lawyerlounge last edited by lawyerlounge

                  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!

                  1 Reply Last reply Reply Quote 0
                  • Mincus
                    Mincus F @Elderrick last edited by

                    @Elderrick Perhaps a 'press a to continue' before the input()
                    Something like

                    Print("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?")
                    
                    1 Reply Last reply Reply Quote 0
                    • pianofire
                      pianofire Fuze Team @lawyerlounge last edited by

                      @lawyerlounge You can do this using https://fuzearena.com/help/view/showKeyboard and https://fuzearena.com/help/view/getKeyboardBuffer

                      1 Reply Last reply Reply Quote 1
                      • First post
                        Last post