var标签=这个.Controls.OfType<;标签>;();控制除1之外的所有标签

本文关键字:标签 控制 gt Controls 这个 OfType lt var | 更新日期: 2023-09-27 17:58:20

我有一个迷宫游戏,它使用标签作为墙和.IntersectsWith来处理共线。我的问题是,由于我的"播放器"也是一个标签,它会把我的代码搞砸。我想要的是球员能够移动,同时它也不能与所有其他标签共存。

真正的问题是,这一部分会让玩家由于某种原因无法移动。别介意,如果休息的话,那只是一个实验。

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

var标签=这个.Controls.OfType<;标签>;();控制除1之外的所有标签

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

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