当发生碰撞时,我该如何阻止玩家朝某个方向移动?

本文关键字:玩家 方向 移动 何阻止 碰撞 | 更新日期: 2023-09-27 18:07:03

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Game1
{
    public partial class Form1 : Form
    {
        Graphics g;
        Rectangle Player;
        Rectangle Enemy;
        Boolean left;
        Boolean right;
        Boolean up;
        Boolean down;
        int Playerx, Playery, Playerw, Playerh;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Playerx = 0;
            Playery = 0;
            Playerw = 32;
            Playerh = 32;
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            g = e.Graphics;
            //Draw Player
            Player = new Rectangle(Playerx, Playery, Playerw, Playerh);
            g.FillRectangle(Brushes.Blue, Player);
            //Draw Enemy
            Enemy = new Rectangle(100, 100, 32, 32);
            g.FillRectangle(Brushes.Red, Enemy);
            if (Player.IntersectsWith(Enemy.Left))
            {
                left = false;
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (left == true)
            {
                Playerx -= 5;
            }
            if (right == true)
            {
                Playerx += 5;
            }
            if (up == true)
            {
                Playery -= 5;
            }
            if (down == true)
            {
                Playery += 5;
            }
            Invalidate();
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.A)
            {
                left = true;
                right = false;
                up = false;
                down = false;
            }
            if (e.KeyCode == Keys.D)
            {
                right = true;
                left = false;
                up = false;
                down = false;
            }
            if (e.KeyCode == Keys.W)
            {
                up = true;
                down = false;
                left = false;
                right = false;
            }
            if (e.KeyCode == Keys.S)
            {
                down = true;
                up = false;
                left = false;
                right = false;
            }
        }
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
        }
        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.A)
            {
                left = false;
            }
            if (e.KeyCode == Keys.D)
            {
                right = false;
            }
            if (e.KeyCode == Keys.W)
            {
                up = false;
            }
            if (e.KeyCode == Keys.S)
            {
                down = false;
            }
        }
    }
}

我想让玩家在碰到敌人的左侧时停止移动,但我总是得到一个错误,并且它突出显示if (Player.IntersectsWith(Enemy.Left))Can't convert int into System.Drawing.Rectangle有什么想法吗?我试着看看这是否有效

if (Player.IntersectsWith(Enemy)) 
{
    left = false; 
    right = false; 
    up = false; 
    down = false;
}

,当然它会保持播放器的位置

当发生碰撞时,我该如何阻止玩家朝某个方向移动?

您似乎想要一个特定的碰撞。

你应该试试:

if(Player.X + Player.Width >= Enemy.X && Player.X < Enemy.X)
{
    right = false;
}

这样你就可以检查玩家的右侧是否与敌人的左侧相交,并且玩家的左侧位置是否小于敌人的左侧位置(玩家在敌人的左侧)

你将不得不添加高度和碰撞的东西,使它,所以你可以通过敌人,但我认为你已经张贴它应该工作ok。

注意:代码是未经测试的,让我知道你如何。