Computer Science Canada

Matrix Pong

Author:  Dan [ Thu Dec 06, 2007 10:53 pm ]
Post subject:  Matrix Pong

I got borad so i decied to make a litte pong game as it seems to be so popualr to do in turing thess days.

I partly made this to be an easy template to edit for diffrent kinds of poing games, the idea is that i break pong down in to the basic produceurs you need (or basic conspects even). It is posible to break it down to more, like spearting the score system in to it's own procuedre.

Turing:

%matrix pong
%Dan's pong template and xkcd joke

View.Set ("graphics, offscreenonly")
colorback (black)
cls

var width : int := 10
var height : int := 65

var dim : int := 10

var scoreLeft : int := 0
var scoreRight : int := 0
var font : int := Font.New ("mono:50:bold")

var vectorX : int := 1
var vectorY : int := 1

var paddelOffX : int := 20
var paddelY : int := maxy div 2
var AIPaddelY : int := maxy div 2

var ballX : int := maxx div 2
var ballY : int := maxy div 2

var delayFor : int := 1

var j : int := 0
var font2 : int := Font.New ("mono:10:bold")


process beep
    Music.Play ("C");
end beep

process scored
    %Put music here to play when scored
end scored

procedure back
    Draw.ThickLine (0, 0, maxx, 0, 10, green)
    Draw.ThickLine (0, 0, 0, maxy, 10, green)
    Draw.ThickLine (0, maxy, maxx, maxy, 10, green)
    Draw.ThickLine (maxx, 0, maxx, maxy, 10, green)
    Draw.DashedLine (maxx div 2, 0, maxx div 2, maxy, drawDash, green)
    Draw.DashedLine (maxx div 2 + 1, 0, maxx div 2 + 1, maxy, drawDash, green)
    Draw.DashedLine (maxx div 2 - 1, 0, maxx div 2 - 1, maxy, drawDash, green)
    Draw.DashedLine (maxx div 2 + 2, 0, maxx div 2 + 2, maxy, drawDash, green)
    Draw.DashedLine (maxx div 2 - 2, 0, maxx div 2 - 2, maxy, drawDash, green)
end back

procedure paddel (x : int, y : int)
    Draw.FillBox (x - (width div 2), y - (height div 2), x + (width div 2), y + (height div 2), green)
end paddel

procedure ball (x : int, y : int)
    Draw.FillBox (x - (dim div 2), y - (dim div 2), x + (dim div 2), y + (dim div 2), green)
end ball

procedure score
    Font.Draw (intstr (scoreLeft), maxx div 2 - 60, maxy - 60, font, green)
    Font.Draw (intstr (scoreRight), maxx div 2 + 10, maxy - 60, font, green)
end score

procedure drawPaddel
    paddel (paddelOffX, paddelY)
    paddel (maxx - paddelOffX, AIPaddelY)
end drawPaddel

procedure drawBall
    ball (ballX, ballY)
end drawBall

procedure cheapAI
    if j < 4 then

        AIPaddelY := ballY

        if (AIPaddelY + (height div 2)) > maxy then
            AIPaddelY := maxy - (height div 2)
        elsif (AIPaddelY - (height div 2)) < 0 then
            AIPaddelY := height div 2
        end if
    end if
end cheapAI

procedure checkHitBorder
    if ballX > (maxx - (dim div 2)) or ballX < (dim div 2) then
        vectorX := vectorX * -1
        vectorY := vectorY * -1

        if ballX < (dim div 2) then
            scoreRight := scoreRight + 1
        else
            scoreLeft := scoreLeft + 1
        end if

        fork scored
       
        ballX := maxx div 2
        ballY := maxy div 2
    elsif ballY > (maxy - (dim div 2)) or ballY < (dim div 2) then
        vectorY := vectorY * -1
    end if
end checkHitBorder

procedure checkHitPaddel
    if (ballX - (dim div 2)) < (paddelOffX + (width div 2)) and (ballX + (dim div 2)) > (paddelOffX - (width div 2)) and (ballY + (dim div 2)) > (paddelY - (height div 2)) and (ballY - (dim div 2))
            < (paddelY + (height div 2)) then
        vectorX := vectorX * -1
        vectorY := vectorY * -1
        fork beep
    elsif (ballX - (dim div 2)) < (maxx - paddelOffX + (width div 2)) and (ballX + (dim div 2)) > (maxx - paddelOffX - (width div 2)) and (ballY + (dim div 2)) > (AIPaddelY - (height div 2))
            and (ballY - (dim div 2)) < (AIPaddelY + (height div 2)) then
        vectorX := vectorX * -1
        vectorY := vectorY * -1
        fork beep
    end if
