✅ How to make program wait before executing next print line?
how do I get the program to wait in between each print statement? I know in Python its
import time time.sleep()
but this isn't python lol Thanks.37 Replies
Thread.Sleep Method (System.Threading)
Suspends the current thread for the specified amount of time.
Yes
I'm like no where near a beginner yet lol how would I do that?
Thread.Sleep(2000)
for now.ok bet. tyvm
one more question please. How do I exit the program based off user's input?
'''cs
Console.WriteLine("Press ESC to stop");
do {
while (! Console.KeyAvailable) {
// Do something
}
} while (Console.ReadKey(true).Key != ConsoleKey.Escape); ''' https://stackoverflow.com/questions/5891538/listen-for-key-press-in-net-console-app example for consoles
} while (Console.ReadKey(true).Key != ConsoleKey.Escape); ''' https://stackoverflow.com/questions/5891538/listen-for-key-press-in-net-console-app example for consoles
Stack Overflow
Listen for key press in .NET console app
How can I continue to run my console application until a key press (like Esc is pressed?)
I'm assuming its wrapped around a while loop. I don't like ReadKey as it blocks operation and asks for a k...
I read over the link, but I'm not sure how to apply it to my code?
I'd appreciate some more help please ❤️
Where is the key register supposed to go
Where 'y' is ?
in the else statement. It's just a little check to ask if you want to keep playing or not. If the user enters
y
or Y
, then it recalls the game after 3000ms, otherwise i triggers the else part and closes the game
so the part
Well in this case you want the main game method to run in the while key isn't available loop
I could give you the full code, but it's over 500 lines
'''cs
Console.WriteLine("Press ESC to stop");
do {
while (! Console.KeyAvailable) {
// here you just wait with rest of game
}
} while (Console.ReadKey(true).Key != ConsoleKey.Escape); '''
} while (Console.ReadKey(true).Key != ConsoleKey.Escape); '''
you're using apostrophes instead of back ticks, and that while loop doesn't make any sense to me
Phone atm
Can't seem to find them
touch and hold teh apostrophe button to get the backtick
if you're on apple
Ahh
I believe to run end game in the do while etc
Since you're just waiting for a key press
how can I do that when the
EndGame()
is triggered based off how many questions are left numberOfQuestions
not waiting for a key press. waiting for a yes or no responseWell a key doesn't take a "yes or no" it listens for whenever a key is pressed
yes, I know. Again, not waiting for a key press. waiting for an entry of yes
y
or no n
then the game either continutes (if part) or ends (else part)You can have a string set to the console.readline and check for that ?
Oh you did that
It's hard to see on phone but I think it goes within after you check for y
the game closes in the else part of the if statement when the user enters anything but
y
. If the user enters y
, it will restart the game. I need the part for the if statement so that if the user enters anything other than y
, then it ends the game and closes the screen!= means not equal to
If the user Input != "y" do something
else quit
I'm really bad at explaining code to people xd
I just cannot find a working line of code to put after
// close the game
in the else
part of the statementTo quit the app?
yes. it's a console based game. So when they do not want to play again, it should close the screen altogether
Environment.Exit(0);
I kept finding Application.Exit() lol idk why I kept finding that, but I never found anything to do with environment
Try that
I am currently doing that. Thank you so much for your help. I'll ping back if I run into anymore trouble 🙂 ❤️
Did it work ?
I haven't made it that far yet. Once I'm able to test it, I'll let you know
Alright sure thing
so I haven't gotten a chance to test out that code yet, but I have a noobie question. I generated this project through Visual Studio. In python, you can run a terminal command
pip freeze > requirements.txt
and that will create a file of all the dependencies inside your workspace if you're doing a virtual environment, and if not in a venv then it'll make the file of all your global libraries that you have pip installed. How do I do this in C#? I'm wanting to list the required dependencies in my read me file, but I'm not sure how to find them. I see a dependencies folder in my projects workspace, but when I expand those lists (folders), then expand for quite a bit, so how can I appropriately list the needed dependencies that the user will need to have installed if they clone the project from my git repo and try to compile and run themselves?
The Environment.Exit(0);
works, but it doesn't close the window. Instead it still waits for a keypress. Like it exits the game, and gives that message to press enter or any key to exit this window instead of just closing the windowIt only happens when you are debugging if i remember correctly
When you build or publish your application, all required libraries are included on the output directory.
So references are actually specified in the project file, .csproj if its c#
When they clone your repo, all packages from nuget can be
restored
with dotnet restore command, which the IDE usually does during project open, so you dont actually need to do the steps like you did in pythonoh ok. awesome. tyvm