开发者

Shooting game Help

开发者 https://www.devze.com 2023-03-31 05:55 出处:网络
I\'m making a shooting game like Space Invaders. Every time I launched the missile, it\'s always on the same position. How will I change it depending on the place where the spaceship is.

I'm making a shooting game like Space Invaders. Every time I launched the missile, it's always on the same position. How will I change it depending on the place where the spaceship is.

Here's my codes for now.

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


You probably want something like

pb2.Location.X = pb.Location.X;
pb2.Location.Y = pb.Location.Y;

in your f_KeyDown() function, so that the missile starts in the same location as the spaceship.


You have to position your bullets, rockets...etc. relative to your space ship's gun.

Imagine a gun that is mounted on the ship. You could represent this gun with an object.

For example:

public class Gun
{
    private ISpaceshipDesign _spaceshipDesign;

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

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

Pass in a reference to your spaceship when creating the gun, so that you know onto which spaceship the gun is mounted.

The spaceship should always know where it is in the 2D-plane (X, Y coördinates). It should also know where on the spaceship the gun is mounted.

public interface ISpaceshipDesign
{
    public Point GunLocation { get; }
}

The GunLocation property must return the gun's location relative to the ship's current position. For example:

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

You can then access this data in the Gun's Fire() method.

For example:

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

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

About a year ago I wrote a 10-series part about creating a similar game (Asteroids) in Silverlight. One article discusses how to make the gun fire. You can find it here:

https://github.com/geersch/SilverlightAsteroids/blob/master/src/part-6/README.md

You can choose to mount several guns on the ship, one that fires regular bullets, another one for missles...etc. Each gun would have a different location on the ship. You can alter the Fire() method to be triggered by different keys (A = missle, space = bullets).

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消