Form1.cs

namespace SpaceInvadersV3
{
public partial class Form1 : Form
{
    public bool isPressed;
    Shooter player;
    List<Missile> bullet;
    List<Enemy> pirate;
    Boundary bottom;
    Boundary top;
    Boundary left;
    Boundary right;



    public Form1()
    {
        InitializeComponent();

        player = new Shooter(450,460);
        bullet = new List<Missile>();
        pirate = new List<Enemy>();
        for (int i = 0; i < 10; i++)
        {
            Enemy temp = new Enemy();
            pirate.Add(temp);
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        player.Move();
        foreach (Missile b in bullet)
        {
            b.Move();
        }

        foreach (Enemy p in pirate)
        {
            p.Move();
        }

        pictureBox1.Invalidate();

        if (IsColliding(player, pirate) == true)
        {
            gameOver();
        }
    }


"pirate"中的错误表明它无法从'System.Collections.Generic.List<SpaceInvadersV3.Enemy>'转换为'SpaceInvadersV3.Enemy'我尝试将下面的'IsColliding'函数从(Enemy b)更改为(List<Enemy> b),但是然后它无法识别b.Bottom并说不包含“底部”的定义。

    // Keybinds
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.A)
        {
            player.goleft = true;
        }

        if (e.KeyCode == Keys.D)
        {
            player.goright = true;
        }

        if (e.KeyCode == Keys.W)
        {
            player.goup = true;
        }

        if (e.KeyCode == Keys.S)
        {
            player.godown = true;
        }

        if (e.KeyCode == Keys.Space)
        {
            Missile temp = new Missile(player.x, player.y);
            bullet.Add(temp);
        }

    }

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.A)
        {
            player.goleft = false;
        }

        if (e.KeyCode == Keys.D)
        {
            player.goright = false;
        }

        if (e.KeyCode == Keys.W)
        {
            player.goup = false;
        }

        if (e.KeyCode == Keys.S)
        {
            player.godown = false;
        }

    }
        // keybinds

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        player.Draw(e.Graphics);

        foreach (Missile b in bullet)
        {
            b.Draw(e.Graphics);
        }

        foreach (Enemy p in pirate)
        {
            p.Draw(e.Graphics);
        }
    }

    private bool IsColliding(Shooter a, Enemy b)
    {
        bool colliding = true; // presume collision
        if (a.Top() > b.Bottom())
        {
            colliding = false;
        }
        return colliding;
    }

    private void gameOver()
    {
        timer1.Stop();
        MessageBox.Show("you died");
    }
}
}


Box.cs,其中敌人和射击类都继承自

using System.Drawing;

namespace SpaceInvadersV3
{
class Box
{
    public Image pic;
    public float x;
    public float y;
    public float speed;





    public Box()
    {
        x = 0;
        y = 0;
        speed = 0;
    }
    // Image Resizing Code
    public static Image resizeImage(Image imgToResize, Size size)
    {
        return (Image)(new Bitmap(imgToResize, size));
    }
    // image resizing code


    public void Draw(Graphics g)
    {
        g.DrawImage(pic, x, y);
    }

    public float Width()
    {
        return pic.Width;
    }

    public float Height()
    {
        return pic.Height;
    }

    public float Left()
    {
        return x;
    }

    public float Right()
    {
        return x + Width();
    }

    public float Top()
    {
        return y;
    }

    public float Bottom()
    {
        return y + Height();
    }



}
}


我不认为Shooter和Enemy类是否真的相关,但是如果您需要它们,我会发布它们。谢谢你的帮助。

最佳答案

if (IsColliding(player, pirate) == true)


首先,永远不要写。说“如果它们确实发生碰撞,这确实是业余的”。说“如果这些东西发生冲突”:

if (IsColliding(player, pirate))


同样,将if (!whatever)替换为if (whatever == false)

其次,请对集合使用复数名词。应该是pirates,而不是pirate。您想强调的是,读者可以将它们收集起来。


  “海盗”中的错误表明它无法从“列表”转换为“敌人”


您的IsColliding带了一个射手和一个敌人,但您却给了它一个射手和一个敌人列表。 IsColliding不知道该如何处理。

您已经知道如何修复它。您想移动每个敌人,所以您写道:

foreach (Enemy p in pirate)
{
    p.Move();
}


现在,您要检查每个敌人是否发生碰撞,因此请执行相同的操作:

foreach (Enemy p in pirate)
{
    if (IsColliding(player, p)) { ... }
}


您最终将学习的一种高级技术是对序列使用查询理解:

var collisions = from p in pirate
                 where IsColliding(player, p)
                 select p;
foreach (Enemy p in collisions)
{
  ... handle the collision...
}


但是,在尝试跑步之前,请先学会走路。

关于c# - C#如何获得两个对象之间的碰撞识别,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50258494/

10-10 01:31