## Gravity test program

Game development specific discussions.
neil
Posts: 594
Joined: Mar 17, 2022 23:26

### Gravity test program

The gravity test program has been updated with scoring fuel and ships are working.
You have to land before you run out of fuel. When you land perfectly you get 100 points.
When you crash or run out of fuel you lose a ship. Also I added legs to the spacecraft.
You only use fuel when you use the rocket engines.

Code: Select all

``````' gravity test program by neil

Screenres 640,480,32,,1 'fullscreen
'Screenres 640,480 'small screen

Dim As ULong fps,wht,ylw,sc
Dim AS UByte en,shp
Dim AS Single x, y, vx, vy, fuel
Dim As String key
ylw = rgb(255,255,0)
wht = rgb(255,255,255)
setmouse 0,0,0
Function Regulate(Byval MyFps As Long,Byref fps As Long) As Long
Static As Double timervalue,_lastsleeptime,t3,frames
frames+=1
If (Timer-t3)>=1 Then t3=Timer:fps=frames:frames=0
Var sleeptime=_lastsleeptime+((1/myfps)-Timer+timervalue)*1000
If sleeptime<1 Then sleeptime=1
_lastsleeptime=sleeptime
timervalue=Timer
Return sleeptime
End Function

start:
sc = 0:shp = 5

resume_game:
x = 320 ' Initial x position
y = 40 ' Initial y position
vx = .05 ' Initial x velocity
vy = .05 ' Initial y velocity

fuel = 1000 ' Initial fuel level
en = 0

DO
Screenlock
Cls

' Draw the lander
CIRCLE (x, y), 10, ylw ' Draw the body
LINE (x - 12, y + 10)-(x + 12, y + 10), ylw ' the lander bar
LINE (x - 10, y + 10)-(x + 15, y + 20), ylw ' the lander leg
LINE (x - 15, y + 20)-(x + 10, y + 10), ylw ' the lander leg
LINE (10 - 10, 400 + 10)-(540 + 100, 400 + 10), wht ' Moonscape

' Display fuel level
locate 2,2:Print "Score ";sc
locate 2,35:print "Fuel: "; fuel
locate 2,70:Print "Ships = ";shp

'locate 4,1:print "gravity = .005"
'locate 6,1:print "Y Velocity = ";vy
'locate 8,1:print "X Velocity = ";vx
'locate 10,1:print "X Coordinate= ";x
'locate 12,1:print "Y Coordinate = ";y
'locate 14,1:print "FPS = ";fps
Screenunlock

' Update lander position based on velocity
x += vx
y += vy

' Apply gravity
vy += .005

if fuel > 0 Then
' Move space ship with arrow keys
IF multikey(&H4B) and multikey(&H4D) = 0 THEN  vx -= .05:fuel -= 2 ' Move left
IF multikey(&H4D) and multikey(&H4B) = 0 THEN vx += .05:fuel -= 2 ' Move right
IF multikey(&H48) and multikey(&H50) = 0 THEN vy = vy - .05:fuel -= 5 ' Move up
if fuel <= 0 Then fuel = 0
End If
key = Inkey

' Check for collision with the ground
if x < -100 or x > 700 or y < -50 Then y = 393
IF y > 392 THEN
Locate 8,35: PRINT "Crash!"
shp -= 1
locate 2,70:Print "Ships = ";shp
SLEEP 2000,1
if shp = 0 Then
Locate 14,33:Print "Game Over!"
Locate 20,20:Print "Press (Y) To Play again or (N) to Quit!"

DO
if Multikey(&H15) Then Goto start
if Multikey(&H31) Then End
sleep 10,1
Loop until multikey(&H01)
en = 1
End If
if en = 1 Then End
goto resume_game
END IF

' Check for successful landing
IF vy <= 0 AND ABS(vx) <= 2 AND y >= 386 THEN
Locate 8,30: PRINT "Landing successful!"
sc += 100
locate 2,2:Print "Score ";sc
SLEEP 3000,1
goto resume_game
END IF

' set at 60 fps lower number to slow down
Sleep regulate(60,fps),1
Loop Until (key = Chr(27)) Or (key = Chr(255) & "k")``````
Last edited by neil on Jan 26, 2024 20:09, edited 4 times in total.
Posts: 2594
Joined: May 24, 2007 22:10
Location: The Netherlands

### Re: Gravity test program

neil wrote: Jan 25, 2024 0:23Could someone let me know if the gravity simulation for the moon is OK?
Both acceleration and gravity implementation look good. For a real simulation with "meters" en "seconds", numbers will be different, but for a more game-like use, that is not so relevant.
neil
Posts: 594
Joined: Mar 17, 2022 23:26

### Re: Gravity test program

The gravity test program has been updated. Please read the updated description in my opening post.
David Watson
Posts: 59
Joined: May 15, 2013 16:48
Location: England

### Re: Gravity test program

Your program looks good so far, but it would be better if you only burn fuel when thrusting.
neil
Posts: 594
Joined: Mar 17, 2022 23:26

### Re: Gravity test program

@David Watson
The code has been updated. Now you only use fuel when you use the rocket engines.