A Brown A Brown - 24 days ago 14
C# Question

Moving Smiling Face C#

Below I've created a program using C# that creates a smiley face. It also moves across the screen. I cannot figure out how to get the smiley face to bounce off the edges and around the screen. Please Help. Thank you.

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

Answer

Well, I was a bit bored. I'll assume that the object is going to move in a 45 degrees trajectory,and that when it collides with the bounds it would change by 90ยบ.

What I would do (this is a very simple solution) is, first of all, define the direction in both axes in which i want the "smiley" to move,the step in each timer tick, the position of the center and the size of the object, something like:

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

The timer would just increase the x and y positions:

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

The checkBounds method check if the object collides with the bounds:

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

Finally, the Paint method is similar to yours, just adjusting some values:

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

This code could be highly improved, but this may help you think how it should be done. Hope it helps.

Comments