Microsoft Small Basic

Program Listing: NVC065
'Lunar lander - M.Featherstone
'26thApril 2012

GraphicsWindow.BackgroundColor = "Black"
GraphicsWindow.Width = 800
GraphicsWindow.Height = 600
GraphicsWindow.canResize = "false"
GraphicsWindow.Title = "Lunar Lander"
GraphicsWindow.Left = (Desktop.Width - 800) / 2
GraphicsWindow.Top = (Desktop.Height - 600) / 2
GraphicsWindow.KeyDown = KeyDown
GraphicsWindow.KeyUp = KeyUp
GraphicsWindow.FontName = "Comic Sans MS"
GraphicsWindow.FontSize = 36
GraphicsWindow.BrushColor = "White"


root = Program.Directory
backdrop = ImageList.LoadImage(root + "\star_field800x600.jpg")
surface = ImageList.LoadImage(root + "\moon_surface_slice.png")
pad = Shapes.AddImage(root + "\pad.png")
ship = Shapes.AddImage(root + "\ship.png")
shipThrust = Shapes.AddImage(root + "\ship2.png")
shipThrust2 = Shapes.AddImage(root + "\ship3.png")
explode = Shapes.AddImage(root + "\explode.png")
Shapes.HideShape(explode)
'dot = Shapes.AddEllipse(20,20)
GraphicsWindow.Show()
score = 0
maxFuel = 999
SetMaxFuel()
level = 0

Sub Initialise
up = 0
left = 0
right = 0
thrust = 0
turnRate = 2
angle = 0
velocityX = 0
velocityY = 0
shipPosX = 400
shipPosY = 0
gravity = 0.1
fuel = maxFuel
Shapes.Move(ship,400,0)
padX = 350 '50 + Math.GetRandomNumber(550)
padY = 550
Shapes.Move(pad,padX,padY)
GraphicsWindow.DrawImage(backdrop,0,0)
GraphicsWindow.DrawResizedImage(surface, 0, 541, GraphicsWindow.Width, GraphicsWindow.Height-541)
counter = 0
mode = "falling"
fuelColour = "Blue"
speedColour = "Blue"
EndSub

Initialise()

Main_Loop:

startTime = Clock.ElapsedMilliseconds
If mode = "falling" Then
dirX = 0
dirY = 0
If up = 1 And fuel>0 then
UpPressed()
dirX = 0 * thrust
dirY = -1 * thrust
Stack.PushValue("p",dirX)
Stack.PushValue("p",dirY)
Stack.PushValue("p",angle)
RotVecRet()
dirX = Stack.PopValue("p")
dirY = Stack.PopValue("p")
EndIf
If left = 1 then
LeftPressed()
EndIf
If right = 1 then
RightPressed()
EndIf

velocityX = velocityX + dirX
velocityY = velocityY + dirY
velocityY = velocityY + gravity
Print()
shipPosX = shipPosX + velocityX
shipPosY = shipPosY + velocityY
If Up = 1 And fuel>0 And thrust>0 Then
If Math.Remainder(counter,2)=1 then
Shapes.ShowShape(shipThrust)
Shapes.Rotate(shipThrust,angle)
Shapes.Move(shipThrust,shipPosX,shipPosY)
Shapes.HideShape(shipThrust2)
else
Shapes.ShowShape(shipThrust2)
Shapes.Rotate(shipThrust2,angle)
Shapes.Move(shipThrust2,shipPosX,shipPosY)
Shapes.HideShape(shipThrust)
EndIf
Shapes.HideShape(ship)
Else
Shapes.ShowShape(ship)
Shapes.Rotate(ship,angle)
Shapes.Move(ship,shipPosX,shipPosY)
Shapes.HideShape(shipThrust)
Shapes.HideShape(shipThrust2)
EndIf
EndIf
'GraphicsWindow.BrushColor = "Red"
'Shapes.Move(dot,shipPosX,shipPosY)

tooFast = "false"
CheckLandingSpeed()
If shipPosY > 520 And mode = "falling" Then
If tooFast = "false" And shipPosX>padX And shipPosX<(padX+80) And (angle<10 Or angle>350) Then
angle = 0
Shapes.Rotate(shipThrust,angle)
mode = "landed"
Landed()
NewLevel()
fuel = maxFuel
Shapes.Move(pad,padX,padY)
Else
Die()
EndIf
ElseIf shipPosY < -60 And mode <> "dead" Then
Die()
ElseIf shipPosX < -64 Then
shipPosX = GraphicsWindow.Width-1
ElseIf shipPosX > GraphicsWindow.Width Then
shipPosX = -63
EndIf

