2014-03-26 5 views
1

안녕하세요 저는이 작품을 처음 접했습니다! Rectangle과 Circle을 성공적으로 만들었습니다. 내 화살표 키 (왼쪽 & 오른쪽)를 사용하여 양식 및 직사각형에서 원을 움직입니다. 문제는 그냥 그래서 내가 인쇄 할 수 있습니다이 둘 사이의 충돌을 감지 할 수있는 방법을 알 필요가있다 :직사각형과 타원 사이의 충돌 감지

Console.WriteLine("COLLISION OCCURS!"); 

I 만 원과 사각형의 검출을 할 수 있습니다. 내 원이 Y 축에서 벗어나면 메시지 (CIRCLE WENT OF OF BOUNDS)가 나타납니다! 내가 시도한 것은 :

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Paddle_Test 
{ 
    public partial class Form1 : Form 
    { 
     Rectangle rec; 
     Rectangle circle; 
     int wLoc=0; 
      int hLoc=0; 
      int eWL; 
      int eHL; 
      int dx = 4; 
      int dy = 4; 

     public Form1() 
     { 
      InitializeComponent(); 
      wLoc=(this.Width) - 100; 
      hLoc=(this.Height) - 100; 
      eWL = 10; 
      eHL = 10; 
      rec = new Rectangle(wLoc,hLoc , 60, 10); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      this.Refresh(); 
     } 

     private void Form1_Paint(object sender, PaintEventArgs e) 
     { 
      Graphics g = e.Graphics; 

      g.FillRectangle(new SolidBrush(Color.Blue), rec); 
      g.FillEllipse(new SolidBrush(Color.Red), eWL, eHL, 40, 40); 

     } 

     private void Form1_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.KeyCode==Keys.Left) 
      { 
       rec.X -= 30; 
       this.Refresh(); 
      } 

      if (e.KeyCode==Keys.Right) 
      { 
       rec.X += 30; 
       this.Refresh(); 
      } 
     } 

     private void timer_Tick(object sender, EventArgs e) 
     { 
      eWL = eWL + dx; 
      eHL = eHL + dy; 

      if (eWL >= (this.Width) - 100) 
      { 
       dx = dx * (-1); 
      } 
      else if (eHL >= (this.Height) - 100) 
      { 
       dy = dy * (-1); 
       //timer.Enabled = false; 
       //MessageBox.Show("Game Over!"); 
      } 
      else if (eWL<=0) 
      { 
       dx = dx * (-1); 
      } 
      else if (eHL <= 0) 
      { 
       dy = dy * (-1); 
      } 
      else if (eWL == rec.X || eHL == rec.Y) //here I'm trying to detect the collision! 
      { 
       dx = dx * (-1);//invert the direction x-axis 
       dy = dy * (-1);//invert the direction y-axis 


      } 
      this.Refresh(); 
     } 
    } 
} 

누군가 나를 도와주세요! 미리 감사드립니다.

답변

1

이것은 내가 얻은 대답입니다!

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Paddle_Test 
{ 
    public partial class Form1 : Form 
    { 
     Rectangle rec; 
     Rectangle circle; 
     int wLoc=0; 
      int hLoc=0; 
      int eWL; 
      int eHL; 
      int dx = 2; 
      int dy = 2; 

     public Form1() 
     { 
      InitializeComponent(); 
      wLoc=(this.Width) - 100; 
      hLoc=(this.Height) - 100; 
      eWL = 10; 
      eHL = 10; 
      rec = new Rectangle(wLoc,hLoc , 100, 10); 
      circle = new Rectangle(eWL, eHL, 40, 40); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      this.Refresh(); 
     } 

     private void Form1_Paint(object sender, PaintEventArgs e) 
     { 
      Graphics g = e.Graphics; 

      g.FillRectangle(new SolidBrush(Color.Blue), rec); 
      g.FillEllipse(new SolidBrush(Color.Red), circle); 

     } 

     private void Form1_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.KeyCode==Keys.Left) 
      { 
       rec.X -= 30; 
       this.Refresh(); 
      } 

      if (e.KeyCode==Keys.Right) 
      { 
       rec.X += 30; 
       this.Refresh(); 
      } 
     } 

     int count=0; 
     private void timer_Tick(object sender, EventArgs e) 
     { 

      circle.X += dx; 
      circle.Y += dy; 
      count += 1; 

      if (circle.X >= (this.Width) - 100) 
      { 
       dx = dx * (-1); 
      } 
      else if (circle.Y >= (this.Height)) 
      { 
       //dy = dy * (-1); 
       timer.Enabled = false; 
       MessageBox.Show("Game Over!"); 
      } 
      else if (circle.X <= 0) 
      { 
       dx = dx * (-1); 
      } 
      else if (circle.Y <= 0) 
      { 
       dy = dy * (-1); 
      } 

      else if (rec.IntersectsWith(circle))//detects collision! 
      { 
       //dx = dx * (-1); 
       dy = dy * (-1); 

       Console.WriteLine("hello"); 
      } 

      this.Refresh(); 
     } 
    } 
}