More Code | ||||||||
JUMP CODE | ||||||||
`Illustrating the Basic Jump `Author: Hop A Long `June 1, 2004 `This subject is covered in greater detail in `in the Dark Basic Example 25 `************************************************ ` SET UP `************************************************ autocam off sync rate 20 sync on hide mouse `************************************************ ` Declare a Variable `************************************************ ` dy# is a float variable used to increment the player's y position giving the ` impression of the rise and fall in a jump. dy#=2.0 `************************************** ` Make the player `************************************** make object cylinder 1,10 position object 1,0,0,50 make object collision box 1,-5,-5,-5,5,5,5,0 `************************************** ` Make the first platform `************************************** make object box 2,20,2,20 : position object 2,0,-1,0 : color object 2,rgb(120,120,120) ` Transform our platform into a Static Object make static object 2 objx#=object position x(2) objy#=object position y(2) objz#=object position z(2) objsx#=object size x(2)/2.0 objsy#=object size y(2)/2.0 objsz#=object size z(2)/2.0 Make Static Collision Box objx#-objsx#,objy#-objsy#,objz#-objsz#,objx#+objsx#,objy#+objsy#,objz#+objsz# delete object 2 `************************************** ` Make the second platform `************************************** make object box 3,20,2,20 : position object 3,0,-1,50 : color object 3,rgb(120,120,120) ` Transform our platform into a Static Object make static object 3 objx#=object position x(3) objy#=object position y(3) objz#=object position z(3) objsx#=object size x(3)/2.0 objsy#=object size y(3)/2.0 objsz#=object size z(3)/2.0 Make Static Collision Box objx#-objsx#,objy#-objsy#,objz#-objsz#,objx#+objsx#,objy#+objsy#,objz#+objsz# delete object 3 `****************************************************************************** ` THE MAIN LOOP `****************************************************************************** Do `****************************************** ` STORE THE PRESENT LOCATION OF THE PLAYER `****************************************** ` These are later used in collision detection oldposx#=object position x(1) oldposy#=object position y(1) oldposz#=object position z(1) `****************************************** ` ASSIGN VARIABLES TO WORK WITH `****************************************** posx#=object position x(1) posy#=object position y(1) posz#=object position z(1) `****************************************** ` LAUNCH THE JUMP `****************************************** If spacekey()=1 and dy#=0.0 ` Set the change in y value to the starting value of 2.0. dy#=2.0 Endif `****************************************** ` GENERATE JUMP DATA (dy#) `****************************************** ` Modify the y value to simulate a gradually slowing ` rise and then gradually increasing fall. ` Each time through the loop this value is reduced by 0.1 ` thus the Player will rise smaller and smaller amounts. ` After dy# = 0, dy# becomes an increasingly negetive value. ` Now the Player will desend faster and faster. ` When the Player collides with the platform dy# is set to 0.0 ` and no more vertical movement is possible dy#=dy#-0.1 ` Adjust the Player's height by the new change in y value posy#=posy#+dy# `****************************************** ` SIMPLE MOVEMENT `****************************************** If Upkey()=1 then posz#=posz#+1.0 If Downkey()=1 then posz#=posz#-1.0 `****************************************** ` PLACE THE PLAYER USING JUMP AND MOVE DATA `****************************************** position object 1,posx#,posy#,posz# `****************************************** ` TEST FOR A COLLISION WITH THE PLATFORM `****************************************** ` Set a size for the player object ` This will next be used to define two collision boxes for the player, whose ` positions can be compared to Static Objects s#=object size y(1)/2.0 ` Detect any Static Collision Boxes and Slide If Get Static Collision Hit(oldposx#-s#,oldposy#-s#,oldposz#-s#,oldposx#+s#,oldposy#+s#,oldposz#+s#,posx#-s#,posy#-s#,posz#-s#,posx#+s#,posy#+s#,posz#+s#)=1 dec posx#,get static collision x() dec posy#,get static collision y() dec posz#,get static collision z() ` When true the command returns 1 If get static collision y()<>0.0 ` The player is touching the platform, keep him there by setting change in y = 0 dy#=0.0 ` The jump has ended Endif Endif `****************************************** ` RELOCATE THE PLAYER IF A COLLISION OCCURS `****************************************** position object 1,posx#,posy#,posz# `****************************************** ` START AGAIN IF PLAYER FALLS `****************************************** If posy#<-200 dy#=0.0 position object 1 ,0,5,50 Endif `****************************************** ` CAMERA ROUTINES `****************************************** Position Camera 0,15,-40 Point Camera 0,15,0 `****************************************** ` PRINT JUMP DATA TO THE SCREEN `****************************************** ` Print informative text Center Text 320,50, "ILLUSTRATING THE BASIC JUMP" Center Text 320,70, "Space Key to Jump, Up Arrow Moves In, Down Arrow Moves Out" ` Update screen sync `****************************************************************************** ` END OF MAIN LOOP `****************************************************************************** Loop |