我是新來的編程,我開始與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