Bouncing Ball in C#

In this tutorial, I will teach you how to create a program that has the animation of a bouncing ball in C#. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add only a timer named Timer1. You must design your interface like this: design 3. In your View Code, put this Global Variable for initializing the Ball image and the timer.
  1.                 private int ball_Size = 8; //FRACTION OF BALL SIZE
  2.                 private int move_Size = 4; //FRACTION OF CLIENT AREA
  3.                 private Bitmap btmp;
  4.                 private int ball_PositionX;
  5.                 private int ball_PositionY;
  6.                 private int ball_RadiusX;
  7.                 private int ball_RadiusY;
  8.                 private int ball_MoveX;
  9.                 private int ball_MoveY;
  10.                 private int ball_BitmapWidth;
  11.                 private int ball_BitmapHeight;
  12.                 private int bitmap_WidthMargin;
  13.                 private int bitmap_HeightMargin;
  14.                
4. In your Timer1_Tick, put this code below as this will be used for the movement of the ball.
  1.         public void Timer1_Tick(System.Object sender, System.EventArgs e)
  2.                 {
  3.                        
  4.                         //DECLARE A VARIABLE TO OBTAIN THE GRAPHICS OBJECT.
  5.                         Graphics grafx = CreateGraphics();
  6.                         //DRAW THE BALL IN THE FORM.
  7.                         grafx.DrawImage(btmp, (int) (ball_PositionX - ball_BitmapWidth / 2), (int) (ball_PositionY - ball_BitmapHeight / 2), ball_BitmapWidth, ball_BitmapHeight);
  8.                        
  9.                         grafx.Dispose();
  10.                        
  11.                         //INCREAMENT THE POSITION OF THE BALL BY ITS DISTANCE TO MOVED BOTH X AND Y AXIS.
  12.                         ball_PositionX += ball_MoveX;
  13.                         ball_PositionY += ball_MoveY;
  14.                        
  15.                         //REVERSE THE DIRECTION OF THE BALL WHEN IT HITS TO THE BOUNDARY.
  16.                         if (ball_PositionX + ball_RadiusX >= ClientSize.Width | ball_PositionX - ball_RadiusX <= 0)
  17.                         {
  18.                                 ball_MoveX = System.Convert.ToInt32(- ball_MoveX);
  19.                                 Interaction.Beep();
  20.                         }
  21.                         //SET THE Y BOUNDARY TO 90 SO THAT IT WILL NOT EXCEED TO THE TITLE OF THE FORM.
  22.                         if (ball_PositionY + ball_RadiusY >= ClientSize.Height | ball_PositionY - ball_RadiusY <= 90)
  23.                         {
  24.                                 ball_MoveY = System.Convert.ToInt32(- ball_MoveY);
  25.                                 Interaction.Beep();
  26.                         }
  27.                 }
5. Create a control class that overrides the OnResize method. OnResize increases the Resize event and it occurs when the control is resized.
  1.                 protected override void OnResize(EventArgs ev_arg)
  2.                 {
  3.                        
  4.                        
  5.                         Graphics grafx = CreateGraphics();
  6.                         //ERASE ANY DRAWINGS.
  7.                         grafx.Clear(BackColor);
  8.                        
  9.                        
  10.                         //DECLARE A VARIBLE THAT HOLDS THE RADIUS OF THE BALL
  11.                         //THEN SET THE WIDTH OR THE HIEGHT OF IT TO A FRACTION WHICHEVER IS LESS TO THE CLIENT AREA.
  12.                         double dbl_Radius = Math.Min(ClientSize.Width / grafx.DpiX, ClientSize.Height / grafx.DpiY) / ball_Size;
  13.                        
  14.                        
  15.                         //SET THE HIEGHT AND WIDTH OF THE BALL.
  16.                         ball_RadiusX = (int) (dbl_Radius * grafx.DpiX);
  17.                         ball_RadiusY = (int) (dbl_Radius * grafx.DpiY);
  18.                        
  19.                         grafx.Dispose();
  20.                         //SET THE DISTANCE THAT THE BALL MOVES INTO 1 PIXEL OR THE BALL SIZE WHICHEVER IS GREATER.
  21.                         //THIS MEANS THAT THE DISTANCE OF THE BALL MOVES EACH TIME IS PROPORTIONAL TO ITS SIZE,
  22.                         //WHICH IS ALSO PROPORTIONAL TO THE SIZE OF THE CLIENT AREA.
  23.                         //THE BALL SLOWS DOWN WHENEVER THE CLIENT AREA IS SHRUNK
  24.                         //AND THE BALL SPEEDS UP WHEN IT IS INCREASED.
  25.                        
  26.                        
  27.                         ball_MoveX = (int) (Math.Max(1, ball_RadiusX / move_Size));
  28.                         ball_MoveY = (int) (Math.Max(1, ball_RadiusY / move_Size));
  29.                         //THE VALUE OF THE BALL'S MOVEMENT SERVES AS THE MARGIN AROUND THE BALL,
  30.                         //THAT DETERMINES THE ACTUAL SIZE OF BITMAP ON WHICH THE BALL IS DRAWN.
  31.                         //THE DISTANCE OF THE BALL MOVES IS EQUAL TO THE SIZE OF THE BITMAP,
  32.                         //WHICH ALLOWS THE PREVIOUS BALL'S IMAGE TO BE ERASED BEFORE THE NEXT IMAGE IS DRAWN
  33.                        
  34.                         bitmap_WidthMargin = ball_MoveX;
  35.                         bitmap_HeightMargin = ball_MoveY;
  36.                        
  37.                         //TO FIND OUT THE ACTUAL SIZE OF THE BITMAP ON WHICH THE BALL IS DRAWN
  38.                         //PLUS THE MARGINS TO THE BALL'S DIMENSIONS.
  39.                         ball_BitmapWidth = 2 * (ball_RadiusX + bitmap_WidthMargin);
  40.                         ball_BitmapHeight = 2 * (ball_RadiusY + bitmap_HeightMargin);
  41.                        
  42.                         //CREATE A NEW WIDTH AND HEIGHT OF THE BITMAP.
  43.                         btmp = new Bitmap(ball_BitmapWidth, ball_BitmapHeight);
  44.                         //OBTAIN THE GRAFIX OBJECT SHOWN BY THE BITMAP.
  45.                         grafx = Graphics.FromImage(btmp);
  46.                         //CLEAR THE EXISTING BALL AND DRAW A NEW BALL.
  47.                         grafx.Clear(BackColor);
  48.                         grafx.FillEllipse(Brushes.Blue, new Rectangle(ball_MoveX, ball_MoveY, System.Convert.ToInt32(2 * ball_RadiusX), System.Convert.ToInt32(2 * ball_RadiusY)));
  49.                         grafx.Dispose();
  50.                        
  51.                         //RESET THE POSITION OF THE BALL TO THE CENTER OF THE CLIENT AREA.
  52.                         ball_PositionX = (int) (ClientSize.Width / 2);
  53.                         ball_PositionY = (int) (ClientSize.Height / 2);
  54.                        
  55.                        
  56.                 }
6. Now, in your Form_Load, put this code below for starting the time.
  1.                 public void Form1_Load(System.Object sender, System.EventArgs e)
  2.                 {
  3.                         Timer1.Start();
  4.                 }
Output: output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Comments

Ciao

Add new comment