为什么当我尝试在 C# 中制作太空入侵者时无法移动播放器

  • 本文关键字:入侵者 太空 播放器 移动 c#
  • 更新时间 :
  • 英文 :


我的代码有什么问题我正在尝试使用visual studio制作空间入侵者

using System;
using System.Windows.Forms;
namespace _2nd_game_try
{
public partial class Form1 : Form
{
bool goleft, goright;
int playerSpeed = 13;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void keydown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
goleft = true;
}
if (e.KeyCode == Keys.Right)
{
goright = true;
}
}
private void keyup(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
goleft = false;
}
if (e.KeyCode == Keys.Right)
{
goright = false;
}
}
private void gameTime_Tick(object sender, EventArgs e)
{
}
private void Walk()
{
while (true)
{
if (goleft)
{
player.Left -= playerSpeed;
}
if (goright)
{
player.Left += playerSpeed;
}
}
}
}
}
  1. 启用定时器并使用有效间隔
  2. Walk()方法中移除while (true)
  3. 呼叫Walk()内定时器回呼。
  4. 订阅keyup和keydown事件处理程序
  5. 设置keypreview属性为true