This work is licensed under a
Creative Commons
import java.util.Random
Random rand = new Random()
def moveApple() {
this.applePos = [
rand.nextInt( 15 ),
rand.nextInt( 15 ),
]
}
def game = new Game( gridSize, rand );
class Game
{
def gridSize
// ...
def isApple( pos )
{
return ( pos == applePos )
}
def t = new Thread(
{ println( "foo" ) } as Runnable )
# Or even:
def t = new Thread( { println( "foo" ) } )
def x = [
'get': { return 'dummy' },
'put': {}
] as IGetAndPut
class MyStuff
{
def something
int anInt
}
def s = new MyStuff(
something:"S", anInt:3 )
println( s.getAnInt() )
def s = [255, 0, 128] as java.awt.Color
def swing = SwingBuilder.build()
{
frame(
id:'frame',
title:'Snake in Groovy',
defaultCloseOperation:WC.EXIT_ON_CLOSE,
)
{
panel( new SnakePanel( gridSize ), id:'canvas' )
}
}
frame.keyPressed =
{ game.keyPressed( it.keyCode ) }
def gridSize = [ 20, 20 ] def m = [ 'a':0, 'b':2 ]
# Draw a dot for each bit of the snake
game.snakeBody.each {
scaledGfx.dot( Colors.snake, it )
}
stringList.eachWithIndex()
{ obj, i -> println( i, obj ) };
// See also find, findAll, lots of others
scaledGfx.write(
"Score: ${game.snakeBody.size()}." )
doOutside {
// Something on a background thread
doLater {
// Async, on the event thread
}
edt {
// On the event thread, and we wait
// for it before we continue
}
}
while( true )
{
frame.repaint()
sleep( 100 )
game.step()
}
class KeyCode {
static int ESCAPE = 27
static int SPACE = 32
static int RETURN = 10
}
| Videos | youtube.com/user/ajbalaam |
|---|---|
| @andybalaam | |
| Blog | artificialworlds.net/blog |
| Projects | artificialworlds.net |