2016-11-10 63 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

它确实有帮助!谢谢,我只是没有事情来定义方向!我没有考虑到足够广泛。再次感谢 –