0 Members and 1 Guest are viewing this topic.
Here is a cleaner code.Also, the reason(s) why it's not working the same on-calc is becuase the computer forces the refresh of the screen, but you have to call it yourself by calling [lua]platform.window:invalidate[/lua]Also, since you want the game to be always running (the AI), call it in a timer. (See code)I also made the duck images transparent (thanks to http://bwns.be/jim/sprite.html )Quick note for the optimizations : when you see stuff like "a = (b>1) and 3 or 2" it's the same as : "if b>1 then a = 3 else a = 2 end"And ... I recoded the AI (quickly)Cleaner Code with properindentations, separations of events and functions, init code at the end.Code: [Select]platform.apilevel = "1.0"----------------------------- Constants -----------------------------inc = 5 -- image movement incrementbg_gap = 50 -- background gap in coord-- X----------------------------- Events -----------------------------function on.paint(gc) -- Paint background gc:setColorRGB(0, 0, 0) gc:fillRect(0, 0, ww, wh) gc:drawImage(img_background, bg_gap, 0) if alive then gc:drawImage(img_duckAlive, dx, dy) else if dy + duck.h < 194 then gc:drawImage(img_duckDead, dx, dy) else gc:drawImage(img_dog, dx, dy-duck.h) end end gc:setColorRGB(255, 255, 255) gc:drawString(msg, 0, 0, "top")endfunction on.resize(width, height) cursor.set("translation") cursor.show() ww, wh = width, height platform.window:invalidate()endfunction on.mouseDown(x, y) if between(dx, x, dx + duck.w) and between(dy, y, dy + duck.h) then shot() else miss() end platform.window:invalidate()endfunction on.escapeKey() init() platform.window:invalidate()endfunction on.timer() AI() platform.window:invalidate()end----------------------------- Functions -----------------------------function init() dx = 100 dy = 50 dirX = 1 dirY = 1 alive = true x = 0 y = 0 msg = ""endfunction between(a, x, b) -- Checks if "x" is between range [a,b] return (a <= x) and (x <= b)endfunction AI() if alive then dx = dx + dirX*inc dy = dy + dirY*inc elseif dy + duck.h < 194 then dy = dy + inc end if dx <= bg_gap then dirX = 1 end if dx >= ww-duck.w then dirX = -1 end if dy <= 0 then dirY = 1 end if dy >= 170-duck.h then dirY = -1 endendfunction shot() alive = false msg = "Shot !"endfunction miss() -- Determine what happens if you missed a shot msg = "Missed !"end----------------------------- Init: ------------------------------- images --img_dog = image.new(dogSRC)img_duckAlive = image.new(duckAliveSRC)img_duckDead = image.new(duckDeadSRC)img_background = image.new(backSRC)duck = { h = image.height(img_duckAlive), -- 30 px w = image.width(img_duckAlive) -- 37 px}init()timer.start(0.05)
platform.apilevel = "1.0"----------------------------- Constants -----------------------------inc = 5 -- image movement incrementbg_gap = 50 -- background gap in coord-- X----------------------------- Events -----------------------------function on.paint(gc) -- Paint background gc:setColorRGB(0, 0, 0) gc:fillRect(0, 0, ww, wh) gc:drawImage(img_background, bg_gap, 0) if alive then gc:drawImage(img_duckAlive, dx, dy) else if dy + duck.h < 194 then gc:drawImage(img_duckDead, dx, dy) else gc:drawImage(img_dog, dx, dy-duck.h) end end gc:setColorRGB(255, 255, 255) gc:drawString(msg, 0, 0, "top")endfunction on.resize(width, height) cursor.set("translation") cursor.show() ww, wh = width, height platform.window:invalidate()endfunction on.mouseDown(x, y) if between(dx, x, dx + duck.w) and between(dy, y, dy + duck.h) then shot() else miss() end platform.window:invalidate()endfunction on.escapeKey() init() platform.window:invalidate()endfunction on.timer() AI() platform.window:invalidate()end----------------------------- Functions -----------------------------function init() dx = 100 dy = 50 dirX = 1 dirY = 1 alive = true x = 0 y = 0 msg = ""endfunction between(a, x, b) -- Checks if "x" is between range [a,b] return (a <= x) and (x <= b)endfunction AI() if alive then dx = dx + dirX*inc dy = dy + dirY*inc elseif dy + duck.h < 194 then dy = dy + inc end if dx <= bg_gap then dirX = 1 end if dx >= ww-duck.w then dirX = -1 end if dy <= 0 then dirY = 1 end if dy >= 170-duck.h then dirY = -1 endendfunction shot() alive = false msg = "Shot !"endfunction miss() -- Determine what happens if you missed a shot msg = "Missed !"end----------------------------- Init: ------------------------------- images --img_dog = image.new(dogSRC)img_duckAlive = image.new(duckAliveSRC)img_duckDead = image.new(duckDeadSRC)img_background = image.new(backSRC)duck = { h = image.height(img_duckAlive), -- 30 px w = image.width(img_duckAlive) -- 37 px}init()timer.start(0.05)
platform.apilevel = "1.0"----------------------------- Constants -----------------------------inc = 5 -- image movement incrementbg_gap = 50 -- background gap in coord-- Xsec = 0seclim = 32----------------------------- Events -----------------------------function on.paint(gc) -- Paint background gc:setColorRGB(0, 0, 0) gc:fillRect(0, 0, ww, wh) gc:drawImage(img_background, bg_gap, 0) if alive == 1 then gc:drawImage(img_duckAlive, dx, dy) else if dy + duck.h < 194 and alive ~= 2 then gc:drawImage(img_duckDead, dx, dy) else if alive ~= 2 then gc:drawImage(img_dog, dx, dy-duck.h) else gc:drawImage(img_duckAlive,dx,dy) AI() end end end if alive == 2 then gc:setColorRGB(0,0,0) gc:fillRect(100,50,65,25) gc:setColorRGB(255,255,255) gc:drawString("Fly Away",100,70) end gc:setColorRGB(255, 255, 255) gc:drawString(msg, 0, 0, "top")endfunction on.resize(width, height) cursor.set("crosshair") cursor.show() ww, wh = width, height platform.window:invalidate()endfunction on.mouseDown(x, y) if between(dx, x, dx + duck.w) and between(dy, y, dy + duck.h) then shot() else miss() end platform.window:invalidate()endfunction on.escapeKey() init() platform.window:invalidate()endfunction on.timer() sec=sec+0.5 AI() platform.window:invalidate() if sec >= seclim then alive = 2 platform.window:invalidate() endend----------------------------- Functions -----------------------------function init() dx = 100 dy = 50 dirX = 1 dirY = 1 alive = 1 x = 0 y = 0 msg = "" sec = 0 endfunction between(a, x, b) -- Checks if "x" is between range [a,b] return (a <= x) and (x <= b)endfunction AI() if alive == 1 then dx = dx + dirX*inc dy = dy + dirY*inc elseif dy + duck.h < 194 then dy = dy + inc end if alive == 2 then dx = dx - dirY*inc end if dx <= bg_gap then dirX = 1 end if dx >= ww-duck.w then dirX = -1 end if dy <= 0 then dirY = 1 end if dy >= 170-duck.h then dirY = -1 endendfunction shot() alive = 0 msg = "Shot !" seclim = seclim - 2endfunction miss() -- Determine what happens if you missed a shot msg = "Missed !"end----------------------------- Init: ------------------------------- images --img_dog = image.new(dogSRC)img_duckAlive = image.new(duckAliveSRC)img_duckDead = image.new(duckDeadSRC)img_background = image.new(backSRC)duck = { h = image.height(img_duckAlive), -- 30 px w = image.width(img_duckAlive) -- 37 px}init()timer.start(0.05)