저는 우주 침략자와 같은 슈팅 게임을 만들고 있습니다. 내 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);
}
}
}
}