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

Some Unfriendly, Annoying Balls on the Desktop

, 3 Oct 2007
Rate this:
Please Sign up or sign in to vote.
Some unfriendly, annoying balls on the desktop bouncing around and trying to catch the cursor - a mix of GDI+, transparent forms and bit of AI
Screenshot - balls.jpg


This program creates three balls on your desktop. If you move the cursor towards them, they move around and jump towards your cursor, trying to catch it. If they succeed, they will pull your cursor with them and you have to get rid of them by waiting or clicking around with your mouse.


First I wanted to create some sort of robot, which duplicates itself, and its children do that again, and again, and again... But as it did not work as expected, I created this out of the code I had so far - just to let you know why the project name is Replicator.

Using the Program

Just execute the program and see the balls bouncing around. Try to avoid them. To get rid of them after they catch you, just left-click a few times. If you want to close the program, right-click on one of them (e.g. when one catches you). The current state of the robot is presented by the filled circle in the middle. Red is Attack, blue is Follow, grey is Standby and black represents Inactive.

Using the Code

The graphical representation, as described further in my other articles, is done by a transparent Form. Each robot has its own dialog.

But the more interesting part is the movement/AI:

public void Loop() 
    while (!Disposing) 

As soon as the form is shown, a thread is created which processes the action part. The thread calls two methods as long as the robot is there. The first one is the Think() part. It "analyses" the current situation and decides what to do, where to walk, where to jump. The second one is the Act() part, which actually handles the physics (gravity, bouncing, movement).

public enum eBrain 

The Think() part uses a brain state eBrain to describe the current situation. In this case, Idle is used as a deciding part. It decides which brain state comes next depending on the distance to the target, and it is set when the current condition for the current brain state is no longer valid.

The actions taken by the brain are represented by the functions Walk(int x), Jump(int y) and Stand(). They just change the current velocity a bit and depend on being called on every tick, so that after some ticks, the expected movement is done.

The Act() part then moves the ball around and applies gravity, friction and other physical properties to it.

In Short

The AI is done here by a thread containing the AI and the physics, which calls the Think() part every time, which in turn causes the movement to be changed a little on every tick.


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


About the Author


Germany Germany
No Biography provided

Comments and Discussions

Generalyou got my five.. Pinmembershivam_kalra4-Dec-09 16:10 
Generalcool PinmemberMubi | www.mubshir.com20-Jun-09 9:05 
General[Message Deleted] PinmemberDanny Rodriguez26-Jan-08 18:10 
GeneralA Blast!!! Pinmemberjason_lakewhitney17-Oct-07 8:21 
QuestionSTAThread Attribute PinmemberMartin Surasky10-Oct-07 7:19 
AnswerRe: STAThread Attribute Pinmember]Metty[10-Oct-07 10:53 
GeneralInteresting.. PinmemberMurat Firat9-Oct-07 9:25 
GeneralBrilliant ! PinmemberYclkvnc9-Oct-07 7:40 
GeneralCute PinmemberBoneSoft9-Oct-07 5:45 
It's funny how some of the silly projects that have very little practical use are the most popular. 5 from me, this thing is fun. I think I'll wait until some people go to the bathroom and run it on some machines in the office. Poke tongue | ;-P

Try code model generation tools at

QuestionThat about multiplayer version? PinmemberChernichkin Stanislav9-Oct-07 2:28 

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.150327.1 | Last Updated 3 Oct 2007
Article Copyright 2007 by ]Metty[
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid