LUNAR LANDERThis 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.pngDEVICE SCREEN SHOTThis a screen shot of it running on an Android G1
http://www.hypernextandroid.com/ScriptSampleImages/LunarLander.pngCOMPARE WITH JAVALines 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.htmlSCRIPTSThese are the scripts from the buttons, animation event, MainCode and menu handlers.
CARD open scriptCall AnimSetup
Call LoadImages
BUTTON StartCall SetupGame
AnimationRun
BUTTON StopAnimationStop
BUTTON FireGlobal fuelNow
if fuelNow>0 then
Call EngineFire(1)
endif
BUTTON LeftGlobal fuelNow
if fuelNow>0 then
Call EngineFire(2)
endif
BUTTON RightGlobal fuelNow
if fuelNow>0 then
Call EngineFire(3)
endif
MAINCODE - SPECIAL - ANIMATION EVENT 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 - KeyActionLocal 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 @ 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@ Global variables
Global gameOverFlag
Global spriteID,shipWidth,shipHeight
Global shipX,shipY
Global fuelNow,fuelMax
Put 100 into fuelMax
Global deltaX,deltaY,deltaYOld
GLOBAL PROCEDURE - SetupGameGlobal 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 - DrawFuelGaugeGlobal 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 - DrawSpeedGaugeGlobal 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 - DrawLandingPadGlobal 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 - LoadImagesGlobal 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 - AnimSetupGlobal animWidth,animHeight
AnimationClear
AnimationSetCoords(10,10)
Put 300 into animWidth
Put 360 into animHeight
AnimationSetSize(animWidth,animHeight)
AnimationStop
AnimationShow
AnimationEnable
AnimationSetPeriod(40)
GLOBAL PROCEDURE - RefreshPanelCall DrawFuelGauge
Call DrawSpeedGauge
GLOBAL PROCEDURE - EngineFire@ 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 - GameOverGlobal 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 - QuitQuitSave(0)
MENU - EasyGlobal safeSpeed,easylGame
Put easyGame into safeSpeed
MENU - NormalGlobal safeSpeed,normalGame
Put normalGame into safeSpeed
MENU - HardGlobal safeSpeed,hardGame
Put hardGame into safeSpeed