end checkHitPaddel

procedure moveBall
    ballX += vectorX
    ballY += vectorY
end moveBall

procedure input
    var chars : array char of boolean
    Input.KeyDown (chars)

    if chars (KEY_UP_ARROW) then
        paddelY := paddelY + 1
    end if

    if chars (KEY_DOWN_ARROW) then
        paddelY := paddelY - 1
    end if

    if (paddelY + (height div 2)) > maxy then
        paddelY := maxy - (height div 2)
    elsif (paddelY - (height div 2)) < 0 then
        paddelY := height div 2
    end if
end input

procedure joke
    if vectorX > 0 and ballX >= (maxx div 2) + 50 then
        vectorX := 0
        vectorY := 1
    end if
end joke

procedure drawJoke
    if j >= 1 then
        Font.Draw ("Wait a minute! None of this is real!", maxx div 2 + 10, maxy - 80, font2, green)
    end if

    if j >= 2 then
        Font.Draw ("I can see through the world!", maxx div 2 + 10, maxy - 100, font2, green)
    end if

    if j >= 3 then
        Font.Draw ("I can see the code!", maxx div 2 + 10, maxy - 120, font2, green)
    end if

    if j >= 4 then
        Font.Draw ("I AM THE ONE!", maxx div 2 + 10, maxy - 140, font2, green)

        if vectorX not= 0 then
            joke
        end if
    end if
end drawJoke

loop
    cls

    if Rand.Int (0, 2000) = 42 then
        j := j + 1
    end if

    input
    moveBall
    checkHitBorder
    checkHitPaddel
    cheapAI

    back
    drawBall
    drawPaddel
    score
    drawJoke
    View.Update

    delay (delayFor)
end loop


The things we do to advoied doing homework

Spoiler:

This is ment to be a joke more then anything, do not take it as a real pong game or an acurite mesure of my work.

Pong Joke: http://xkcd.com/117

Author:  Nick [ Fri Dec 07, 2007 8:26 am ]
Post subject:  Re: Matrix Pong

lol Dan you beat me to it

Dan @ Thu Dec 06, 2007 10:53 pm wrote:
procedure cheapAI


you can say that again you cant win!!!

Author:  Dan [ Fri Dec 07, 2007 12:44 pm ]
Post subject:  RE:Matrix Pong

The paddel is the one. How can you beat neo at pong?

Author:  Zampano [ Fri Dec 07, 2007 1:06 pm ]
Post subject:  Re: Matrix Pong

Lol, I guess the paddle found the true meaning of life at 42!

Author:  Dan [ Fri Dec 07, 2007 1:28 pm ]
Post subject:  RE:Matrix Pong

Acuacatly, "Following this, the pong paddle went on a mission to destroy Atari headquarters and, due to a mixup, found himself inside the game The Matrix Reloaded. Boy, was THAT ever hard to explain to him."

Author:  Nick [ Fri Dec 07, 2007 3:18 pm ]
Post subject:  RE:Matrix Pong

you should add in that to the game (you play as the paddle in a first person shooter much like the matrix)

Author:  Dan [ Fri Dec 07, 2007 7:34 pm ]
Post subject:  RE:Matrix Pong

If i ever had alot of free time i might aucatly do somthing in opengl about it. However i never have free time Sad

Author:  MihaiG [ Fri Dec 07, 2007 8:10 pm ]
Post subject:  Re: Matrix Pong

theres a sweet spot for the paddle, if u leave it there, and you can walk away from the computer and not loose Very Happy

Author:  petree08 [ Wed Dec 12, 2007 1:24 pm ]
Post subject:  RE:Matrix Pong

I think compsci should have a thread dedicated just to old game remakes because we have so many "pong , pacman , space invader ect" posts (not that this is a bad thing) it could leave the main submission thread for more "new" stuff

Author:  Dan [ Wed Dec 12, 2007 1:28 pm ]
Post subject:  Re: RE:Matrix Pong

petree08 @ 12th December 2007, 1:24 pm wrote:
i like your code , i might have but my procedures above the vars for modularity sake


You put your golbal variables below your procedures? I don't see that much, persoanly i perfure the other way around.

Author:  LaZ3R [ Wed Dec 12, 2007 10:54 pm ]
Post subject:  RE:Matrix Pong

