Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 射击游戏帮助_C#_Winforms_Timer_Picturebox - Fatal编程技术网

C# 射击游戏帮助

C# 射击游戏帮助,c#,winforms,timer,picturebox,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

我正在做一个像太空入侵者一样的射击游戏。每次我发射导弹,它总是在同一个位置。我将如何改变它取决于太空船的位置

这是我现在的密码

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

你可能想要像这样的东西

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

在f_KeyDown()函数中,使导弹在与宇宙飞船相同的位置启动。

您可能希望

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

在f_KeyDown()函数中,使导弹在与宇宙飞船相同的位置启动。

您必须定位子弹、火箭等。相对于你的太空船的枪

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

例如:

public class Gun
{
    private ISpaceshipDesign _spaceshipDesign;

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

    public void Fire()
    {
        //...
    }
}
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);
    }
}
public void Fire()
{
    Point gunLocation = _spaceshipDesign.GunLocation;

    // Position your missle using the gun's current coördinates (X, Y).     
}
创建枪时,请输入对您的宇宙飞船的引用,以便您知道枪安装在哪个宇宙飞船上

public interface ISpaceshipDesign
{
    public Point GunLocation { get; }
}
宇宙飞船应该知道它在二维平面上的位置(X,Y坐标)。它还应该知道在宇宙飞船上枪的安装位置

public interface ISpaceshipDesign
{
    public Point GunLocation { get; }
}
GunLocation属性必须返回枪相对于船舶当前位置的位置。例如:

public class Gun
{
    private ISpaceshipDesign _spaceshipDesign;

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

    public void Fire()
    {
        //...
    }
}
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);
    }
}
public void Fire()
{
    Point gunLocation = _spaceshipDesign.GunLocation;

    // Position your missle using the gun's current coördinates (X, Y).     
}
然后可以在Gun的Fire()方法中访问这些数据

例如:

public class Gun
{
    private ISpaceshipDesign _spaceshipDesign;

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

    public void Fire()
    {
        //...
    }
}
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);
    }
}
public void Fire()
{
    Point gunLocation = _spaceshipDesign.GunLocation;

    // Position your missle using the gun's current coördinates (X, Y).     
}
大约一年前,我写了一篇关于在Silverlight中创建一个类似游戏(小行星)的10系列文章。一篇文章讨论了如何使枪开火。你可以在这里找到它:

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


希望这能有所帮助。

你必须确定子弹、火箭等的位置。相对于你的太空船的枪

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

例如:

public class Gun
{
    private ISpaceshipDesign _spaceshipDesign;

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

    public void Fire()
    {
        //...
    }
}
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);
    }
}
public void Fire()
{
    Point gunLocation = _spaceshipDesign.GunLocation;

    // Position your missle using the gun's current coördinates (X, Y).     
}
创建枪时,请输入对您的宇宙飞船的引用,以便您知道枪安装在哪个宇宙飞船上

public interface ISpaceshipDesign
{
    public Point GunLocation { get; }
}
宇宙飞船应该知道它在二维平面上的位置(X,Y坐标)。它还应该知道在宇宙飞船上枪的安装位置

public interface ISpaceshipDesign
{
    public Point GunLocation { get; }
}
GunLocation属性必须返回枪相对于船舶当前位置的位置。例如:

public class Gun
{
    private ISpaceshipDesign _spaceshipDesign;

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

    public void Fire()
    {
        //...
    }
}
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);
    }
}
public void Fire()
{
    Point gunLocation = _spaceshipDesign.GunLocation;

    // Position your missle using the gun's current coördinates (X, Y).     
}
然后可以在Gun的Fire()方法中访问这些数据

例如:

public class Gun
{
    private ISpaceshipDesign _spaceshipDesign;

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

    public void Fire()
    {
        //...
    }
}
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);
    }
}
public void Fire()
{
    Point gunLocation = _spaceshipDesign.GunLocation;

    // Position your missle using the gun's current coördinates (X, Y).     
}
大约一年前,我写了一篇关于在Silverlight中创建一个类似游戏(小行星)的10系列文章。一篇文章讨论了如何使枪开火。你可以在这里找到它:

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


希望这有帮助。

您可以缩短
pb.Left=pb.Left-=5
pb.Left-=5pb
是宇宙飞船,
pb2
是导弹。什么是f和pb1?@Mike
f
表单(见构造函数),
pb1
可能是敌舰,你可以缩短
pb.Left=pb.Left-=5
pb.Left-=5pb
是宇宙飞船,
pb2
是导弹。什么是f和pb1?@Mike
f
表单(参见构造函数),
pb1
可能是某艘敌舰我尝试了代码,但出现了错误。无法修改Windows.Forms.Location的返回值,因为它不是变量。请创建一个新的
对象,并将其分配给
pb2.Location属性
。它现在位于正确的位置。我把pb2.Location=新点(pb.Location.X,pb.Location.Y);我尝试了代码,但有一个错误。无法修改Windows.Forms.Location的返回值,因为它不是变量。请创建一个新的
对象,并将其分配给
pb2.Location属性
。它现在位于正确的位置。我把pb2.Location=新点(pb.Location.X,pb.Location.Y);