Alright! I got something going I think. I did some hard thinking and started trying to figure things out for myself. Not the most amazing display of AI, but it's a really good start for me and best of all, I figured it all out for myself, which isn't saying much. But here's the new code for anyone who may care to look. It's all ready to copy and paste in DBPro with no media required. Soo far now I've got the compute rmoving around on the x and y axis and it tries to go into defensive mode when the ball makes it to a certain coordinate. May not be th ebest way to do it, but hey it works for me. Not bad for a not too smart noob eh?
remstart
DarkBasic Pro Pong Game
Created by Clickable SMiley 2005
remend
`________________________________________________________________________________________
sync on : sync rate 0
set display mode 800,600,32
backdrop on : color backdrop rgb(0,0,0)
hide mouse
`position variables
boardcenterx=395 : boardtopy=205 : boardbottomy=370
`player 1 image variables
left=0 : top=0 : right=5 : bottom=30
player1image=1
`computer player image variables
`use same as player, just define new image for computer player
computerplayerimage=4
`bounding box variables
`top of screen and bottom
boundleft=0 : boundtop=0 : boundright=400 : boundbottom=5
boundtopbottom=2
`left and right side of screen
sideleft=0 : sidetop=0 : sideright=5 : sidebottom=200
boundleftright=3
`middle line for court division
midleft=0 : midtop=0 : midright=5 : midbottom=5
midline=10
`pong ball variables for size
ballleft=0 : balltop=0 : ballright=5 : ballbottom=5
ballimage=5
`create player image
ink rgb(255,0,0),1
box left, top, right, bottom
get image player1image, left, top, right, bottom
ink rgb(255,255,255),1
`create the computer opponent image
ink rgb(255,0,0),1
box left, top, right, bottom
get image computerplayerimage, left, top, right, bottom
ink rgb(255,255,255),1
`create dotted court division line
box midleft, midtop, midright, midbottom
get image midline, midleft, midtop, midright, midbottom
`create bounding boxes for the play area (top and bottom)
box boundleft, boundtop, boundright, boundbottom
get image boundtopbottom, boundleft, boundtop, boundright, boundbottom
`create bounding boxes for the play area (left and right)
box sideleft, sidetop, sideright, sidebottom
get image boundleftright, sideleft, sidetop, sideright, sidebottom
`create ball for playing
box ballleft, balltop, ballright, ballbottom
get image ballimage, ballleft, balltop, ballright, ballbottom
`position the bounding boxes for the players area (top and bottom)
sprite 2, 200, 200, boundtopbottom
sprite 3, 200, 400, boundtopbottom
`position the bounding boxes for the players area (left and right)
sprite 4, 200, 200, boundleftright
sprite 6, 595, 200, boundleftright
`position the dotted lines down the center
for dots=10 to 205 step 10
sprite dots, boardcenterx, 195+dots, midline
next dots
`set player 1 initial starting point in the play area
playerx#=256 : playery#=206
`set computer player initial starting point in the play area
computerplayerx#=539 : computerplayery#=206
`set ball initial starting point
ballpositionx#=390 : ballpositiony#=290
`ball variables for x and y coordinate speeds to give angled rebounds
ballmovespeedx#=0.5
ballmovespeedy#=0.5
`set directions for balls x and y movements. A 0 will move ball left.A 1 will move ball right.
balldirectionx=0
balldirectiony=0
`this section will randomize the x and y direction for each time the ball hits
ballrnddirectionx=rnd(1)
ballrnddirectiony=rnd(1)
`this section will start the ball off in a random direction each time you start
balldirectionx=ballrnddirectionx
balldirectiony=ballrnddirectiony
`this section will establish player scores using an array
dim score(2)
`________________________________________________________________________________________
do
`this section will calculate the new position of the player based on mouse movement
playery#=playery#+mousemovey() : playerx#=playerx#+mousemovex()
`this section attempts to create an AI system for the computer to try and get the ball
gosub _computer_AI
`this section defines the x and y movements of the ball
if balldirectionx=0 then dec ballpositionx#,ballmovespeedx#
if balldirectionx=1 then inc ballpositionx#,ballmovespeedx#
if balldirectiony=0 then dec ballpositiony#,ballmovespeedy#
if balldirectiony=1 then inc ballpositiony#,ballmovespeedy#
`this section will keep the player 1 sprite within boundaries of own section
if playerx#=<205 then playerx#=205
if playerx#=>390 then playerx#=390
if playery#=<205 then playery#=205
if playery#=>370 then playery#=370
`this section will keep the computer player sprite within boundaries of own section
if computerplayerx#=<400 then computerplayerx#=400
if computerplayerx#=>590 then computerplayerx#=590
if computerplayery#=<205 then computerplayery#=205
if computerplayery#=>370 then computerplayery#=370
`this section will display text messages reporting the x and y position of player
set cursor 0,0 : print "Player X Coordinate - ", playerx#
set cursor 0,12 : print "Player Y Coordinate - ", playery#
set cursor 0,24 : print "Ball X Coordinate - ", ballpositionx#
set cursor 0,36 : print "Ball Y Coordinate - ", ballpositiony#
set cursor 0,48 : print "Ball Speed X Axis - ", ballmovespeedx#
set cursor 0,60 : print "Ball Speed Y Axis - ", ballmovespeedy#
set cursor 0,72 : print "Ball Direction X - ", balldirectionx
set cursor 0,84 : print "Ball Direction Y - ", balldirectiony
center text 300,180,"Player 1 : " + str$(score(1))
center text 500,180,"Player 2 : " + str$(score(2))
`this section will update the final new position of the player and computer player
sprite 1, playerx#, playery#, player1image
sprite 7, computerplayerx#, computerplayery#, computerplayerimage
sprite 8, ballpositionx#, ballpositiony#, ballimage
`this section will determine if ball has collided on wall coordinates
if ballpositionx#=<205 and sprite collision(1,8)=0 then balldirectionx=1 : score(2)=score(2)+1
if ballpositionx#=>590 and sprite collision(7,8)=0 then balldirectionx=0 : score(1)=score(1)+1
if ballpositiony#=<205 then balldirectiony=1
if ballpositiony#=>395 then balldirectiony=0
`this section will check for collision between the ball and both players
`if collision occurs, reverse the X direction to go back the other way. Y will already be random.
if sprite collision(1,8)=1
balldirectionx=1
endif
if sprite collision(7,8)=1
balldirectionx=0
endif
sync
loop
`________________________________________________________________________________________
_computer_AI:
oldcomputerplayerx#=computerplayerx# : oldcomputerplayery#=computerplayery#
computerreturnspeed#=150.0
`let's make the computer try and think. if the ball gets closer, put computer into defensive
`mode and back up to make sure it doesn't miss the shot. there are chances it will fail though
`at moving up and down quick enough.
if ballpositiony#=>288 and balldirectiony=1
computerreturny#=ballpositiony#
endif
if ballpositiony#=<288 and balldirectiony=0
computerreturny#=ballpositiony#
endif
if ballpositionx#=>390 and balldirectionx=1
computerreturnx#=600
endif
if ballpositionx#=<390 and balldirectionx=0
computerreturnx#=450
endif
if ballpositiony#<>computerplayery#
computerplayery#=curvevalue(ballpositiony#,computerplayery#,ballmovespeedy#+12.0)+0.10
endif
computerplayerx#=curvevalue(computerreturnx#,computerplayerx#,computerreturnspeed#)
computerplayery#=curvevalue(computerreturny#,computerplayery#,computerreturnspeed#)
return