
-----------------------------------
zylum
Mon Feb 16, 2004 12:16 am

basic pong game
-----------------------------------
here is a basic pong game i whipped up, it's fairly straight forward and easy to understand...


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...

-----------------------------------
recneps
Mon Feb 16, 2004 4:15 pm


-----------------------------------
Wrong forum :\ , but its good, better than my first version :(

-----------------------------------
jonos
Mon Feb 16, 2004 6:23 pm


-----------------------------------
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
Mon Feb 16, 2004 6:33 pm


-----------------------------------
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
Mon Feb 16, 2004 6:41 pm


-----------------------------------
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
Mon Feb 16, 2004 8:38 pm


-----------------------------------
haha netequette :P

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 :)  Though not complicated Physics :P
zylum post your pong game!  doesn't matter if its hard to understand I wanna see it :P

-----------------------------------
shorthair
Mon Feb 16, 2004 9:23 pm


-----------------------------------
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
Mon Feb 16, 2004 9:52 pm


-----------------------------------
nicely done... keep it up

-----------------------------------
the_short1
Sun Feb 22, 2004 8:22 pm


-----------------------------------
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
Sun Feb 22, 2004 8:32 pm


-----------------------------------
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 :(

-----------------------------------
the_short1
Sun Feb 22, 2004 8:56 pm


-----------------------------------
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
Sun Feb 22, 2004 8:59 pm


-----------------------------------
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
Sat Mar 06, 2004 8:16 pm


-----------------------------------
Good game


A bit simple, but still cool! :D

-----------------------------------
recneps
Sat Mar 06, 2004 8:52 pm


-----------------------------------
Since this has resurfaced... :) I guess no one who replied here looked at my Spencer's Games :cry: since my paddles stayed in game area, and had the same idea except for speed increase :)

-----------------------------------
the_short1
Sat Mar 06, 2004 9:28 pm


-----------------------------------
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!!!

-----------------------------------
Jodo Yodo
Sat Mar 06, 2004 10:13 pm


-----------------------------------
Interesting program.
