移动图片框不起作用 [英] Move picturebox not function
问题描述
大家好,我的名字是Dan,我是论坛的新人
我的问题如下。
我有一个学校项目,是游戏战斗机。
我需要我的船可以用Space键拍摄(PictureBox),所以很多次按空格键发送PictureBox(拍摄)
但我没看到我的错误在哪里。
这是代码:
<
完整的项目在这里:
https://drive.google.com/file/d/1T_stjuQx-g1_jn0VijbTQ0yij3o0OqCz/view?usp=sharing
提前致谢
DS
我的尝试:
Hello all, my name is Dan and I'm new in the forum
My question is the following.
I have a school project, is the game Fighter Jet.
I need my ship can to shoot (PictureBox) with the Space key, so many times press the Space key send a PictureBox (shoot)
But I don't see where have my error.
This is the code:
<
The complete project is here:
https://drive.google.com/file/d/1T_stjuQx-g1_jn0VijbTQ0yij3o0OqCz/view?usp=sharing
Thanks in advance
DS
What I have tried:
int indice_Bala = 0;
public PictureBox[] pbBala = new PictureBox[1];
public void AgregarBala(PictureBox pbPadre)
{
Array.Resize(ref pbBala, indice_Bala + 2);
pbBala[indice_Bala] = new PictureBox();
pbBala[indice_Bala].Visible = true;
pbBala[indice_Bala].Image = Image.FromFile(Application.StartupPath + "\\Cohete.jpg");
pbBala[indice_Bala].Height = 10;
pbBala[indice_Bala].Width = 75;
pbBala[indice_Bala].Top = pbPadre.Top + (pbPadre.Height / 2);
pbBala[indice_Bala].Left = pbPadre.Left + pbPadre.Width;
pbBala[indice_Bala].SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.Controls.Add(pbBala[indice_Bala]);
indice_Bala++;
//Timer Function
private void playTimer(object sender, EventArgs e)
{
//player.Left += moveLeft;
PicEne1.Top += enemyMove;
PicEne2.Top += enemyMove;
PicEne3.Top += enemyMove;
PicEne4.Top += enemyMove;
PicEne5.Top += enemyMove;
LblPuntos.Text = "" + score;
for (int i = 0; i < indice_Bala; i++)
{
pbBala[i].Left = pbBala[i].Left + 20;
}
if (PicEne1.Top == 660 || PicEne2.Top == 660 || PicEne3.Top == 660 || PicEne4.Top == 660 || PicEne5.Top == 660)
{
gameOver();
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
{
if ((PicNave.Location.Y - Distancia) >= 0)
PicNave.Location = new Point(PicNave.Location.X, PicNave.Location.Y - Distancia);
break;
}
case Keys.Down:
{
if ((PicNave.Height + (PicNave.Location.Y + Distancia)) <= ClientSize.Height)
PicNave.Location = new Point(PicNave.Location.X, PicNave.Location.Y + Distancia);
break;
}
case Keys.Left:
{
if ((PicNave.Location.X - Distancia) >= 0)
PicNave.Location = new Point(PicNave.Location.X - Distancia, PicNave.Location.Y); break;
}
case Keys.Right:
{
if ((PicNave.Width + (PicNave.Location.X + Distancia)) <= ClientSize.Width)
PicNave.Location = new Point(PicNave.Location.X + Distancia, PicNave.Location.Y); break;
}
case Keys.Space:
{
{
AgregarBala(PicNave);
break;
}
}
}
}
}
推荐答案
嗨丹,
有一些修改可以让你的项目有效。
First在Form Key按下方法:
Keypress eventargument无法定义keycode所以你必须使用keychar
Hi Dan,
There are some modification that will make your project working.
First in Form Key Press Method:
Keypress eventargument can not have the defination of keycode so you have to use keychar
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
var ky = Keys.Space;
if (e.KeyChar == ' ')
{
AgregarBala(PicNave);
}
{
AgregarBala(PicNave);
}
}
Second in
AgregarBala
方法:
这里你需要更改宽度和高度,最重要的是将pbBala添加到PicFondo控件中mainform(this)。
Method:
Here you hvae to change the width and hight size and the most important to add the "pbBala" in to the "PicFondo" control insted of mainform (this).
public void AgregarBala(PictureBox pbPadre)
{
Array.Resize(ref pbBala, indice_Bala + 2);
pbBala[indice_Bala] = new PictureBox();
pbBala[indice_Bala].Visible = true;
pbBala[indice_Bala].Image = Image.FromFile(Application.StartupPath + "\\Cohete.jpg");
pbBala[indice_Bala].Height = 75;
pbBala[indice_Bala].Width = 10;
pbBala[indice_Bala].Top = pbPadre.Top + (pbPadre.Height / 2);
pbBala[indice_Bala].Left = pbPadre.Left + pbPadre.Width;
pbBala[indice_Bala].SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
PicFondo.Controls.Add(pbBala[indice_Bala]);
indice_Bala++;
}
第三个定时器:
更改
THird iN Timer:
Change
pbBala[i].Left= pbBala[i].Left + 20;
进入
pbBala[i].Top = pbBala[i].Top - 20;
private void playTimer(object sender, EventArgs e)
{
//player.Left += moveLeft;
Misil.Top -= bulletSpeed;
PicEne1.Top += enemyMove;
PicEne2.Top += enemyMove;
PicEne3.Top += enemyMove;
PicEne4.Top += enemyMove;
PicEne5.Top += enemyMove;
LblPuntos.Text = "" + score;
for (int i = 0; i < indice_Bala; i++)
{
pbBala[i].Top = pbBala[i].Top - 20;
}
if (PicEne1.Top == 660 || PicEne2.Top == 660 || PicEne3.Top == 660 || PicEne4.Top == 660 || PicEne5.Top == 660)
{
gameOver();
}
if (shooting && Misil.Top < 0)
{
shooting = false;
for (int i = 1; i <= 50; i++)
bulletSpeed = 0;
//Misil.Top = -100;
//Misil.Left = -100;
}
enemyHit();
}
AND最后IN
AND Finally IN
private void playTimer(object sender, EventArgs e)
使用PicEne.Location和picNav.Location玩游戏的方法
Method for Game over Play with PicEne.Location and picNav.Location insted of this
if (PicEne1.Top == 660 || PicEne2.Top == 660 || PicEne3.Top == 660 || PicEne4.Top == 660 || PicEne5.Top == 660)
{
gameOver();
}
希望这会对你有帮助,如果是这样 。请注明接受解决方案
Hope This will help you, IF SO . Kindly mark "Accept Solution"
您好,谢谢你的回答。还有一个问题,这个代码会阻止它最终如何?
当AgregarBala(PicNave)与任何PicEnemy相交时,我的分数会增加。
这是代码块:
Hello thanks for you answer. A question more, this code block how would will it finally be?
When "AgregarBala(PicNave)" IntersectsWith any PicEnemy then my score then increment.
This is code block:
//Funcion Enemigos
private void enemyHit()
{
if (Misil.Bounds.IntersectsWith(PicEne1.Bounds))
{
score += 1;
PicEne1.Top = -500;
int ranP = rnd.Next(1, 300);
PicEne1.Left = ranP;
shooting = false;
bulletSpeed = 0;
}
else if (Misil.Bounds.IntersectsWith(PicEne2.Bounds))
{
score += 1;
PicEne2.Top = -900;
int ranP = rnd.Next(1, 400);
PicEne2.Left = ranP;
shooting = false;
bulletSpeed = 0;
}
else if (Misil.Bounds.IntersectsWith(PicEne3.Bounds))
{
score += 1;
PicEne3.Top = -1300;
int ranP = rnd.Next(1, 500);
PicEne3.Left = ranP;
shooting = false;
bulletSpeed = 0;
}
else if (Misil.Bounds.IntersectsWith(PicEne4.Bounds))
{
score += 1;
PicEne3.Top = -1300;
int ranP = rnd.Next(1, 500);
PicEne4.Left = ranP;
shooting = false;
bulletSpeed = 0;
}
else if (Misil.Bounds.IntersectsWith(PicEne5.Bounds))
{
score += 1;
PicEne5.Top = -1300;
int ranP = rnd.Next(1, 500);
PicEne5.Left = ranP;
shooting = false;
bulletSpeed = 0;
}
}
提前感谢您。
DS
Thanks for you in advance.
DS
这篇关于移动图片框不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!