Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 72 lines (65 sloc) 1.92 KB
#!/usr/bin/env gura
import(sdl)
import(gif)
import(jpeg)
import(freetype)
I(filename:string) = path.join(sys.datadir, 'sample/resource', filename)
sdl.Init(sdl.INIT_EVERYTHING)
[wdScreen, htScreen] = [800, 600]
screen = sdl.SetVideoMode(wdScreen, htScreen, 16, sdl.SWSURFACE)
wdImage = 200
imageBg = image(I('seashore.jpg'), `rgb).resize(wdScreen, htScreen)
imageText = image(`rgba, wdScreen, htScreen)
imagesL = gif.content(I('chicken-anim.gif'), `rgba).images::resize(wdImage)
imagesR = imagesL::flip(`horz)
xStep = wdImage / 10
font = freetype.font(path.join(sys.fontdir, 'mplus-1p-regular.ttf'))
font.height = 32, font.color = `white, font.mode = `alpha
Chicken = class {
__init__(x:number, y:number, dir:symbol) = {
[this.x, this.y] = [x, y]
if (dir == `right) { this.turnRight() } else { this.turnLeft() }
}
turnRight():void = {
this.xDir = xStep
this.imageIter = imagesR.cycle()
}
turnLeft():void = {
this.xDir = -xStep
this.imageIter = imagesL.cycle()
}
doAction():void = {
image = this.imageIter.next()
sdl.BlitSurface(image, nil,
screen, sdl.Rect(this.x, this.y - image.height, 0, 0))
this.x += this.xDir
if (this.x + wdImage > wdScreen) { this.turnLeft() }
if (this.x < 0) { this.turnRight() }
}
}
chickens = repeat(3):list {
x = rand(wdScreen - wdImage)
y = rand(htScreen - wdImage) + wdImage
dir = cond(rand() < .5, `left, `right)
Chicken(x, y, dir)
}
sdl.AddTimer(100) {
sdl.BlitSurface(imageBg, nil, screen, nil)
imageText.clear()
imageText.drawtext(font, 10, 50, 'Chickens: %d' % chickens.len())
sdl.BlitSurface(imageText, nil, screen, nil)
chickens::doAction()
screen.UpdateRect()
true
}
repeat {
event = sdl.WaitEvent()
if (event.type == sdl.QUIT) {
break
} elsif (event.type == sdl.KEYDOWN) {
(event.sym == sdl.K_ESCAPE) && break
} elsif (event.type == sdl.MOUSEBUTTONDOWN) {
dir = choose(event.button, `left, `left, `right, `right)
chickens.add(Chicken(event.x, event.y, dir))
}
}