2013-04-28 4 views
-1

두 객체 간의 충돌을 어떻게 감지 할 수 있는지 누군가가 도와 줄 수 있습니까? 처음에는 IntersectsWith이 도움이 될 것이라고 생각했지만 솔루션에 IntersectsWith의 정의가 포함되어 있지 않다는 오류가 표시됩니다.C에서 교차로 사용 #

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(); 
    } 
} 
+0

'Rectangle.IntersectsWith()'가 작동해야합니다. 귀하의 프로젝트가 확실히'System.Drawing'을 참조합니까? –

+0

나는 당신이'public int X {get; 개인 집합; }' –

+0

public int X는 어떻게 사용합니까? – qobacha

답변

2

당신은 다른 네임 스페이스가있을 수는 Rectangle라는 클래스가 포함되어 여기에 나에게 오류를 제공 내 코드입니다.

Rectangle의 매개 변수 선언을 System.Drawing.Rectangle으로 해보십시오.