
-----------------------------------
lufthansa747
Sat Dec 05, 2009 6:15 pm

my pong game grade 9
-----------------------------------
this is my first try at pong. it is 2 players and right now i am working on an AI for the second player. pease give me some pointers on how to make it better

View.Set ("graphics:max;max,offscreenonly,nobuttonbar,nocursor")

var player1Score : int := 0
var player2Score : int := 0
var playerWinFont : int := Font.New ("sans serif:100:bold")
var continue : char
var fontWidth : int

class ball
    export start, draw
    var x : int := maxx div 2
    var y : int := maxy div 2
    var radius : int := 20
    var xint, yint : int := 3
    var upState, sideState : int
    loop
        upState := Rand.Int (-1, 1)
        exit when upState not= 0
    end loop
    loop
        sideState := Rand.Int (-1, 1)
        exit when sideState not= 0
    end loop

    procedure start
        Draw.FillBox (0, 0, maxx, maxy, black)
        Draw.FillOval (x, y, radius, radius, white)
    end start

    procedure draw (x1, x3, y1, y2, y3, y4 : int)
        if x - xint = maxx and sideState = 1 then % check right wall
            sideState := -1
            player1Score += 1
        end if
        if y + yint >= maxy and upState = 1 then %check top wall
            upState := -1
        end if
        if y - yint = x3 and sideState = 1 and y > y3 and y < y4 then % check left guy hits ball
            sideState := -1
        end if

        Draw.FillOval (x, y, radius, radius, black)
        x += xint * sideState
        y += yint * upState
        Draw.FillOval (x, y, radius, radius, white)
    end draw
end ball

class player
    export start, player2, move, x2, y, y2, x
    var sizex : int := 20
    var sizey : int := 200
    var x : int := 0
    var x2 : int := x + sizex
    var y : int := maxy div 2 - sizey div 2
    var y2 : int := maxy div 2 + sizey div 2
    var yint : int := 5

    procedure start
        Draw.FillBox (x, y, x2, y2, white)
    end start

    procedure move (userAction : char)
        if userAction = 'w' and y2 + yint = 0 then
            Draw.FillBox (x, y, x2, y2, black)
            y -= yint
            y2 -= yint
            Draw.FillBox (x, y, x2, y2, white)
        end if

        if userAction = KEY_UP_ARROW and y2 + yint = 0 then
            Draw.FillBox (x, y, x2, y2, black)
            y -= yint
            y2 -= yint
            Draw.FillBox (x, y, x2, y2, white)
        end if
    end move

    procedure player2
        x := maxx - sizex
        x2 := maxx
    end player2
end player

procedure updateScore ()
    locate (1, 1)
    put "Player 1: ", player1Score, " Player 2: ", player2Score
end updateScore

loop
    player1Score := 0
    player2Score := 0
    var userAction : array char of boolean
    var scoreToReach : int := 5

    var ball1 : pointer to ball
    new ball, ball1
    ball1 -> start

    var p : pointer to player
    new player, p
    p -> start

    var p2 : pointer to player
    new player, p2
    p2 -> player2
    p2 -> start

    for decreasing count : 5 .. 1
        var countDown : string := intstr (count)
        fontWidth := Font.Width (countDown, playerWinFont)
        Font.Draw (countDown, maxx div 2 - fontWidth div 2, maxy div 2, playerWinFont, brightblue)
        View.Update
        delay (1000)
        ball1 -> start
        p -> start
        p2 -> start
        View.Update
    end for

    loop
        Input.KeyDown (userAction)
        if userAction ('w') then
            p -> move ('w')
        end if
        if userAction ('s') then
            p -> move ('s')
        end if
        if userAction (KEY_UP_ARROW) then
            p2 -> move (KEY_UP_ARROW)
        end if
        if userAction (KEY_DOWN_ARROW) then
            p2 -> move (KEY_DOWN_ARROW)
        end if
        ball1 -> draw (p -> x2, p2 -> x, p -> y, p -> y2, p2 -> y, p2 -> y2)
        updateScore
        exit when player1Score = scoreToReach or player2Score = scoreToReach
        View.Update
    end loop

    if player1Score = scoreToReach then
        fontWidth := Font.Width ("Player 1 Wins !!!!!", playerWinFont)
        Draw.Text ("Player 1 Wins!!!!!", maxx div 2 - fontWidth div 2, maxy div 2, playerWinFont, brightred)
    else
        fontWidth := Font.Width ("Player 2 Wins !!!!!", playerWinFont)
        Draw.Text ("Player 2 Wins!!!!!", maxx div 2 - fontWidth div 2, maxy div 2, playerWinFont, brightred)
    end if
    View.Update

    continue := getchar
end loop