I agree with Dan... I've always kept my global variables at the top of the program :p

I don't see why you guys write out the full "procedure' instead of just putting "proc" Very Happy

Saves a few characters on each proc Very Happy!

Author:  Tony [ Thu Dec 13, 2007 1:24 am ]
Post subject:  Re: RE:Matrix Pong

LaZ3R @ Wed Dec 12, 2007 10:54 pm wrote:
Saves a few characters on each proc Very Happy!

Why are you using Turing instead of Perl? Laughing

Author:  petree08 [ Mon Dec 17, 2007 8:47 pm ]
Post subject:  RE:Matrix Pong

well i my self put globals above procedures when i'm using globals. but i noticed that in your procedures there are no parameters to them. this almost deafets the purpose of wirting a procedure to begin with. i see that it makes the code easier to read and debugging is easy. but it also makes a procedure "limited" to the program it was written for.
Code is much more recyleable(sorry for the spelling) with parameters .

there are huge arguments between globals and parameters and both sides have very good points but on big projects i tend not to use globals. but on random compsci games that i post i tend to use the above style. (if i use procedures at all)

any who thought i'd just put my points in
- happy coding

Author:  Dan [ Mon Dec 17, 2007 10:13 pm ]
Post subject:  Re: RE:Matrix Pong

petree08 @ 17th December 2007, 8:47 pm wrote:
well i my self put globals above procedures when i'm using globals. but i noticed that in your procedures there are no parameters to them. this almost deafets the purpose of wirting a procedure to begin with. i see that it makes the code easier to read and debugging is easy. but it also makes a procedure "limited" to the program it was written for.
Code is much more recyleable(sorry for the spelling) with parameters .


You cleary missed the hole point of program on all levels.....

petree08 @ 17th December 2007, 8:47 pm wrote:

there are huge arguments between globals and parameters and both sides have very good points but on big projects i tend not to use globals. but on random compsci games that i post i tend to use the above style. (if i use procedures at all)


Ummmm, no. There is almost no debate about when to use globals or not and it is almost allways langue spefic to there aproite use. (tho i guses there are arugments about should you fallow what the langue dictakes or not)



If had read the text i posted with the game you whould know that:

1. It is a template for pong games witch means i intentaly whonted to break pong down it in to it's most basic compoents and divied them in to the most simple procedure posible. Fallowing this logic, golbals are a good idea as it cleary states the starting point of every values and expains there intention cleary. If i had made everything a paramter to procedure the complexity of the code whould be much higher and it whould be harder for peoleop to edit this a template to make there own pong game. Shure i could have made a new set of golabal varibles for the starting value of each paramter but then we whould be back where we started.

2. This is ment to be a joke, if you run the pong game you whould see why.

3. If i whonted i could have made everything a paramter and had no variables at all just to make it like funcaitional programing in turing, but there was no reason to do that for the above 2 points. At the time i was working on a OpenGL porject in pure C and this was done as a break based on a XKCD comic.


Now about golbals in turing in general. Due to the way turing is set up, when you are not using clases or moduels you almost have to use a globals some where in your code unless you are going to wrap things in a main function witch aucatl goses agisted the hole idea of symplisticy that turing is based on (and is totaly unessary). As for procedures being reused in other programs, that is not realy the point of procedures as much as it is of clases or modules. It is very rare you can just copy over a procedure with paramters or not and have it useable in another program unless that program is exteramly simualr to the one it came from. For example this pong program weather there was paramters or not the procedures could not be used in another program unless it was another pong game. Now in langues that do force a main method or class of some kind on the programmer (witch is both good and bad) the improetnce of globals is increased as there aucatly is a diffrence btween the scope of a vaibale in the main logic (the main method) and at the top of the code. However in turing there is no diffrence in scope if i declair the vaible at the top or just above the main loop (deacaring it in the loop whould be a bad idea as it whould be redecleared each interation of the loop). Now if i had gone the class, modeual way for pong then i whould not have used globals but i probly whould have had varaibles with class scope, class scope varibles witch are basicly golbals just for a class are comuany usesd rather then having the programer pass the paramters as it whould not make sence as the class whould all ready have that value and you whould be passing somthing like myClass.myMethod(myClass.myVar) rather then just myClass.myMethod(). (Note that myClass.myVar whould likey be the only resonable input to that method any how). So in my option when it comes to turing and you are not using other clases or moudles then you might as well treat it like the hole program is one class and the contrsutcer and driver of the class is the part not in a method. Also if we are to go by the offical turing documeation and expamles provied by holt soft this methodogly/way of thinkg is also comonaly used.

