
-----------------------------------
0~Luc~0
Fri Jan 20, 2006 3:00 pm

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 = pady1 and bally = pad2x1 and bally >= pad2y1 and bally = pad4x1 and ballx  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
%------------------------------

-----------------------------------
Drakain Zeil
Fri Jan 20, 2006 4:05 pm


-----------------------------------
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.

-----------------------------------
Delos
Fri Jan 20, 2006 5:17 pm


-----------------------------------
Please use [code] or [syntax] tags when posting code.  Also, if you are going to post code that long, please attach a file instead.

-----------------------------------
0~Luc~0
Fri Jan 20, 2006 6:02 pm


-----------------------------------
Ok sorry about that, I just joined today. And thanks for the tip, I'll try that.
