Author Topic: Lunar Lander  (Read 5369 times)

Malkom

  • Administrator
  • Sr. Member
  • *****
  • Posts: 286
Lunar Lander
« on: October 02, 2011, 12:33:15 PM »
LUNAR LANDER

This is similar to the Java Lunar Lander except this HAC version adds buttons so it can work on Android devices that lack a trackerball.

It uses two buttons to start/stop the game and three buttons to guide the ship. The ship can also be controlled via the trackerball.
The speed gauge has areas for vertical, left and right speeds. If a speed is too high in any direction then that direction turns red and is green when safe. This example also has a Game Over screen whose color shows whether the ship landed safely or not.

http://www.hypernextandroid.com/ScriptSampleImages/LunarLanderCard.png



DEVICE SCREEN SHOT

This a screen shot of it running on an Android G1

http://www.hypernextandroid.com/ScriptSampleImages/LunarLander.png



COMPARE WITH JAVA

Lines of code (not counting empty lines and comments):-
HAC: 251
Java: 586

The Java version has more lines because it sets ups its display using code whereas HAC sets it up with is screen designer.
The Java version also needs an XML file to define its display.
http://developer.android.com/resources/samples/LunarLander/src/com/example/android/lunarlander/index.html



SCRIPTS

These are the scripts from the buttons, animation event, MainCode and menu handlers.

CARD open script

Code: [Select]
Call AnimSetup
Call LoadImages



BUTTON Start

Code: [Select]
Call SetupGame

AnimationRun



BUTTON Stop

Code: [Select]
AnimationStop


BUTTON Fire

Code: [Select]
Global fuelNow

if fuelNow>0 then
       Call EngineFire(1)
endif



BUTTON Left

Code: [Select]
Global fuelNow

if fuelNow>0 then
    Call EngineFire(2)
endif



BUTTON Right

Code: [Select]
Global fuelNow

if fuelNow>0 then
    Call EngineFire(3)
endif




MAINCODE - SPECIAL - ANIMATION EVENT

Code: [Select]
Global gameOverFlag
Global spriteID,shipPlain,firingFrames
Global shipX,shipY,deltaX,deltaY
Global pullGravity
Local okay

if gameOverFlag=1 then
    AnimationStop
    GotoLabel 1
Endif

@ gravity
Add pullGravity to deltaY

if fuelNow>0 then
    Call KeyAction
EndIf

Call DrawSpeedGauge

if firingFrames>0 then
    Decrement firingFrames
else
    SpriteFetchBank(spriteID,shipPlain)
endif

Add deltaX to shipX
Add deltaY to shipY

SpriteSetCoords(spriteID,shipX,shipY)

if (shipX<10) OR (shipX>275) OR (shipY>275) then
       Call GameOver
endif

@ +++ Tell to update canvas draws +++
AnimationEndFrame

label 1



ANIMATION LOCAL PROCEDURE - KeyAction

Code: [Select]
Local kval


@ Up trackerball center
Put SpriteKeyTestFN(11) into kval
if kval=1 then
    Call EngineFire(1)
    GotoLabel 1
endif

@ Up trackerball
Put SpriteKeyTestFN(126) into kval
if kval=1 then
    Call EngineFire(1)
    GotoLabel 1
endif

@ Left trackerball
Put SpriteKeyTestFN(123) into kval
if kval=1 then
    Call EngineFire(2)
    GotoLabel 1
endif

@ Right
Put SpriteKeyTestFN(124) into kval
if kval=1 then
    Call EngineFire(3)
    GotoLabel 1
endif

label 1



MAINCODE - CONSTANTS

Code: [Select]
@ Constants - pseudo global vars

@ +++ Ship constants for ImageBanks +++
Global shipPlain,shipFiring,shipCrashed
Put 2 into shipPlain
Put 3 into shipFiring
Put 4 into shipCrashed

@ +++ Physics +++
Global pullGravity,pushUp,pushSide
Global safeSpeed,firingFrames
Global easyGame,normalGame,hardGame

