按下按钮时,C#控制台程序运行速度更快
出于兴趣,我正在C#console中创建一个程序生成的platformer。但是,我发现程序运行速度比我预期的慢,除非我按住任何按钮,然后它返回到作为timeLapse输入的预期数量 目前唯一具有功能的按钮是空格键,它可以让你跳转。我也测试了代码,但代码行//中没有代码,这就解决了问题。有人对此有解释或文章吗?我自己找不到很多 可以添加PlatformGenerator代码,尽管我认为这不是必需的 程序代码:按下按钮时,C#控制台程序运行速度更快,c#,performance,console,C#,Performance,Console,出于兴趣,我正在C#console中创建一个程序生成的platformer。但是,我发现程序运行速度比我预期的慢,除非我按住任何按钮,然后它返回到作为timeLapse输入的预期数量 目前唯一具有功能的按钮是空格键,它可以让你跳转。我也测试了代码,但代码行//中没有代码,这就解决了问题。有人对此有解释或文章吗?我自己找不到很多 可以添加PlatformGenerator代码,尽管我认为这不是必需的 程序代码: namespace TestProject.Procedural_Generation
namespace TestProject.Procedural_Generation
{
public class PlatformManager
{
#region Variables
// Game settings
private static char[,] screen;
private static int screenWidth = 96;
private static int screenHeight = 20;
// Platform generation settings
private static int minGeneration = 4;
private static int maxGeneration = 18;
private static PlatformGenerator pg;
private static List<Platform> platforms = new List<Platform>();
// Physics variables
private static int oldX = 0;
private static int currentX = 0;
private static int currentY = 8;
private static string toSkip = "";
private static Player player;
// Timer variables
private static float timeLapse = 100;
private static float jumpTime = 200;
private static Timer graphicUpdater;
private static Timer physicUpdater;
private static Timer jumpTimer;
private static bool isJumping = false;
private static long score = 0;
#endregion
public PlatformManager()
{
screen = new char[screenHeight, screenWidth];
pg = new PlatformGenerator(minGeneration, maxGeneration);
player = new Player();
// Physics
physicUpdater = new Timer(timeLapse);
physicUpdater.Elapsed += ExecutePhysics;
physicUpdater.AutoReset = true;
physicUpdater.Enabled = true;
jumpTimer = new Timer(jumpTime);
jumpTimer.Elapsed += Land;
jumpTimer.Enabled = true;
// Graphics
graphicUpdater = new Timer(timeLapse);
graphicUpdater.Elapsed += ExecuteGraphics;
graphicUpdater.AutoReset = true;
graphicUpdater.Enabled = true;
while (true)
{
score++;
}
}
private void Land(object source, EventArgs e)
{
isJumping = false;
}
#region Graphics
private void ExecuteGraphics(object source, ElapsedEventArgs e)
{
Console.Clear();
Console.Write(Display());
}
// Makes sure everything is printed correctly
public string Display()
{
string result = "";
for (int y = 0; y < screenHeight; y++)
{
for (int x = 0; x < screenWidth; x++)
{
if (y == player.y && x == player.x)
{
result += "O";
}
else if (screen[y, x] == '=')
{
result += "=";
}
else
{
result += " ";
}
}
result += "\n";
}
return result;
}
#endregion
#region Physics
// Controls platform generation and movement
private void ExecutePhysics(object source, ElapsedEventArgs e)
{
Platformer();
if (player.y == screenHeight - 1)
{
graphicUpdater.Stop();
physicUpdater.Stop();
Console.WriteLine();
Console.WriteLine("Game over! Score: " + Math.Floor(Math.Sqrt(score)));
}
else if (isJumping)
{
player.y -= 1;
}
else if (Console.ReadKey(true).Key == ConsoleKey.Spacebar && screen[player.y + 1, player.x] == '=') // THIS CODE
{
isJumping = true;
jumpTimer.Start();
player.y -= 1;
}
else if (screen[player.y + 1, player.x] != '=' && !isJumping)
{
player.y += 1;
}
}
// Generate a new platform
public void Platformer()
{
Platform newPlatform = pg.Generate(currentX, currentY);
currentY = newPlatform.y;
if (currentX + newPlatform.size + newPlatform.xDif > screenWidth)
{
MoveScreen(newPlatform.size + newPlatform.xDif);
currentX -= newPlatform.size + newPlatform.xDif;
oldX -= newPlatform.size + newPlatform.xDif;
}
while (currentX < oldX + newPlatform.size + newPlatform.xDif)
{
screen[currentY, currentX] = '=';
currentX += 1;
}
oldX = currentX;
}
// Update all rows so the newly added ones fit.
public void MoveScreen(int amount)
{
for (int y = 0; y < screenHeight; y++)
{
for (int x = amount; x < screenWidth; x++)
{
screen[y, x - amount] = screen[y, x];
}
}
for (int y = 0; y < screenHeight; y++)
{
for (int x = screenWidth - amount; x < screenWidth; x++)
{
screen[y, x] = '\0';
}
}
}
#endregion
}
}
namespace TestProject.Procedural\u生成
{
公共类平台管理器
{
#区域变量
//游戏设置
私有静态字符[,]屏幕;
私有静态int屏幕宽度=96;
专用静态int屏幕高度=20;
//平台生成设置
私有静态int minGeneration=4;
专用静态int maxGeneration=18;
专用静态平台生成器;
私有静态列表平台=新列表();
//物理变量
私有静态int oldX=0;
私有静态int currentX=0;
专用静态输入电流y=8;
私有静态字符串toSkip=“”;
私人静态播放器;
//定时器变量
专用静态浮点timeLapse=100;
专用静态浮动跳线时间=200;
私有静态计时器图形更新程序;
专用静态定时器;
专用静态定时器;
私有静态bool isJumping=false;
私人静态长期得分=0;
#端区
公共平台管理器()
{
屏幕=新字符[屏幕高度,屏幕宽度];
pg=新平台生成器(minGeneration、maxGeneration);
player=新玩家();
//物理学
physicUpdater=新计时器(timeLapse);
physicUpdater.Appead+=执行物理;
physicUpdater.AutoReset=true;
physicUpdater.Enabled=true;
jumpTimer=新定时器(jumpTime);
jumpTimer.Appead+=着陆;
jumpTimer.Enabled=true;
//图形
graphicUpdater=新计时器(timeLapse);
graphicUpdater.Appead+=ExecuteGraphics;
graphicUpdater.AutoReset=true;
graphicUpdater.Enabled=true;
while(true)
{
分数++;
}
}
私有虚地(对象源,事件参数e)
{
isJumping=false;
}
#区域图形
私有void ExecuteGraphics(对象源,ElapsedEventArgs e)
{
Console.Clear();
Console.Write(Display());
}
//确保所有内容都正确打印
公共字符串显示()
{
字符串结果=”;
对于(int y=0;y<屏幕高度;y++)
{
对于(int x=0;xscreenWidth)
{
移动屏幕(newPlatform.size+newPlatform.xDif);
currentX-=newPlatform.size+newPlatform.xDif;
oldX-=newPlatform.size+newPlatform.xDif;
}
而(currentX
不确定我是否理解正确,但是
控制台