/
sdl-chicken.guraw
executable file
·70 lines (65 loc) · 1.9 KB
/
sdl-chicken.guraw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/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('Winter.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(I('Kaufmann_Bold_BT.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))
}
}