我是新来的编程,我开始与C#。所以我遵循了一个关于在控制台中制作简单游戏的教程。 Here's the tutorial。当我完成教程后,我留下了一个体面的比赛,但没有打磨。游戏不会重新启动。我如何重新启动游戏机?
我试过一些不同的方式试图绕过它,但没有我试过似乎工作(因为即时新的和一半的编码没有意义)。
所以我试了这个。为什么这不工作?
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);
}
}
}
我要重新启动我的游戏,但不关闭窗口。
对不起,如果这没有足够的信息来帮助。
这不是你应该提供的信息的一部分,它不够。 – Ogbe
是的,最重要的是要展示主函数的外观和游戏循环过程中会发生什么(我们当然可以检查教程,但这意味着你的问题不包含足够的信息) – Icepickle
好吧,这证明了多少我不知道:/ – deviousPriest