Author Topic: Duck hunt nspire port!  (Read 9013 times)

0 Members and 1 Guest are viewing this topic.

Offline annoyingcalc

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1953
  • Rating: +140/-72
  • Found in Eclipse.exe
    • View Profile
Re: Duck hunt nspire port!
« Reply #15 on: September 29, 2012, 11:05:36 pm »
Never mind my previous post I fixed the problem!

Here is v4

added a time limit
added fly away
added fly away animation
added level time

Todo:
add main menu
optimize
add more dog and bird animations
more ducks
Maybe even dog mode
This used to contain a signature.

Offline Spenceboy98

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 547
  • Rating: +59/-2
    • View Profile
Re: Duck hunt nspire port!
« Reply #16 on: January 04, 2013, 05:06:31 pm »
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 increment
bg_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")
end

function on.resize(width, height)
    cursor.set("translation")
    cursor.show()
    ww, wh = width, height
    platform.window:invalidate()
end

function 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()
end

function on.escapeKey()
    init()
    platform.window:invalidate()
end

function 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 = ""
end

function between(a, x, b) -- Checks if "x" is between range [a,b]
    return (a <= x) and (x <= b)
end

function 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 end
end

function shot()
    alive = false
    msg = "Shot !"
end

function 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)

Is this the most recent code?
I like milk.

Offline annoyingcalc

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1953
  • Rating: +140/-72
  • Found in Eclipse.exe
    • View Profile
Re: Duck hunt nspire port!
« Reply #17 on: January 05, 2013, 01:09:26 pm »
No, here is the most recent code
Code: [Select]
platform.apilevel = "1.0"

-----------------------
------ Constants ------
-----------------------

inc = 5 -- image movement increment
bg_gap = 50 -- background gap in coord-- X
sec = 0
seclim = 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")
end

function on.resize(width, height)
    cursor.set("crosshair")
    cursor.show()
    ww, wh = width, height
    platform.window:invalidate()
end

function 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()
end

function on.escapeKey()
    init()
    platform.window:invalidate()
end

function on.timer()
sec=sec+0.5
    AI()
    platform.window:invalidate()
if sec >= seclim then
alive = 2
platform.window:invalidate()
end
end

-----------------------
------ Functions ------
-----------------------

function init()
    dx = 100
    dy = 50
    dirX = 1
    dirY = 1
    alive = 1
    x = 0
    y = 0
    msg = ""
sec = 0
end
function between(a, x, b) -- Checks if "x" is between range [a,b]
    return (a <= x) and (x <= b)
end

function 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 end
end

function shot()
    alive = 0
    msg = "Shot !"
seclim = seclim - 2
end

function 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)
This used to contain a signature.

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Re: Duck hunt nspire port!
« Reply #18 on: January 05, 2013, 01:21:56 pm »
Another nice improvement might be the use of a class for the duck (and other). That'll make it much more easy do code, and a lot cleaner.