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
- ▶Language Reference
- ▶Core
- ▶Graphics
- ▶Image Class
- new
- clear
- clearTransforms
- clone
- drawCircle
- drawEllipse
- drawImage
- drawInnerSegment
- drawLine
- drawPixel
- drawPolygon
- drawRect
- drawRotatedImage
- drawRoundedRect
- drawSegment
- drawText
- fill
- fillCircle
- fillEllipse
- fillInnerSegment
- fillPolygon
- fillRect
- fillRoundedRect
- fillSegment
- fillText
- getAlpha
- getBlue
- getColors
- getGreen
- getHeight
- getPixel
- getPixelAlpha
- getPixelBlue
- getPixelGreen
- getPixelRed
- getRed
- getWidth
- isOverlap
- isPixelOverlap
- multAlpha
- rotate
- scale
- setAlpha
- setBlend
- setColor
- setFont
- setPixel
- setTransform
- size
- transform
- translate
- undoTransform
- ▶Transformations
- How Alignment Works
- Using Colors
- drawCircle
- drawEllipse
- drawImage
- drawInnerSegment
- drawLine
- drawPixel
- drawPolygon
- drawRect
- drawRotatedImage
- drawRoundedRect
- drawSegment
- drawText
- fill
- fillCircle
- fillEllipse
- fillInnerSegment
- fillPolygon
- fillRect
- fillRoundedRect
- fillSegment
- fillText
- getAlpha
- getBlue
- getColors
- getGreen
- getRed
- multAlpha
- setAlpha
- setBlend
- setColor
- setFont
- ▶Image Class
- ▶Audio
- ▶Controls
- ▶Collisions
- ▶Utility
- ▶Debugging
