C
C#2y ago
Mekasu0124

✅ How to make program wait before executing next print line?

Console.WriteLine("This is line one");
// sleep 2 seconds
Console.WriteLine("This is line two");
// sleep 2 seconds
Console.WriteLine("This is line three");
Console.WriteLine("This is line one");
// sleep 2 seconds
Console.WriteLine("This is line two");
// sleep 2 seconds
Console.WriteLine("This is line three");
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
Pink Elsa💖
Pink Elsa💖2y ago
Yes
Mekasu0124
Mekasu0124OP2y ago
I'm like no where near a beginner yet lol how would I do that?
Pobiega
Pobiega2y ago
Thread.Sleep(2000) for now.
Mekasu0124
Mekasu0124OP2y ago
ok bet. tyvm one more question please. How do I exit the program based off user's input?
if (userInput == "y")
{
// continue program by calling necessary function to run again
}
else
{
// exit program back to desktop
}
if (userInput == "y")
{
// continue program by calling necessary function to run again
}
else
{
// exit program back to desktop
}
Pink Elsa💖
Pink Elsa💖2y ago
'''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
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...
Mekasu0124
Mekasu0124OP2y ago
I read over the link, but I'm not sure how to apply it to my code?
using System;
using System.Threading;

namespace MeksMathGame
{
class Program
{

static void EndGame()
{
Console.WriteLine("You've Answered All The Questions! Would You Like To Play Again? Y/N");

string userOption = Console.ReadLine();

if (userOption.ToLower() == "y")
{
Console.WriteLine("You Selected To Play Again. Re-Launching Application!\n");
Thread.Sleep(3000);
DifficultySelection();
}
else
{
Console.WriteLine("You Selected To Not Play Again. Closing Application!\n");
Thread.Sleep(3000);
}
}
using System;
using System.Threading;

namespace MeksMathGame
{
class Program
{

static void EndGame()
{
Console.WriteLine("You've Answered All The Questions! Would You Like To Play Again? Y/N");

string userOption = Console.ReadLine();

if (userOption.ToLower() == "y")
{
Console.WriteLine("You Selected To Play Again. Re-Launching Application!\n");
Thread.Sleep(3000);
DifficultySelection();
}
else
{
Console.WriteLine("You Selected To Not Play Again. Closing Application!\n");
Thread.Sleep(3000);
}
}
I'd appreciate some more help please ❤️
Pink Elsa💖
Pink Elsa💖2y ago
Where is the key register supposed to go Where 'y' is ?
Mekasu0124
Mekasu0124OP2y ago
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
else
{
Console.WriteLine("You Selected To Not Play Again. CLosing Application!\n");
Thread.Sleep(3000);
// close entire application here
}
else
{
Console.WriteLine("You Selected To Not Play Again. CLosing Application!\n");
Thread.Sleep(3000);
// close entire application here
}
Pink Elsa💖
Pink Elsa💖2y ago
Well in this case you want the main game method to run in the while key isn't available loop
Mekasu0124
Mekasu0124OP2y ago
I could give you the full code, but it's over 500 lines
Pink Elsa💖
Pink Elsa💖2y ago
'''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); '''
Mekasu0124
Mekasu0124OP2y ago
you're using apostrophes instead of back ticks, and that while loop doesn't make any sense to me
Pink Elsa💖
Pink Elsa💖2y ago
Phone atm Can't seem to find them
Mekasu0124
Mekasu0124OP2y ago
touch and hold teh apostrophe button to get the backtick if you're on apple
Pink Elsa💖
Pink Elsa💖2y ago
Ahh
Mekasu0124
Mekasu0124OP2y ago
on android, it should be it's own button https://pastebin.com/Ui9GMUr1 this is the full code
Pink Elsa💖
Pink Elsa💖2y ago
I believe to run end game in the do while etc Since you're just waiting for a key press
Mekasu0124
Mekasu0124OP2y ago
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 response
Pink Elsa💖
Pink Elsa💖2y ago
Well a key doesn't take a "yes or no" it listens for whenever a key is pressed
Mekasu0124
Mekasu0124OP2y ago
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)
Pink Elsa💖
Pink Elsa💖2y ago
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
Mekasu0124
Mekasu0124OP2y ago
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
Pink Elsa💖
Pink Elsa💖2y ago
!= means not equal to If the user Input != "y" do something else quit I'm really bad at explaining code to people xd
Mekasu0124
Mekasu0124OP2y ago
static void EndGame()
{
// ask user if they want to play again
Console.WriteLine("You've Answered All The Questions! Would You Like To Play Again? Y/N");
// get user input
string userOption = Console.ReadLine();
// if the user enters 'y' for yes to play again
if (userOption.ToLower() == "y")
{
// let them know that the game is restarting
Console.WriteLine("You Selected To Play Again. Re-Launching Application!\n");
// do a fake sleep to simulate game restarting
Thread.Sleep(3000);
// recall the first menu in the game
DifficultySelection();
}
// if the user enters 'n' for no, or anything other than 'y'
else
{
// tell them they selected to exit the game
Console.WriteLine("You Selected To Not Play Again. Closing Application!\n");
// fake simulation of game closing down
Thread.Sleep(3000);
// close the game
}
}
static void EndGame()
{
// ask user if they want to play again
Console.WriteLine("You've Answered All The Questions! Would You Like To Play Again? Y/N");
// get user input
string userOption = Console.ReadLine();
// if the user enters 'y' for yes to play again
if (userOption.ToLower() == "y")
{
// let them know that the game is restarting
Console.WriteLine("You Selected To Play Again. Re-Launching Application!\n");
// do a fake sleep to simulate game restarting
Thread.Sleep(3000);
// recall the first menu in the game
DifficultySelection();
}
// if the user enters 'n' for no, or anything other than 'y'
else
{
// tell them they selected to exit the game
Console.WriteLine("You Selected To Not Play Again. Closing Application!\n");
// fake simulation of game closing down
Thread.Sleep(3000);
// close the game
}
}
I just cannot find a working line of code to put after // close the game in the else part of the statement
Pink Elsa💖
Pink Elsa💖2y ago
To quit the app?
Mekasu0124
Mekasu0124OP2y ago
yes. it's a console based game. So when they do not want to play again, it should close the screen altogether
Pink Elsa💖
Pink Elsa💖2y ago
Environment.Exit(0);
Mekasu0124
Mekasu0124OP2y ago
I kept finding Application.Exit() lol idk why I kept finding that, but I never found anything to do with environment
Pink Elsa💖
Pink Elsa💖2y ago
Try that
Mekasu0124
Mekasu0124OP2y ago
I am currently doing that. Thank you so much for your help. I'll ping back if I run into anymore trouble 🙂 ❤️
Pink Elsa💖
Pink Elsa💖2y ago
Did it work ?
Mekasu0124
Mekasu0124OP2y ago
I haven't made it that far yet. Once I'm able to test it, I'll let you know
Pink Elsa💖
Pink Elsa💖2y ago
Alright sure thing
Mekasu0124
Mekasu0124OP2y ago
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 window
kunio_kun
kunio_kun2y ago
It 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 python
Mekasu0124
Mekasu0124OP2y ago
oh ok. awesome. tyvm
Want results from more Discord servers?
Add your server