Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 100 lines (93 sloc) 2.5 KB
#!/usr/bin/env gura
import(tk)
import(png)
//import(gif)
//tk.tclDump()
imgfile(filename:string) = path.join(sys.datadir, 'sample', 'resource', filename)
img_kangaroo = image(`rgba).read@png(imgfile('kangaroo.png'))
//imgs_chicken = gif.read(imgfile('chicken-anim.gif'), `rgba).images
images = %{
`kangarooL => img_kangaroo.to_tk()
`kangarooR => img_kangaroo.flip(`horz).to_tk()
`kangarooHalfL => \
img_kangaroo.resize(img_kangaroo.width / 2, img_kangaroo.height).to_tk()
`kangarooHalfR => \
img_kangaroo.flip(`horz).resize(img_kangaroo.width / 2, img_kangaroo.height).to_tk()
}
imagesTurnR = images[`kangarooHalfR, `kangarooHalfL, `kangarooL]
imagesTurnL = images[`kangarooHalfL, `kangarooHalfR, `kangarooR]
squTbl = [((interval(-1, 1, 20):open_r) ** 2 - 1)]
Kangaroo = class {
__init__(c:tk.Canvas, x:number, y:number, direction:symbol) = {
this._c = c
[this._x0, this._y0] = [x, y]
this._item = c.Image(x, y, anchor => 'c')
if (direction == `left) {
this._xdir = -5
this._item[`image] = images[`kangarooL]
} else {
this._xdir = 5
this._item[`image] = images[`kangarooR]
}
}
startJump(htJump:number) = {
[x1, y1, x2, y2] = this._item.bbox()
[wdItem, htItem] = [x2 - x1, y2 - y1]
[x, xdir] = [this._x0, this._xdir]
iter_y = squTbl.cycle() * htJump + this._y0
imageIter = nil
this._c.bindtimer(0, 40) {
wdCanvas = this._c.winfo$width()
y = iter_y.next()
this._item.coords(x, y)
x += xdir
if (x < wdItem / 2) {
imageIter = imagesTurnL.each()
xdir = 5
} elsif (x + wdItem / 2 > wdCanvas) {
imageIter = imagesTurnR.each()
xdir = -5
}
if (imageIter) {
image = imageIter.next()
if (image) {
this._item[`image] = image
} else {
imageIter = nil
}
}
}
}
}
tk.mainwindow() {|mw|
mw.wm$geometry('640x480')
mw.Canvas(bg => 'white') {|c|
c.pack(fill => 'both', expand => true)
Kangaroo(c, 150, 110, `left).startJump(50)
c.Text(10, 10, text => 'Left or Right click for new kangaroos',
anchor => 'nw', font => tk.Font(size => 20))
kangaroo = nil
c.bind('<1>') {|x:number, y:number|
if (!kangaroo) {
kangaroo = Kangaroo(c, x, y, `left)
tick = os.clock()
}
}
c.bind('<3>') {|x:number, y:number|
if (!kangaroo) {
kangaroo = Kangaroo(c, x, y, `right)
tick = os.clock()
}
}
c.bind('<ButtonRelease>') {
if (kangaroo) {
duration = os.clock() - tick
htJump = int(duration * 200)
htJump = min(max(htJump, 50), 200)
kangaroo.startJump(htJump)
kangaroo = nil
}
}
}
}
tk.mainloop()