I have written some code for a basic 3D Pong game and no matter what I set the sync rate to (other than 0) I can not get the FPS above 64.
Rem Project: 3D Pong Game
Rem Created: 2/6/2005 11:12:44 AM
Rem ***** Main Source File *****
gosub _gameSetup
do
`--------------------------------------------------------------
sync `update the screen
`--------------------------------------------------------------
gameSpeed = screen fps()/20
if gameSpeed =< 1 then gameSpeed = 2
`------------------------------------------------------------------------******remove*****
gosub _game_Reset
`----------------------------------------------------------------******remove******
gosub _moveBall `move the ball
gosub _movePaddle_1 `move the paddle
gosub _moveCamera `move the camera
gosub _move_2_Paddle `move computer controled paddle
gosub _win_Game
`--------------------------------------------------------------******remove******
`these are used for testing to verify positions and flag status
gosub _print
`---------------------------------------------------------------******remove******
loop `end main loop
`--------------------------------------------------------------
_moveBall:
` get the current positon of the ball
ox# = object position x(100)
oy# = object position y(100)
oz# = object position z(100)
remstart
this is for the ball movement
xFactor will be set depending on how the ball hits the players paddle
will get the x position of the ball and the x Position of the paddle and determine
when the ball hits the paddle where it hits by looking at the two figures
the center of the paddle will equal where the center of the ball is
if you subtract the paddle x position from the ball x position this should tell
you where on the paddle the ball hits. Change the x and z factor depending where on
the paddle the ball hits. Paddle will need to be divided into 7 sections and will change
the directions as follows:
hit the center of paddle - ball heads straight out from paddle 0 to 1 factor (x to z)
hit in section -1 or +1 ball bounces in same direction on a 1 to 2 factor (x to z)
hit in section -2 or +2 ball bounces in same direction on a 1 to 1 factor (x to z)
hit in section -3 or +3 ball reverses direction at same factor as arriving 2 to 1
remend
if directionX = 1 then x# = x#+xFactor*gameSpeed
if directionX = 0 then x# = x#-xFactor*gameSpeed
if directionZ = 1 then z# = z#+zFactor*gameSpeed
if directionZ = 0 then z# = z#-zFactor*gameSpeed
gosub _ballCollision1
position object 100,x#,y#,z# `moves the ball
if ballRotation = 0
xro# = xro#-gameSpeed*3 : xrotate object 100,xro#
else xro# = xro#+gameSpeed*3 : xrotate object 100,xro#
endif
return
_ballCollision1:
`sets the new position for the intersect object command for the walls and paddle
` and keeps the ball from going too far into wall before reversing
if directionX = 1 then nx# = x#+8 else nx# = x#-8
if directionZ = 1 then nz# = z#+8 else nz# = z#-8
` if directionY = 1 then ny# = y#+8 else ny# = y#-8
ny# = y#
`this code reverses the direct of the ball and sets the ball back to the old position
`if it hits a wall in any of the three directions
`let the collision check object 1 by itself. This will judge if it hits the wall
`and ball should bounce off wall.
`Will make an exact check for objects 2 and 3 so we can apply xFactors and zFactors
`depending on how ball hits the paddle ***see ballMove for explanation.
`-----------------------------------------------------------------
obj=1 `check to see if it hits the wall
ballHitX = intersect object(obj,ox#,oy#,oz#,nx#,oy#,oz#)
if ballHitX => 1
x# = ox#
if directionX=1 then directionX = 0 else directionX = 1
endif
ballHitY = intersect object(obj,ox#,oy#,oz#,ox#,ny#,oz#)
if ballHitY => 1
y# = oy#
if directionY = 1 then directionY = 0 else directionY = 1
endif
ballHitZ = intersect object(obj,ox#,oy#,oz#,ox#,oy#,nz#)
if ballHitZ => 1
z# = oz#
if directionZ = 1 then directionZ = 0 else directionZ = 1
endif
`-----------------------------------------------------------------
for paddle=2 to 3 `check to see if it hits either paddle
ballHitX = intersect object(paddle,ox#,oy#,oz#,nx#,oy#,oz#)
if ballHitX => 1
x# = ox#
if paddle = 3 then if directionX=1 then directionX = 0 else directionX = 1
endif
ballHitY = intersect object(paddle,ox#,oy#,oz#,ox#,ny#,oz#)
if ballHitY => 1
y# = oy#
if paddle = 3 then if directionY = 1 then directionY = 0 else directionY = 1
endif
ballHitZ = intersect object(paddle,ox#,oy#,oz#,ox#,oy#,nz#)
if ballHitZ => 1
z# = oz#
cbx# = object position x (100) ` center of ball X
cpx# = object position x(paddle) `center of ball X
gosub _x_z_Factor
if paddle = 3
if directionZ = 1 then directionZ = 0:ballRotation = 0
else directionZ = 1:ballRotation = 1
endif
endif
next paddle
`This covers each corner of the paddle
cbx# = object position x(100) `center of ball X
cpx# = object position x(2) `center of paddle X
cbz# = object position z(100) `center of ball Z
cpz# = object position z(2) `center of paddle Z
`if center of ball - center of paddle is between 33 and 40 and the
` and if the ball is somewhere even with the paddle then send the ball
` off in a different direction
if cbx# - cpx# => 33 and cbx# - cpx# =< 40 and cbz# =< 80 and cbz# => 62
directionZ = 1: directionX = 1: xFactor=2: zFactor=1
ballRotation = 1
endif
`if center of ball - center of paddle is between -33 and -40
` and if ball is somewhere even with the paddle change the direction of the ball
` and send off at opposite angle
if cbx# - cpx# =< -33 and cbx# - cpx# => -40 and z# =< 70 and z# => 62
directionZ = 1: directionX = 0: xFactor=2: zFactor=1
ballRotation = 1
endif
`-------------------------------------------------------------------------
`Need code similar to above code for other paddle
`-------------------------------------------------------------------
`this is used to keep score
`if this hits the far wall score a point for player
scoreHitBox = intersect object (10,ox#,oy#,oz#,nx#,ny#,nz#)
if scoreHitBox => 1
score = score + 1
ballRotation = 0
startTimer = timer()
endif
print "Score: ";score
`if ball gets stuck behind computer paddle
if cbz#>990
endTime = timer()
if endTime - startTime > 3000
position object 100, oz#, oy#, oz# - 10
endif
endif
scoreHitBox1 = intersect object (11,ox#,oy#,oz#,nx#,ny#,nz#)
if scoreHitBox1 => 1 then score1 = score1 + 1 : ballRotation = 1
print "Score1: ";score1
return
_movePaddle_1:
`get position of paddle before the move
opx1# = object position x (2)
opy1# = object position y (2)
opz1# = object position z (2)
opx2# = object position x (3)
opy2# = object position y (3)
opz2# = object position z (3)
`controls for Player Paddle
if rightkey()=1 and controlkey() = 0
paddleStopLR = 32 `stops paddle from going through wall
move object right 2,5
endif
if leftkey()=1 and controlkey() = 0
paddleStopLR = -32 `stops paddle from going through wall
move object left 2,5
endif
gosub _paddle_1_Collision
return
_move_2_Paddle:
ballMove = object position x (100) `track the ball
paddleMove = object position x (3) `track the paddle
amt = rnd(3):multi = rnd(4)
move = amt*multi
if move < 4 then move = 5
if move > 7 then move = 7
if ballMove < paddleMove
move object left 3,move
endif
if ballMove > paddleMove
move object right 3,move
endif
` position object 3,ballMove,10,950
gosub _paddle_2_Collision
return
_paddle_1_Collision:
`this will need to be move into the paddle move command
` so that the paddle will not into move into the wall area's
`will also need to look at moving the paddle move into a section of the loop
`where the paddle to ball collision check is done so that the ball does not
`move through paddle when the paddle is in motion.
npx1# = object position x (2)
npy1# = object position y (2)
npz1# = object position z (2)
paddleHitX = intersect object(1,opx1#,opy1#,opz1#,npx1#+paddleStopLR,opy1#,opz1#)
if paddleHitX =>1 then npx1# = opx1#
paddleHitY = intersect object(1,opx1#,opy1#,opz1#,opx1#,npy1#+paddleStopUD,opz1#)
if paddleHitY =>1 then npy1# = opy1#
paddleHitZ = intersect object(1,opx1#,opy1#,opz1#,opx1#,opy1#,npz1#)
if paddleHitZ =>1 then npz1# = opz1#
position object 2,npx1#,npy1#,npz1#
return
_paddle_2_Collision:
npx2# = object position x (3)
npy2# = object position y (3)
npz2# = object position z (3)
paddleHitX = intersect object(1,opx2#,opy2#,opz2#,npx2#+paddleStopLR,opy2#,opz2#)
if paddleHitX =>1 then npx2# = opx2#
paddleHitY = intersect object(1,opx2#,opy2#,opz2#,opx2#,npy2#+paddleStopUD,opz2#)
if paddleHitY =>1 then npy2# = opy2#
paddleHitZ = intersect object(1,opx2#,opy2#,opz2#,opx2#,opy2#,npz2#)
if paddleHitZ =>1 then npz2# = opz2#
position object 3,npx2#,npy2#,npz2#
return
_moveCamera:
cox# = object position x(2) `current object X (paddle)
coy# = object position y(2) `current object Y (paddle)
coz# = object position z(2) `current object Z (paddle)
position camera 0,cox#,coy#+25,coz#-25 `posision camera behind paddle
point camera 0, x#,y#,z# `point camera in direction of ball x,y,z
return
_x_z_Factor:
`if ball hits the center of the paddle
factor = cbx#-cpx#
if factor => -3 and factor <= 3
xFactor = 0: zfactor = 2: directionX = 0: directionZ = 1
endif
`------------------------------------------------------------
` if ball hits the first level out on right side of center
if factor => 4 and factor <= 14
if xFactor = 0 and directionX = 0 and directionZ = 0
directionX = 1: directionZ = 1
endif
if directionX = 1 and directionZ=0
directionX = 1: directionZ = 1
endif
if directionX = 0 and directionZ = 0:
directionX = 0: directionZ = 1
endif
xFactor = 1: zFactor = 2
endif
`if ball hits the first level out on left side of center
if factor =< -4 and factor => -14
if xFactor = 0 and directionX = 0 and directionZ = 0
directionX = 0: directionZ = 1
endif
if directionX = 1 and directionZ=0
directionX = 1: directionZ = 1
endif
if directionX = 0 and directionZ = 0:
directionX = 0: directionZ = 1
endif
xFactor = 1: zFactor = 2
endif
`------------------------------------------------------------
` if ball hits the second level out on right side of center
if factor => 15 and factor <= 23
if xFactor = 0 and directionX = 0 and directionZ = 0
directionX = 1: directionZ = 1
endif
if directionX = 1 and directionZ=0
directionX = 1: directionZ = 1
endif
if directionX = 0 and directionZ = 0:
directionX = 0: directionZ = 1
endif
xFactor = 2: zFactor = 2
endif
` if ball hits the second level out on left side of center
if factor =< -15 and factor => -23
if xFactor = 0 and directionX = 0 and directionZ = 0
directionX = 0: directionZ = 1
endif
if directionX = 1 and directionZ=0
directionX = 1: directionZ = 1
endif
if directionX = 0 and directionZ = 0:
directionX = 0: directionZ = 1
endif
xFactor = 2: zFactor = 2
endif
`--------------------------------------------------------------
` if ball hits the third level out on right side of center
if factor => 24 and factor <= 32
if xFactor = 0 and directionX = 0 and directionZ = 0
directionX = 1: directionZ = 1
endif
if directionX = 1 and directionZ=0
directionX = 1: directionZ = 1
endif
if directionX = 0 and directionZ = 0:
directionX = 0: directionZ = 1
endif
xFactor = 2: zFactor = 1
endif
` if ball hits the third level out on left side of center
if factor =< -24 and factor => -32
if xFactor = 0 and directionX = 0 and directionZ = 0
directionX = 0: directionZ = 1
endif
if directionX = 1 and directionZ=0
directionX = 1: directionZ = 1
endif
if directionX = 0 and directionZ = 0:
directionX = 0: directionZ = 1
endif
xFactor = 2: zFactor = 1
endif
return
_gameSetup:
`basic game setup
sync on
sync rate 200
autocam off
hide mouse
set camera range .01,5000
` Variables to make changes to the game set up
cubeSize = 200 `quick way to change the size of the playing field
ballSize = 20 `quick way to change the size of the ball
` Variables to contol the direction of the ball
` These flags will be used to add to or subtract from the x,y,z position of the ball
` ie: if the flag is set to 1 add to the direction,
` if flag = 0 then subtract from the direction
directionX = 1
directionZ = 1
directionY = 3
ballRotation = 1
xFactor = 1
zFactor = 2
score = 0
score1 = 0
` Make the game objects
load object "MediaRoomsRoom_1.x",1 `Loads the Room Cube choose from 4
scale object 1,cubeSize,cubeSize/4,cubeSize `Scale the Room Cube
position object 1,0,0,0 `Positions the Room Cube
set object collision to boxes 1 `Turns on collision detection
roomXsize = object size x(1) `Get the witdth of Room
roomYsize = object size y(1) `Get the height of Room
roomZsize = object size z(1) `Get depth of Room
make object sphere 100,ballSize `Makes the ball that will bounce around
position object 100,roomXsize/2,10,roomZsize/2
load image "Mediacamo2.jpg",10
texture object 100,10
set object collision to spheres 100
`makes the paddle
make object box 2,64,16,8
set object collision to boxes 2
position object 2,500,10,40
load image "MediaPaddlesGold.bmp",2
texture object 2,2
`makes the 2nd paddle
make object box 3, 64,16,8
set object collision to boxes 3
position object 3,500,10,980
load image "MediaPaddlesSilver.bmp",3
texture object 3,3
`this makes a box and places it at the far facing wall for scoring
make object box 10, 1080,roomYsize,1 `makes the box
set object collision to boxes 10 `turn on collision
position object 10, roomXsize/2-32,roomYsize/2,roomZsize-126 `positions the box at the far side
hide object 10 `hide it so you can not see it
`this makes a box and places behind paddle for scoring
make object box 11,1080,roomYsize,1
set object collision to boxes 11
position object 11,roomXsize/2-32,roomYsize/2,-1
hide object 11
return
_win_Game:
if score1 = 10
set cursor screen width()/2-140,screen height()/2
print "LOSER! - YOU LOST BIG TIME!"
set cursor screen width()/2-140,screen height()/2+15
print "Press 'R' to Play again or Esc. to Quit"
sync
wait key
if inkey$() = "r":
position object 100,500,10,500
score = 0 : score1 = 0
endif
endif
if score = 10
set cursor screen width()/2-140,screen height()/2
print "Winner! - YOU JUST SQUEAKED THAT ONE OUT!"
set cursor screen width()/2-140,screen height()/2+15
print "Press 'R' to Play again or Esc. to Quit"
sync
wait key
if inkey$() = "r"
position object 100,500,10,500
score = 0 : score1 = 0
endif
endif
return
_game_Reset:
`Reset if the ball goes out of playing field
if inkey$() = "b"
x# = roomXsize/2
y# = 10
z# = roomZsize/2
position object 100,x#,y#,z#
endif
if inkey$() = "p" then position object 2,500,10,150 `reset the paddle
`controls for X Movement
if inkey$() = "w" then directionX = 1
if inkey$() = "s" then directionX = 0
if inkey$() = "x" then directionX = 3
`controls for Z Movement
if inkey$() = "q" then directionZ = 1
if inkey$() = "a" then directionZ = 0
if inkey$() = "z" then directionZ = 3
`controls for Y Movement
if inkey$() = "e" then directionY = 1
if inkey$() = "d" then directionY = 0
if inkey$() = "c" then directionY = 3
return
_print:
set cursor 0,0
print " Press 'b' to reset the ball"
print "FPS: ";screen fps()
`print gameSpeed
`print "ball: " ;ballMove
`print "Paddle: ";paddleMove
`print "xFactor: "; xFactor
`print "zFactor: "; zFactor
`print "DirectionX: ";directionX
`print "DirectionZ: ";directionZ
`print " Ball X: ";object position x(100); " PaddleX: ";object position x(2)
`bpx# = object position x(100):ppx# = object position x(2)
`print " ball strikes paddle at: ";bpx#-ppx#
return
Any help would be great.
If you want the media files you can find them here...
http://home.comcast.net/~johnnybolt/3D_Pong_Game.zip
The only true failure is not to try at all...