射击游戏帮助

发布于 2024-12-01 04:18:19 字数 2694 浏览 4 评论 0原文

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

这是我现在的代码。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流心雨 2024-12-08 04:18:19

您可能需要类似

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

f_KeyDown() 函数的内容,以便导弹在与宇宙飞船相同的位置启动。

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.

欲拥i 2024-12-08 04:18:19

你必须放置你的子弹、火箭……等等。相对于你的太空飞船的枪。

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

例如:

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 中创建类似游戏(Asteroids)的 10 系列部分。一篇文章讨论了如何让枪开火。您可以在这里找到它:

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

你可以选择在船上安装几门枪,一门发射普通子弹,另一门发射普通子弹用于导弹...等每把枪在船上都有不同的位置。您可以更改 Fire() 方法,使其由不同的键触发(A = 导弹,空格 = 子弹)。

希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文