Pardon the mess, Play My Code is in beta!

READY TO PLAY?
CLICK TO LOG IN!

sign up - lost password

Try Play My Code »

Play with our IDE without having an account. Sign up to build, save and publish games.

/**
 * Hello World example,
 * Feel free to tinker and mess about with this code, that's what it's for.
 * 
 * Controls!
 *  Use the mouse to move the player,
 *  press up and down to move the crocs.
 * 
 * This aims to show off some example code for:
 *  loading images
 *  setting font
 *  clearing the screen
 *  getting mouse and keyboard controls
 *  drawing text and images
 * 
 * Enjoy!
 */

// global, how fast the crocodile moves
$CROC_SPEED   = 2.5
// the padding between crocs
$CROC_PADDING = 32
// how many crocs to draw
$NUM_CROCS    = 10

// Our Images, all locals
crocImg        = new Image( "croc.png" )
playerImgLeft  = new Image( "player_left.png" )
playerImgRight = new Image( "player_right.png" )
playerImg      = playerImgRight

// for tracking player moving left or right
lastMouseX = 0
isMovingLeft = false

// croc starts at the bottom of the screen
crocY = getScreenHeight() - 40

// the mainloop, this is run 60 times a second
onEachFrame() do |delta|
    /* Background
     * 
     * Fill the whole screen with one colour to wipe the last frme.
     * Then draw an array of boxes.
     */
    fill( :green )
    
    /* Player
     * 
     * Draw the user where the mouse is
     * and have player face left or right.
     */
    controls = getControls()
    controls.getMouseXY() do |mouseX, mouseY|
        // work out if we look left, right
        if mouseX < lastMouseX
            playerImg = playerImgLeft
        else if mouseX > lastMouseX
            playerImg = playerImgRight
        end
    
        // the true says you want it centred at that location
        drawImage( playerImg, mouseX, mouseY, true )
        lastMouseX = mouseX
    end

    /* Crocodiles
     * 
     * Move crocadile up or down,
     * and draw lots of them in a row!
     */
    crocSpeed = $CROC_SPEED * delta
    if controls.isKeyDown([ "up", "w" ])
        crocY = crocY - crocSpeed
    end
    
    if controls.isKeyDown([ "down", "s" ])
        crocY = crocY + crocSpeed
    end
    
    // ensure the crcs don't go outside the screen!
    crocY = crocY.limit( 0, getScreenHeight() )
    
    crocWidth = crocImg.getWidth() + $CROC_PADDING
    $NUM_CROCS.times() do |i|
        crocX = i*crocWidth + $CROC_PADDING*1.5
        drawImage( crocImg, crocX, crocY, true )
    end

    /* Text
     *
     * Draw the text with a highlighting outline.
     * It appearres partially transparent so you can see
     * the player behind it.
     */
    setFont( 'Arial', 72, 'bold italic' )
    setColor( 255, 245, 238, 0.75 )
    // fill fills the area,..
    fillText( "Hello World!", 40, 100 )
    
    setColor( :white, 0.75 )
    // ... draw draws an outline.
    drawText( "Hello World!", 40, 100 )
end

ERRORS

YOUR BROWSER DOES NOT SUPPORT HTML5!

Please use one of these instead

Our games cannot run in your browser