简体   繁体   中英

var labels = this.Controls.OfType<Label>(); Control all labels except 1

I have a maze game that uses labels as walls and .IntersectsWith to handle colision. My problem is that since my "player" is also a label it messes things up with my code. What i want is for the player to be able to move meanwhile it also cant colide with all the other labels.

What the problem really is is that this part makes the player unable to move for some reason. Never mind the if () with break it was just an experiment.

 var labels = this.Controls.OfType<Label>();

        foreach (var label in labels)
        {
            if (label.Bounds.IntersectsWith(player.Bounds))
            {
                break;
            }
            if (player.Bounds.IntersectsWith(label.Bounds))
            {

 namespace mazeGame
{
public partial class Form1 : Form
{
    bool down;
    bool left;
    bool right;
    bool up;

    public Form1()
    {
        InitializeComponent();
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {

    }

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {

    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Right)
        {
            right = true;
            left = false;
            up = false;
            down = false;
        }
        if (e.KeyCode == Keys.Left)
        {
            left = true;
            right = false;
            up = false;
            down = false;
        }
        if (e.KeyCode == Keys.Up)
        {
            up = true;
            left = false;
            right = false;
            down = false;
        }
        if (e.KeyCode == Keys.Down)
        {
            down = true;
            left = false;
            up = false;
            right = false;
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {


        var labels = this.Controls.OfType<Label>();

        foreach (var label in labels)
        {
            if (label.Bounds.IntersectsWith(player.Bounds))
            {
                break;
            }
            if (player.Bounds.IntersectsWith(label.Bounds))
            {

                if (right == true)
                {
                    right = false;
                    left = true;
                }
                else if (left == true)
                {
                    left = false;
                    right = true;
                }
                else if (up == true)
                {
                    up = false;
                    down = true;
                }
                else if (down == true)
                {
                    down = false;
                    up = true;
                }
            }



            if (right == true)
            {
                player.Left += 1;
            }
            if (left == true)
            {
                player.Left -= 1;
            }
            if (up == true)
            {
                player.Top -= 1;
            }
            if (down == true)
            {
                player.Top += 1;
            }


        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void panel2_Paint(object sender, PaintEventArgs e)
    {

    }
}
}

您可以通过以下方式将player标签从选择中排除:

var labels = this.Controls.OfType<Label>().Where(l => l.Name != "Player")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM