var labels = this.Controls.OfType <label>();控制除1 </label>之外的所有标签

时间:2014-04-14 16:51:13

标签: c# label var

我有一个迷宫游戏,使用标签作为墙壁和.IntersectsWith来处理colision。我的问题是,因为我的&#34;播放器&#34;也是一个标签,它与我的代码混淆了。 我想要的是让玩家能够移动同时它也不能与所有其他标签相媲美。

问题的真正原因在于这部分让玩家因某种原因无法移动。别介意if()中断它只是一个实验。

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

    }
}
}

1 个答案:

答案 0 :(得分:1)

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

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