0
我有两个PictureBox。一个是移动的PictureBox,我用WASD键移动,另一个是随机生成的位置(敌人)。当我按空格键移动PicBox时,会抛出一个球,然后当球与敌人相撞时,我想要这个球,敌人的PictureBox将被移除。我想创建一个这样做的事件,但我不知道如何。你可以帮我吗?这是我的全部代码:C#自定义事件
namespace Gioco
{
public partial class Form1 : Form
{
Image img;
Image tmpSx;
Image tmpDx;
Image tmpUp;
Image tmpDw;
List<PictureBox> fireBox = new List<PictureBox>();
int count = 0; //COUNT GLOBALE
Timer tm = new Timer();
bool go = true;
EventHandler test;
EventHandler enemy;
Timer tmEnem = new Timer();
public Form1()
{
InitializeComponent();
panel1.Controls.Add(pictureBox1);
pictureBox1.Parent = panel1;
img = pictureBox1.Image;
tmpSx = (Image)img.Clone();
tmpSx.RotateFlip(RotateFlipType.RotateNoneFlipX);
tmpDx = (Image)img.Clone();
tmpDw = (Image)img.Clone();
tmpDw.RotateFlip(RotateFlipType.Rotate90FlipNone);
tmpUp = (Image)img.Clone();
tmpUp.RotateFlip(RotateFlipType.Rotate270FlipNone);
//COMPARSA NEMICO
tmEnem.Interval = 5000;
enemy = new EventHandler(Appear);
tmEnem.Tick += enemy;
tmEnem.Start();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D)
{
pictureBox1.Image = tmpDx;
pictureBox1.Location = new Point(pictureBox1.Location.X + 15, pictureBox1.Location.Y);
pictureBox1.SendToBack();
}
else if (e.KeyCode == Keys.A)
{
pictureBox1.Image = tmpSx;
pictureBox1.Location = new Point(pictureBox1.Location.X - 15, pictureBox1.Location.Y);
pictureBox1.SendToBack();
}
else if (e.KeyCode == Keys.W)
{
pictureBox1.Image = tmpUp;
pictureBox1.Location = new Point(pictureBox1.Location.X, pictureBox1.Location.Y - 15);
pictureBox1.SendToBack();
}
else if (e.KeyCode == Keys.S)
{
pictureBox1.Image = tmpDw;
pictureBox1.Location = new Point(pictureBox1.Location.X, pictureBox1.Location.Y + 15);
pictureBox1.SendToBack();
}
else if (e.KeyCode == Keys.Space)
{
if (go)
{
Image fire = Image.FromFile(@"c:\users\user\documents\visual studio 2015\Projects\Gioco\Gioco\Resources\494979.gif");
PictureBox picFire = new PictureBox();
fireBox.Add(picFire);
count++;
picFire.Image = fire;
picFire.Visible = true;
picFire.BackColor = Color.Transparent;
picFire.BringToFront();
picFire.SizeMode = PictureBoxSizeMode.Zoom;
switch (Direction(pictureBox1))
{
case "dx":
picFire.Location = new Point(pictureBox1.Location.X + pictureBox1.Width, pictureBox1.Location.Y + pictureBox1.Height/2);
panel1.Controls.Add(picFire);
tm.Interval = 35;
test = new EventHandler(tm_TickDx);
tm.Tick += test;
tm.Start();
break;
case "sx":
picFire.Location = new Point(pictureBox1.Location.X - picFire.Width, pictureBox1.Location.Y + pictureBox1.Height/2);
panel1.Controls.Add(picFire);
tm.Interval = 35;
test = new EventHandler(tm_TickSx);
tm.Tick += test;
tm.Start();
break;
case "up":
picFire.Location = new Point(pictureBox1.Location.X + pictureBox1.Width/2, pictureBox1.Location.Y - picFire.Height);
panel1.Controls.Add(picFire);
tm.Interval = 35;
test = new EventHandler(tm_TickUp);
tm.Tick += test;
tm.Start();
break;
case "dw":
picFire.Location = new Point(pictureBox1.Location.X, pictureBox1.Location.Y + pictureBox1.Height);
panel1.Controls.Add(picFire);
tm.Interval = 35;
test = new EventHandler(tm_TickDw);
tm.Tick += test;
tm.Start();
break;
}
}
}
}
void Appear(object sender, EventArgs e)
{
Image imgEnemy = Properties.Resources.tengu_warrior_enemy_sprite_by_sanctumpolis_d6wsk6q;
PictureBox picEnemy = new PictureBox();
picEnemy.SizeMode = PictureBoxSizeMode.Zoom;
picEnemy.Image = imgEnemy;
Random rand = new Random(DateTime.Now.Millisecond);
Point random = new Point(rand.Next(0, ClientSize.Width - picEnemy.Width), rand.Next(0, ClientSize.Height - picEnemy.Height));
picEnemy.Location = random;
panel1.Controls.Add(picEnemy);
}
void tm_TickDx(object sender, EventArgs e)
{
go = false;
if (!go)
{
if(fireBox[count - 1].Location.X < panel1.Location.X + panel1.Width)
{
fireBox[count - 1].Left = fireBox[count - 1].Left + 25;
}
else
{
tm.Stop();
go = true;
tm.Tick -= test;
}
}
}
void tm_TickSx(object sender, EventArgs e)
{
go = false;
if (!go)
{
if (fireBox[count - 1].Location.X > panel1.Location.X - fireBox[count - 1].Width)
fireBox[count - 1].Left = fireBox[count - 1].Left - 25;
else
{
tm.Stop();
go = true;
tm.Tick -= test;
}
}
}
void tm_TickUp(object sender, EventArgs e)
{
go = false;
if (!go)
{
if (fireBox[count - 1].Location.Y > panel1.Location.Y - fireBox[count - 1].Height)
fireBox[count - 1].Top = fireBox[count - 1].Top - 25;
else
{
tm.Stop();
go = true;
tm.Tick -= test;
}
}
}
void tm_TickDw(object sender, EventArgs e)
{
go = false;
if (!go)
{
if (fireBox[count - 1].Location.Y < panel1.Location.Y + panel1.Height)
fireBox[count - 1].Top = fireBox[count - 1].Top + 25;
else
{
tm.Stop();
go = true;
tm.Tick -= test;
}
}
}
string Direction(PictureBox p)
{
if (p.Image == tmpDx)
return "dx";
else if (p.Image == tmpSx)
return "sx";
else if (p.Image == tmpUp)
return "up";
else if (p.Image == tmpDw)
return "dw";
return "dx";
}
}
}
我想,主要的想法是,每次抛出的球移动时检查它的坐标是否与敌人碰撞。 –