Share My Creation lunar lander

colin9876

Active Member
Licensed User
Ive just updated this simple game to use timers rather than just a loop.
Touch screen to boost the rocket upwards. Dont land too fast or it will crash!

B4X:
Sub Process_Globals
End Sub

Sub Globals
Dim h,oldh,gap, thrust As Float
Dim canvas1 As Canvas
Dim Timer1 As Timer
End Sub

Sub Activity_Create(FirstTime As Boolean)
    canvas1.Initialize(Activity)
    If FirstTime Then
        Timer1.Initialize("Timer1", 50)
    End If
    Timer1.Enabled = True
    h=8%y
    thrust=0
    gap=1%y   
End Sub

Sub Timer1_Tick
    oldh=h
    h=h+thrust
    thrust=thrust+(gap/50)
    canvas1.DrawLine(45%x,oldh,55%x,oldh,Colors.Black,4%Y)
    canvas1.DrawLine(45%x,h,55%x,h,Colors.Yellow,4%Y)
    Activity.Invalidate
    If h>97%Y Then crash
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Activity_Touch (Action As Int, X As Float, Y As Float)
    thrust=thrust-gap/4
End Sub

Sub crash
    canvas1.DrawColor(Colors.Black)
    If thrust <gap Then canvas1.DrawText("Landed", 5%x, 20%y,Typeface.DEFAULT, 30, Colors.Blue,"LEFT")
    If thrust>=gap Then canvas1.DrawText("CRASH", 5%x, 20%y,Typeface.DEFAULT, 30,Colors.Red,"LEFT")
    h=8%y
    thrust=0
End Sub
 
Last edited:

etLux

Member
Licensed User
Neat! This could be addictive... lol.

GMailed to my Droid X and it installed fine.

Runs fine the first time -- but when I
reopen it, it no longer responds to
finger taps. I have to Force Stop
first to get it going again.

Also, how is this scaled? On the Droid X
screen, it only covers about 2/3 across
on the screen. Can you autoscale
Canvas work like this? (I'm new at this.)

Side Note:

I'd love to see a "Test Zone" on the
forum, where members could volunteer
to check-out each other's apps...

Best,


David
-----
David Sosnowski
www.DavidSosnowski.com
 

Erel

Administrator
Staff member
Licensed User
DoEvents is usually not required. DoEvents is only required if you are doing a long loop and want to update the screen during this loop. In most cases a Timer is preferred.

Invalidate is required after drawing. I could have added a call to invalidate after each drawing.
When you design an API you should consider several factors. Usability and performance are always considered.
Calling Invalidate after each drawing is wasteful. In many cases you need to do several drawings and only then update the screen (for performance reasons). It would not have been possible if Invalidate was called internally.

Basic4android performance is similar to native applications performance in most cases. I think that this is important if you want to write large, complex applications with Basic4android.
 
Top