Simple Pong with AI by dlefik 20083rd May 2005 3:55
|
---|
Summary Simple Player Vs. Computer Pong Description This is actually my first game attempt in DarkBasic Pro, but it's a great learning experience for me, despite the fact the game is way outdated and done way too many times. The controls are simple, mouse controls the players x and y movement, "P" pauses the game, and "R" resumes the game during pause stage. The computer player will also move around on X and Y axis to get the ball. This is my first ever sttempt at a simple AI so it may look silly and not act the smartest, but it makes the game playable. Hope this can be a good starter for someone new such as myself, or just something to look at. No media required, just copy and paste into DarkBasic. This was programmed on Pro, but tested in DBC 1.13. Game is still functional in DBC but pretty slow and laggy. Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com remstart DarkBasic Pro Pong Game Created by Clickable SMiley 2005 Controls are as follows.... P = Pause R = Resume Mouse Controls Player 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 ink rgb(0,255,0),1 box midleft, midtop, midright, midbottom get image midline, midleft, midtop, midright, midbottom ink rgb(255,255,255),1 `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. `if you find the desire to do so, unremark these lines for different stats. `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 `this will display the new updated player scores 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 `check to see if player has pushed the P button to pause the game. If so jump to pause function. if inkey$()="p" then gamepause() 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#=<495 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 `________________________________________________________________________________________ function gamepause() `this idea was found on the DB forums, great job to DavidT for this one! do `just to keep the scores showing ink rgb(255,255,255),1 center text 300,180,"Player 1 : " + str$(score(1)) center text 500,180,"Player 2 : " + str$(score(2)) `color text red and paste centered on screen for indication game is paused. ink rgb(255,0,0),1 : center text 400,300,"P A U S E D" `the resume key if inkey$()="r" ink rgb(255,255,255),1 exit endif `this will disable the mouse during pause so the player will not jump when game is resumed. playery#=playery#+mousemovey()*-1 : playerx#=playerx#+mousemovex()*-1 sync loop `return the white ink to the numbers for when game is resumed. ink rgb(255,255,255),1 endfunction |