Your code
Rem Project: pong
Rem Created: 2005-03-04 22:28:44
Rem ***** Main Source File *****
rem settings
settings()
rem load level
make_lvl()
rem load ball, players and settings
load_players()
rem included variabels
do
rem player movvement code
players_movement()
rem ball movement code
ball_movement()
rem score
if ballx#>34
player1score#=player1score#+1
endif
text 10,50,str$(player1score#)
sync
loop
rem functions
function settings()
sync on
sync rate 70
hide mouse
make camera 1 : position camera 1,0,0,-50 : color backdrop 1,RGB(0,0,0)
player1score#=0
endfunction
function make_lvl()
load object "media/models/tak.3ds",4 : position object 4,0,25,0 : color object 4,RGB(0,0,255) : scale object 4,20,20,20 : rotate object 4,0,90,0
load object "media/models/tak.3ds",5 : position object 5,0,-25,0 : color object 5,RGB(0,0,255) : scale object 5,20,20,20 : rotate object 5,180,90,0
make object box 6,2,46.5,6
position object 6,0,0,0
ghost object on 6
color object 6,RGB(0,128,255)
endfunction
function load_players()
load object "media/models/hover.3ds",1 : color object 1,RGB(0,0,255) : position object 1,-32,0,0 : scale object 1,20,20,20 : rotate object 1,160,0,90
load object "media/models/hover.3ds",2 : color object 2,RGB(0,0,255) : position object 2,32,0,0 : scale object 2,20,20,20 : rotate object 2,340,180,90
make object sphere 3,1.5
`We need to store the x and y speed of the ball , so first we make a little array to hold this info
dim ballspeed#(10) : ` we have dimed it to 2 as thats 1 slot for x and one for y
ballspeed#(1)=0.2 : ` Ok now we set the x speed to 0.2
ballspeed#(2)=0.2 : ` and the y speed to 0.2
endfunction
function players_movement()
p1mvu=0
p1mvd=0
p2mvu=0
p2mvd=0
if upkey()=1 then p2mvu=1
if downkey()=1 then p2mvd=1
if keystate(17)=1 then p1mvu=1
if keystate(31)=1 then p1mvd=1
x#=object position x(1)
y#=object position y(1)
z#=object position z(1)
x2#=object position x(2)
y2#=object position y(2)
z2#=object position z(2)
if p1mvu=1
y#=y#+0.5
endif
if p1mvd=1
y#=y#-0.5
endif
if p2mvu=1
y2#=y2#+0.5
endif
if p2mvd=1
y2#=y2#-0.5
endif
rem makes the players to stay in screen
if y#>21 then y#=21
if y#<-21 then y#=-21
if y2#>21 then y2#=21
if y2#<-21 then y2#=-21
position object 1,x#,y#,z#
position object 2,x2#,y2#,z2#
endfunction
remstart
function ball_movement()
ballx#=object position x(3)
bally#=object position y(3)
ballz#=object position z(3)
rem making the ball move after shooting it
ballx#=ballx#+0.2
bally#=bally#+0.2
if bally#>=21 then bally#=bally#-0.8
if ballx#>=21 then ballx#=ballx#-0.8
position object 3,ballx#,bally#,ballz#
endfunction
remend
function ball_movement()
ballx#=object position x(3)
bally#=object position y(3)
ballz#=object position z(3)
rem making the ball move after shooting it
` to find the new position we add the speed to the current position so..
ballx#=ballx#+ballspeed#(1)
bally#=bally#+ballspeed#(2) : ` and the y speed to the y position
text 10,10,str$(ballx#)
text 10,30,str$(ballspeed#(1))
` ok so if the ball hits the wall then we want it to bounce off...which mean reversing the y speed
if bally#>24 then ballspeed#(2)=(ballspeed#(2)*-1) : bally#=24 : ` here we check if its at the top wall and if it is reverse..
` the speed and also put the ball at position 21...now we have to set up the same kind of thing for the bottom wall.
if bally#<-24 then ballspeed#(2)=(ballspeed#(2)*-1) : bally#=-24: ` and that is the y boucing handled
` Next we have to sort out the paddels...so we check if the x position is over ___ and whether the ball is in the y range
` of the padel
` this is for collision with player 1
if ballx#>30 and ballx#<32
if bally#>(object position y(2)-6) and bally#<(object position y(2)+6)
ballx#=30
ballspeed#(1)=ballspeed#(1)*-1
endif
endif
` this is for collision with player 2
if ballx#<-30 and ballx#>-32
if bally#>(object position y(1)-6) and bally#<(object position y(1)+6)
ballx#=-30
ballspeed#(1)=ballspeed#(1)*-1
endif
endif
position object 3,ballx#,bally#,ballz#
endfunction
`----------------------------------
Quote: "
if ballx#>34
player1score#=player1score#+1
endif
"
Ok so this is the bit from the main loop to see if the ball is has gone past one of the players.
The problem is that you are checking ballx# for the balls position.
The reason that this does not work is that ballx# is a local variable , so it cannot be read except in the function it was made.
The solution to this is that we replace...
Quote: "
if ballx#>34
player1score#=player1score#+1
endif
"
..with...
Quote: "
if Object position x(3)>34
player1score#=player1score#+1
endif
"
That should fix the problem, you will have to move the ball back to the starting line though, youll see why when you run the modified code !
Now i assumed that you know about local and global variable in this answer, however if you dont just ask again and ill explain them.
Hope this helps !