Computer Science Canada Improving Pong AI |
Author: | 0~Luc~0 [ Fri Jan 20, 2006 3:00 pm ] |
Post subject: | Improving Pong AI |
I've made a 4 player pong for fun. It has a working AI, but the ball always ends up going diagonally, and the AI is infalliable. I'd like for it to be more random ball movement and an AI that is possible to defeat, thank you in advance. %4 Player Pong % based on "4D Pong" www.yomgaille.com/pong/ setscreen ("graphics:400,400") %------------------------------ %paddle variables const padx1 := 5 const padx2 := 15 const pad2x1 := 385 const pad2x2 := 395 var pady1 : int := 280 var pady2 : int := 320 var pad2y1 : int := 280 var pad2y2 : int := 320 %------------------------------ %keyboard variable var chars : array char of boolean %------------------------------ %ball variables var ballx : int := maxx div 2 var bally : int := maxy div 2 var ballxs : int var ballys : int var hitcount := 0 ballxs := 1 ballys := 1 %------------------------------ %paddle variables const pad3y1 := 5 const pad3y2 := 15 const pad4y1 := 385 const pad4y2 := 395 var pad3x1 : int := 180 var pad3x2 : int := 220 var pad4x1 : int := 180 var pad4x2 : int := 220 %------------------------------ %input variables var players:int:=0 var delaynum:int:=5 var p1,p2,p3,p4:int:=0 var font:=Font.New ("Times New Roman:12:bold") %------------------------------ %AI procedure P1 %Player 1 AI if pady1 < bally and pady2 < maxy then pady1 += 2 pady2 += 2 elsif pady2 > bally and pady1 > 0 then pady1 -= 2 pady2 -= 2 end if end P1 procedure P2 %Player 2 AI if pad2y1 < bally and pad2y2 < maxy then pad2y1 += 2 pad2y2 += 2 elsif pad2y2 > bally and pad2y1 > 0 then pad2y1 -= 2 pad2y2 -= 2 end if end P2 procedure P3 %Player 3 AI if pad3x1 < ballx and pad3x2 < maxy then pad3x1 += 2 pad3x2 += 2 elsif pad3x2 > ballx and pad3x1 > 0 then pad3x1 -= 2 pad3x2 -= 2 end if end P3 procedure P4 %Player 4 AI if pad4x1 < ballx and pad4x2 < maxy then pad4x1 += 2 pad4x2 += 2 elsif pad4x2 > ballx and pad4x1 > 0 then pad4x1 -= 2 pad4x2 -= 2 end if end P4 %END AI %------------------------------ %Set the Screen View.Set ("offscreenonly") put "4 Player Pong" put "How many players??? (0-4)" View.Update get players put "How fast would you like the game to be? (5 recommended)(lower=faster)" View.Update get delaynum put "Player 1 (left paddle)=up/down arrows, Player 2 (right paddle)=Q,A keys, Player 3 (bottom paddle)=F5,F6 keys, Player 4(top paddle)= Insert,Home keys" View.Update Input.Pause %------------------------------ %Main Loop loop %------------------------------ %Activates the AI if players=0 then P1 P2 P3 P4 elsif players=1 then P2 P3 P4 elsif players=2 then P3 P4 elsif players=3 then P4 end if %------------------------------ %Draws the ball and paddles Input.KeyDown (chars) drawfillbox (0, 0, maxx, maxy, black) drawfillbox (padx1, pady1, padx2, pady2, white) drawfillbox (pad2x1, pad2y1, pad2x2, pad2y2, white) drawfillbox (pad3x1, pad3y1, pad3x2, pad3y2, white) drawfillbox (pad4x1, pad4y1, pad4x2, pad4y2, white) drawoval (ballx, bally, 10, 10, white) Font.Draw (intstr (p1),50,200,font,white) Font.Draw (intstr (p2),350,200,font,white) Font.Draw (intstr (p3),200,50,font,white) Font.Draw (intstr (p4),200,350,font,white) View.Update delay (delaynum) %------------------------------ %moves the ball ballx += ballxs bally += ballys %------------------------------ %Bounce off paddles~`~`~`~Collision sequences %player 1 if ballx - 10 <= padx2 and bally >= pady1 and bally <= pady2 then ballxs *= -1 hitcount += 1 end if %player 2 if ballx+10 >= pad2x1 and bally >= pad2y1 and bally <= pad2y2 then ballxs *= -1 hitcount += 1 end if %player 3 if bally-10 <= pad3y2 and ballx >= pad3x1 and ballx <=pad3x2 then ballys *= -1 hitcount += 1 end if %player 4 if bally+10 >= pad4y1 and ballx >= pad4x1 and ballx <= pad4x2 then ballys *= -1 hitcount += 1 end if %------------------------------ %ball speed variable if hitcount = 15 then ballxs := 2 ballys := 2 end if %------------------------------ %scoring algorithm if ballx - 10 <= 0 then delay (100) bally := Rand.Int (100, 300) ballx := Rand.Int (100, 300) ballxs := 1 ballys := 1 p1+=1 View.Update elsif ballx + 10 >= maxx then delay (100) bally := Rand.Int (100, 300) ballx := Rand.Int (100, 300) ballxs := 1 ballys := 1 p2+=1 View.Update elsif bally - 10 <= 0 then delay (100) bally := Rand.Int (100, 300) ballx := Rand.Int (100, 300) ballxs := 1 ballys := 1 p3+=1 elsif bally + 10 >= maxy then delay (100) bally := Rand.Int (100, 300) ballx := Rand.Int (100, 300) ballxs := 1 ballys := 1 p4+=1 end if %------------------------------ %determines the loser if p1=20 then cls put "Player 1 loses" exit elsif p2=20 then cls put "Player 2 loses" exit elsif p3=20 then cls put "Player 3 loses" exit elsif p4=20 then cls put "Player 4 loses" exit end if %------------------------------ %movement of the paddles %player 1 if chars (KEY_UP_ARROW) and pady2 < 400 then pady1 := pady1 + 4 pady2 := pady2 + 4 elsif chars (KEY_DOWN_ARROW) and pady1 > 0 then pady1 := pady1 - 4 pady2 := pady2 - 4 end if %------------------------------ %player 2 if chars ('q') and pad2y2 < 400 then pad2y1 := pad2y1 + 4 pad2y2 := pad2y2 + 4 elsif chars ('a') and pad2y1 > 0 then pad2y1 := pad2y1 - 4 pad2y2 := pad2y2 - 4 end if %------------------------------ %player 3 if chars (KEY_F6) and pad3x2 < maxy then pad3x1 += 4 pad3x2 += 4 elsif chars (KEY_F5) and pad3x1 > 0 then pad3x1 -= 4 pad3x2 -= 4 end if %-------------------------------- %player 4 if chars (KEY_HOME) and pad4x2 < maxx then pad4x1 += 4 pad4x2 += 4 elsif chars (KEY_INSERT) and pad4x1 > 0 then pad4x1 -= 4 pad4x2 -= 4 end if View.Update end loop %------------------------------ |
Author: | Drakain Zeil [ Fri Jan 20, 2006 4:05 pm ] |
Post subject: | |
Instead of setting a target for the pad to go to where the ball is... 1) To make it worse: Add a random value between a negative and positive offset 2) To make it better: Do the math to figure out where it needs to be when the ball will get there. |
Author: | Delos [ Fri Jan 20, 2006 5:17 pm ] |
Post subject: | |
Please use [code] or [syntax] tags when posting code. Also, if you are going to post code that long, please attach a file instead. |
Author: | 0~Luc~0 [ Fri Jan 20, 2006 6:02 pm ] |
Post subject: | |
Ok sorry about that, I just joined today. And thanks for the tip, I'll try that. |