If mode = "dead" Then
opacity = opacity - 3
If opacity<0 Then
opacity = 0
GraphicsWindow.ShowMessage("Game over, you scored "+score,"Crash landing!")
score = 0
SetMaxFuel()
Initialise()
EndIf
Shapes.SetOpacity(explode,opacity)
EndIf

UpdateFuel()
UpdateSpeed()

elapsed = Clock.ElapsedMilliseconds - startTime
While (40-elapsed)>0
elapsed = Clock.ElapsedMilliseconds - startTime
EndWhile
Goto Main_Loop

Sub Print
If counter = 5 then
counter = 0
'GraphicsWindow.FontSize = 10
'GraphicsWindow.BrushColor = "Black"
'GraphicsWindow.FillRectangle(0,0,200,30)
'GraphicsWindow.BrushColor = "White"
'GraphicsWindow.DrawBoundText(0,0, 200, "x="+shipPosX+", y="+shipPosY)
'GraphicsWindow.DrawBoundText(0,0, 200, "vx="+velocityX+", vy="+velocityY)
EndIf
counter = counter + 1
EndSub


Sub KeyDown
k = GraphicsWindow.LastKey
If k = "Left" Then
left = 1
EndIf
If k = "Right" then
right = 1
EndIf
If k = "Up" Then
up = 1
EndIf
EndSub

Sub KeyUp
k = GraphicsWindow.LastKey
If k = "Left" Then
left = 0
EndIf
If k = "Right" then
right = 0
EndIf
If k = "Up" Then
up = 0
EndIf
EndSub

Sub RotVecRet
RotVecRet_rad = Math.GetRadians( Stack.PopValue("p") )
RotVecRet_y = Stack.PopValue("p")
RotVecRet_x = Stack.PopValue("p")

RotVecRet_cs = Math.Cos(RotVecRet_rad)
RotVecRet_sn = Math.Sin(RotVecRet_rad)

RotVecRet_px = RotVecRet_x * RotVecRet_cs - RotVecRet_y * RotVecRet_sn
RotVecRet_py = RotVecRet_x * RotVecRet_sn + RotVecRet_y * RotVecRet_cs

Stack.PushValue("p",RotVecRet_py)
Stack.PushValue("p",RotVecRet_px)
EndSub

Sub UpdateFuel

If up = 1 Then
fuel = fuel - 1
EndIf
If fuel<0 Then
fuel = 0
EndIf
GraphicsWindow.FontSize = 20
GraphicsWindow.BrushColor = "Grey"
GraphicsWindow.FillRectangle(600,0,200,45)
FuelLeft()
GraphicsWindow.BrushColor = fuelColour
GraphicsWindow.DrawBoundText(600,0, 200, "Fuel:" + fuel)

EndSub

Sub UpdateSpeed

speed = Math.SquareRoot( velocityX*velocityX + velocityY*velocityY )
speed = speed * 10
speed = Math.Round(speed)
CurrentSpeed()
GraphicsWindow.BrushColor = speedColour
GraphicsWindow.DrawBoundText(600,20,200, "Speed:" + speed)

EndSub

Sub Die
Shapes.HideShape(ship)
Shapes.HideShape(shipThrust)
Shapes.HideShape(shipThrust2)
Shapes.Move(explode,shipPosX,shipPosY)
Shapes.ShowShape(explode)
opacity = 100
mode = "dead"
level = 0
EndSub

Sub Landed
mode = "landed"
level = level + 1
score = score + fuel + level*10
GraphicsWindow.ShowMessage("Aced, you scored "+score,"Eagle has landed!")
Initialise()
EndSub

'******************************************************************************************
'******************************************************************************************
' alter the code below here only
'******************************************************************************************
'******************************************************************************************

'the player pressed the UP Arrow button
Sub UpPressed

thrust = 0.2

EndSub


Sub CheckLandingSpeed

If speed > 30 Then
tooFast = "true"
EndIf

EndSub


'set the starting fuel level
Sub SetMaxFuel

maxFuel = 200

EndSub



'the player pressed the LEFT Arrow button
Sub LeftPressed

angle = angle - 1

EndSub



'the player pressed the RIGHT Arrow button
Sub RightPressed

angle = angle + 1

EndSub



'called when the player lands and a new level begins
Sub NewLevel

padX = Math.GetRandomNumber(550)
If maxFuel > 50 Then
maxFuel = maxFuel - 10
EndIf

EndSub



'set the colour of the fuel text
Sub FuelLeft

fuelColour = "Green"
If fuel < 20 Then
fuelColour = "Red"
EndIf

EndSub



'set the colour of the speed text
Sub CurrentSpeed

speedColour = "Green"
If tooFast = "true" Then
SpeedColour = "Red"
EndIf

EndSub