Thanks Conjured Entertainment
The original template did have rather large posts ive updated that now and spaced the posts out more.
My template is still heaps beind the final that is in the lower videos which has extras like sheep.cows etc on road
uses proper models with wireframe textures.
3D Night Driving Original full source no media required makes for a great template
Now includes car headlights a possibility of cows on the road and a stereoscopic option
arrow keys to steer and space to brake
//questions and comments https://forum.thegamecreators.com/thread/222010
#constant KEY_LEFT = 37 //the left arrow key scancode
#constant KEY_UP = 38 //the up arrow key scancode
#constant KEY_RIGHT = 39 //the right arrow key scancode
#constant KEY_DOWN = 40 //the down arrow key scancode
#constant KEY_A = 65 //the scan code for the "a" key
#constant KEY_Z = 90 //the scan code for the "z" key
#constant KEY_SPACE = 32 //the space bar
#constant mapX=20 //maximum blocks on x plane CHANGE THIS TO MAKE ROAD NARROWER IE 20 WHICH WILL SPEED IT UP
#constant mapY=1 //maximum blocks on y plane
#constant mapZ=100 //maximum blocks on z plane
#constant xMaxResolution=1024
#constant yMaxResolution=768
#constant acceleration#=.05 //used to calclate the carSpeed#
#constant maxCarSpeed#=2.5 //The Maximum speed the carBlock will move
#constant minCarSpeed#=0.01 //The Minimum speed the carBlock will move
#constant textTitle=1 //the title text id
#constant textSpeed=2 //the speed text id
#constant textScore=3 //the score text id
#constant textLives=4 //the lives text id
#constant textFPS=5 // the fps text id
#constant textGameOver=6 //the game over text id
#constant textPlayAgain=7 //the Click to play again text id
#constant textHighScore=8 //the highscore text id
#constant textTopSpeed=9 //the top speed text id
#constant textHowFar=10 //the text how far can you go text id
#constant textCredit=11 //My Credit text id
#constant textLevel=12 //current level text id
#constant wireFrameTextureID =1 //the image thats used for all cows and cars
#constant gridTextureID =2
#constant speedoTextureID =3
#constant speedoNeedleTextureID =4 //the speedo texture image this is rotated to show speed
#constant sound1 =1
#constant sound2 =2
#constant camLeftOffsetX=-.250 //the camera offsets for Anaglyph Stereoscopic view
#constant camLeftOffsetY=-.05
#constant camLeftOffsetZ=-.125
#constant camRightOffsetX=.250
#constant camRightOffsetY=.04
#constant camRightOffsetZ=.125
#constant spriteLeftOffsetX=2 //the sprite offsets for Anaglyph Stereoscopic view
#constant spriteLeftOffsetY=-2
#constant spriteRightOffsetX=-2
#constant spriteRightOffsetY=2
#constant stereoButton=1 //the button on the load screen selecting stereoscopic mode
#constant normalButton=2 //the button on the load screen selecting normal mode
#constant cowStart=8000 //the cows id start at 8000
#constant AccelerationFactor#=.025 //the speed your car accelerates at
#constant DecelerationFactor#=.025 //the braking factor
#constant carLightObj=4000 //need to have a car light object to set the point lights off this is the id there are two
#constant otherCarLightObj=4003 //This is the id of the other cars light object
#constant carLight=1 //this is your left car light 2 is right
#constant otherCarLight=3 //other cars light starter Id
#constant myGround=5000 //need a ground object for the lights to work this is the object ID
#constant Bg=1000 // for the rubout sprite gets deleted once text sprites created
#constant startText =100 //textid spriteids for text and imageid starters they all get deleted once game starts playing
//A quick run down is that the code creates all the posts and positions them on the x,y,z planes
//the movement of the car really just moves these posts once the post have reached so far off
//they start at the back again. So basically they make it never ending. There is a sinecurve that
//is used to position them on the xplane. Cows etc move the same way and time that the posts
//do. The other car yes there is only one which starts at a distant z location calculates a vector
//to point it towards the centre of the road and moves at its own rate the same way the posts do.
//The cows on the road are always on the road the only thing that changes is a random factor that
//determines if they should be visible and collisions turned on. once they pass your car whether they
//are visible or not they are positioned back off to a distant z position some rotating and positioning
//again.
//There is a shader which uses camera offsets for the left eye and then the right eye to use the
//stereoscopic shader it needs to use Update(), Render(), Swap() to manually sync so when these
//commands are used which is only when the user presses the stereo button the sync is not called
//and they need to be done in the order as they are in code if you wish for stereoscopic 3D.
global speed# = 2.0 //the following for a sine curve to repesent hils on the yplane
global carSpeed#
global blockCount
global carBlock=10000 //carblock and carMem have the same referrence
global carMem=10000 //the carBlocks memblock id
global otherCarBlock=10006 //the otherCarblock and otherCarMem have the same referrence
global otherCarMem=10006 //the other carBlocks memblock id
global timeCount#
global closestBlock //the closest post to the front
global closestBlock2 //the other closest post to the front as there are two one left one right
global Quad //stores the id of the quad object used for sterescopic
global Stereo //stores the id of the shader used for stereoscopic
global leftRenderImage //left render image used when stereoscopic is selected
global rightRenderImage //Right render image used when stereoscopic is selected
global stereoFlag //used to hold the value of 1 if stereoscopic select otherwise 0
global strlen //holds the length of the string that the scrolling credits use
global lives=3 //The actual lives of a player
global score=0 //holds the player score whiched is based on progression
global highScore=0 //holds the total high score for the day
global topSpeed#=0 //the maximun speed you have reached today
global level=1 //players current level
global levelCounter=0 //levelcounter is a count of how many times the last post has been passed its used to calculate the level once youve passed the last post 5 times
time#=timer() //holds the startTime for calculations of the sineCurve with the movements and used in calculating an acceleration curve
global offsetx as float //offsets the other car from the posta
dim blocksChanged[mapX*mapY*mapZ] as block
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Night Driving" )
SetWindowSize( xMaxResolution, yMaxResolution, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( xMaxResolution, yMaxResolution ) // doesn't have to match the window
SetOrientationAllowed( 0, 0, 1, 1 ) // allow only landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
rem for Credits
type myText
id as integer //rem id of sprite
x as integer //rem x axis
y as integer //rem y-axis
img as integer //rem holds the image number for the sprite
endtype
rem used for pulling the object apart by the vertices
type vertex
xDirection as integer //used to send the a vertice in x direction
yDirection as integer //used to send the a vertice in y direction
zDirection as integer //used to send the a vertice in z direction
endtype
type block
id as integer //the id of the object
randX as integer //the random x offset only applied to cows on road
randZ as integer //the random z offset only applied to cows on road
xPosStart as integer//the x starting location of posts and cows
yPosStart as integer//the y starting location of posts and cows
zPosStart as integer//the z starting location of posts and cows
level as integer //
//the following used for my particle effect
xDirection as float //used for sending objects into orbit when there is a crash
yDirection as float //used for sending objects into orbit when there is a crash
zDirection as float //used for sending objects into orbit when there is a crash
endtype
CreateSpeedoTexture(speedoTextureID)
CreateSprite(1,speedoTextureID)
SetSpritePosition(1,10,yMaxResolution-GetSpriteHeight(1))
CreateSpeedoNeedle(speedoNeedleTextureID)
CreateSprite(2,speedoNeedleTextureID)
SetSpritePosition(2,10,yMaxResolution-GetSpriteHeight(2))
createWireFrameTexture(wireFrameTextureID,32,32, makecolor(255,255,255))
createGridTexture(gridTextureID,32,32,4,4,makecolor(255,255,255))
strlen=setupCredits() //used to set up the scrolling credits
num=1
for y = 1 to mapY
for x = 1 to mapX step mapX-1
for z = 1 to mapZ step 10
if num=1
CreateObjectBox(num,.5,2,.5)
else
InstanceObject(num,1)
endif
c=255:SetObjectColor(num,c,c,c,255)
SetObjectCollisionMode( num, 1 ) //On is needed for raycasting
CreateMemblockFromObjectMesh(num,num,1) //there is exactly the same number of blocks as memblocks and each id is equal
blocksChanged[num].id=num
//the directions are only used for the particle effect when you crash
blocksChanged[num].xDirection=Random2(-100,100)/50.0
blocksChanged[num].yDirection=Random2(-100,100)/50.0
blocksChanged[num].zDirection=Random2(-100,100)/50.0
blocksChanged[num].xPosStart=x
blocksChanged[num].yPosStart=y
blocksChanged[num].zPosStart=z
blocksChanged[num].level=1
SetObjectPosition(num,x,y,z)
createCow(cowStart+(num-1)*7)
if x>10
SetObjectPosition(cowStart+(num-1)*7,blocksChanged[num].xPosStart+random(2,20),blocksChanged[num].yPosStart,blocksChanged[num].zPosStart)
else
SetObjectPosition(cowStart+(num-1)*7,blocksChanged[num].xPosStart-random(2,20),blocksChanged[num].yPosStart,blocksChanged[num].zPosStart)
//RotateObjectLocalY(cowStart+(num-1)*7,180)
endif
RotateObjectGlobalY(cowStart+(num-1)*7,random(1,360))
inc num
next z
next x
next y
blockCount=num-1
//There is a maximum of cows on the road at a time
//to make it simpler there are always four cows on the road at any time
//i use a random number to determine if they are visible or not and switch the collisions
//and visibility at any one time
for num=blockCount+1 to blockCount+4
//sheep collision object
blocksChanged[num].id=num
//the directions are only used for the particle effect when you crash
blocksChanged[num].xDirection=Random2(-100,100)/50.0
blocksChanged[num].yDirection=Random2(-100,100)/50.0
blocksChanged[num].zDirection=Random2(-100,100)/50.0
blocksChanged[num].xPosStart=10
blocksChanged[num].yPosStart=1 //4 for cow
blocksChanged[num].zPosStart=50
blocksChanged[num].level=1
createCow(num+((num-blockCount-1)*7))
RotateObjectGlobalY(num+((num-blockCount-1)*7),random(1,360))
blocksChanged[num].randX=random(5,15)
blocksChanged[num].randZ=random(5,15)
SetObjectPosition(num+((num-blockCount-1)*7),getObjectX(10)+blocksChanged[num].randX,getObjectY(10),getObjectZ(10)+blocksChanged[num].randZ)
ranRange=trunc((log(level)*5.0)+2.0) //this gives it an exponential randomness (basically the higher the level the greater the chance of a cow)
if ranRange>8 then ranRange=8
if Random(1,10)<ranRange
//there are seven parts to a cow set collisions on for each part
SetObjectCollisionMode(num+((num-blockCount-1)*7),1)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+1,1)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+2,1)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+3,1)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+4,1)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+5,1)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+6,1)
//set all parts of a cow to visible
SetObjectVisible(num+((num-blockCount-1)*7),1)
SetObjectVisible(num+((num-blockCount-1)*7)+1,1)
SetObjectVisible(num+((num-blockCount-1)*7)+2,1)
SetObjectVisible(num+((num-blockCount-1)*7)+3,1)
SetObjectVisible(num+((num-blockCount-1)*7)+4,1)
SetObjectVisible(num+((num-blockCount-1)*7)+5,1)
SetObjectVisible(num+((num-blockCount-1)*7)+6,1)
else
//there are seven parts to a cow i set collisions off for each part
SetObjectCollisionMode(num+((num-blockCount-1)*7),0)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+1,0)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+2,0)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+3,0)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+4,0)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+5,0)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+6,0)
//set all parts of a cow to invisible
SetObjectVisible(num+((num-blockCount-1)*7),0)
SetObjectVisible(num+((num-blockCount-1)*7)+1,0)
SetObjectVisible(num+((num-blockCount-1)*7)+2,0)
SetObjectVisible(num+((num-blockCount-1)*7)+3,0)
SetObjectVisible(num+((num-blockCount-1)*7)+4,0)
SetObjectVisible(num+((num-blockCount-1)*7)+5,0)
SetObjectVisible(num+((num-blockCount-1)*7)+6,0)
endif
next num
createCar(carBlock,carMem)
createCar(otherCarBlock,otherCarMem)
createHeadlights()
createSound(sound1,1) //creates a base rumble
createSound(sound2,2) //creates white noise
//SetFogMode( 1 )
//SetFogColor(20,20,20)
//SetFogRange(10,100)
//CreatePointLight(1, getObjectX(carBlock),getObjectY(carBlock),getObjectZ(carBlock), 120, 255, 255,255 )
setCameraPosition(1,mapX/2,10,0)
AddVirtualButton(stereoButton,xMaxResolution/2-72,yMaxResolution/2-50,100)
AddVirtualButton(normalButton,xMaxResolution/2+72,yMaxResolution/2-50,100)
SetVirtualButtonSize(stereoButton,140,100 )
SetVirtualButtonSize(normalButton,140,100 )
SetVirtualButtonText(stereoButton,"Stereoscopic"+chr(10)+"Play")
SetVirtualButtonText(normalButton,"Normal"+chr(10)+"Play")
SetVirtualButtonColor(stereoButton,100,100,100)
SetVirtualButtonColor(normalButton,50,50,50)
CreateShader()
Quad = CreateObjectQuad()
Stereo = LoadShader( "Anaglyph.vs", "Anaglyph.ps" )
leftRenderImage=CreateRenderImage(xMaxResolution,yMaxResolution, 0, 0 )
rightRenderImage=CreateRenderImage(xMaxResolution,yMaxResolution, 0, 0 )
setupText():stereoFlag=0:playMode=0
//SetPointlights to the headlight object positions set by create headlights
//Each cat has two headlights
SetPointLightPosition( carLight, GetObjectWorldX (carLightObj), getObjectWorldY (carLightObj), getObjectWorldZ (carLightObj))
SetPointLightPosition( carLight+1, GetObjectWorldX (carLightObj+1), getObjectWorldY (carLightObj+1), getObjectWorldZ (carLightObj+1))
SetPointLightPosition( otherCarLight, GetObjectWorldX (otherCarLightObj), getObjectWorldY (otherCarLightObj), getObjectWorldZ (otherCarLightObj))
SetPointLightPosition( otherCarLight+1, GetObjectWorldX (otherCarLightObj+1), getObjectWorldY (otherCarLightObj+1), getObjectWorldZ (otherCarLightObj+1))
repeat
if stereoFlag=1 //stereoscopic mode selected
Update(0)
// left render for 3D Analglyp Stereoscopic
SetRenderToImage(leftRenderImage, -1 )
ClearScreen()
setCameraPosition(1,mapX/2+camLeftOffsetX,10+camLeftOffsetY,camLeftOffsetZ)
//setCameraPosition(1,camLeftOffsetX,camLeftOffsetY,camLeftOffsetZ)
SetSpritePosition(1,10+spriteLeftOffSetX,yMaxResolution-GetSpriteHeight(1)+spriteLeftOffSetY)
SetSpritePosition(2,10+spriteLeftOffSetX,yMaxResolution-GetSpriteHeight(2)+spriteLeftOffSetY)
endif
//Change the color of the buttons once pressed
if GetVirtualButtonPressed(stereoButton)=1
stereoFlag=1:playMode=1
SetVirtualButtonColor(stereoButton,50,50,50)
SetVirtualButtonColor(normalButton,100,100,100)
endif
if GetVirtualButtonPressed(normalButton)=1
stereoFlag=0:playMode=2
SetVirtualButtonColor(stereoButton,100,100,100)
SetVirtualButtonColor(normalButton,50,50,50)
setCameraPosition(1,mapX/2,10,0)
SetRenderToScreen()
endif
//if you touch the screen or press the spacebar play game
If GetPointerPressed() or GetRawKeyPressed(32) or playMode>0
//deleteTextScroll(strLen)
setTextVisible(textLevel,1)
SetTextVisible(textGameOver,0):SetTextVisible(textPlayAgain,0):SetTextVisible(textHowFar,0)
SetVirtualButtonVisible(stereoButton,0):SetVirtualButtonVisible(normalButton,0)
lives=3:score=0:playGame()
if score>highScore
highScore=score
SetTextString(textHighScore,"High="+Str(highScore))
endif
if carSpeed#>topSpeed#
topSpeed#=carSpeed#
SetTextString(textTopSpeed,"Top Speed="+Str(Round(topSpeed#/maxCarSpeed#*100))+"%")
endif
SetVirtualButtonVisible(stereoButton,1):SetVirtualButtonVisible(normalButton,1):playMode=0
endif
fubarpkTextScroll(strLen) //used to make the scrolling text scroll
if stereoFlag=1 //stereoscopic has been selected
drawAllObjects()
// right render
SetRenderToImage(rightRenderImage, -1 )
ClearScreen()
setCameraPosition(1,(mapX/2)+camRightOffsetX,10+camRightOffsetY,camRightOffsetZ)
SetSpritePosition(1,10+spriteRightOffSetX,yMaxResolution-GetSpriteHeight(1)+spriteRightOffSetY)
SetSpritePosition(2,10+spriteRightOffSetX,yMaxResolution-GetSpriteHeight(2)+spriteRightOffSetY)
Render3D()
// post process
SetRenderToScreen()
SetObjectImage( Quad, leftRenderImage, 0 )
SetObjectImage( Quad, rightRenderImage, 1 )
SetObjectShader( Quad, Stereo)
DrawObject( Quad )
Render2DFront()
Swap()
else
sync() //normal mode selected
endif
until GetRawKeyPressed(27)
end //exitprogram
function playGame()
speed#=0:level=1:levelCounter=0:restoreBlocks()
carSpeed#=minCarSpeed# //car speed used for acceleration with timerResetTimer():time#=timer():starttime#=timer()
x=xMaxResolution/2:yAngle=0:offsetx=4.25:timeCount#=0
ResetTimer():time#=timer():starttime#=timer()
x1#=GetObjectAngleX(carBlock+2) //car wheels
y1#=GetObjectAngleY(carBlock+2) //get the angle of wheels so rotation can be applied
z1#=GetObjectAngleZ(carBlock+2)
x2#=GetObjectAngleX(otherCarBlock+2)
y2#=GetObjectAngleY(otherCarBlock+2)
z2#=GetObjectAngleZ(otherCarBlock+2)
SetTextString(textLevel,"Level "+str(Level) )
SetTextVisible(textLevel,1):iAlpha=255
SetTextColorAlpha(textLevel, iAlpha )
otherCarSpeed#=random(2,10)/10.0:timeCount#=0
carSpeed#=minCarSpeed#
repeat
//Used for the cars headlights
SetPointLightPosition( carLight, GetObjectWorldX (carLightObj), getObjectWorldY (carLightObj), getObjectWorldZ (carLightObj))
SetPointLightPosition( carLight+1, GetObjectWorldX (carLightObj+1), getObjectWorldY (carLightObj+1), getObjectWorldZ (carLightObj+1))
SetPointLightPosition( otherCarLight, GetObjectWorldX (otherCarLightObj), getObjectWorldY (otherCarLightObj), getObjectWorldZ (otherCarLightObj))
SetPointLightPosition( otherCarLight+1, GetObjectWorldX (otherCarLightObj+1), getObjectWorldY (otherCarLightObj+1), getObjectWorldZ (otherCarLightObj+1))
if stereoFlag=1 //sterescopic selected from start
Update(0)
// left render
SetRenderToImage(leftRenderImage, -1 )
ClearScreen()
setCameraPosition(1,mapX/2+camLeftOffsetX,10+camLeftOffsetY,camLeftOffsetZ)
SetSpritePosition(1,10+spriteLeftOffSetX,yMaxResolution-GetSpriteHeight(1)+spriteLeftOffSetY)
SetSpritePosition(2,10+spriteLeftOffSetX,yMaxResolution-GetSpriteHeight(2)+spriteLeftOffSetY)
endif
yAngle=abs(GetDirectionX()*25) //get an angle based on the gyroscope
if GetDirectionX()<=0 //-.15 //uses gyroscope for movement
moveBlocks(abs(GetDirectionX()*2.5),0,carSpeed#)
yAngle=-abs(GetDirectionX()*25) //get gyroscope angle
SetObjectRotation( carBlock,0,yAngle,0 ) //rotate your car based on gyroscope
SetObjectRotation(carBlock+2,x1#,yAngle*2,z1#) //yAngle used to turn the left front wheel left
SetObjectRotation(carBlock+3,x1#,yAngle*2,z1#) //yAngle used to turn the right front wheel right
SetObjectRotation(carBlock+4,x1#,y1#,z1#)
SetObjectRotation(carBlock+5,x1#,y1#,z1#)
else
moveBlocks(-abs(GetDirectionX()*2.5),0,carSpeed#)
yAngle=abs(GetDirectionX()*25) //get gyroscope angle
SetObjectRotation( carBlock,0,yAngle,0 ) //rotate your car based on gyroscope
SetObjectRotation(carBlock+2,x1#,yangle*2,z1#) //yAngle used to turn the left front wheel left
SetObjectRotation(carBlock+3,x1#,yangle*2,z1#) //yAngle used to turn the right front wheel right
SetObjectRotation(carBlock+4,x1#,y1#,z1#)
SetObjectRotation(carBlock+5,x1#,y1#,z1#)
endif
SetObjectRotation(otherCarBlock+2,x2#,y2#,z2#) //sets the rotation of the oncoimng car
SetObjectRotation(otherCarBlock+3,x2#,y2#,z2#)
SetObjectRotation(otherCarBlock+4,x2#,y2#,z2#)
SetObjectRotation(otherCarBlock+5,x2#,y2#,z2#)
x1#=x1#+carSpeed#:x2#=x2#-carSpeed#
otherCarSpeed#=moveOtherCarBlock(carSpeed#,otherCarSpeed#)
//the second raycasting is used for crashes
x#=getObjectX(carBlock)
y#=GetObjectY(carBlock)
z#=getObjectZ(carBlock)
// calculate the start of the ray cast
// a car is 2,2,4
start_x# = x#
start_y# = y#
start_z# = z#
// calculate the end of the ray cast
end_x# = x#+2.0
end_y# = y#
end_z# = z#+4.0 + carSpeed#
hit=ObjectRayCast(0,start_x#,start_y#,start_z#,end_x#,end_y#,end_z#)
if hit>0 or getObjectx(carBlock)<getObjectx(closestBlock) or getObjectx(carBlock)>getObjectx(closestBlock2) //player crashed
dec lives
PlaySound(sound1,100,0)
PlaySound(sound2,50,0)
timeCount#=0
blocksExplode()
restoreBlocks()
carSpeed#=minCarSpeed# //Set the speed back to its default as you crashed
yAngle=0:speed#=0
levelCounter=0
SetTextString(textLevel,"Level "+str(Level) )
SetTextVisible(textLevel,1):iAlpha=255
SetTextColorAlpha(textLevel, iAlpha )
time#=timer():starttime#=timer()
endif
if GetRawKeyState ( KEY_SPACE ) or GetPointerPressed()
if timer()>time#+.05
carSpeed#=carSpeed#-((maxCarSpeed#-carSpeed#)*DecelerationFactor#+.01) //+.01 otherwise it doesnt decelerate if top speed reached
if carSpeed#<minCarSpeed# then carSpeed#=minCarSpeed#
time#=timer()
endif
else
if timer()>time# +.125
if timer()>starttime#+2 then speed#=speed#+.125
carSpeed#=carSpeed#+((maxCarSpeed#-carSpeed#)*AccelerationFactor#) //increase the car speed every 1 second
timeCount#=timeCount#+(carSpeed#*.5) //.50
if carSpeed#>maxCarSpeed# then carSpeed#=maxCarSpeed#
time#=timer()
//endif
endif
endif
if timer()>time#+.125
carSpeed#=carSpeed#+((maxCarSpeed#-carSpeed#)*AccelerationFactor#)
timeCount#=timeCount#+(carSpeed#*.5)
time#=timer()
endif
if carSpeed#>maxCarSpeed# then carSpeed#=maxCarSpeed#
SetSpriteAngle(2,carSpeed#*100.0) //sets the speedo needle to indicate speed
SetTextString(textScore,"Score="+Str(score))
SetTextString(textLives,"Cars="+Str(lives))
if score>highScore
highScore=score
SetTextString(textHighScore,"High="+Str(highScore))
endif
if carSpeed#>topSpeed#
topSpeed#=carSpeed#
endif
fubarpkTextScroll(strLen) //the scolling text at bottom it can be commented out
if stereoFlag=1 //stereoscopic selected from main menu
drawAllObjects()
// right render
SetRenderToImage(rightRenderImage, -1 )
ClearScreen()
setCameraPosition(1,(mapX/2)+camRightOffsetX,10+camRightOffsetY,camRightOffsetZ)
SetSpritePosition(1,10+spriteRightOffSetX,yMaxResolution-GetSpriteHeight(1)+spriteRightOffSetY)
SetSpritePosition(2,10+spriteRightOffSetX,yMaxResolution-GetSpriteHeight(2)+spriteRightOffSetY)
Render3D()
// post process
SetRenderToScreen()
SetObjectImage( Quad, leftRenderImage, 0 )
SetObjectImage( Quad, rightRenderImage, 1 )
SetObjectShader( Quad, Stereo)
DrawObject( Quad )
Render2DFront()
Swap()
else
sync()
endif
if iAlpha>1 then iAlpha=iAlpha-5
SetTextColorAlpha(textLevel, iAlpha )
if levelCounter > 5 //go to next level
inc level
levelCounter=0
SetTextString(textLevel,"Level "+str(Level) )
SetTextVisible(textLevel,1):iAlpha=255
SetTextColorAlpha(textLevel, iAlpha )
endif
until GetRawKeyPressed(27) or lives <1
SetTextVisible(textGameOver,1)
SetTextVisible(textPlayAgain,1)
SetTextVisible(textHowFar,1)
endfunction
function setupText()
x=xMaxResolution/2
CreateText(textTitle,"Night Driving")
SetTextSize(textTitle,50)
SetTextPosition(textTitle,x-120,5)
SetTextColor(textTitle,255,255,255,255)
CreateText(textScore,"Score=")
SetTextSize(textScore,35)
SetTextPosition(textScore,13,5)
SetTextColor(textScore,255,255,255,255)
CreateText(textLives,"Cars=")
SetTextSize(textLives,35)
SetTextPosition(textLives,26,35)
SetTextColor(textLives,255,255,255,255)
CreateText(textGameOver,"GameOver")
SetTextSize(textGameOver,55)
SetTextPosition(textGameOver,x-112,yMaxResolution/2-18)
SetTextColor(textGameOver,255,255,255,255)
SetTextVisible(textGameOver,0)
CreateText(textPlayAgain,"How far can you go?")
SetTextSize(textPlayAgain,55)
SetTextPosition(textPlayAgain,x-200,yMaxResolution/2+20)
SetTextColor(textPlayAgain,255,255,255,255)
SetTextVisible(textPlayAgain,1)
CreateText(textHowFar,"Click to Play")
SetTextSize(textHowFar,55)
SetTextPosition(textHowFar,x-126,yMaxResolution/2+58)
SetTextColor(textHowFar,255,255,255,255)
SetTextVisible(textHowFar,1)
CreateText(textHighScore,"High=0")
SetTextSize(textHighScore,35)
SetTextPosition(textHighScore,xMaxResolution-150,5)
SetTextColor(textHighScore,255,255,255,255)
//CreateText(textCredit,"Programmed by fubarpk using AGK")
//SetTextSize(textCredit,35)
//SetTextPosition(textCredit,5,yMaxResolution-32)
//SetTextColor(textCredit,255,255,255,255)
CreateText(textLevel,"Level ")
SetTextSize(textLevel,50)
SetTextPosition(textLevel,x-75,300)
SetTextColor(textLevel,255,255,255,255)
SetTextVisible(textLevel,0)
endfunction
function fubarpkCreateTextImage (myString as string,imgNum as integer)
//this function creates a sprite from a text letter used for the fubarpktextscroll function
CreateSprite(bg,0)
SetSpriteSize(bg,150,150)
SetSpriteColor(bg,0,0,0,255)
clearscreen()
CreateText(startText,myString)
SetTextSize(startText,50)
SetTextPosition(startText,0,0)
Render():If GetImageExists(imgNum) then DeleteImage(imgNum)
GetImage(imgNum,0,0,50,50):ClearScreen()
SetImageTransparentColor( imgNum,0,0,0 )
DrawSprite(bg)
ClearScreen()
DeleteText(startText):deleteSprite(bg)
endfunction
function fubarpkTextScroll(strlen as integer)
//scrolls the credit text
for num = 1 to strlen
textSpr[num].x=textSpr[num].x-1
if GetSpriteExists(textSpr[num].id) then SetSpritePosition(textSpr[num].id,textSpr[num].x,textSpr[num].y)
if textSpr[num].x <-10 then textSpr[num].x=xMaxResolution +100
next num
endfunction
function deleteTextScroll(strlen)
//can be used to clean up and delete all the sprites created for the textscroller
for num = 1 to strlen
DeleteSprite(textSpr[num].id)
DeleteImage(textSpr[num].img)
next num
endfunction
function setupCredits()
rem Create a heap of sprites so they can be scrolled and have other effects that text dont allow
text$=("Programmed by fubarpk in AGK"):strLen=len(text$)
global dim textSpr[strLen] as myText
for num=1 to strLen
textSpr[num].id=num+startText+(num-1)
textSpr[num].img=num+startText+(num-1)
textSpr[num].x=(num-1)*15
textSpr[num].y=yMaxResolution-33
fubarpkCreateTextImage(Mid(text$,num,1),textSpr[num].img)
createSprite(textSpr[num].id,textSpr[num].img)
SetSpriteSize(textSpr[num].id,24,28)
SetSpriteColor(textSpr[num].id,255,255,255,255)
SetSpriteTransparency(textSpr[num].id,1)
SetSpriteDepth(textSpr[num].id,10)
SetSpriteGroup(textSpr[num].id,1)
next num
endfunction strlen
function modifyBlock(block as integer, ammount as integer)
rem This function is used to modify the car roof block so as it is angled
rem there are the following 8 vertexs that we need to modify to extend in the ydirection [0,2,4,6,8,9,10,11]
vertex as integer[7] = [0,2,4,6,8,9,10,11]
maxVertex=GetMemblockInt(block,0)-1
for index = 0 to 7
if index<=maxVertex
SetMeshMemblockVertexPosition(block,vertex[index],GetMeshMemblockVertexX(block,vertex[index]),GetMeshMemblockVertexY(block,vertex[index])+ammount,GetMeshMemblockVertexZ(block,vertex[index]))
endif
next index
SetObjectMeshFromMemblock(block,1,block)
endfunction
function moveBlocks(x as float,y as float,z as float)
rem this function moves all the posts and cows
rem These define the maximum and minimum an object may be placed in this example all objects
rem are 1,1,1 so defing these locations was done as following but could be calculated for
rem other objects of different dimmensions
xNum=mapX-1 //number of objects placed accross the xplane-1
yNum=mapY-1 //number of objects placed accross the yplane-1
zNum=mapZ-1 //number of objects placed accross the zplane-1
rem defing the 3d window workspace
xMin#=x:xMax#=mapX+x //the minimum and maximum xlocations for wrapping blocks on x plane
yMin=1:yMax=mapY //the minimum and maximum ylocation for wrapping blocks on the y plane
zMin#=0:zMax#=mapZ //the minimum and maximum zlocations for wrapping blocks on z plane
//time#=timer()
//myRed=random(0,255):myGreen=random(0,255):myBlue=random(0,255)
multiplier#=timer()*5:if multiplier#>150 then multiplier# =150
for num=1 to blockCount //move posts and collision cows on road blocks
SetObjectPosition(num,getObjectX(num)+x,getObjectY(num),getObjectZ(num))
if z>0 //move blocks toward screen
if GetObjectWorldZ(num)<zMin#
x# = sin(timeCount#)*34 //this is used to adjust the xposition of the posts so as we get curves in the road
SetObjectPosition(num,x#+getObjectX(num),getObjectY(num),getObjectZ(num)+zNum) //reset position as post has gone off screen
else
SetObjectPosition(num,getObjectX(num),getObjectY(num),getObjectZ(num)-z)
endif
endif
next num
num=1
for cows = 0 to 19 //move cows
SetObjectPosition(cowStart+(cows*7),getObjectX(cowStart+(cows*7))+x,getObjectY(cowStart+(cows*7)),getObjectZ(cowStart+(cows*7)))
if z>0
if GetObjectWorldZ(cowStart+(cows*7))<zMin#
x# = sin(timeCount#)*34 //this is used to adjust the xposition of the cows so as they move on the same curve as the road
SetObjectPosition(cowStart+(cows*7),getObjectX(cowStart+(cows*7))+x#,getObjectY(cowStart+(cows*7)),getObjectZ(cowStart+(cows*7))+zNum)
else
SetObjectPosition(cowStart+(cows*7),getObjectX(cowStart+(cows*7)),getObjectY(cowStart+(cows*7)),getObjectZ(cowStart+(cows*7))-z) //reset position as cow has gone off screen
endif
endif
next cows
for num=blockCount+1 to blockCount+4
//this is the four cows that may or may not appear on the road
if GetObjectWorldZ(num+((num-blockCount-1)*7))<zMin#
//cows are coming forward and they are set relative to the post at object 10
RotateObjectGlobalY(num+((num-blockCount-1)*7),random(1,360))
blocksChanged[num].randX=random(5,15)
blocksChanged[num].randZ=random(5,15)
SetObjectPosition(num+((num-blockCount-1)*7),getObjectX(10)+blocksChanged[num].randX,getObjectY(10),getObjectZ(10)+zNum+blocksChanged[num].randZ)
ranRange=trunc((log(level)*5.0)+2.0) //used to make it exponentially have more cars depending on level
if ranRange>8 then ranRange=8
if Random(1,10)<ranRange //max difficulty 80 percent
//Set Collision for all cow parts to on
SetObjectCollisionMode(num+((num-blockCount-1)*7),1)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+1,1)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+2,1)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+3,1)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+4,1)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+5,1)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+6,1)
//set all cow parts to visible
SetObjectVisible(num+((num-blockCount-1)*7),1)
SetObjectVisible(num+((num-blockCount-1)*7)+1,1)
SetObjectVisible(num+((num-blockCount-1)*7)+2,1)
SetObjectVisible(num+((num-blockCount-1)*7)+3,1)
SetObjectVisible(num+((num-blockCount-1)*7)+4,1)
SetObjectVisible(num+((num-blockCount-1)*7)+5,1)
SetObjectVisible(num+((num-blockCount-1)*7)+6,1)
else
//Set Collision for all cow parts to off
SetObjectCollisionMode(num+((num-blockCount-1)*7),0)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+1,0)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+2,0)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+3,0)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+4,0)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+5,0)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+6,0)
//set all cow parts to invisible
SetObjectVisible(num+((num-blockCount-1)*7),0)
SetObjectVisible(num+((num-blockCount-1)*7)+1,0)
SetObjectVisible(num+((num-blockCount-1)*7)+2,0)
SetObjectVisible(num+((num-blockCount-1)*7)+3,0)
SetObjectVisible(num+((num-blockCount-1)*7)+4,0)
SetObjectVisible(num+((num-blockCount-1)*7)+5,0)
SetObjectVisible(num+((num-blockCount-1)*7)+6,0)
endif
else
SetObjectPosition(num+((num-blockCount-1)*7),getObjectX(10)+blocksChanged[num].randX,getObjectY(num+((num-blockCount-1)*7)),getObjectZ(num+((num-blockCount-1)*7))-z) //reset cows back to start position
endif
next num
//once the last post block has moved off screen increase score and the levelcounter
if getObjectZ(blockCount)<zMin# //num=10
score=score+(((100*level)*carSpeed#)/2)
inc levelCounter
endif
endfunction
function moveOtherCarBlock(spd as float, otherSpd as float)
zMin#=0
zNum=mapZ-1 //number of objects placed accross the zplane-1
closestBlock=1:closestBlock2=1:furthestBlock=1:rem closestBlockToCar=1:lastBlockToCar=1
//used to calculate a vector to point the oncoming car
for num=2 to blockCount
tempZ#=getObjectZ(num)
if getObjectZ(closestBlock)>tempZ# then closestBlock=num
if getObjectZ(closestBlock2)>=tempZ# then closestBlock2=num
if getObjectZ(furthestBlock)<tempZ# then furthestBlock=num
next num
spd=spd+otherSpd
x#=getObjectX(otherCarBlock)
y#=getObjectY(otherCarBlock)
z#=getObjectZ(otherCarBlock)
if GetObjectWorldZ(otherCarBlock)<zMin#
SetObjectPosition(otherCarBlock,getObjectX(furthestBlock)+offsetx,y#,z#+zNum)
otherSpd=random(2,10)/10.0 //sets a random speed for oncoming car
else
xx#=getObjectX(closestBlock)+10
zz#=getObjectZ(closestBlock)
ang# = GetAngle(x#,z#,xx#,zz#)
SetObjectPosition(otherCarBlock,x#-sin(ang#)*spd,y#,z#-spd)
angle# = GetAngle(getObjectX(blockCount),getObjectZ(blockCount),getObjectX(blockCount-2),getObjectZ(blockCount-2))
SetObjectRotation(otherCarBlock,0,0,0)
RotateObjectGlobalY(otherCarBlock,angle#)
endif
endfunction otherSpd
function blocksExplode()
//Set Pointlightmode to vertex mode (the lights wont work doing this but it slows down too much)
SetPointLightMode(carLight,0)
SetPointLightMode(carLight+1,0)
SetPointLightMode(otherCarLight,0)
SetPointLightMode(otherCarLight+1,0)
SetSpriteAngle(2,0)
maxVertex=GetMemblockInt(carBlock,0)-1
Dim CarVertex[maxVertex] as vertex //get all the vertices of bottom car block and give them directions
For index=0 to maxVertex
//set each vertex a random direction
CarVertex[index].xDirection=random2(-100,100)/50.0
CarVertex[index].yDirection=Random2(-100,100)/50.0
CarVertex[index].zDirection=Random2(-100,0)/50.0
next index
maxVertex=GetMemblockInt(carBlock+1,0)-1
Dim CarTopVertex[maxVertex] as vertex //get all the vertices of top car block and give them directions
For index=0 to maxVertex
//set each vertex a random direction
CarTopVertex[index].xDirection=random2(-100,100)/50.0
CarTopVertex[index].yDirection=Random2(-100,100)/50.0
CarTopVertex[index].zDirection=Random2(-100,0)/50.0
next index
maxVertex=GetMemblockInt(otherCarBlock,0)-1
Dim OtherCarVertex[maxVertex] as vertex //get all the vertices of bottom other car block and give them directions
For index=0 to maxVertex
//set each vertex a random direction
OtherCarVertex[index].xDirection=random2(-100,100)/50.0
OtherCarVertex[index].yDirection=Random2(-100,100)/50.0
OtherCarVertex[index].zDirection=Random2(-100,0)/50.0
next index
maxVertex=GetMemblockInt(otherCarBlock+1,0)-1
Dim OtherCarTopVertex[maxVertex] as vertex
For index=0 to maxVertex
//set each vertex a random direction
OtherCarTopVertex[index].xDirection=random2(-100,100)/50.0
OtherCarTopVertex[index].yDirection=Random2(-100,100)/50.0
OtherCarTopVertex[index].zDirection=Random2(-100,0)/50.0
next index
dim wheels[7] as vertex //get all the wheels and send them into orbit
for wheel = 0 to 7 //8 wheels with both cars
wheels[wheel].xDirection=random2(-100,100)/50.0
wheels[wheel].yDirection=random2(-100,100)/50.0
wheels[wheel].zDirection=random2(-100,100)/50.0
next wheel
dim cowsDirection[blockCount+3] as vertex
for cows=0 to blockCount+2 //send all the cows that are positioned off the road into orbit
cowsDirection[cows].xDirection=random2(-100,100)/50.0
cowsDirection[cows].yDirection=random2(-100,100)/50.0
cowsDirection[cows].zDirection=random2(-100,100)/50.0
next cows
startTime#=Timer()
repeat
if stereoFlag=1 //stereoscopic has been selected
Update(0)
// left render
SetRenderToImage(leftRenderImage, -1 )
ClearScreen()
setCameraPosition(1,mapX/2+camLeftOffsetX,10+camLeftOffsetY,camLeftOffsetZ)
SetSpritePosition(1,10+spriteLeftOffSetX,yMaxResolution-GetSpriteHeight(1)+spriteLeftOffSetY)
SetSpritePosition(2,10+spriteLeftOffSetX,yMaxResolution-GetSpriteHeight(2)+spriteLeftOffSetY)
endif
for num=1 to blockCount
//move the ground blocks in their set random directions
SetObjectPosition(num,getObjectX(num)+blocksChanged[num].xDirection,getObjectY(num)+blocksChanged[num].yDirection,getObjectZ(num)+blocksChanged[num].zDirection)
next num
for num=blockCount+1 to blockCount+4 //move the sheep on road in random directions
SetObjectPosition(num+((num-blockCount-1)*7),getObjectX(num)+blocksChanged[num].xDirection,getObjectY(num)+blocksChanged[num].yDirection,getObjectZ(num)+blocksChanged[num].zDirection)
next num
//just send wheels into orbit
for wheel = 0 to 3 //your car wheels
SetObjectPosition(carBlock+2+wheel,getObjectX(carBlock+2+wheel)+wheels[wheel].xDirection,getObjectY(carBlock+2+wheel)+wheels[wheel].yDirection,getObjectZ(carBlock+2+wheel)+wheels[wheel].zDirection)
next wheel
for wheel = 4 to 7 //other car wheels
SetObjectPosition(otherCarBlock-2+wheel,getObjectX(otherCarBlock-2+wheel)+wheels[wheel].xDirection,getObjectY(otherCarBlock-2+wheel)+wheels[wheel].yDirection,getObjectZ(otherCarBlock-2+wheel)+wheels[wheel].zDirection)
next wheel
for cows=0 to blockCount-1 //there are 20 sheep +3 on road =+2
SetObjectPosition((cowStart+(cows*7)),getObjectX(cowStart+(cows*7))+cowsDirection[cows].xDirection,getObjectY(cowStart+(cows*7))+cowsDirection[cows].yDirection,getObjectZ(cowStart+(cows*7))+cowsDirection[cows].zDirection)
next cows
for index = 0 to maxVertex
//Tear the cars vertexs apart as set by there random direction
SetMeshMemblockVertexPosition(carMem,index,GetMeshMemblockVertexX(carMem,index)+CarVertex[index].xDirection,GetMeshMemblockVertexY(carMem,index)+CarVertex[index].yDirection,GetMeshMemblockVertexZ(carMem,index)+CarVertex[index].zDirection)
SetMeshMemblockVertexPosition(otherCarMem,index,GetMeshMemblockVertexX(OtherCarMem,index)+otherCarVertex[index].xDirection,GetMeshMemblockVertexY(otherCarMem,index)+OtherCarVertex[index].yDirection,GetMeshMemblockVertexZ(otherCarMem,index)+otherCarVertex[index].zDirection)
SetMeshMemblockVertexPosition(carMem+1,index,GetMeshMemblockVertexX(carMem+1,index)+CarTopVertex[index].xDirection,GetMeshMemblockVertexY(carMem+1,index)+CarTopVertex[index].yDirection,GetMeshMemblockVertexZ(carMem+1,index)+CarTopVertex[index].zDirection)
SetMeshMemblockVertexPosition(otherCarMem+1,index,GetMeshMemblockVertexX(OtherCarMem+1,index)+otherCarTopVertex[index].xDirection,GetMeshMemblockVertexY(otherCarMem+1,index)+OtherCarTopVertex[index].yDirection,GetMeshMemblockVertexZ(otherCarMem+1,index)+otherCarTopVertex[index].zDirection)
next index
SetObjectMeshFromMemblock(carBlock,1,carMem)
SetObjectMeshFromMemblock(carBlock+1,1,carMem+1)
SetObjectMeshFromMemblock(otherCarBlock,1,otherCarMem)
SetObjectMeshFromMemblock(otherCarBlock+1,1,otherCarMem+1)
//sets the position of the lights in relation to carblock positions
SetPointLightPosition( carLight, GetObjectWorldX (carBlock+2), getObjectWorldY (carBlock+2), getObjectWorldZ (carBlock+2))
SetPointLightPosition( carLight+1, GetObjectWorldX (carBlock+3), getObjectWorldY (carBlock+3), getObjectWorldZ (carBlock+3))
SetPointLightPosition( otherCarLight, GetObjectWorldX (otherCarBlock+2), getObjectWorldY (otherCarBlock+2), getObjectWorldZ (otherCarBlock+2))
SetPointLightPosition( otherCarLight+1, GetObjectWorldX (otherCarBlock+3), getObjectWorldY (otherCarBlock+3), getObjectWorldZ (otherCarBlock+3))
if stereoFlag=1 //stereoscopic has been set
drawAllObjects()
// right render
SetRenderToImage(rightRenderImage, -1 )
ClearScreen()
setCameraPosition(1,(mapX/2)+camRightOffsetX,10+camRightOffsetY,camRightOffsetZ)
SetSpritePosition(1,10+spriteRightOffSetX,yMaxResolution-GetSpriteHeight(1)+spriteRightOffSetY)
SetSpritePosition(2,10+spriteRightOffSetX,yMaxResolution-GetSpriteHeight(2)+spriteRightOffSetY)
//
Render3D()
// post process
SetRenderToScreen()
SetObjectImage( Quad, leftRenderImage, 0 )
SetObjectImage( Quad, rightRenderImage, 1 )
SetObjectShader( Quad, Stereo)
DrawObject( Quad )
Render2DFront()
Swap()
else
sync()
endif
until timer()>StartTime#+3
//sets poitlights back to pixel mode as it is the only way to show the light appearing on the road
SetPointLightMode(carLight,1)
SetPointLightMode(carLight+1,1)
SetPointLightMode(otherCarLight,1)
SetPointLightMode(otherCarLight+1,1)
endfunction
function restoreBlocks()
//restore all the block properties after a collision
for num=1 to blockCount
//restore blocks to the starting positions
SetObjectPosition(blocksChanged[num].id,blocksChanged[num].xPosStart,blocksChanged[num].yPosStart,blocksChanged[num].zPosStart)
createCow(cowStart+(num-1)*7)
if blocksChanged[num].xPosStart>10
SetObjectPosition(cowStart+(num-1)*7,blocksChanged[num].xPosStart+random(2,20),blocksChanged[num].yPosStart,blocksChanged[num].zPosStart)
else
SetObjectPosition(cowStart+(num-1)*7,blocksChanged[num].xPosStart-random(2,20),blocksChanged[num].yPosStart,blocksChanged[num].zPosStart)
endif
RotateObjectGlobalY(cowStart+(num-1)*7,random(1,360)) //randomly rotate a cow so they dont all face same direction
next num
for num=blockCount+1 to blockCount+4 //create the four cows that may appear on the road
//sheep collision object
blocksChanged[num].id=num
//the directions are only used for the particle effect when you crash
blocksChanged[num].xDirection=Random2(-100,100)/50.0
blocksChanged[num].yDirection=Random2(-100,100)/50.0
blocksChanged[num].zDirection=Random2(-100,100)/50.0
blocksChanged[num].xPosStart=10
blocksChanged[num].yPosStart=1 //4 for cow
blocksChanged[num].zPosStart=50
blocksChanged[num].level=1
createCow(num+((num-blockCount-1)*7))
RotateObjectGlobalY(num+((num-blockCount-1)*7),random(1,360))
blocksChanged[num].randX=random(5,15)
blocksChanged[num].randZ=random(5,15)
SetObjectPosition(num+((num-blockCount-1)*7),getObjectX(10)+blocksChanged[num].randX,getObjectY(10),getObjectZ(10)+blocksChanged[num].randZ)
ranRange=trunc((log(level)*5.0)+2.0)
if ranRange>8 then ranRange=8
if Random(1,10)<ranRange
SetObjectCollisionMode(num+((num-blockCount-1)*7),1)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+1,1)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+2,1)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+3,1)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+4,1)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+5,1)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+6,1)
SetObjectVisible(num+((num-blockCount-1)*7),1)
SetObjectVisible(num+((num-blockCount-1)*7)+1,1)
SetObjectVisible(num+((num-blockCount-1)*7)+2,1)
SetObjectVisible(num+((num-blockCount-1)*7)+3,1)
SetObjectVisible(num+((num-blockCount-1)*7)+4,1)
SetObjectVisible(num+((num-blockCount-1)*7)+5,1)
SetObjectVisible(num+((num-blockCount-1)*7)+6,1)
else
SetObjectCollisionMode(num+((num-blockCount-1)*7),0)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+1,0)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+2,0)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+3,0)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+4,0)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+5,0)
SetObjectCollisionMode(num+((num-blockCount-1)*7)+6,0)
SetObjectVisible(num+((num-blockCount-1)*7),0)
SetObjectVisible(num+((num-blockCount-1)*7)+1,0)
SetObjectVisible(num+((num-blockCount-1)*7)+2,0)
SetObjectVisible(num+((num-blockCount-1)*7)+3,0)
SetObjectVisible(num+((num-blockCount-1)*7)+4,0)
SetObjectVisible(num+((num-blockCount-1)*7)+5,0)
SetObjectVisible(num+((num-blockCount-1)*7)+6,0)
endif
next num
//as the cars vertics and faces have all been destroyed iy is just easier to recreate them which dont take long
createCar(carBlock,carMem)
createCar(otherCarBlock,otherCarMem)
createHeadlights()
speed# = 0
resetTimer() //need to reset time for the road curves
endfunction
function createSound(SoundId as integer,num as integer)
//create sound wave in memory
local sound as integer
local frames as integer
local data as integer
local size as integer
local mem as integer
local sec as float
sec = .4
data = sec * (44100 * (2*2)) // 176400 bytes per second
frames = data / 4
size = 2+2+4+4 + data
mem = CreateMemblock(size)
SetMemblockByte(mem,0, 2) //The first 2 bytes of the memlbock store the number of channels (1 or 2 supported)
SetMemblockByte(mem,1, 0)
SetMemblockByte(mem,2,16) //the next 2 bytes store the bits per sample (8 (1 byte) or 16 (2 byte) supported)
SetMemblockByte(mem,3, 0)
SetMemblockint(mem,4,44100) // the next 4 bytes store the samples per second, for example 44100
SetMemblockint(mem,8,frames) // The next 4 bytes are the number of frames in the sound data, the size of the sound data in bytes can be calculated from this with the formula numFrames*(bitsPerSample/8)*channels.
local i as integer
local value as integer
local Hz as float // the hertz of the sound
local w as float
value=0
w=0
local randomize as integer[8]=[80,53,-25,73,44,0,24,80] //deep base rumble
maxW=360:if num=2 then maxW=32768 //360 is normal
for i=0 to data -1 step 4
Hz=randomize[random2(1,8)] //deep base rumble
w=w+(maxW / 44100.0) * Hz
if w > maxW then w = w -maxW
value = sin(w)*(32767.0) //Max −32768 through 32767
SetMemblockShort(mem,12+i+0 ,value) // short 2 bytes −32768 through 32767
SetMemblockShort(mem,12+i+2 ,value) // short 2 bytes −32768 through 32767
next i
CreateSoundFromMemblock(SoundId,mem)
DeleteMemblock(mem)
endFunction //sound
function createWireFrameTexture(ImgId as integer,sizex as integer, sizey as integer, color)
SetClearColor(0,0,0)
ClearScreen()
Render()
drawbox(0,0,sizex-1, sizey-1, color, color,color,color, 0)
Render()
//img = getimage(0,0,sizex, sizey)
getimage(ImgId,0,0,sizex, sizey)
Swap()
endfunction //img
function createGridTexture(ImgId as integer,sizex as integer, sizey as integer,stepx as integer,stepy as integer, color)
SetClearColor(0,0,0)
ClearScreen()
Render()
for y=0 to sizey-1 step stepy
DrawLine(0,y,sizex,y,color,color)
next y
for x=0 to sizex-1 step stepx
DrawLine(x,0,x,sizey,color,color)
next x
Render():
//img = getimage(0,0,sizex, sizey)
getimage(ImgId,0,0,sizex, sizey)
Swap()
endfunction //img
function CreateSpeedoTexture(imgId as integer)
SetClearColor(0,0,0)
ClearScreen()
text1=createText("10"):SetTextSize(text1,20):SetTextPosition(text1,15,167):DrawText(text1)
text2=createText("20"):SetTextSize(text2,20):SetTextPosition(text2,0,130):DrawText(text2)
text3=createText("30"):SetTextSize(text3,20):SetTextPosition(text3,1,87):DrawText(text3)
text4=createText("40"):SetTextSize(text4,20):SetTextPosition(text4,18,46):DrawText(text4)
text5=createText("50"):SetTextSize(text5,20):SetTextPosition(text5,50,17):DrawText(text5)
text6=createText("60"):SetTextSize(text6,20):SetTextPosition(text6,90,1):DrawText(text6)
text7=createText("70"):SetTextSize(text7,20):SetTextPosition(text7,134,1):DrawText(text7)
text8=createText("80"):SetTextSize(text8,20):SetTextPosition(text8,177,17):DrawText(text8)
text9=createText("90"):SetTextSize(text9,20):SetTextPosition(text9,209,46):DrawText(text9)
text10=createText("100"):SetTextSize(text10,20):SetTextPosition(text10,226,87):DrawText(text10)
text11=createText("110"):SetTextSize(text11,20):SetTextPosition(text11,228,130):DrawText(text11)
text12=createText("120"):SetTextSize(text12,20):SetTextPosition(text12,220,167):DrawText(text12)
Render()
//img = getimage(0,0,255,255)
getimage(ImgId,0,0,255,255)
Swap()
DeleteText(text1):DeleteText(text2):DeleteText(text3):DeleteText(text4):DeleteText(text5):DeleteText(text6)
DeleteText(text7):DeleteText(text8):DeleteText(text9):DeleteText(text10):DeleteText(text11):DeleteText(text12)
endfunction //img
function CreateSpeedoNeedle(imgId as integer)
ClearScreen()
DrawLine( 125, 126, 58, 194,255,255,255 )
DrawLine( 126, 126, 59, 194,255,255,255 )
DrawLine( 127, 126, 60, 194,255,255,255 )
DrawEllipse(126,126,4,4,makecolor(255,255,255),makecolor(255,255,255),1)
Render()
//img = getimage(0,0,255,255)
getimage(ImgId,0,0,255,255)
Swap()
endfunction //img
function getAngle(x1#, y1#, x2#, y2#)
result# = ATan2(x1# - x2#,y1# - y2#)
endfunction result#
function drawAllObjects()
DrawObject(carBlock) //draw your car
DrawObject(otherCarBlock) //draw other car
for wheel = 2 to 5 //your car wheels
drawObject(carBlock+wheel) //draw your cars wheels
drawObject(otherCarBlock+wheel) //draw other cars wheels
next wheel
for num=1 to blockCount
DrawObject(num) //draw posts
DrawObject(cowStart+(num-1)*7) //draw sheep
next num
for num=blockCount+1 to blockCount+3
DrawObject(num+3) //draw road sheep
next num
DrawSprite(1):DrawSprite(2)
endfunction
function createCar(carId as integer,carMemId as integer)
DeleteObject(carId):DeleteObject(CarId+1)
DeleteObject(carId+2):DeleteObject(CarId+3)
DeleteObject(carId+4):DeleteObject(CarId+5)
DeleteMemBlock(carMemId):DeleteMemblock(carMemId+1)
vertex as integer[7] = [17,15,7,13, 1,19,3,5]
CreateObjectBox(carId,2,2,4)
CreateMemblockFromObjectMesh(carId,carMemId,1)
setObjectPosition(carId,mapX/2,mapY,10)
SetObjectImage(carId,wireFrameTextureID,0)
//SetObjectColor(carId,200,0,0,255) //red cars go faster
SetObjectColor(carId,255,255,255,255)
SetObjectTransparency(carId,1)
SetObjectCollisionMode(carId,1) //on is set or raycasting wont work
CreateObjectBox(carId+1,2,1,1) //the car top
SetObjectImage(carId+1,wireFrameTextureID,0)
SetObjectTransparency(carId+1,1)
setObjectPosition(carId+1,getObjectX(carId)-(mapX/2),getObjectY(carId)-mapY+1.5,getObjectZ(carId)-10)
CreateMemblockFromObjectMesh(carId+1,carMemId+1,1)
for i = 0 to 7
SetMeshMemblockVertexPosition(carMemId+1,vertex[i],GetMeshMemblockVertexX(carMemId,vertex[i]),GetMeshMemblockVertexY(carMemId,vertex[i])+.5,GetMeshMemblockVertexZ(carMemId,vertex[i])*.5)
next i
//SetMeshMemblockVertexPosition(carMemId+1,vertexBottom[2],GetMeshMemblockVertexX(carMemId,vertexBottom[2]),GetMeshMemblockVertexY(carMem,vertexBottom[2]),GetMeshMemblockVertexZ(carMem,vertexBottom[2])-1)
SetObjectMeshFromMemblock(carId+1,1,carMemId+1)
SetObjectColor(carId+1,255,255,255,255) //red cars go faster
SetObjectCollisionMode(carId+1,1) //on is set or raycasting wont work
FixObjectToObject(carId+1,carId)
//now for four wheels
CreateObjectCylinder(carId+2,.5,1,10) //front left
SetObjectImage(carId+2,gridTextureID,0)
SetObjectTransparency(carId+2,1)
SetObjectRotation(carId+2,0,0,90)
SetObjectPOsition(carId+2,-.75,-1,1.25)
SetObjectColor(carId+2,255,255,255,255)
FixObjectToObject(carId+2,carId)
//SetObjectCollisionMode(carId+2,1)
CreateObjectCylinder(carId+3,.5,1,10) //front right
SetObjectImage(carId+3,gridTextureID,0)
SetObjectTransparency(carId+3,1)
SetObjectRotation(carId+3,0,0,90)
SetObjectPOsition(carId+3,.75,-1,1.25)
SetObjectColor(carId+3,255,255,255,255)
FixObjectToObject(carId+3,carId)
//SetObjectCollisionMode(carId+3,1)
CreateObjectCylinder(carId+4,.5,1,10) //back left
SetObjectImage(carId+4,gridTextureID,0)
SetObjectTransparency(carId+4,1)
SetObjectRotation(carId+4,0,0,90)
SetObjectPOsition(carId+4,-.75,-1,-1.15)
SetObjectColor(carId+4,255,255,255,255)
FixObjectToObject(carId+4,carId)
//SetObjectCollisionMode(carId+4,1)
CreateObjectCylinder(carId+5,.5,1,10) //back right
SetObjectImage(carId+5,gridTextureID,0)
SetObjectTransparency(carId+5,1)
SetObjectRotation(carId+5,0,0,90)
SetObjectPOsition(carId+5,.75,-1,-1.15)
SetObjectColor(carId+5,255,255,255,255)
FixObjectToObject(carId+5,carId)
//SetObjectCollisionMode(carId+5,1)
endfunction
function createCow(cowId as integer)
//cows are made up of 7 objects
DeleteObject(cowId):DeleteObject(cowId+1):DeleteObject(cowId+2):DeleteObject(cowId+3)
DeleteObject(cowId+4):DeleteObject(cowId+5):DeleteObject(cowId+6)
//cow body
CreateObjectBox(cowId,2.0,1.0,1)
SetObjectImage(cowId,wireFrameTextureID,0)
setObjectPosition(cowId,mapX/2,mapY+4,10)
SetObjectTransparency(cowId,1)
SetObjectCollisionMode(cowId,1) //on is set or raycasting wont work
//cow head
CreateObjectBox(cowId+1,0.65,0.25,.5) //head
SetObjectImage(cowId+1,wireFrameTextureID,0)
setObjectPosition(cowId+1,getObjectX(cowId)-(mapX/2)-1,getObjectY(cowId)-mapY-3.5,getObjectZ(cowId)-10)
SetObjectRotation(cowId+1,0,0,20)
SetObjectTransparency(cowId+1,1)
//SetObjectCollisionMode(cowId+1,1) //on is set or raycasting wont work
FixObjectToObject(cowId+1,cowId)
//cow tail
CreateObjectBox(cowId+2,.1,1.0,.25)
SetObjectImage(cowId+2,wireFrameTextureID,0)
setObjectPosition(cowId+2,getObjectX(cowId)-(mapX/2)+1.15,getObjectY(cowId)-mapY-4,getObjectZ(cowId)-10)
SetObjectRotation(cowId+2,0,0,20)
SetObjectTransparency(cowId+2,1)
//SetObjectCollisionMode(cowId+2,1) //on is set or raycasting wont work
FixObjectToObject(cowId+2,cowId)
//leg left front
CreateObjectBox(cowId+3,.1,.50,.25)
SetObjectImage(cowId+3,wireFrameTextureID,0)
setObjectPosition(cowId+3,getObjectX(cowId)-(mapX/2)-1.0,getObjectY(cowId)-mapY-4.75,getObjectZ(cowId)-10.25)
SetObjectRotation(cowId+3,0,0,0)
SetObjectTransparency(cowId+3,1)
//SetObjectCollisionMode(cowId+3,1) //on is set or raycasting wont work
FixObjectToObject(cowId+3,cowId)
//leg right front
CreateObjectBox(cowId+4,.1,.50,.25)
SetObjectImage(cowId+4,wireFrameTextureID,0)
setObjectPosition(cowId+4,getObjectX(cowId)-(mapX/2)+1.0,getObjectY(cowId)-mapY-4.75,getObjectZ(cowId)-10.25)
SetObjectRotation(cowId+4,0,0,0)
SetObjectTransparency(cowId+4,1)
//SetObjectCollisionMode(cowId+4,1) //on is set or raycasting wont work
FixObjectToObject(cowId+4,cowId)
//leg left back
CreateObjectBox(cowId+5,.1,.50,.25)
SetObjectImage(cowId+5,wireFrameTextureID,0)
setObjectPosition(cowId+5,getObjectX(cowId)-(mapX/2)-1.0,getObjectY(cowId)-mapY-4.75,getObjectZ(cowId)-9.75)
SetObjectRotation(cowId+5,0,0,0)
SetObjectTransparency(cowId+5,1)
//SetObjectCollisionMode(cowId+5,1) //on is set or raycasting wont work
FixObjectToObject(cowId+5,cowId)
//leg right back
CreateObjectBox(cowId+6,.1,.50,.25)
SetObjectImage(cowId+6,wireFrameTextureID,0)
setObjectPosition(cowId+6,getObjectX(cowId)-(mapX/2)+1.0,getObjectY(cowId)-mapY-4.75,getObjectZ(cowId)-9.75)
SetObjectRotation(cowId+6,0,0,0)
SetObjectTransparency(cowId+6,1)
//SetObjectCollisionMode(cowId+6,1) //on is set or raycasting wont work
FixObjectToObject(cowId+6,cowId)
endfunction
function createHeadlights()
SetAmbientColor( 0, 0, 0 )
SetSunActive (0)
If GetObjectExists(myGround) then DeleteObject(myGround)
CreateObjectPlane(myGround, 250, 250 )
SetObjectPosition (myGround, 0, 0, 0)
SetObjectRotation (myGround, 90, 0, 0)
setObjectColor (myGround, 255, 255,225,255)
if GetObjectExists(carLightObj) then DeleteObject(carLightObj)
if GetObjectExists(carLightObj+1) then DeleteObject(carLightObj+1)
CreateObjectBox (carLightObj,1.0, 1.0, 1.0)
SetObjectPosition (carLightObj,-2.0, 0, 10)
CreateObjectBox (carLightObj+1,1.0, 1.0, 1.0)
SetObjectPosition (carLightObj+1,2.0, 0, 10)
SetObjectVisible(carLightObj,0)
SetObjectCollisionMode(carLightObj,0)
SetObjectVisible(carLightObj+1,0)
SetObjectCollisionMode(carLightObj+1,0)
if GetObjectExists(otherCarLightObj) then DeleteObject(otherCarLightObj)
if GetObjectExists(otherCarLightObj+1) then DeleteObject(otherCarLightObj+1)
CreateObjectBox (otherCarLightObj,1.0, 1.0, 1.0)
SetObjectPosition (otherCarLightObj,-2.0, 0, -10)
CreateObjectBox (otherCarLightObj+1,1.0, 1.0, 1.0)
SetObjectPosition (otherCarLightObj+1,2.0, 0, -10)
SetObjectVisible(otherCarLightObj,0)
SetObjectCollisionMode(otherCarLightObj,0)
SetObjectVisible(otherCarLightObj+1,0)
SetObjectCollisionMode(otherCarLightObj+1,0)
if GetPointLightExists(carLight) then DeletePointLight(carLight)
if GetPointLightExists(carLight+1) then DeletePointLight(carLight+1)
CreatePointLight( carLight, getObjectX (carLightObj), getObjectY (carLightObj), getObjectZ (carLightObj), 200.0, 255, 255, 255 )
SetPointLightMode(carLight, 1 )
CreatePointLight( carLight+1, getObjectX (carLightObj+1), getObjectY (carLightObj+1), getObjectZ (carLightObj+1), 200.0, 255, 255, 255 )
SetPointLightMode(carLight+1, 1 )
if GetPointLightExists(otherCarLight) then DeletePointLight(otherCarLight)
if GetPointLightExists(otherCarLight+1) then DeletePointLight(otherCarLight+1)
CreatePointLight( otherCarLight, getObjectX (otherCarBlock+1), getObjectY (otherCarBlock+1), getObjectZ (otherCarBlock+1), 200.0, 255, 255, 255 )
SetPointLightMode(otherCarLight, 1 )
CreatePointLight( otherCarLight+1, getObjectX (otherCarBlock+1), getObjectY (otherCarBlock+1), getObjectZ (otherCarBlock+1), 200.0, 255, 255, 255 )
SetPointLightMode(otherCarLight+1, 1 )
FixObjectToObject( carLightObj, carBlock )
FixObjectToObject( carLightObj+1, carBlock )
FixObjectToObject( otherCarLightObj, otherCarBlock )
FixObjectToObject( otherCarLightObj+1, otherCarBlock )
SetPointLightPosition( carLight, GetObjectWorldX (carLightObj), getObjectWorldY (carLightObj), getObjectWorldZ (carLightObj))
SetPointLightPosition( carLight+1, GetObjectWorldX (carLightObj+1), getObjectWorldY (carLightObj+1), getObjectWorldZ (carLightObj+1))
SetPointLightPosition( otherCarLight, GetObjectWorldX (otherCarLightObj), getObjectWorldY (otherCarLightObj), getObjectWorldZ (otherCarLightObj))
SetPointLightPosition( otherCarLight+1, GetObjectWorldX (otherCarLightObj+1), getObjectWorldY (otherCarLightObj+1), getObjectWorldZ (otherCarLightObj+1))
endfunction
function createShader()
file = OpenToWrite("Anaglyph.vs")
WriteLine(file,"attribute vec3 position;")
WriteLine(file,"varying vec2 uvVarying;")
WriteLine(file,"uniform vec4 uvBounds0;")
WriteLine(file,"uniform float agk_invert;")
WriteLine(file,"void main()")
WriteLine(file,"{")
WriteLine(file,"gl_Position = vec4(position.xy * vec2(1, agk_invert), 0.5, 1.0);")
WriteLine(file,"uvVarying = (position.xy * vec2(0.5, -0.5) + 0.5) * uvBounds0.xy + uvBounds0.zw;")
WriteLine(file,"}")
CloseFile(file)
file = OpenToWrite("Anaglyph.ps")
WriteLine(file,"uniform sampler2D texture0;")
WriteLine(file,"uniform sampler2D texture1;")
WriteLine(file,"varying mediump vec2 uvVarying;")
WriteLine(file,"void main()")
WriteLine(file,"{")
WriteLine(file,"vec3 LColor = texture2D(texture0, uvVarying).xyz;")
WriteLine(file,"vec3 RColor = texture2D(texture1, uvVarying).xyz;")
WriteLine(file,"vec4 Color;")
WriteLine(file,"Color.r = 0.299 * LColor.r + 0.587 * LColor.g + 0.114 * LColor.b;")
WriteLine(file,"Color.g = RColor.g;")
WriteLine(file,"Color.b = RColor.b;")
WriteLine(file,"Color = clamp(Color,0.0,1.0);") //restricts color range
WriteLine(file,"gl_FragColor = Color;")
WriteLine(file,"}")
CloseFile(file)
endfunction
A brief explanation of what is happening PS: If I use a normal code block here it upsets AppGameKit code blocks in the same comment
a quick run down is that the code creates all the posts and positions them on the x,y,z planes
the movement of the car really just moves these posts once the post have reached so far off
they start at the back again. So basically they make it never ending. There is a sinecurve that
is used to position them on the xplane. Cows etc move the same way and time that the posts
do. The other car yes there is only one which starts at a distant z location calculates a vector
to point it towards the centre of the road and moves at its own rate the same way the posts do.
The cows on the road are always on the road the only thing that changes is a random factor that
determines if they should be visible and collisions turned on. once they pass your car whether they
are visible or not they are positioned back off to a distant z position some rotating and positioning
again.
There is a shader which uses camera offsets for the left eye and then the right eye to use the
stereoscopic shader it needs to use Update(), Render(), Swap() to manually sync so when these
commands are used which is only when the user presses the stereo button the sync is not called
and they need to be done in the order as they are in code if you wish for stereoscopic 3D.
fubar