我正在尝试制作一个简单的乒乓球游戏,但遇到了这个问题,如果按下“S”或“W”按钮,每个 timer1 滴答声(间隔设置为 1ms)应该移动 1 个像素的白色桨矩形,这个从理论上讲,我的 400px 高度图片框内的白色矩形应该在 0.4 - 0.8 秒内从 y = 0 移动到 y = 400,但显然它需要超过 4 秒。
我知道如果 cpu 已经很忙或处理速度问题,计时器滴答事件可能会被“跳过”,但我试图让蛇游戏比这 50 行代码更复杂,并且绘制蛇的速度实际上是准确的低时间间隔
为什么需要这么多?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox1.BackColor = Color.Black;
timer1.Interval = 1;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
PongGame.CheckIfMoving();
PongGame.DrawIt(pictureBox1);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.S)
{ PongGame.movesDown = true; }
if (e.KeyData == Keys.W)
{ PongGame.movesUp = true; }
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.S)
{ PongGame.movesDown = false; }
if (e.KeyData == Keys.W)
{ PongGame.movesUp = false; }
}
}
public class PongGame
{
public static Rectangle paddle1 = new Rectangle(14, 370, 20, 100);
public Rectangle paddle2;
public static bool movesUp;
public static bool movesDown;
public static Graphics Draw;
public static void DrawIt(PictureBox pb1)
{
Draw = pb1.CreateGraphics();
SolidBrush sb = new SolidBrush(Color.White);
Draw.Clear(Color.Black);
Draw.FillRectangle(sb, paddle1);
}
public static void CheckIfMoving()
{
if (movesUp == true)
{
paddle1.Y -= 1;
}
if (movesDown == true)
{
paddle1.Y += 1;
}
}
}
提前感谢您的答案(: