移动微笑的脸C#

时间:2016-11-10 16:07:36

标签: 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();
        }
    }
}*/

1 个答案:

答案 0 :(得分:0)

嗯,我有点无聊。我假设物体将以45度的轨迹移动,当它与边界碰撞时,它会改变90度。

我会做什么(这是一个非常简单的解决方案)首先,在两个轴上定义我想要“笑脸”移动的方向,每个计时器滴答中的步骤,中心的位置和对象的大小,如:

Repo.delete_all/2

计时器只会增加x和y位置:

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

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

最后,Paint方法与您的方法类似,只是调整一些值:

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;
   }
}

此代码可以大大改进,但这可以帮助您思考应该如何完成。希望它有所帮助。