Post by pwhuk on Mar 3, 2005 21:01:30 GMT 1
I had a few minutes to spare so made this simple game, here is the code for it, hope you enjoy.
Just copy, paste, and play!!
Just copy, paste, and play!!
`Setup global variables
global ang#,speed#,start,stx,sty,level=1,ball=1
`Initiate newton world, and setup first level
hide mouse
NDB_NewtonCreate
makelevel()
`Main program loop
do
`Simple Camera control
Set camera to follow object position x(1),object position y(1),object position z(1), ang#, 150, 50, 100, 1
`Create a delay for ball to land at start, and process simple checks
if start>500 then checkkey() else inc start
checkfall()
checkwon()
`Display data and update world
text 10,10,"Level : "+str$(level)
text 10,20,"Ball : "+str$(ball)
NDB_NewtonUpdate
loop
`Destroy world on exit
NDB_NewtonDestroy
end
`Check if ball falls off platform
function checkfall()
`Check if ball is lower than platform and destroy ball
if object position y(1)<-100
NDB_NewtonDestroyBody ndb_getbody(1)
`Reset variables and gravity
speed#=0
ang#=0
start=0
NDB_SetVector 0,-50.0,0
NDB_SetStandardGravity
`Create a new ball and increase ball count
makeball(stx,sty)
inc ball
endif
endfunction
`Check if level is complete
function checkwon()
`Check to see if ball colides with goal
if object collision (1,2) = 1
`Destroy world and set up a new wolrd
ndb_newtondestroy
ndb_newtoncreate
`Reset variables and gravity
speed#=0
ang#=0
NDB_SetVector 0,-50.0,0
NDB_SetStandardGravity
`Make a new level and increase level number
makelevel()
inc level
endif
endfunction
`Check for keystrokes
function checkkey()
`Check if a keystroke was made
if scancode()<>0
`Change gravity variables depending on keystate
if leftkey()=1 then ang#=wrapvalue(ang#-0.5)
if rightkey()=1 then ang#=wrapvalue(ang#+0.5)
if upkey()=1 then inc speed#,0.1
if downkey()=1 then dec speed#,0.1
else
`Reset gravity variables if no key is pressed
if int(speed#)>0 then dec speed#,0.1
if int(speed#)<0 then inc speed#,0.1
if int(speed#)=0 then speed#=0
endif
`Set gravity with new settings
NDB_SetVector speed#*sin(ang#), -50.0, speed#*cos(ang#)
NDB_SetStandardGravity
endfunction
`Display game complete screen
function complete()
`Destroy newton world
ndb_newtondestroy
do
`Display winning text
text 10,10,"Congratulations, You Win"
sync
loop
endfunction
`Make a new level
function makelevel()
`Read dimensions of world, if dimension is 0 then game complete
read ii
if ii=0 then complete()
read jj
`Loop through data to build world
for j=1 to jj
for i=1 to ii
read block
`Create platform
if block=1 then makebox(i,j)
`Create platform and ball
if block=2 then makebox(i,j) : makeball(i,j)
`Create platform and goal
if block=3 then makebox(i,j) : makegoal(i,j)
next i
next j
`Reset timer
start=0
endfunction
`Make a platform section
function makebox(x,y)
`Find a free object
obj=findobj()
`Create newton data for box dimensions
Col = NDB_NewtonCreateBox(100, 5, 100)
Body = NDB_NewtonCreateBody(Col)
`Position object
NDB_BuildMatrix 0, 0, 0, x*100, 0, y*-100
NDB_NewtonBodySetMatrix Body
NDB_NewtonReleaseCollision Col
`Make visual representation for object and link to newton object
make object box obj,100,5,100
position object obj,x*100,0,y*-100
color object obj,RGB(255,255,255)
NDB_BodySetDBProData Body, obj
NDB_NewtonBodySetDestructorCallback Body
endfunction
`Make the goal object
function makegoal(x,y)
`Create newton data for goal dimensions
Col = NDB_NewtonCreatebox(30,100,30)
Body = NDB_NewtonCreateBody(Col)
`Position object
NDB_BuildMatrix 0, 0, 0, x*100, 50, y*-100
NDB_NewtonBodySetMatrix Body
NDB_NewtonReleaseCollision Col
`Make visual representation for object and link to newton object
make object box 2,30,100,30
position object 2,x*100,50,y*-100
color object 2 ,RGB(0,0,255)
NDB_BodySetDBProData Body, 2
NDB_NewtonBodySetDestructorCallback Body
endfunction
`Make the ball
function makeball(x,y)
`Create newton data for the ball
Col = NDB_NewtonCreateSphere(10,10,10)
Body = NDB_NewtonCreateBody(Col)
`Position the ball
NDB_BuildMatrix 0, 0, 0, x*100, 100, y*-100
NDB_NewtonBodySetMatrix Body
`Set mass of ball
NDB_CalculateMISphereSolid 10,10
NDB_NewtonBodySetMassMatrix Body, 10
NDB_NewtonReleaseCollision Col
`Make visual representation for ball and link to newton ball
make object sphere 1,2*10
position object 1,x*100,100,y*-100
color object 1,rgb(rnd(255),rnd(255),rnd(255))
NDB_BodySetDBProData Body, 1
NDB_NewtonBodySetDestructorCallback Body
`Always keep ball active, and apply a gravity to ball
NDB_NewtonBodySetAutoFreeze Body, 0
NDB_BodySetGravity Body, 1
`Set co-ords for start if ball ever needs replacing
stx=x
sty=y
endfunction
`Find a free object
function findobj()
i=3
while object exist(i)
inc i
endwhile
endfunction i
`World data, first 2 values are the dimensions of level, if this is 0, game is complete, other data is the level
`0=Nothing
`1=Platform
`2=Start position
`3=Goal
data 2,5
data 0,3
data 0,1
data 1,1
data 1,0
data 2,0
data 7,5
data 1,1,1,0,0,0,3
data 1,0,1,0,0,0,1
data 1,0,1,1,1,0,1
data 1,0,0,0,1,1,1
data 2,0,0,0,0,0,0
data 8,8
data 0,0,0,1,1,1,0,3
data 1,1,1,1,0,1,1,1
data 1,0,1,0,0,0,1,0
data 1,1,0,0,1,0,1,0
data 0,1,1,0,1,0,1,0
data 0,1,0,0,1,1,1,1
data 1,1,1,1,1,0,0,1
data 2,0,0,0,1,1,1,1
data 7,4
data 2,0,1,1,1,0,3
data 1,0,1,0,1,0,1
data 1,0,1,0,1,0,1
data 1,1,1,0,1,1,1
data 9,7
data 1,1,1,0,0,0,1,1,1
data 1,0,1,1,0,1,1,0,1
data 1,1,0,1,1,1,0,1,1
data 0,1,1,0,0,0,1,1,0
data 0,0,1,0,0,0,1,0,0
data 0,1,1,0,0,0,1,1,0
data 2,1,0,0,0,0,0,1,3
data 7,8
data 1,1,1,1,1,1,1
data 1,0,0,0,0,0,1
data 1,1,1,1,1,0,1
data 0,0,0,0,1,0,1
data 1,1,1,0,1,0,1
data 1,0,1,0,1,0,1
data 1,0,1,1,1,0,1
data 2,0,0,0,0,0,3
data 4,8
data 0,0,0,3
data 0,0,0,1
data 0,0,0,1
data 0,0,1,0
data 0,1,0,0
data 1,0,0,0
data 1,0,0,0
data 2,0,0,0
data 7,7
data 1,1,1,0,1,1,3
data 1,0,0,1,0,0,0
data 1,0,0,0,0,0,0
data 0,1,0,0,0,0,0
data 1,0,0,0,0,0,0
data 1,0,0,0,0,0,0
data 2,0,0,0,0,0,0
data 0