Motyar

@motyar

Freelance Web Developer

Static Web Hosting, made easy

Apr 8, 2010

Angel & Dreams - A jquery game

The "Angel and dreams" is a jquery game inspired by the cursor chaser game.
In this game there is an angel, chases dreams that appears on the screen with random colors and positions. You can use mouse to get the dream (by clicking) before the angel does.

Idea

Dreams appear, angel follows and get them, you can too, by clicking them. Who does first gets a score.
Here are two elements (dream and angel) and six functions(init(), do_dream(), chase, explode, lose, won).

init()

This function creates the angel and them start creating dream bubbles.

 function init(){
    // create angel
    // you can download it from 
    //http://sites.google.com/site/dharmmotyar/Home/angel.gif?attredirects=0&d=1
    angel = $('<img>').attr({
        'src''angel.gif'
    }).css({
        'position''absolute',
        'z-index'75,
        top: -10,
        left: -10
    });

    //append it to body
    $(document.body).append(angel);

    //start dreaming
    do_dream();
} 


do_dream()

This function creates dreams and calls angel to chase them, also sets a time period to repeat itself.

 function do_dream() {
    //take a random color
    var color 'rgb(' Math.floor(Math.random() * 255) + ',' +
                        Math.floor(Math.random() * 255) + ',' + 
                        Math.floor(Math.random() * 255) + ')';

    //generate random position
    var Math.floor(Math.random() * $(window).width());
    var Math.floor(Math.random() * $(window).height());
    //decorating the dream
    dream = $('<span>').css({
        'position''absolute',
        height'100px',
        width'100px',
        'background-color'color,
        'border-radius''100px',
        '-moz-border-radius''100px',
        '-webkit-border-radius''100px',
        top50//offsets
        left50 //offsets
    });
    //append it to body
    $(document.body).append(dream);
 
    //bind the explode on click event
    dream.bind('click', function (e) {
        //you won
        won(e); 
        //hide the dream
        explode(e.pageXe.pageY, $(e.target));
    });
    //call angel to chase the dream
    chase(xydream);
    //dreams are endless
    window.setTimeout('do_dream()'1500);
} 


chase(x, y, dream)

Angel chases the dream, this function hides the dream as the angel touches.

 function chase(xydream) {
    //angel gets the dream 
    angel.animate({
        topy,
        leftx 
    }, 1000, function () {
        //explode the dream
        explode(xydream);
        //you lose
        lose();
    });
} 


explode(x, y, dream)

This function fadesOut the dream with explosion effect.

 function explode(xydream) {
    dream.animate({
        height'200px',
        width'200px',
        'border-radius''500px',
        '-moz-border-radius''500px',
        '-webkit-border-radius''500px',
        opacity0.1,
        top100,
        left100
    }, 100, function () {
        dream.hide();
    });
} 


lose() and won()

These functions simply updates the scores.

 function lose() {
    $('#angel').html(parseInt($('#angel').html()) + 1);
 }
function won() {
    //stop the angel
    angel.stop();
    $('#you').html(parseInt($('#you').html()) + 1);
} 


<span id='you'>0</span> /

<span id='angel'>0</span



Demo

Okey? this is the time to see this code in action, yes sure the demo is here.

Labels: , ,




By :