C#如何在两个对象之间识别冲突

时间:2022-01-20 21:30:28

Form1.cs

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

Error in "pirate" says that it cannot convert from 'System.Collections.Generic.List<SpaceInvadersV3.Enemy>' to 'SpaceInvadersV3.Enemy' I tried changing the 'IsColliding' function below from (Enemy b) to (List<Enemy> b) but then it doesn't recognize b.Bottom and says that List<Enemy> does not contain a definition for Bottom.

“盗版”中的错误表示它无法从'System.Collections.Generic.List '转换为'SpaceInvadersV3.Enemy'我尝试将下面的'IsColliding'功能从(Enemy b)更改为(List b)但是它不识别b.Bottom并且说List 不包含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 where both Enemy and Shooter classes inherit from

Box.cs,Enemy和Shooter类都继承自

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



}
}

I Don't think if Shooter and Enemy classes are really relevant, but if you need them, I'll post them. Thanks for your help.

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

1 个解决方案

#1


4  

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

First of all, never write that. It looks amateurish to say "if it is true that these are colliding". Say "if these things are colliding":

首先,永远不要写那个。看起来很“业余”,说“如果它们碰撞是真的”。说“如果这些事情发生碰撞”:

if (IsColliding(player, pirate))

Similarly, prefer if (!whatever) to if (whatever == false).

同样,喜欢if(!whatever)to if(whatever == false)。

Second, please use plural nouns for collections. That should be pirates, not pirate. You want to emphasize that there is a collection of them to the reader.

其次,请使用复数名词进行收藏。那应该是海盗,而不是海盗。你想强调它有一个集合给读者。

Error in "pirate" says that it cannot convert from 'List' to 'Enemy'

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

Your IsColliding takes a shooter and an enemy, but you are giving it a shooter and a list of enemies. IsColliding doesn't know how to deal with that.

你的IsColliding需要一个射手和一个敌人,但你给它一个射手和一个敌人的名单。 IsColliding不知道如何处理。

You already know how to fix it. You wanted to move every enemy so you wrote:

你已经知道如何解决它。你想移动每个敌人,所以你写道:

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

Now you want to check every enemy for collisions, so do the same thing:

现在你想检查每个敌人的碰撞,所以做同样的事情:

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

An advanced technique that you will eventually learn is to use query comprehensions on sequences:

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

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

But learn to walk before you try to run.

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

#1


4  

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

First of all, never write that. It looks amateurish to say "if it is true that these are colliding". Say "if these things are colliding":

首先,永远不要写那个。看起来很“业余”,说“如果它们碰撞是真的”。说“如果这些事情发生碰撞”:

if (IsColliding(player, pirate))

Similarly, prefer if (!whatever) to if (whatever == false).

同样,喜欢if(!whatever)to if(whatever == false)。

Second, please use plural nouns for collections. That should be pirates, not pirate. You want to emphasize that there is a collection of them to the reader.

其次,请使用复数名词进行收藏。那应该是海盗,而不是海盗。你想强调它有一个集合给读者。

Error in "pirate" says that it cannot convert from 'List' to 'Enemy'

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

Your IsColliding takes a shooter and an enemy, but you are giving it a shooter and a list of enemies. IsColliding doesn't know how to deal with that.

你的IsColliding需要一个射手和一个敌人,但你给它一个射手和一个敌人的名单。 IsColliding不知道如何处理。

You already know how to fix it. You wanted to move every enemy so you wrote:

你已经知道如何解决它。你想移动每个敌人,所以你写道:

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

Now you want to check every enemy for collisions, so do the same thing:

现在你想检查每个敌人的碰撞,所以做同样的事情:

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

An advanced technique that you will eventually learn is to use query comprehensions on sequences:

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

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

But learn to walk before you try to run.

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