Put 0.1 into pullGravity

Put 2.0 into pushUp

Put 1.0 into pushSide

Put 8.0 into easyGame
Put 5.0 into normalGame
Put 2.0 into hardGame
 
Put easyGame into safeSpeed

@ how long firing ship sprite lasts
Put 0 into firingFrames



MAINCODE - VARIABLES

Code: [Select]
@ Global variables

Global gameOverFlag
Global spriteID,shipWidth,shipHeight

Global shipX,shipY
Global fuelNow,fuelMax
Put 100 into fuelMax

Global deltaX,deltaY,deltaYOld



GLOBAL PROCEDURE - SetupGame

Code: [Select]
Global gameOverFlag
Global animWidth,animHeight
Global shipID,shipX,shipY
Global deltaX,deltaY

Global fuelMax,fuelNow,firingFrames

DebugMessage('Animation','SetupGame Starting')

Reset gameOverFlag

AnimationBackDrop(1)
AnimationUpdate

Call DrawLandingPad

@ +++ Setup Displays +++
Put fuelMax into fuelNow

Reset firingFrames

Call RefreshPanel

@ +++ Background graphics +++
Call DrawLandingPad

@ +++ Place ship +++
Local xmax,ymax,result

Reset deltaX
Reset deltaY
Reset deltaYOld

Put animWidth into xmax
Subtract 50 from xmax
Put RndFN(50,xmax) into shipX

Put 40 into shipY



GLOBAL PROCEDURE - DrawFuelGauge

Code: [Select]
Global fuelNow,fuelMax
Local mid,top

GraphicsDirectionSet(1)

CanvasSetColor(1,0,255,255)
CanvasFillRect(1,20,5,fuelNow,10)

CanvasSetColor(1,0,0,0)
Put 20 into mid
Add fuelNow to mid

Put fuelMax into top
Subtract fuelNow from top
CanvasFillRect(1,mid,5,top,10)

GraphicsDirectionSet(0)



GLOBAL PROCEDURE - DrawSpeedGauge

Code: [Select]
Global deltaX,deltaY,safeSpeed

GraphicsDirectionSet(1)

@ Horizontal speed
CanvasSetColor(1,0,0,0)
CanvasFillRect(1,30,30,50,10)

if deltaX<>0 then
    CanvasSetColor(1,255,0,0)
    if deltaX<0 then
        CanvasFillRect(1,30,30,30,10)
    else
        CanvasFillRect(1,50,30,30,10)
    endif
else
    CanvasSetColor(1,0,255,0)
    CanvasFillRect(1,30,30,50,10)
endif

@ Vertical speed
if deltaY>safeSpeed then
    CanvasSetColor(1,255,0,0)
else
    CanvasSetColor(1,0,255,0)
endif
CanvasFillRect(1,50,40,10,50)

GraphicsDirectionSet(0)


 
GLOBAL PROCEDURE - DrawLandingPad

Code: [Select]
Global animWidth,animHeight
Local x1,y1,x2,y2,midx

GraphicsDirectionSet(1)

CanvasSetColor(1,0,0,255)

Put animHeight into y1
Subtract 10 from y1

Put animWidth into x1
Divide x1 by 2
Subtract 50 from x1

CanvasFillRect(1,x1,y1,100,10)

GraphicsDirectionSet(0)



GLOBAL PROCEDURE - LoadImages

Code: [Select]
Global spriteID,shipWidth,shipHeight
Global shipPlain,shipFiring,shipCrashed

Local result,n,x,y,w,h,mess,fname,priority Local imagebase,fishimage,direct

ImageBankReserve(4)

Put 'Local:earthrise2.png' into fname
ImageBankLoad(1,fname,0)
AnimationBackDrop(1)
AnimationUpdate

Put 'Local:lander_plain.png' into fname
ImageBankLoad(shipPlain,fname,0)

