我正在制作像太空侵略者一样的射击游戏。我的PictureBox pb不会使用按键移动,并且在PictureBox pb1上有问题,空间光盘在一秒钟后消失。它不会左右移动。使用按键和定时器移动PictureBox的帮助
这是我的代码到目前为止。
namespace SpaceShoot
{
class Shoot
{
private Form f;
private Button bLeft, bRight;
private PictureBox pb;
private PictureBox pb1;
private Boolean check = false;
private int X, Y;
Timer Clock = new Timer();
private int k = 0;
int x = 0;
public Shoot()
{
f = new Form();
bLeft = new Button();
bRight = new Button();
pb = new PictureBox(); //spaceship
pb1 = new PictureBox(); //spacedisc
Clock = new Timer();
}
public void Launch()
{
pb1.Image = new Bitmap("spacedisc.png");
pb1.SetBounds(20, 20, 120, 70);
pb1.SizeMode = PictureBoxSizeMode.StretchImage;
f.BackColor = System.Drawing.Color.Black;
f.Size = new Size(700, 700);
f.StartPosition = FormStartPosition.CenterScreen;
f.Controls.Add(pb);
f.Controls.Add(pb1);
bLeft.KeyDown += new KeyEventHandler(Space_KeyDown);
bRight.KeyDown += new KeyEventHandler(Space_KeyDown);
pb.SetBounds(300, 580, 60, 60);
pb.Image = new Bitmap("spaceship.png");
pb.SizeMode = PictureBoxSizeMode.StretchImage;
Clock = new Timer();
Clock.Interval = 800;
Clock.Tick += new EventHandler(Clock_Tick);
Clock.Start();
f.ShowDialog();
}
public void Clock_Tick(object sender, EventArgs e)
{
for (x = 0; x >= 0; x += 2)
{
pb1.Location = new Point(0 + x, 20);
}
for (x = 500; x >= 500; x -= 2)
{
pb1.Location = new Point(500 - x, 20);
}
f.Invalidate();
}
public void Space_KeyDown(object sender, KeyEventArgs e)
{
int x = pb.Location.X;
if (e.KeyCode == Keys.Right)
{
x += 5;
pb.Location = new Point(300 + x, 580);
}
else if (e.KeyCode == Keys.Left)
{
x -= 5;
pb.Location = new Point(300 + x, 580);
}
}
}
}