Click here to Skip to main content
15,070,139 members
Articles / Multimedia / GDI+
Posted 30 Sep 2007


24 bookmarked

A Ball Bouncing on the Desktop

Rate me:
Please Sign up or sign in to vote.
3.26/5 (14 votes)
30 Sep 2007CPOL1 min read
This article describes how to create a simple user-drawn form which moves over the desktop
Screenshot - ball.png


When I first started with C#, I thought about what I could do. This simple example came to my mind, as it is done in some different places, e.g. like the Direct X Browser. I just redid that and added some gravity and transparency.


1. Creating a Way to Display the Ball

We somehow need to display the ball/box we want to bounce around. One of the easiest ways is to create a new form, set its border style to none and draw it yourself.

this.ControlBox = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximizeBox = false;
this.MinimizeBox = false;
private void BallForm_Paint(object sender, PaintEventArgs e)
    Graphics g = e.Graphics;
       g.Clear(Color.Cyan); //Transparent color
       g.FillPie(Brushes.Red, 0, 0, Width - 1, Height - 1, 0, 360);
       g.DrawArc(Pens.Black, 0, 0, Width - 1, Height - 1, 0, 360);

2. Making the Ball Move

For moving the ball, I used my own Application loop, but you might as well just create a timer calling an OnTick method.

We move our ball by having two Vectors: The first one for the current position, the second one for the velocity.

moveY += 0.1; //gravity
X += moveX;
Y += moveY;
Location = new Point((int)X, (int)Y);

3. Making the Ball Bounce

As a wall we will use the borders of the desktop; for the collision, simply check if the position of the ball exceeds them.

 if (X < 0)
     X = 0;
        moveX = -moveX;
        moveX *= 0.75;
        moveY *= 0.95;
if (X > Screen.PrimaryScreen.WorkingArea.Width - 1 - Width)
    X = Screen.PrimaryScreen.WorkingArea.Width - 1 - Width;
        moveX = -moveX;
        moveX *= 0.75;
        moveY *= 0.95;

I decrease both movement variables to create a more realistic movement, as a ball normally wouldn't bump endless times because of the friction.

4. Making the Ball Bounce Again after Standing Still

As we added friction, the ball will stand still after some time. Therefore we just give it another kick to let it start again.

if (Math.Abs(moveX) < 0.1 && Math.Abs(moveY) < 0.5 &&
    Y > Screen.PrimaryScreen.WorkingArea.Height - 1 - Height - 40)

Note: As the ball also stands still when being at the top of the screen, we check if the ball is at the bottom of the desktop before bouncing it.

Other Changes

You may get some neat results by changing the physical properties like gravity/friction/speed ...


  • 30th September, 2007: Initial post


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


About the Author

Comments and Discussions

GeneralClever! Pin
Muammar©20-Sep-09 0:07
Member Muammar©20-Sep-09 0:07 
GeneralBouncing several Balls Pin
Shima198028-Oct-07 10:36
MemberShima198028-Oct-07 10:36 
GeneralFun! Pin
Windmiller2-Oct-07 3:31
MemberWindmiller2-Oct-07 3:31 

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.