Click here to Skip to main content
15,881,281 members
Articles / Programming Languages / C#

Simple Ball Animation with Border Collision using VS 2005 and .NET 2.0

Rate me:
Please Sign up or sign in to vote.
2.83/5 (6 votes)
12 May 2006CPOL 61.8K   1.3K   18   3
A very primitive article on simple animation of balls using double buffering technique
Sample Image - BallAnimation.jpg

Introduction

In this project, I have demonstrated a simple technique on how we can animate some balls in a Picture Box. The balls collides with the walls and change directions. Using the double buffering of .NET 2.0, it is very easy to handle large processing of graphical animation.

I have created a Ball class which can be reused and extended for other projects as well. The Ball class has the following methods:

C#
DrawBall(Graphics g) 
DrawBall(Graphics g, int move_x, int move_y) 
MoveBall(Ball ball, int screenHeight, int screenWidth)

The DrawBall with the single parameter draws an ellipse on the Graphics object. On the other hand, the Drawball with three parameters draws the updated ball on the screen.

The ball is created using the following code:

C#
g.DrawEllipse(new Pen(_ballColor), _x, _y, _width, _height);
// Create solid brush. 
SolidBrush redBrush = new SolidBrush(_ballColor); 
// Fill ellipse on screen
g.FillEllipse(redBrush, _x, _y, _width, _height);

The double buffering is achieved by the following code:

C#
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
this.SetStyle(ControlStyles.UserPaint, true); 

We have used a timer with interval 1 millisecond to animate the balls. In the Timer Tick method I used:

C#
for (int i = 0; i < TOTAL_BALLS; i++) 
{
    manyBall[i].MoveBall(
       manyBall[i],
       pictureBox1.Height, 
       pictureBox1.Width
    ); 
} 
this.Refresh();

To Refresh the picture box on where we are painting the balls, I wrote:

C#
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 
{
    for(int i = 0; i < TOTAL_BALLS; i++) 
    { 
        manyBall[i].DrawBall(e.Graphics); 
    } 
}

Conclusion

We have seen how we can animate balls using simple animation techniques. Hope you like the code. Till then, Eat Bytes Regularly for good health.

History

  • 13th May, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO FastBikri.com
Bangladesh Bangladesh
Zakaria has a keen interest in developing new solutions and exploring new technologies. He has a wide experience in web technologies and business solutions.

Visit my works @ https://play.google.com/store/apps/dev?id=8713255821916123360

His latest project includes Library Management System for University of Dhaka using ASP.NET and Sql Server.

Zakaria completed Masters in Information Technology from Institute of Information Technology http://iit.univdhaka.edu/,
University of Dhaka, had his BSc in Computer Science from AIUB (www.aiub.edu).
School and college from SKBZBIS (www.skbzbis.com)

Latest Project : www.alamgroupae.com
for Alam Group of Companies, Abu Dhabi

Some of his Completed projects:

www.zakasoft.com
www.musaffah.info
www.shonpapri.com
www.skbzbis.com
www.bdembuae.org (The official website of Embassy of the People's Republic of Bangladesh, Abu Dhabi, United Arab Emirates)

Bangla Translation of the Holy Quran (C#/Access)
Library Management System (ASP.NET/C#/SQLServer)
Seat Reservation System (ASP.NET/C#/SQLServer)

Besides programming, he has published his first poetry book (na bola kothamala/Unspoken Words) and in the process of writing several new novels.

Founded ZakaSoft, a division of ZCom, Providing state-of-the-art solutions in United Arab Emirates.

Zakaria Lives in Abu Dhabi and enjoys his spare time in writing and music.

Comments and Discussions

 
GeneralInteresting and useful Pin
BigWorld12-Jan-09 21:17
BigWorld12-Jan-09 21:17 
GeneralRe: Interesting and useful Pin
milkplus19-Oct-09 7:05
milkplus19-Oct-09 7:05 
QuestionWhy a PictureBox? Pin
Robert Rohde13-May-06 4:28
Robert Rohde13-May-06 4:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.