我在做一个类似太空入侵者的射击游戏。每次我发射导弹,它总是在同一位置。我如何根据宇宙飞船所在的位置来改变它呢?
这是我现在的代码。
class GraphicsApplication
{
private Form f;
private PictureBox pb;
private PictureBox pb1;
private PictureBox pb2;
private Boolean bMove;
Timer Clock = new Timer();
Timer Missile = new Timer();
int x = 0;
public GraphicsApplication()
{
f = new Form();
pb = new PictureBox();
pb1 = new PictureBox();
pb2 = new PictureBox();
bMove = false;
}
public void Launch()
{
f.Size = new Size(600, 600);
f.StartPosition = FormStartPosition.CenterScreen;
f.KeyDown += new KeyEventHandler(f_KeyDown);
f.KeyPress += new KeyPressEventHandler(f_KeyPress);
pb.SetBounds(300, 470, 70, 70);
pb.Image = new Bitmap("spaceship.png");
pb.SizeMode = PictureBoxSizeMode.StretchImage;
f.Controls.Add(pb);
pb1.Image = Image.FromFile("spacedisc.png");
pb1.SetBounds(20, 20, 130, 80);
pb1.SizeMode = PictureBoxSizeMode.StretchImage;
f.Controls.Add(pb1);
pb2.Image = Image.FromFile("missile.png");
pb2.SetBounds(pb.Location.X, pb.Location.Y, 25, 40); //pb2 missile //pb spaceship
pb2.SizeMode = PictureBoxSizeMode.StretchImage;
Clock = new Timer();
Clock.Interval = 40;
Clock.Start();
Clock.Tick += new EventHandler(Clock_Tick);
Missile = new Timer();
Missile.Interval = 40;
Missile.Tick += new EventHandler(Missile_Tick);
f.ShowDialog();
}
private void f_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
Missile.Start();
}
}
public void Missile_Tick(object sender, EventArgs e)
{
if (bMove == true)
{
f.Controls.Add(pb2);
pb2.Top = pb2.Top -= 5;
}
}
private void f_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 'd')
{
pb.Left = pb.Left += 5;
}
if (e.KeyChar == 'a')
{
pb.Left = pb.Left -= 5;
}
}
public void Clock_Tick(object sender, EventArgs e)
{
if(x == 400)
{
bMove = true;
}
else if (x == 30)
{
bMove = false;
}
if (bMove == false)
{
x += 5;
pb1.Location = new Point(20 + x, 20);
}
else
{
x -= 5;
pb1.Location = new Point(x - 20, 20);
}
}
}
}发布于 2011-08-25 22:21:46
你可能想要像这样的东西
pb2.Location.X = pb.Location.X;
pb2.Location.Y = pb.Location.Y;在f_KeyDown()函数中,以便导弹从与宇宙飞船相同的位置开始。
发布于 2011-08-25 22:27:39
你得把子弹放好,rockets...etc。相对于你的太空船的枪。
想象一下安装在船上的一把枪。你可以用一个物体来代表这把枪。
例如:
public class Gun
{
private ISpaceshipDesign _spaceshipDesign;
public Gun(ISpaceshipDesign spaceshipDesign)
{
this._spaceshipDesign = spaceshipDesign;
}
public void Fire()
{
//...
}
}在创建枪时传入对您的宇宙飞船的引用,以便您知道枪安装在哪艘宇宙飞船上。
宇宙飞船应该总是知道它在2D平面上的位置(X,Y坐标)。它还应该知道火炮安装在宇宙飞船上的什么位置。
public interface ISpaceshipDesign
{
public Point GunLocation { get; }
}GunLocation属性必须返回枪相对于舰船当前位置的位置。例如:
public Point GunLocation
{
get
{
double x = (double) this.GetValue(Canvas.LeftProperty) + 21;
double y = (double) this.GetValue(Canvas.TopProperty) + 17;
return new Point(x, y);
}
}然后,您可以在Gun的Fire()方法中访问此数据。
例如:
public void Fire()
{
Point gunLocation = _spaceshipDesign.GunLocation;
// Position your missle using the gun's current coördinates (X, Y).
}大约一年前,我写了一个关于在Silverlight中创建一个类似的游戏(小行星)的10系列文章。其中一篇文章讨论了如何让枪开火。你可以在这里找到它:
https://github.com/geersch/SilverlightAsteroids/blob/master/src/part-6/README.md
你可以选择在船上装几门枪,一门是普通的子弹,另一门是missles...etc的。每门枪在飞船上都有不同的位置。您可以更改Fire()方法,使其由不同的键触发(A = missle,space = bullets)。
希望这能有所帮助。
https://stackoverflow.com/questions/7191967
复制相似问题