在c#中使用IntersectsWith

本文关键字:IntersectsWith | 更新日期: 2023-09-27 18:11:25

有人可以帮助我如何检测两个对象之间的碰撞。起初我认为IntersectsWith会有所帮助,但我得到的错误说我的解决方案不包含IntersectsWith的定义。下面是我的代码,给我的错误:

class Car {
    private int _x, _y, _width, _height, _xvel;
    public Car(Random r, int y, int width, int height, int xvel) {
        this._x=r.Next(500)+20; // this._x = x;
        this._y=y;
        this._width=width;
        this._height=height;
        this._xvel=xvel;
    }
    public int X {
        get {
            return _x;
        }
    }
    public int Y {
        get {
            return _y;
        }
    }
    public int Width {
        get {
            return _width;
        }
    }
    public int Height {
        get {
            return _height;
        }
    }
    public int Xvel {
        get {
            return _xvel;
        }
    }
    public void DrawCar(Graphics g) {
        g.DrawRectangle(Pens.Blue, new Rectangle(X, Y, Width, Height));
    }
    public void MoveCar(int gamewidth) {
        if(_x+_width>=gamewidth) {
            _x=0;
        }
        _x=_x+_xvel;
    }
}
class Player {
    private int _x, _y, _width, _height, _xvel, _yvel;

    public Player(int x, int y, int width, int height, int xvel, int yvel) {
        this._x=x;
        this._y=y;
        this._width=width;
        this._height=height;
        this._xvel=xvel;
        this._yvel=yvel;
    }
    public int X {
        get {
            return _x;
        }
    }
    public int Y {
        get {
            return _y;
        }
    }
    public int Width {
        get {
            return _width;
        }
    }
    public int Height {
        get {
            return _height;
        }
    }
    public int Xvel {
        get {
            return _xvel;
        }
    }
    public int Yvel {
        get {
            return _yvel;
        }
    }
    public void DrawPlayer(Graphics g) {
        g.DrawRectangle(Pens.Red, new Rectangle(X, Y, Width, Height));
    }
    public void CollisionDetection(Rectangle player, Rectangle car) {
        if(player.IntersectsWith(car)) {
            MessageBox.Show("You Lose!");
        }
    }
    public void MovePlayerLeft(int gamewidth) {
        if(_x>0) {
            _x-=_xvel;
        }
    }
}
public partial class Form1: Form {
    Car cars;
    Player player;
    public Form1() {
        InitializeComponent();
        player=new Player(((Width/2)-15), (Height-75), 30, 30, 10, 10);
        Random r=new Random();
        cars=new Car(r, 200, 30, 30, 10);
    }
    private void Form1_Paint(object sender, PaintEventArgs e) {
        Graphics g=e.Graphics;
        g.Clear(Color.White);
        cars.DrawCar(g);
        player.DrawPlayer(g);
        player.CollisionDetection(player, cars); // This is the part that implements collision detection
    }
    private void timer1_Tick(object sender, EventArgs e) {
        cars.MoveCar(this.Width);
        this.Invalidate();
    }
}

在c#中使用IntersectsWith

您可能有另一个名称空间包含名为Rectangle的类。

尝试将Rectangle的参数声明为System.Drawing.Rectangle