-1

有人可以帮助我如何检测两个物体之间的碰撞。起初我认为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();
    }
}
4

1 回答 1

2

您可能有另一个命名空间包含一个名为Rectangle.

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

于 2013-04-28T11:39:26.967 回答