Click here to Skip to main content
15,883,901 members
Articles / Multimedia / GDI+
Article

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 47.2K   2.8K   24   2
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.

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

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

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

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralClever! Pin
Muammar©20-Sep-09 0:07
Muammar©20-Sep-09 0:07 
GeneralBouncing several Balls Pin
Shima198028-Oct-07 10:36
Shima198028-Oct-07 10:36 

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.