Put 'Local:lander_firing.png' into fname
ImageBankLoad(shipFiring,fname,0)

Put 'Local:lander_crashed.png' into fname ImageBankLoad(shipCrashed,fname,0)

Put ImageBankWidthFN(shipPlain) into shipWidth Put ImageBankHeightFN(shipPlain) into shipHeight

Put SpriteCreateFN(shipPlain,-100,-100,shipWidth,shipHeight,1,shipPlain,0) into spriteID

AnimationReorder
AnimationUpdate



GLOBAL PROCEDURE - AnimSetup

Code: [Select]
Global animWidth,animHeight

AnimationClear

AnimationSetCoords(10,10)

Put 300 into animWidth
Put 360 into animHeight
AnimationSetSize(animWidth,animHeight)
AnimationStop
AnimationShow
AnimationEnable
AnimationSetPeriod(40)



GLOBAL PROCEDURE - RefreshPanel

Code: [Select]
Call DrawFuelGauge

Call DrawSpeedGauge



GLOBAL PROCEDURE - EngineFire

Code: [Select]
@ direction:- 1 up, 2 left, 3 right
Global fuelNow,deltaX,deltaY
Global pushUp,pushSide
Global spriteID,shipFiring,firingFrames

Local direct,res

Subtract 10 from fuelNow
Call DrawFuelGauge

Put direction  into direct

if direct=1 then
    Subtract pushUp from deltaY
       else
        if direct=2 then
            Subtract pushSide from deltaX
        else
            Add pushSide to deltaX
        endif
endif

Put 5 into firingFrames

SpriteFetchBank(spriteID,shipFiring)



GLOBAL PROCEDURE - GameOver

Code: [Select]
Global gameOverFlag,spriteID,shipPlain,shipCrashed Global animWidth,animHeight
Local xmid,ymid,x1,y1,safelanding,xlow,xhigh

Set gameOverFlag

@ calc middle of animation
Put animWidth into xmid
Divide xmid by 2
Put animHeight into ymid
Divide ymid by 2

Put 0 into safelanding
@ Check if crashed, speed and landing pad if (deltaY<=safeSpeed) and (deltaX=0) then
    Put 1 into safelanding
    Put xmid into xlow
    Subtract 50 from xlow
    Put xmid into xhigh
    Add 50 to xhigh
    Subtract shipWidth from xhigh
    if (shipX>=xlow) and (shipX<=xhigh) then
        Put 2 into safelanding
    endif
endif

@ +++++ Graphics ++++++
GraphicsDirectionSet(1)

@ +++ Centralize +++
@ x
Put xmid into x1
Subtract 100 from x1
@ y
Put ymid into y1
Subtract 15 from y1

@ +++ Box +++
CanvasSetColor(1,255,255,255)
CanvasDrawRect(1,x1,y1,200,30)

@ +++ Text +++
CanvasSetFontSize(1,20)
if (safelanding=2) then
    CanvasSetColor(1,0,255,0)
    SpriteFetchBank(spriteID,shipPlain)
else
    if safelanding=0 then
        CanvasSetColor(1,255,0,0)
    else
        CanvasSetColor(1,0,0,255)
    endif
    SpriteFetchBank(spriteID,shipCrashed) endif

Add 50 to x1
Add 23 to y1

CanvasDrawText(1,'GAME OVER',x1,y1)

GraphicsDirectionSet(0)



MENU - Quit

Code: [Select]
QuitSave(0)


MENU - Easy

Code: [Select]
Global safeSpeed,easylGame

Put easyGame into safeSpeed



MENU - Normal

Code: [Select]
Global safeSpeed,normalGame

Put normalGame into safeSpeed



MENU - Hard

Code: [Select]
Global safeSpeed,hardGame

Put hardGame into safeSpeed


« Last Edit: October 02, 2011, 12:37:44 PM by Malkom »
I am sorry but I do not have time to answer questions by PM or email.
If you post your questions in this forum then it might help others.

 

anything