射击游戏帮助

时间:2011-08-25 14:17:49

标签: c# winforms timer picturebox

我正在制作像太空入侵者这样的射击游戏。每次我发射导弹时,它总是在同一个位置上。我将如何根据飞船的位置进行更改。

这是我现在的代码。

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);
        }
    }  
 }
 }

2 个答案:

答案 0 :(得分:4)

你可能想要像

这样的东西
pb2.Location.X = pb.Location.X;
pb2.Location.Y = pb.Location.Y;
在你的f_KeyDown()函数中

,以便导弹在与宇宙飞船相同的位置启动。

答案 1 :(得分:2)

你必须放置你的子弹,火箭......等等。相对于太空船的枪。

想象一下安装在船上的枪。你可以用一个物体代表这支枪。

例如:

public class Gun
{
    private ISpaceshipDesign _spaceshipDesign;

    public Gun(ISpaceshipDesign spaceshipDesign)
    {
        this._spaceshipDesign = spaceshipDesign;
    }

    public void Fire()
    {
        //...
    }
}

在制造喷枪时,请参考您的宇宙飞船,以便知道喷枪安装在哪艘宇宙飞船上。

宇宙飞船应该始终知道它在2D平面中的位置(X,Ycoördinates)。它还应该知道枪在飞船上的位置。

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's Fire()方法访问此数据。

例如:

public void Fire()
{
    Point gunLocation = _spaceshipDesign.GunLocation;

    // Position your missle using the gun's current coördinates (X, Y).     
}

大约一年前,我写了一篇关于在Silverlight中创建类似游戏(小行星)的10个系列文章。一篇文章讨论了如何使枪射击。你可以在这里找到它:

http://cgeers.com/2010/05/07/silverlight-asteroids-part-6-fire/

你可以选择在船上安装几支枪,一支发射常规子弹,另一支发射导弹......等等。每把枪在船上都有不同的位置。您可以将Fire()方法更改为由不同的键触发(A = missle,space = bullets)。

希望这有帮助。