Click here to Skip to main content
15,867,895 members
Articles / Multimedia / GDI+
Article

Some Unfriendly, Annoying Balls on the Desktop

Rate me:
Please Sign up or sign in to vote.
4.84/5 (44 votes)
3 Oct 20072 min read 75.1K   873   54   23
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

Introduction

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.

Background

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:

C#
public void Loop() 
{
    while (!Disposing) 
    {
        Think();
        Act();
        Thread.Sleep(33);
    }
}

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

C#
public enum eBrain 
{
    Idle,
    Following,
    Attacking,
    CursorCatched,
    Wait
}

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.

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



Comments and Discussions

 
Generalyou got my five.. Pin
shivamkalra4-Dec-09 15:10
shivamkalra4-Dec-09 15:10 
Generalcool Pin
Mubi | www.mrmubi.com20-Jun-09 8:05
professionalMubi | www.mrmubi.com20-Jun-09 8:05 
General[Message Deleted] Pin
Danny Rodriguez26-Jan-08 17:10
Danny Rodriguez26-Jan-08 17:10 
GeneralA Blast!!! Pin
lost in transition 17-Oct-07 7:21
lost in transition 17-Oct-07 7:21 
QuestionSTAThread Attribute Pin
Martin Surasky10-Oct-07 6:19
Martin Surasky10-Oct-07 6:19 
AnswerRe: STAThread Attribute Pin
User 273912110-Oct-07 9:53
User 273912110-Oct-07 9:53 
Yeah, you are right, it isn't needed in this case - But VS creates a PSVM with this attribute automatically when creating a windows form app; thats how it got there Wink | ;) . Furthermore I remember some dialog controls of windows forms need this attribute (FileBrowser if I'm right), so I let the attribute be there for this case. Anyway, it doesn't harm anyone, so why not? Smile | :)

modified 28-Dec-20 21:02pm.

GeneralInteresting.. Pin
Murat Firat9-Oct-07 8:25
Murat Firat9-Oct-07 8:25 
GeneralBrilliant ! Pin
Yclkvnc9-Oct-07 6:40
Yclkvnc9-Oct-07 6:40 
GeneralCute Pin
BoneSoft9-Oct-07 4:45
BoneSoft9-Oct-07 4:45 
QuestionThat about multiplayer version? Pin
Chernichkin Stanislav9-Oct-07 1:28
Chernichkin Stanislav9-Oct-07 1:28 
GeneralNoice Pin
Liqdfire8-Oct-07 9:07
Liqdfire8-Oct-07 9:07 
GeneralMy god, that is THE best application i ever ran Pin
Anthony Mushrow5-Oct-07 7:26
professionalAnthony Mushrow5-Oct-07 7:26 
JokeBug Pin
gore013-Oct-07 21:44
gore013-Oct-07 21:44 
Jokelol Pin
Ben Daniel3-Oct-07 19:43
Ben Daniel3-Oct-07 19:43 
GeneralRe: lol Pin
Paul_Williams10-Oct-07 0:54
Paul_Williams10-Oct-07 0:54 
GeneralMultiple monitors Pin
Lieutenant Frost3-Oct-07 6:57
Lieutenant Frost3-Oct-07 6:57 
GeneralRe: Multiple monitors Pin
Lee Humphries3-Oct-07 14:45
professionalLee Humphries3-Oct-07 14:45 
GeneralRe: Multiple monitors Pin
dave.kelly3-Oct-07 21:04
professionaldave.kelly3-Oct-07 21:04 
GeneralNice piece of code and enjoyed playing it. Pin
shokisingh3-Oct-07 6:01
shokisingh3-Oct-07 6:01 
Generallol Pin
Seishin#3-Oct-07 3:29
Seishin#3-Oct-07 3:29 
GeneralHa, thats cool Pin
Tom Lawrance3-Oct-07 3:21
professionalTom Lawrance3-Oct-07 3:21 
GeneralRe: Ha, thats cool Pin
Dwight Johnson9-Oct-07 3:19
Dwight Johnson9-Oct-07 3:19 
GeneralRe: Ha, thats cool Pin
Tom Lawrance9-Oct-07 3:23
professionalTom Lawrance9-Oct-07 3:23 

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.