我是编程新手,我从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);
}
}
}
我想在不关闭窗口的情况下重启游戏。
很抱歉,如果这还不足以帮助您。
答案 0 :(得分:1)
您可以使用while循环。检查用户是想退出还是开始游戏(例如使用ESC和Space)然后再适当地处理它。
ConsoleKeyInfo cki = Console.ReadKey(true);
while(cki.Key != KeyCode.Esc)
{
if(cki.Key == KeyCode.Space) Game();
cki = Console.ReadKey(true);
}
答案 1 :(得分:1)
我想你有主要的功能/调用功能如下;
import json
import numpy as np
import pandas as pd
import requests
token ='xxxx' #the public token given when you are registered
segments = ['120'] #list of segment id's to be used
header={'Authorization': 'Bearer ' + token, 'per_page':'100', 'page':'2'}
for segment in segments:
url ='https://www.strava.com/api/v3/segments/' + segment + 'all_efforts'
r=requests.get(url,headers = header )
jobj= json.loads(r.text)
你可以添加一个while循环并检查用户输入,如果他想退出,那么他可以按指定键(Esc可能......)
参考以下代码;
static void Main(string[] args)
{
var game=new Game();
//May be some initialization logic
game.Run();
}
您可以根据需要调整此逻辑,但我认为您会明白我的观点。
答案 2 :(得分:-2)
Game()中的内容。你可以使用新实例。
System.Diagnostics.Process.Start(Application.ExecutablePath);
退出当前流程。 envoinment.exit();