我正在尝试使用C#(Windows窗体)制作迷你游戏,例如太空入侵者。飞船飞行得很好,但是我没有得到多发射击,我有一种制作动画的方法timer_Tick
,当我按下太空进行射击时,第一发可以正常工作,但是第二发可以。此半位置的第一杆消失。镜头是pictureBox
,我尝试了pictureBox
列表,但没有成功。
我有这样的事情:
public void Form1_Load(object sender, EventArgs e)
{
shootP = new PictureBox();
shootP.Image = Properties.Resources.shoot_1;
shootP.SizeMode = PictureBoxSizeMode.Zoom;
shootP.Size = new Size(10, 72);
ListShoot = new List<PictureBox>();
int i = -1;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
control = false;
i++;
ListShoot.Add(shootP);
timeShoot.Start();
}
private void timeShoot_Tick(object sender, EventArgs e)
{
if (control == false)
{
ListShoot[i].Location = new Point(spacecraft._imageBox.Location.X + 50, spacecraft._imageBox.Location.Y - 55); // align the shoot with spacecraft
control = true;
}
ListShoot[i].Top -= 40;
}
我究竟做错了什么?
最佳答案
您每次都需要创建一个新图像,就像aliasesce的答案一样。
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
control = false;
shootP = new PictureBox();
shootP.Image = Properties.Resources.shoot_1;
shootP.SizeMode = PictureBoxSizeMode.Zoom;
shootP.Size = new Size(10, 72);
ListShoot.Add(shootP);
timeShoot.Start();
}
}
然后,您需要更新列表中所有图像的位置。使用foreach循环并放弃int迭代器。
private void timeShoot_Tick(object sender, EventArgs e)
{
if (control == false)
{
foreach PictureBox shot in ListShot
{
shot.Location = new Point(spacecraft._imageBox.Location.X + 50, spacecraft._imageBox.Location.Y - 55); // align the shoot with spacecraft
shot.Top -= 40;
}
control = true;
}
}
您可能还必须更新绘制图像的代码,以使其遍历列表。与类似:
foreach PictureBox shot in ListShoot
{
draw(shot);
}
最后,您可能需要不时地清理列表,以消除屏幕上不再存在的不再需要更新或绘制的镜头。
关于c# - 迷你游戏形式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12992006/