Microsoft Small Basic

Program Listing: GDP775
'GravityGame version 1

'Copyright (C) 2010 Softabar
'
'This program is free software: you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation, either version 3 of the License, or
'(at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
'GNU General Public License for more details.
'
'You should have received a copy of the GNU General Public License
'along with this program. If not, see .
'
'Check our web site:
'http://www.softabar.com
'
'There may be more Small Basic stuff. Other stuff as well.


GraphicsWindow.MouseDown=OnMouseDown
GraphicsWindow.MouseMove=OnMouseMove
GraphicsWindow.MouseUp=OnMouseUp
GraphicsWindow.KeyUp=OnKeyUp
GraphicsWindow.Title="Gravity Game v1"
bgColor="black"
fgColor="white"
GraphicsWindow.BackgroundColor=bgColor
GraphicsWindow.BrushColor=fgColor
GraphicsWindow.PenColor=fgColor

width=640
height=480
GraphicsWindow.Width=width
GraphicsWindow.Height=height

objectMass=50000
objectRadius=20
startSimulation=0

Main()

Sub SetVars
staticObjectX=Math.GetRandomNumber(width-objectRadius)
staticObjectY=Math.GetRandomNumber(height-objectRadius)

startPointX=Math.GetRandomNumber(width-25)
While startPointX<25
startPointX=Math.GetRandomNumber(width-25)
EndWhile

goalPointX=(width/2)-25

EndSub

Sub Init
staticObject["x"]=staticObjectX
staticObject["y"]=staticObjectY
staticObject["sx"]=0.0
staticObject["sy"]=0.0
staticObject["mass"]=objectMass
staticObject["r"]=objectRadius

'movingParticle is particle that moves in the gravity field
movingParticle["x"]=startPointX
movingParticle["y"]=height
movingParticle["sx"]=0.0
movingParticle["sy"]=0.0
movingParticle["mass"]=1000
movingParticle["r"]=4

EndSub


Sub Main
SetVars()
Init()
While 1=1
If startSimulation=1 Then
Move()
EndIf
Draw()
xc=movingParticle["x"]
yc=movingParticle["y"]

'check if goal
If (xc>goalPointX And xc<(goalPointX+50)) And (yc<5) Then
startSimulation=0
GraphicsWindow.FontSize=30
GraphicsWindow.DrawBoundText(width/4,(height/2)-50,width/2,"Congratulations!")
Program.Delay(1000)
GraphicsWindow.ShowMessage("You made it! Start new.","Congratulations")
Reset()
Goto end
EndIf
'check out of bounds
If (xc<0 Or xc>width Or yc<0 Or yc>height) Then
startSimulation=0
GraphicsWindow.ShowMessage("Out of bounds. Try Again","Out of Bounds")
Init()
Draw()
EndIf

'check collision
If (xc>staticObject["x"] And xc<(staticObject["x"]+objectRadius)) And (yc>staticObject["y"] And yc<(staticObject["y"]+objectRadius)) Then
startSimulation=0

For i = 1 To 100 Step 5
GraphicsWindow.DrawEllipse(xc- (i) yc- (i) , i * 2, i * 2)
Program.Delay(20)
EndFor
Program.Delay(500)
GraphicsWindow.ShowMessage("Collision. Try Again","Collision")
Init()
Draw()

EndIf

end:
If startSimulation=0 Then
Program.Delay(30)
EndIf
EndWhile
EndSub

Sub Move

xdiff=staticObject["x"]-movingParticle["x"]
ydiff=staticObject["y"]-movingParticle["y"]
distance=Math.SquareRoot(xdiff*xdiff+ydiff*ydiff)
If distance<10 then
distance=10
EndIf
force=0.125*(movingParticle["mass"]*staticObject["mass"])/(distance*distance)
acceleration=force/movingParticle["mass"]
xc=xdiff/distance
yc=ydiff/distance
movingParticle["sx"]=movingParticle["sx"]+acceleration*xc
movingParticle["sy"]=movingParticle["sy"]+acceleration*yc

movingParticle["x"]=movingParticle["x"]+movingParticle["sx"]
movingParticle["y"]=movingParticle["y"]+movingParticle["sy"]

Program.Delay(20)

EndSub


Sub Draw
GraphicsWindow.FillEllipse(staticObject["x"], staticObject["y"], staticObject["r"],staticObject["r"])

'draw goal
GraphicsWindow.FillRectangle(goalPointX,0,50,5)

'draw start
GraphicsWindow.FillRectangle(startPointX-25,height-5,50,5)

'draw movingParticle
GraphicsWindow.FillEllipse(movingParticle["x"], movingParticle["y"],movingParticle["r"],movingParticle["r"])

GraphicsWindow.FontSize=12
GraphicsWindow.DrawText(width-120,5,"Press 'R' to reset")

EndSub

Sub SetObjects

EndSub

Sub OnMouseUp
startSimulation=1
EndSub


Sub OnMouseDown
prevX = GraphicsWindow.MouseX
prevY = GraphicsWindow.MouseY
EndSub

Sub OnMouseMove
If startSimulation=0 Then
If Mouse.IsLeftButtonDown Then
GraphicsWindow.Clear()
Draw()
EndIf
x = GraphicsWindow.MouseX
y = GraphicsWindow.MouseY
If Mouse.IsLeftButtonDown Then
GraphicsWindow.DrawLine(startPointX, height, x, y)
'set moving particle speed
movingParticle["sx"]=(x-startPointX)/100
movingParticle["sy"]=(y-height)/100
EndIf
EndIf
EndSub

Sub Reset
startSimulation=0
GraphicsWindow.Clear()
SetVars()
Init()
Draw()
EndSub

Sub OnKeyUp
If GraphicsWindow.LastKey="R" Then
Reset()
EndIf
EndSub