So in summary, you sould not try to force one langue to be another when you program in it (like trying to make turing like C by wraping things in main methods or make everything a class like java) but stay with the style, gudie lines and ideolgoy of that langue. Shure you could probly make a working program using idegolgoys of other langues but you will be fighting the langue you are using the hole way threw and in the end you probly will end up with exteramly hard to understand code that is not that efffshent becues the langue was not optmized to that idelogigy to beging with.

P.S. This is why i don't post code >_>

Author:  Dan [ Tue Dec 18, 2007 12:14 am ]
Post subject:  Re: Matrix Pong

Just for petree08 and ultrahex here is needless complexed recureisve pong:

Turing:

View.Set ("graphics, offscreenonly")

type point :
    record
        x : int
        y : int
    end record

type box :
    record
        h : int
        w : int
        p : point
    end record

function makePoint (x : int, y : int) : point
    var p : point
    p.x := x
    p.y := y
    result p
end makePoint

function makeBox (h : int, w : int, p : point) : box
    var b : box
    b.h := h
    b.w := w
    b.p := p
    result b
end makeBox

function move(v : point, p : point) : point
    var np : point
    np.x := p.x + v.x
    np.y := p.y + v.y
    result np
end move

function cheapAI(b : box, p : point) : box
    var nb : box
    nb := b
    nb.p.y := p.y
    result nb
end cheapAI

function checkHitBorder(p : point, v : point) : point
    var nv : point
    nv.x := v.x
    nv.y := v.y
    if p.x > maxx - 5 or p.x < 5 then
        nv.x := v.x * -1
        nv.y := v.y * -1
    elsif p.y > maxy - 5 or p.y < 5 then
        nv.y := v.y * -1
        nv.x := v.x
    end if
    result nv
end checkHitBorder

function checkHitPaddel(p : point, v : point, b : box) : point
    var nv : point
    nv.x := v.x
    nv.y := v.y
    if p.x - 5 < b.p.x + b.w div 2 and p.x + 5 > b.p.x - b.w div 2 and p.y + 5 > b.p.y - b.h div 2 and p.y - 5 < b.p.y + b.h div 2 then
        nv.x := v.x * -1
        nv.y := v.y * -1
    end if
    result nv
end checkHitPaddel

procedure update(p : point, b1 : box, b2 : box, v : point)
    cls
    Draw.FillBox (b1.p.x - (b1.w div 2), b1.p.y - (b1.h div 2), b1.p.x + (b1.w div 2), b1.p.y + (b1.h div 2), green)
    Draw.FillBox (b2.p.x - (b2.w div 2), b2.p.y - (b2.h div 2), b2.p.x + (b2.w div 2), b2.p.y + (b2.h div 2), green)
    Draw.FillBox (p.x - 5, p.y - 5, p.x + 5, p.y + 5, green)
    View.Update
    update(move(checkHitBorder(p, checkHitPaddel(p, checkHitPaddel(p, v, b2), b1)), p), cheapAI(b1, p), cheapAI(b2, p), checkHitBorder(p, checkHitPaddel(p, checkHitPaddel(p, v, b2), b1)))
end update

update(makePoint(maxx div 2, maxy div 2), makeBox(60, 10, makePoint(10, maxy div 2)), makeBox(60, 10, makePoint(maxx - 10, maxy div 2)), makePoint(-1, -1))


Right now it is cheapAI vs cheapAI but you could "easly" modify (don't know why you whould) to have a humman player. Becues this is rescurive for no reason it will eventualy overflow the stack and crash.

Author:  zylum [ Tue Dec 18, 2007 12:37 am ]
Post subject:  RE:Matrix Pong

lol very nice dan Wink

Author:  shakin cookie [ Sat Dec 22, 2007 6:41 pm ]
Post subject:  RE:Matrix Pong

I dont think that the world of programming needs more pong games.

This was great, though=)

Author:  Carey [ Sun Dec 23, 2007 2:58 pm ]
Post subject:  RE:Matrix Pong

it was meant to be a joke. (both of them)

Author:  Gooie [ Sun Dec 23, 2007 4:00 pm ]
Post subject:  Re: RE:Matrix Pong

shakin cookie @ December 22nd, 6:41 pm wrote:
I dont think that the world of programming needs more pong games.

This was great, though=)


The world of programming always needs me more Pong.


: