Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 classic pong
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cpu_hacker4.0




PostPosted: Tue Oct 09, 2007 7:38 pm   Post subject: classic pong

This is a classic style pong game that I made and I am looking for suggestions on how to improve it. It's not finished yet, so some constructive criticism would be useful Wink
code:

const RADIUS : int := 10

var chars : array char of boolean
var paddle1X : int := 5
var paddle1Y : int := maxy div 2 - 50
var paddle2X : int := maxx - 5
var paddle2Y : int := maxy div 2 - 50
var ballX : int := maxx div 2
var ballY : int := maxy div 2
var xInc : int := 1
var yInc : int := 1
var p1score : int := 0
var p2score : int := 0
var p1name : string
var p2name : string
var reply : string (1)

put "Please enter the name for player 1: " ..
get p1name

put "Please enter the name for player 2: " ..
get p2name

setscreen ("offscreenonly")

function MathDistance (x1, y1, x2, y2 : real) : real
    var dx : real := x1 - x2
    var dy : real := y1 - y2
    result sqrt (dx * dx + dy * dy)
end MathDistance


function MathDistancePointLine (px, py, x1, y1, x2, y2 : real) : real
    var lineSize : real := MathDistance (x1, y1, x2, y2)
    if lineSize = 0 then
        result MathDistance (px, py, x1, y1)
    end if

    var u : real := ((px - x1) * (x2 - x1) +
        (py - y1) * (y2 - y1)) / (lineSize * lineSize)

    if u < 0.0 then
        result MathDistance (px, py, x1, y1)
    elsif u > 1.0 then
        result MathDistance (px, py, x2, y2)
    else
        var ix : real := x1 + u * (x2 - x1)
        var iy : real := y1 + u * (y2 - y1)
        result MathDistance (px, py, ix, iy)
    end if
end MathDistancePointLine

loop
    loop
        cls
        ballX += xInc
        ballY += yInc
        drawfillbox (0, 0, maxx, maxy, yellow)
        drawline (paddle1X, paddle1Y, paddle1X, paddle1Y + 100, black)
        drawline (paddle2X, paddle2Y, paddle2X, paddle2Y + 100, black)
        drawfilloval (ballX, ballY, RADIUS, RADIUS, black)
        Input.KeyDown (chars)
        if chars ('w') then
            paddle1Y += 1
        elsif chars ('s') then
            paddle1Y -= 1
        end if
        if chars (KEY_UP_ARROW) then
            paddle2Y += 1
        elsif chars (KEY_DOWN_ARROW) then
            paddle2Y -= 1
        end if
        if paddle1Y + 100 >= maxy then
            paddle1Y -= 1
        elsif paddle1Y <= 0 then
            paddle1Y += 1
        end if
        if paddle2Y + 100 >= maxy then
            paddle2Y -= 1
        elsif paddle2Y <= 0 then
            paddle2Y += 1
        end if
        if ballY + RADIUS >= maxy then
            yInc *= -1
        elsif ballY - RADIUS <= 0 then
            yInc *= -1
        end if
        if MathDistancePointLine (ballX, ballY, paddle1X, paddle1Y, paddle1X, paddle1Y + 100) <= RADIUS or MathDistancePointLine (ballX, ballY, paddle2X, paddle2Y, paddle2X, paddle2Y + 100) <= RADIUS
                then
            xInc *= -1
        end if
        if ballX - RADIUS <= 0 then
            p2score := p2score + 1
            cls
            ballX := maxx div 2
            ballY := maxy div 2
            drawfillbox (0, 0, maxx, maxy, yellow)
            drawline (paddle1X, paddle1Y, paddle1X, paddle1Y + 100, black)
            drawline (paddle2X, paddle2Y, paddle2X, paddle2Y + 100, black)
            drawfilloval (ballX, ballY, RADIUS, RADIUS, black)
            put p2name, " scored!"
            View.Update
            delay (1000)
        elsif ballX + RADIUS >= maxx then
            p1score := p1score + 1
            cls
            ballX := maxx div 2
            ballY := maxy div 2
            drawfillbox (0, 0, maxx, maxy, yellow)
            drawline (paddle1X, paddle1Y, paddle1X, paddle1Y + 100, black)
            drawline (paddle2X, paddle2Y, paddle2X, paddle2Y + 100, black)
            drawfilloval (ballX, ballY, RADIUS, RADIUS, black)
            put p1name, " scored!"
            View.Update
            delay (1000)
        end if
        if p1score = 5 then
            put p1name, " wins!"
            cls
            exit
        elsif p2score = 5 then
            put p2name, " wins!"
            exit
        end if
        View.Update
    end loop
    cls
    put "Would you like to play again? (y/n)"
    View.Update
    getch (reply)
    if reply = 'n' then
        exit
    end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
jolly50




PostPosted: Wed Oct 24, 2007 12:59 pm   Post subject: RE:classic pong

Hey,

Its a great game....it amused me for hours, but one suggestion is to finish the code so that you can press "y" and play it again. Other then that...great game
Degensquared




PostPosted: Fri Oct 26, 2007 6:39 am   Post subject: Re: classic pong

You may want to throw in a Time.DelaySinceLast in there. On my home computer I couldn't even see the ball move. Razz
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: