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

Some Unfriendly, Annoying Balls on the Desktop

By , 3 Oct 2007
 
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:

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

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

About the Author

]Metty[
Germany Germany
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalyou got my five..membershivam_kalra4 Dec '09 - 15:10 
This a best & most intresting program I've seen ever.. Big Grin | :-D
GeneralcoolmemberMubi | www.mubshir.com20 Jun '09 - 8:05 
nice stuff...
 
Mubshir Raza Ali
^^^^^^^^^^^^^^^^^^^^
http://www.mubshir.com

General[Message Deleted]memberDanny Rodriguez26 Jan '08 - 17:10 
[Message Deleted]
GeneralA Blast!!!memberjason_lakewhitney17 Oct '07 - 7:21 
I fixed the multiple monitor support and up the number
of balls to to 10.
 
What a fun way to pass a little time.
 
Need to work on the cpu usage to bring it down a bit.
 
This is really fun, thanks.
 

God Bless,
Jason

I am not perfect but I try to be better than those before me.
So those who come after me will be better than I am.

QuestionSTAThread AttributememberMartin Surasky10 Oct '07 - 6:19 
Thanks Metty!
 
The code is pretty simple, but yet a great example for other practical uses of AI. I liked the friction and gravity logic.
 
One thing I'm not so sure about is what is the reason for the STAThread attribute. Don't get me wrong...I know what that attribute is for.
 
What I'm trying to say is that I see no COM objects being used whatsoever so no COM threads should be initialized or, in other words CoInitializeEx() API is not called at all. In that context the Thread Apartment model specified plays no difference.
 
I tried to remove that attribute and the application still works as expected. Any one is curious enough to investigate into this matter and confirm that the STAThread attribute here is useless?
 
Thanks!Big Grin | :-D
 
Martin Surasky
AnswerRe: STAThread Attributemember]Metty[10 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 | :)
GeneralInteresting..memberMurat Firat9 Oct '07 - 8:25 
very interesting and really good idea..
GeneralBrilliant !memberYclkvnc9 Oct '07 - 6:40 
Brilliant !
GeneralCutememberBoneSoft9 Oct '07 - 4: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 BoneSoft.com.

QuestionThat about multiplayer version?memberChernichkin Stanislav9 Oct '07 - 1:28 
Subj Smile | :) )
GeneralNoicememberLiqdfire8 Oct '07 - 9:07 
Now thats slick, I think the chasing mode you be awesome.
GeneralMy god, that is THE best application i ever ranmemberThe Undefeated5 Oct '07 - 7:26 
Really, fantastic.

 
My current favourite word is: Waffle
 
Cheese is still good though.

JokeBugmembergore013 Oct '07 - 21:44 
You can switch to the ball-window by pressing ALT-TAB and close it pressing F4. If you do this you get ObjectDisposedException
JokelolmemberBen Daniel3 Oct '07 - 19:43 
I love how you say you wanted to create a robot that replicated itself (!), but that didn't work so you did a bouncy ball thing instead, lol.
 
As for me, I think for my next project I might try to build a fully functioning terminator...but if that doesn't work I might make pong, lol. Wink | ;)
 
Thanks,
Ben Smile | :)

GeneralRe: lolmemberPaul Bryn Williams10 Oct '07 - 0:54 
Laugh | :laugh:
 
Best regards,
 
Paul

Generalgoodmemberrajantawate1(http//www.jhatak.com)3 Oct '07 - 7:50 
I see a very good potential here. hmm..just thinking of how this can be used.
 
http://www.jhatak.com
 

GeneralMultiple monitorsmemberLieutenant Frost3 Oct '07 - 6:57 
It doesn't seem that they like a multiple monitor setup...the balls are trapped on the primary, and try as they might, they can't reach the second one. Wink | ;)
GeneralRe: Multiple monitorsmemberLee Humphries3 Oct '07 - 14:45 
Lieutenant Frost wrote:
It doesn't seem that they like a multiple monitor setup...the balls are trapped on the primary, and try as they might, they can't reach the second one.

 
Same for me too - All the active balls are busy bashing away at the edge of the active monitor.
 
A couple of suggestions (although I might try coding these myself):
Stealth mode - the ball shows as inactive but slowly creeps up on the mouse.
Competition - each ball tries to steal the mouse for itself and run away with it from the others. Now that could be really frustrating.
GeneralRe: Multiple monitorsmemberdave.kelly3 Oct '07 - 21:04 
This is a great bit of fun!
 
To use multiple monitors, just change the:

else if (X > Screen.PrimaryScreen.WorkingArea.Width)
 
to
 
else if (X > Screen.AllScreens.Length)
 

GeneralNice piece of code and enjoyed playing it.memberShoki3 Oct '07 - 6:01 
Enjoyed playing it.Smile | :) good bit of code as well.
 
Ashok Singh
Real Programmer's do not comment their code, if it is hard to write then it should hard to understand - Anony..

GenerallolmemberSeishin#3 Oct '07 - 3:29 
those little f*** caught my cursor.. :P
nice one Big Grin | :-D
 
for an article it would be nice to see some code explaination here..
 
life is study!!!

GeneralHa, thats coolmemberTom Lawrance3 Oct '07 - 3:21 
only thing is, if i move the ball under my cursor to my secondary monitor, the attack ball cant get it...it looks really frustrated bouncing around in the bottom right of my primary screen!Laugh | :laugh:
GeneralRe: Ha, thats coolmemberDwight Johnson9 Oct '07 - 3:19 
See response to Multiple Monitors by Dave.Kelly. He shows how to allow the balls access to secondary monitors.
GeneralRe: Ha, thats coolmemberTom Lawrance9 Oct '07 - 3:23 
yeah saw that, thanks for the head up though.
Smile | :)

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 3 Oct 2007
Article Copyright 2007 by ]Metty[
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid