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

Username:   Password: 
 RegisterRegister   
 basic pong game
Index -> Programming, Turing -> Turing Submissions
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
zylum




PostPosted: Mon Feb 16, 2004 12:16 am   Post subject: basic pong game

here is a basic pong game i whipped up, it's fairly straight forward and easy to understand...

code:

setscreen ("offscreenonly")

%declare constants
const paddleH := 80
const paddleW := 10
const x := 1
const y := 2
const paddleClr := 7
const ballClr := 7
const ballR := 10
const paddleSpd := 4
const ballInitSpd := 2
const ballAccel := 1.01

%declare variables
var paddle : array 1 .. 2, 1 .. 2 of int
paddle (1, x) := 50
paddle (1, y) := maxy div 2
paddle (2, x) := maxx - 50
paddle (2, y) := maxy div 2
var paddleDir : array 1 .. 2 of int := init (0, 0)

var ballSpd : array 1 .. 2 of real := init (ballInitSpd, ballInitSpd)
var ballCoords : array 1 .. 2 of real
ballCoords (x) := maxx div 2
ballCoords (y) := maxy div 2

var score : array 1 .. 2 of int := init (0, 0)

var keys : array char of boolean

%moves the paddle as long as it will end up within the screen
procedure movePaddle (pNum, direction : int)
    if paddle (pNum, y) + direction > paddleH div 2 and paddle (pNum, y) + direction < maxy - paddleH div 2 then
        paddle (pNum, y) += direction
    end if
    drawfillbox (paddle (pNum, x) - paddleW div 2, paddle (pNum, y) - paddleH div 2, paddle (pNum, x) + paddleW div 2, paddle (pNum, y) + paddleH div 2, paddleClr)
end movePaddle

%checks whether the ball hits one of the paddles
%if it does, the balls direction is reversed
procedure ballHitsPaddle
    if ballCoords (y) > paddle (1, y) - paddleH div 2 and ballCoords (y) < paddle (1, y) + paddleH div 2 then
        if ballCoords (x) > (paddle (1, x) - paddleW div 2) + ballR and ballCoords (x) < (paddle (1, x) + paddleW div 2) + ballR then
            ballSpd (x) *= -1
            ballCoords (x) := paddle (1, x) + ballR + paddleW
        end if
    end if
    if ballCoords (y) > paddle (2, y) - paddleH div 2 and ballCoords (y) < paddle (2, y) + paddleH div 2 then
        if ballCoords (x) > (paddle (2, x) - paddleW div 2) - ballR and ballCoords (x) < (paddle (2, x) + paddleW div 2) - ballR then
            ballSpd (x) *= -1
            ballCoords (x) := paddle (2, x) - ballR - paddleW
        end if
    end if
end ballHitsPaddle

%moves the ball
%if it hits the top/bottom wall, the speed is increased
%if the ball hits the left/right wall, the balls x,y and direction variables are reset and the
%appropriate score is incremented
procedure moveBall
    ballCoords (x) += ballSpd (x)
    ballCoords (y) += ballSpd (y)
    if ballCoords (x) < ballR then
        ballCoords (x) := maxx div 2
        score (2) += 1
        ballSpd (1) := ballInitSpd
        ballSpd (2) := ballInitSpd
    elsif ballCoords (x) > maxx - ballR then
        ballCoords (x) := maxx div 2
        score (1) += 1
        ballSpd (1) := ballInitSpd
        ballSpd (2) := ballInitSpd
    end if
    if ballCoords (y) < ballR or ballCoords (y) > maxy - ballR then
        ballSpd (y) *= -ballAccel
        ballSpd (x) *= ballAccel
    end if
    drawfilloval (round (ballCoords (x)), round (ballCoords (y)), ballR, ballR, ballClr)
end moveBall

%checks which keys are pressed
%changes the appropriate paddle direction if a key is pressed
procedure getKeys
    Input.KeyDown (keys)
    if keys (KEY_UP_ARROW) then
        paddleDir (2) := paddleSpd
    elsif keys (KEY_DOWN_ARROW) then
        paddleDir (2) := -paddleSpd
    else
        paddleDir (2) := 0
    end if
    if keys (KEY_SHIFT) then
        paddleDir (1) := paddleSpd
    elsif keys (KEY_CTRL) then
        paddleDir (1) := -paddleSpd
    else
        paddleDir (1) := 0
    end if
end getKeys

