You're welcome. Glad to help.
I updated the AI a little bit and the ball Y movement is based upon the direction of the paddle when hit. I also changed the way you were moving the ball. I have left the music and sound stuff remmed out because I keep having to REM it out every time I work on it.
There is a problem in that the ball can go further down the screen than what the paddles can. I left that for you
sync on : sync rate 60
set display mode 800,600,32
backdrop off : randomize timer()
hide mouse
`VARIABLES
global start_menu as integer
global ypos as integer
global xpos as integer
global x2pos as integer
global y2pos as integer
global bxpos as integer
global bypos as integer
global movey as integer
global movex as integer
global GameOver as integer
global mode as integer
global P0YMove as integer : global P1YMove as integer
global ScrWid as integer : ScrWid = screen width()
global ScrHgt as integer : ScrHgt = screen height()
dim score(1)
ypos = 260
xpos = 20
x2pos = 780
y2pos = 260
bxpos = ScrWid / 2
bypos = 280
movey = rnd(2) : if rnd(100) > 50 then movey = movey * -1
movex = rnd(2) + 2 : : if rnd(100) > 50 then movex = movex * -1
score(0) = 0 : score(1) = 0
`LOAD ALL MEDIA
`LOAD IMAGES
load image "media\startscreen.bmp",1
load image "media\paddle1.bmp",2
load image "media\paddle2.bmp",3
load image "media\startbutton.bmp",4
load image "media\ball.png",5
` load image "media\border.bmp",6
` load image "media\background.bmp",7
load image "media\highscorebutton.bmp",8
`LOAD MUSIC/SOUNDS
` load sound "media\Hit metal.wav",1
` load music "media\music space loop.wav",1
load_menu()
sprite 1,xpos,ypos,2
sprite 2,x2pos,y2pos,3
`MAIN LOOP
repeat
`store the player's y positions
OldP0Y = sprite y(1) : OldP1Y = sprite y(2)
cls
playermovement()
if mode = 1
playermovement2()
else
gosub AIMovement
endif
`calculate the amount the players have moved on the Y axis
P0Ymove = sprite y(1) - OldP0Y
P1Ymove = sprite y(2) - OldP1Y
collision()
NeedToRestart = ballmovement()
scoring()
if NeedToRestart = 1 then ReStart()
if start_menu = 0 then load_menu()
if GameOver = 1 then GameOver()
sync
until GameOver = 1
End
function ReStart()
` make sure no one is already pressing a mouse button
repeat
sync
until mouseclick() = 0
` reset the ball direction
movey = rnd(2) : if rnd(100) > 50 then movey = movey * -1
movex = rnd(2) + 2 : : if rnd(100) > 50 then movex = movex * -1
` reset the ball's position to the center of the screen
bxpos = ScrWid / 2 : bypos = ScrHgt / 2
sprite 4,bxpos,bypos,5
repeat
cls
` allow the players to move while waiting to start
playermovement()
if mode = 1
playermovement2()
else
AIMovement()
endif
ink rgb(255,255,0),0
center text ScrWid / 2,10,"Press any mouse button to start."
scoring()
sync
until mouseclick() > 0
endfunction
function load_menu()
mode = 2 : ` using AI
repeat
` if music playing(1) = 0 then play music 1
cls
paste image 1,0,0
sprite 10,270,300,4
sprite 11,260,400,8
show mouse
mx = mousex() : my = mousey()
if mx > 270 and my > 300 and mx < 526 and my < 364 and mouseclick() = 1
delete sprite 10
delete sprite 11
hide mouse
start_menu = 1 : GameOver = 0
` stop music 1
mode = 2 : ` temporary until the AI routines are put in (normally mode = 2)
Endif
if mx > 260 and my > 400 and mx < 516 and my < 464 and mouseclick() = 1
delete sprite 10
delete sprite 11
hide mouse
start_menu = 1 : GameOver = 0
` stop music 1
mode = 1
Endif
sync
until start_menu = 1
endfunction
`Paddle movement (y-axis only)
function playermovement()
if keystate(17) = 1 then ypos = ypos - 4
if keystate(31) = 1 then ypos = ypos + 4
if ypos < 20 then ypos = ypos + 4
if ypos > 510 then ypos = ypos - 4
sprite 1,xpos,ypos,2
ENDFUNCTION
`Paddle movement for 2nd player
function playermovement2()
if upkey() = 1 then y2pos = y2pos - 4
if downkey() = 1 then y2pos = y2pos + 4
if y2pos < 20 then y2pos = y2pos + 4
if y2pos > 510 then y2pos = y2pos - 4
sprite 2,x2pos,y2pos,3
ENDFUNCTION
function ballmovement()
NeedToRestart = 0
inc bxpos,movex : inc bypos,movey
if bypos > 580 then movey = movey * -1
if bypos < 20 then movey = movey * -1
sprite 4,bxpos,bypos,5
if bxpos < xpos
score(1) = score(1) + 1
if score(1) = 3
GameOver = 1 : sync : ` sync so that we can see the player's updated score
else
NeedToRestart = 1
endif
endif
if bxpos > x2pos
score(0) = score(0) + 1
if score(0) = 3
GameOver = 1 : sync : ` sync so that we can see the player's updated score
else
NeedToRestart = 1
endif
endif
ENDFUNCTION NeedToRestart
function collision()
` check if the ball hit player 0's paddle
if sprite hit(1,4) = 1
movex = movex * - 1 : ` change x direction
movey = P0Ymove : ` change ball's Y direction based upon the movement of player 0's paddle
endif
`check if the ball hit player 1's paddle
if sprite hit(2,4) = 1
movex = movex * -1
movey = P1Ymove : ` change ball's Y direction based upon the movement of player 1's paddle
endif
endfunction
function scoring()
ink rgb(255,0,0),0
text 40,10,"PLAYER 1: " +str$(score(0))
ink rgb(0,255,0),0
text 680,10,"PLAYER 2: " +str$(score(1))
endfunction
function GameOver()
set text size 60 : wait 5
ink rgb(255,0,255),0
center text ScrWid / 2,160,"GAME OVER"
PTW = 1 : if score(1) = 3 then PTW = 2
set text size 36 : wait 5
ink rgb (255,0,0),0
center text ScrWid / 2,300,"PLAYER " + str$(PTW) + " WINS!!"
DeleteAllSprites()
sync
set text size 14
wait 3000
start_menu = 0
load_menu()
sprite 1,xpos,ypos,2
sprite 2,x2pos,y2pos,3
score(0) = 0 : score(1) = 0
bxpos = ScrWid / 2 : bypos = ScrHgt / 2
endfunction
function DeleteAllSprites()
if sprite exist(1) = 1 then delete sprite 1
if sprite exist(2) = 1 then delete sprite 2
if sprite exist(4) = 1 then delete sprite 4
endfunction
function AImovement()
If bypos < y2pos
Dec y2pos,3
If y2pos < 10 Then y2pos = 10
else
Inc y2pos,3
If y2pos > 540 Then y2pos = 540
endif
sprite 2,x2pos,y2pos,3
endfunction
Computers do exactly what you tell them.........don't you hate that sometimes?