简体   繁体   English

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

[英]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. 我有一个迷宫游戏,它使用标签作为墙和.IntersectsWith来处理大肠杆菌。 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. 不用介意if()与break只是一个实验。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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