%main loop which displays the score and calls all the procedures
loop
    locate (1, 5)
    put "Player 1: ", score (1)
    locate (1, 47)
    put "Player 2 : ", score (2)
    drawline (maxx div 2, maxy, maxx div 2, 0, 7)
    getKeys
    ballHitsPaddle
    movePaddle (1, paddleDir (1))
    movePaddle (2, paddleDir (2))
    moveBall
    View.Update
    delay (10)
    cls
end loop


if you have any questions about it let me know...
Sponsor
Sponsor
Sponsor
sponsor
recneps




PostPosted: Mon Feb 16, 2004 4:15 pm   Post subject: (No subject)

Wrong forum :\ , but its good, better than my first version Sad
jonos




PostPosted: Mon Feb 16, 2004 6:23 pm   Post subject: (No subject)

nice. really smooth and the paddles don't leave the game area - first ive seen of that. but does the ball actually bounce off at different angles and use real physics?
zylum




PostPosted: Mon Feb 16, 2004 6:33 pm   Post subject: (No subject)

nah this is just a very basic version of pong... i made one with real physics in flash but i thought it would be too hard for a begginer to understand...
newbie_programmer




PostPosted: Mon Feb 16, 2004 6:41 pm   Post subject: (No subject)

thats even hard code for me to understand...lol

Mod Edit: watch the spam, it looks like you might be a newbie in netiquette too, not just programming
Cervantes




PostPosted: Mon Feb 16, 2004 8:38 pm   Post subject: (No subject)

haha netequette Razz

Jonos the collision in that is simply multiplying the change in x and y by -1. In other words, making it bounce straight back. I guess that's real physics Smile Though not complicated Physics Razz
zylum post your pong game! doesn't matter if its hard to understand I wanna see it Razz
shorthair




PostPosted: Mon Feb 16, 2004 9:23 pm   Post subject: (No subject)

Wow , another wowzer program by zylum , he is just heating up the turing scene , keep em coming and il keep usin em , this pong is well done , i quite like your use of physics , looks good but its really not real, i didnt notice till i looked athte code
we64




PostPosted: Mon Feb 16, 2004 9:52 pm   Post subject: (No subject)

nicely done... keep it up
Sponsor
Sponsor
Sponsor
sponsor
the_short1




PostPosted: Sun Feb 22, 2004 8:22 pm   Post subject: (No subject)

i still dont get how you have all this time to be making programs...

unless you are able to make them in like 1H.... god..... Zylum ur one crazy Turing Programmer... u step on the scene and just keep dealing cards... GJ
PS: Are u going to by Software Engineer for career??? cuz u got some skillz

EDIT: Wooot this post puts me higher then 1k bits!!!! WOW
zylum




PostPosted: Sun Feb 22, 2004 8:32 pm   Post subject: (No subject)

lol, they take me less than an hour... it's not like im making anything really spectacular... the ones that took the longest so far are my isometric engine which took about 12 hours total work because of its complexity and then there is my 3d engine which took about 3 hours...

btw cervantes, the source for the game has been up the whole time lol (first post) Wink and it's supposed to be straight forward not complicates... maybe i failed at making it simple Sad
the_short1




PostPosted: Sun Feb 22, 2004 8:56 pm   Post subject: (No subject)

yea... wow... for me it takes a while... then again tho.... Pong is ezier then pac man...but yea... i just realized i had made a couple small but significant errors in pac man dots... and i built program around it,.... cause: like another 2 hours of work.... maybe less.. but stilll... yea pong v4 will have to wait...
2500 lines of CODE is a LOT!



maybe you could make this pong more colored... adda a AI... then post under user submit...??
PS:ur code is very straight forward.. well as much as it can be...

BTW: did you think of becoming a Software Engineer...???
Andy




PostPosted: Sun Feb 22, 2004 8:59 pm   Post subject: (No subject)

i'm guessing the mod wuz azn_s cuz hes the only one in this world who uses purple cuz it stands for royalty
LJ




PostPosted: Sat Mar 06, 2004 8:16 pm   Post subject: (No subject)

Good game


A bit simple, but still cool! Very Happy
recneps




PostPosted: Sat Mar 06, 2004 8:52 pm   Post subject: (No subject)

Since this has resurfaced... Smile I guess no one who replied here looked at my Spencer's Games Crying or Very sad since my paddles stayed in game area, and had the same idea except for speed increase Smile
the_short1




PostPosted: Sat Mar 06, 2004 9:28 pm   Post subject: (No subject)

i think anyone that finished the gr.10 compsci corse above 60 percent average can make a pong game... almost everyone has.... but it takes a special individual to make a wicked AI and to make it look good.... and recepneps... you closing in on me... noooo!!!
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 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: