• We just launched and are currently in beta. Join us as we build and grow the community.

Bouncing Ball in C#

kmfkmsfk

Senior Member
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
53
Likes
12
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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:

balldesign.png


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;


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. //DECLARE A VARIABLE TO OBTAIN THE GRAPHICS OBJECT.
  4. Graphics grafx =

    CreateGraphics(

    )

    ;
  5. //DRAW THE BALL IN THE FORM.
  6. grafx.

    DrawImage

    (

    btmp, (

    int

    )

    (

    ball_PositionX -

    ball_BitmapWidth /

    2

    )

    , (

    int

    )

    (

    ball_PositionY -

    ball_BitmapHeight /

    2

    )

    , ball_BitmapWidth, ball_BitmapHeight)

    ;

  7. grafx.

    Dispose

    (

    )

    ;

  8. //INCREAMENT THE POSITION OF THE BALL BY ITS DISTANCE TO MOVED BOTH X AND Y AXIS.
  9. ball_PositionX +=

    ball_MoveX;
  10. ball_PositionY +=

    ball_MoveY;

  11. //REVERSE THE DIRECTION OF THE BALL WHEN IT HITS TO THE BOUNDARY.
  12. if

    (

    ball_PositionX +

    ball_RadiusX >=

    ClientSize.

    Width

    |

    ball_PositionX -

    ball_RadiusX <=

    0

    )
  13. {
  14. ball_MoveX =

    System

    .

    Convert

    .

    ToInt32

    (

    -

    ball_MoveX)

    ;
  15. Interaction.

    Beep

    (

    )

    ;
  16. }
  17. //SET THE Y BOUNDARY TO 90 SO THAT IT WILL NOT EXCEED TO THE TITLE OF THE FORM.
  18. if

    (

    ball_PositionY +

    ball_RadiusY >=

    ClientSize.

    Height

    |

    ball_PositionY -

    ball_RadiusY <=

    90

    )
  19. {
  20. ball_MoveY =

    System

    .

    Convert

    .

    ToInt32

    (

    -

    ball_MoveY)

    ;
  21. Interaction.

    Beep

    (

    )

    ;
  22. }
  23. }

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. Graphics grafx =

    CreateGraphics(

    )

    ;
  4. //ERASE ANY DRAWINGS.
  5. grafx.

    Clear

    (

    BackColor)

    ;


  6. //DECLARE A VARIBLE THAT HOLDS THE RADIUS OF THE BALL
  7. //THEN SET THE WIDTH OR THE HIEGHT OF IT TO A FRACTION WHICHEVER IS LESS TO THE CLIENT AREA.
  8. double

    dbl_Radius =

    Math.

    Min

    (

    ClientSize.

    Width

    /

    grafx.

    DpiX

    , ClientSize.

    Height

    /

    grafx.

    DpiY

    )

    /

    ball_Size;


  9. //SET THE HIEGHT AND WIDTH OF THE BALL.
  10. ball_RadiusX =

    (

    int

    )

    (

    dbl_Radius *

    grafx.

    DpiX

    )

    ;
  11. ball_RadiusY =

    (

    int

    )

    (

    dbl_Radius *

    grafx.

    DpiY

    )

    ;

  12. grafx.

    Dispose

    (

    )

    ;
  13. //SET THE DISTANCE THAT THE BALL MOVES INTO 1 PIXEL OR THE BALL SIZE WHICHEVER IS GREATER.
  14. //THIS MEANS THAT THE DISTANCE OF THE BALL MOVES EACH TIME IS PROPORTIONAL TO ITS SIZE,
  15. //WHICH IS ALSO PROPORTIONAL TO THE SIZE OF THE CLIENT AREA.
  16. //THE BALL SLOWS DOWN WHENEVER THE CLIENT AREA IS SHRUNK
  17. //AND THE BALL SPEEDS UP WHEN IT IS INCREASED.


  18. ball_MoveX =

    (

    int

    )

    (

    Math.

    Max

    (

    1

    , ball_RadiusX /

    move_Size)

    )

    ;
  19. ball_MoveY =

    (

    int

    )

    (

    Math.

    Max

    (

    1

    , ball_RadiusY /

    move_Size)

    )

    ;
  20. //THE VALUE OF THE BALL'S MOVEMENT SERVES AS THE MARGIN AROUND THE BALL,
  21. //THAT DETERMINES THE ACTUAL SIZE OF BITMAP ON WHICH THE BALL IS DRAWN.
  22. //THE DISTANCE OF THE BALL MOVES IS EQUAL TO THE SIZE OF THE BITMAP,
  23. //WHICH ALLOWS THE PREVIOUS BALL'S IMAGE TO BE ERASED BEFORE THE NEXT IMAGE IS DRAWN

  24. bitmap_WidthMargin =

    ball_MoveX;
  25. bitmap_HeightMargin =

    ball_MoveY;

  26. //TO FIND OUT THE ACTUAL SIZE OF THE BITMAP ON WHICH THE BALL IS DRAWN
  27. //PLUS THE MARGINS TO THE BALL'S DIMENSIONS.
  28. ball_BitmapWidth =

    2

    *

    (

    ball_RadiusX +

    bitmap_WidthMargin)

    ;
  29. ball_BitmapHeight =

    2

    *

    (

    ball_RadiusY +

    bitmap_HeightMargin)

    ;

  30. //CREATE A NEW WIDTH AND HEIGHT OF THE BITMAP.
  31. btmp =

    new

    Bitmap(

    ball_BitmapWidth, ball_BitmapHeight)

    ;
  32. //OBTAIN THE GRAFIX OBJECT SHOWN BY THE BITMAP.
  33. grafx =

    Graphics.

    FromImage

    (

    btmp)

    ;
  34. //CLEAR THE EXISTING BALL AND DRAW A NEW BALL.
  35. grafx.

    Clear

    (

    BackColor)

    ;
  36. grafx.

    FillEllipse

    (

    Brushes.

    Blue

    , new

    Rectangle(

    ball_MoveX, ball_MoveY, System

    .

    Convert

    .

    ToInt32

    (

    2

    *

    ball_RadiusX)

    , System

    .

    Convert

    .

    ToInt32

    (

    2

    *

    ball_RadiusY)

    )

    )

    ;
  37. grafx.

    Dispose

    (

    )

    ;

  38. //RESET THE POSITION OF THE BALL TO THE CENTER OF THE CLIENT AREA.
  39. ball_PositionX =

    (

    int

    )

    (

    ClientSize.

    Width

    /

    2

    )

    ;
  40. ball_PositionY =

    (

    int

    )

    (

    ClientSize.

    Height

    /

    2

    )

    ;


  41. }

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:
balloutput.png


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


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

329,143

329,151

Top