Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Ball Bouncing on the Desktop

0.00/5 (No votes)
30 Sep 2007 1  
This article describes how to create a simple user-drawn form which moves over the desktop
Screenshot - ball.png

Introduction

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.

Steps

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

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 ...

History

  • 30th September, 2007: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here