2016-11-10 107 views
-4

下面我創建了一個使用C#創建笑臉的程序。它也在屏幕上移動。我無法弄清楚如何讓笑臉在屏幕邊緣和周圍反彈。請幫忙。謝謝。移動笑臉C#

*/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 HappyFace 
{ 
    public partial class HappyFace : Form 
    { 
     int xpos = 0; 
     int ypos = 0; 
     int width = 0; 
     int length = 0; 
     int startAngle = 45; 
     int sweepAngle = 90; 

     public HappyFace() 
     { 
      InitializeComponent(); 
     } 

     private void HappyFace_Load(object sender, EventArgs e) 
     { 

     } 

     private void HappyFace_Paint(object sender, PaintEventArgs e) 
     { 
      Graphics g = e.Graphics; 
      Pen myPen = new Pen(Brushes.Red, 7); 
      Pen myPen2 = new Pen(Brushes.Green, 7); 
      //g.DrawLine(myPen, 0, 0, 500, 500); 
      //g.DrawLine(myPen, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height); 
      //g.DrawLine(myPen2, 0, this.ClientRectangle.Height, this.ClientRectangle.Width, 0); 
      //g.DrawLine(myPen2, this.ClientRectangle.Left, this.ClientRectangle.Bottom, this.ClientRectangle.Right, ClientRectangle.Top); 

      int endX = this.ClientRectangle.Width; 
      int endY = this.ClientRectangle.Height; 

      //string msg = String.Format("endX = {0} endY = {1}", endX, endY); 
      //MessageBox.Show(msg); 

      int xCenter = this.ClientRectangle.Left + (this.ClientRectangle.Width/2); 
      int yCenter = this.ClientRectangle.Top + (this.ClientRectangle.Height/2); 

      Pen circlePen = new Pen(Brushes.Black, 9); 

      //g.DrawEllipse(circlePen, xCenter - 50, yCenter - 50, 100, 100); 
      // g.FillEllipse(Brushes.Orange, xCenter -50, yCenter - 50, 100, 100); 

      Font myFont = new Font("Monotype Corsiva", 43, FontStyle.Bold); 
      g.DrawString("Happy Face", myFont, Brushes.Aqua, 300, 25); 

      //g.DrawArc(circlePen, xpos, width, length, startAngle, sweepAngle); 

      g.DrawEllipse(circlePen, xpos, ypos + 130, 250, 250); 
      g.FillEllipse(Brushes.PeachPuff, xpos, ypos + 130, 250, 250); 
      g.DrawEllipse(circlePen, xpos + 65, ypos + 200, 20, 35); 
      g.FillEllipse(Brushes.Black, xpos + 65, ypos + 200, 20, 35); 
      g.DrawEllipse(circlePen, xpos + 160, ypos + 200, 20, 35); 
      g.FillEllipse(Brushes.Black, xpos + 160, ypos + 200, 20, 35); 
      g.DrawArc(circlePen, xpos + 60, ypos + 215, 130, 120, 35, 115); 

     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      xpos = xpos + 3; 
      if(xpos >= this.ClientRectangle.Right - 250) 
      { 
       xpos = 0; 
      } 
      this.Invalidate(); 
     } 
    } 
}*/ 
+3

只要檢查邊緣是否碰到牆壁,改變方向。 – itsme86

+0

它從屏幕上消失並回到另一邊。我該如何解決? –

+0

你認爲你會如何/在哪裏解決它? – itsme86

回答

0

嗯,我有點無聊。我會假定物體將以45度的軌跡移動,當它與邊界碰撞時,它會改變90度。

我會做什麼(這是一個非常簡單的解決方案)首先是定義兩個座標軸中的方向,我希望「笑臉」移動,每個計時器中的步驟刻度,中心與物體的大小,是這樣的:

int xpos = 0; 
int ypos = 130; 
int step = 10; 
int width = 250; 
int height = 250; 
int directionX = +1; 
int directionY = -1; 

計時器只會增加x和y的位置:

private void timer1_Tick(object sender, EventArgs e) 
{ 
    xpos += 10*directionX; 
    ypos += 10*directionY; 
    checkBounds(); //This would check if the object collides with the bounds 
    this.Invalidate(); 
} 

如果對象與邊界碰撞的checkBounds方法檢查:

private void checkBounds() 
{ 
    if (ypos < 0 + step || ypos + height+ step > ClientRectangle.Height) 
    { 
     directionY *= -1; 
    } 

    if (xpos < 0 + step || xpos + width + step > ClientRectangle.Width) 
    { 
     directionX *= -1; 
    } 
} 

最後,Paint方法與你相似,只是調整一些值:

private void Form2_Paint(object sender, PaintEventArgs e) 
{ 
    Graphics g = e.Graphics; 
    Pen myPen = new Pen(Brushes.Red, 7); 
    Pen myPen2 = new Pen(Brushes.Green, 7); 


    int endX = this.ClientRectangle.Width; 
    int endY = this.ClientRectangle.Height; 


    int xCenter = this.ClientRectangle.Left + (this.ClientRectangle.Width/2); 
    int yCenter = this.ClientRectangle.Top + (this.ClientRectangle.Height/2); 


    Pen circlePen = new Pen(Brushes.Black, 9); 

    Font myFont = new Font("Monotype Corsiva", 43, FontStyle.Bold); 
    g.DrawString("Happy Face", myFont, Brushes.Aqua, 300, 25); 

    g.DrawEllipse(circlePen, xpos, ypos, 250, 250); 
    g.FillEllipse(Brushes.PeachPuff, xpos, ypos, 250, 250); 
    g.DrawEllipse(circlePen, xpos + 65, ypos -130 + 200, 20, 35); 
    g.FillEllipse(Brushes.Black, xpos + 65, ypos-130 + 200, 20, 35); 
    g.DrawEllipse(circlePen, xpos + 160, ypos-130 + 200, 20, 35); 
    g.FillEllipse(Brushes.Black, xpos + 160, ypos-130 + 200, 20, 35); 
    g.DrawArc(circlePen, xpos + 60, ypos-130 + 215, 130, 120, 35, 115); 
} 

這個代碼可以大大提高,但是這可能會幫助你認爲應該怎麼做。希望能幫助到你。

+0

它確實有幫助!謝謝,我只是沒有事情來定義方向!我沒有考慮到足夠廣泛。再次感謝 –