Click here to Skip to main content
Click here to Skip to main content

A Ball Bouncing on the Desktop

, 30 Sep 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

]Metty[

Germany Germany
No Biography provided

Comments and Discussions

 
GeneralClever! Pinmember Muammar©20-Sep-09 1:07 
GeneralBouncing several Balls PinmemberShima198028-Oct-07 11:36 
GeneralFun! PinmemberWindmiller2-Oct-07 4:31 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 30 Sep 2007
Article Copyright 2007 by ]Metty[
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid