矩形和图片框的碰撞

时间:2018-09-17 16:51:33

标签: c#

我能问一下什么是检测碰撞并防止玩家穿过障碍物的正确方法。我试着玩代码,但实际上它只能在那个块中起作用,但是当我放上它并尝试访问正确的键时,它将不允许我移动,我还可以问一下如果我有很多物体怎么办我将所有这些碰撞逻辑应用于游戏中的所有对象

Looks

namespace CollisionMovement
{
    public partial class Game : Form
    {

        private int _x, _y;

        public Game()
        {
            _x = 300;
            _y = 177;
            InitializeComponent();

        }

        private void Movementtimer_Tick(object sender, EventArgs e)
        {
            Invalidate();
        }

        private void Game_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.D)
            {
                if(_x == Block.Bounds.X - 48)
                {
                    _x = Block.Bounds.X - 48;
                }
                else
                {
                    _x += 48;
                }
            }
            else if (e.KeyCode == Keys.A)
            {
                _x -= 48;
            }
            else if (e.KeyCode == Keys.W)
            {
                _y -= 48;  
            }
            else if (e.KeyCode == Keys.S)
            {
                _y += 48; 
            }
        }

        private void Game_Paint(object sender, PaintEventArgs _Char)
        {
            _Char.Graphics.FillRectangle(Brushes.Black, _x, _y, 48,48);
        }
    }
}

0 个答案:

没有答案