deviousPriest deviousPriest - 20 days ago 7
C# Question

How do i restart a console game?

I'm new to programming and i started with c#. So i followed a tutorial on making a simple game in console. Here's the tutorial. And when i finished the tutorial i was left with a decent game but it wasnt polished. The game doesn't restart itself.

I've tried some different ways trying to get around it but nothing i tried seemed to work (because im new and half the coding didnt make sense).

So i tried this. why doesn't this work?

using System;
using System.Threading;
using System.Diagnostics;

namespace DodgeGame
{

public class Game
{
//This is our constructor,
// it gets run auto when new instance is made.
public Game()
{
//PLAYER
playerUnit = new PlayerUnit(5, 5, "@");

//ENEMY
enemyUnits = new Unit[ numEnemies ];

Random = new Random();
Score = 0;

for(int i = 0; i < numEnemies; i++)
{
int row = Random.Next(0, Console.WindowHeight - 1);
enemyUnits[i] = new EnemyUnit(Console.WindowWidth -1, row , "<");
}

stopwatch = new Stopwatch();
}



private Stopwatch stopwatch;

private Unit playerUnit;

private int numEnemies = 20;
private Unit[] enemyUnits;

public static Random Random;
public static int Score;

public void Run()
{

stopwatch.Start();
long timeAtPreviousFrame = stopwatch.ElapsedMilliseconds;


while(true)
{
//Time since last frame.
int deltaTimeMS = (int)(stopwatch.ElapsedMilliseconds
- timeAtPreviousFrame);
timeAtPreviousFrame = stopwatch.ElapsedMilliseconds;


//Updating Units.
playerUnit.Update(deltaTimeMS);

for(int i = 0; i < enemyUnits.Length; i++)
{
enemyUnits[i].Update(deltaTimeMS);

if( playerUnit.IsCollidingWith(enemyUnits[i]))
{
//Then game over.
GameOver();
return;
}
}
playerUnit.Draw();

foreach (Unit u in enemyUnits)
{
u.Draw();
}


//Draw the score.
Console.SetCursorPosition(0, Console.WindowHeight - 1);
Console.Write("SCORE: " + Score.ToString() );

//Lets just do a tiny sleep to avoid running at too high of fps.
Thread.Sleep( 5 );

}

//PROGRAM END.
}

void GameOver()
{
Console.Clear();
Console.WriteLine("Game Over. Your Score was: " + Score);
Console.WriteLine("Insert Name to LeaderBoard:");
//TODO: Make a local leaderboard?

if(Console.KeyAvailable == true)
{
ConsoleKeyInfo cki = Console.ReadKey(true);
while(cki.Key != ConsoleKey.Escape)
{
if(cki.Key == ConsoleKey.Spacebar) Game();
cki = Console.ReadKey(true);
}


Console.SetCursorPosition(0,Console.WindowHeight - 1);


}

}


}

I want to restart my game without closing the window.

Sorry if this isnt enough info to help.

Answer

I suppose you have main function/calling function as follows;

static void Main(string[] args)
{
    var game=new Game();
    //May be some initialization logic       
    game.Run();
}

In that you can add a while loop and check for user input, if he wants to exit then he can press specified key (Esc maybe...)

Refer following code;

static void Main(string[] args)
{
    while(true)
    {
        var game = new Game();
        //May be some initialization logic       
        game.Run();

        Console.WriteLine("Press any key to restart, press Esc to close.");
        var userInput = Console.ReadKey();
        if (userInput.Key == ConsoleKey.Escape)
             return;
    }
}

You could tweak this logic as per your need but I think you'll get my point.