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

Username:   Password: 
 RegisterRegister   
 My Old 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
Guest




PostPosted: Thu May 11, 2006 8:23 pm   Post subject: My Old Pong Game

I don't use this anymore, but this is my first pong game. No AI, just 2P. Also how come the pong paddles slow down when two users hit a key at once?

code:

% Drew Martell
% Pong

% This program will recreate the awesome Pong experience you once
% had as a young, young child.

% Need to find out how to cancel a process

var scoresFile : int
var records : string
var downLine := 1

var x, y : int
var dx, dy := 1
var player1, player2 : string
var ok : boolean := false
var background := blue
var introFont := Font.New ("serif:25")
var scoreFont := Font.New ("serif:18")
var getKey : array char of boolean
var dir, dir3 := 100
var dir2, dir4 := 180
var score1P, score2P := 0
var stickSpeed := 3
var ballSpeed := 2
x := 70
y := 313

setscreen ("nocursor")

process DrawText
    loop
        for i : 0 .. 255
            Draw.Text ("WELCOME TO PONG", maxx div 2 - 150, maxy div 2, introFont, i)
        end for
        exit when ok = true
    end loop
end DrawText

procedure MainMenu
    drawfillbox (0, 0, maxx, maxy, red)
    fork DrawText
    colorback (red)
    locate (17, 25)
    put "Enter Player 1's Name: " ..
    get player1 : *
    locate (18, 25)
    put "Enter Player 2's Name: " ..
    get player2 : *
    if (player1 = "del") and (player2 = "del") then
        if File.Exists ("High Scores.txt") then
            File.Delete ("High Scores.txt")
            locate (20, 33)
            put "FILE DELETED"
        else
            locate (20, 30)
            put "FILE DOESN'T EXIST"
        end if
        Input.Pause
        player1 := "Player 1"
        player2 := "Player 2"
    elsif (player1 = "view") and (player2 = "high scores") then
        open : scoresFile, "High Scores.txt", get
        loop
            if (scoresFile = -10) then
                exit
            end if
            get : scoresFile, records : *
            locate (downLine, 28)
            put records
            downLine += 1
            exit when eof (scoresFile)
        end loop
        Input.Pause
        player1 := "Player 1"
        player2 := "Player 2"
    end if
    cls
    score1P := 0
    score2P := 0
    ok := true
end MainMenu

MainMenu

View.Set ("offscreenonly")

drawfillbox (0, 0, maxx, maxy, background)
drawbox (0, 0, maxx, maxy, black)
drawfillbox (20, dir2, 30, dir, white)
drawbox (20, dir2, 30, dir, black)
drawfillbox (maxx - 20, dir4, maxx - 30, dir3, white)
drawbox (maxx - 20, dir4, maxx - 30, dir3, black)

procedure MoveUp1P
    drawfillbox (20, dir2, 30, dir, background)
    dir += 1
    dir2 += 1
    drawfillbox (20, dir2, 30, dir, white)
    drawbox (20, dir2, 30, dir, black)
    delay (stickSpeed)
    %View.Update
end MoveUp1P

procedure MoveDown1P
    drawfillbox (20, dir2, 30, dir, background)
    dir -= 1
    dir2 -= 1
    drawfillbox (20, dir2, 30, dir, white)
    drawbox (20, dir2, 30, dir, black)
    delay (stickSpeed)
    %View.Update
end MoveDown1P

procedure MoveUp2P
    drawfillbox (maxx - 20, dir4, maxx - 30, dir3, background)
    dir3 += 1
    dir4 += 1
    drawfillbox (maxx - 20, dir4, maxx - 30, dir3, white)
    drawbox (maxx - 20, dir4, maxx - 30, dir3, black)
    delay (stickSpeed)
    %View.Update
end MoveUp2P

procedure MoveDown2P
    drawfillbox (maxx - 20, dir4, maxx - 30, dir3, background)
    dir3 -= 1
    dir4 -= 1
    drawfillbox (maxx - 20, dir4, maxx - 30, dir3, white)
    drawbox (maxx - 20, dir4, maxx - 30, dir3, black)
    delay (stickSpeed)
    %View.Update
end MoveDown2P

procedure Pause
end Pause

process Sound
    Music.Sound (100, 70)
    Music.SoundOff
end Sound

process WriteScore
    open : scoresFile, "High Scores.txt", put, mod
    put : scoresFile, player1, " VS ", player2, " : ", score1P, " - ", score2P
    close : scoresFile
end WriteScore

process BouncyBall
    loop
        locate (1, 1)
        colorback (green)
        %put " x ", x, "   y ", y, "   dir ", dir, "   dir2 ", dir2, "          dir3 ", dir3, "   dir4 ", dir4
        %Draw.Text ("1P Score = ", 0, maxy - 18, scoreFont, red)
        put player1, '  ', score1P
        locate (1, 43)
        put player2, '  ', score2P
        x += dx
        y += dy
        delay (ballSpeed)     % Balls speed
        drawfilloval (x, y, 5, 5, yellow)
        drawoval (x, y, 5, 5, black)
        drawfillbox ((maxx div 2) - 10, 0, (maxx div 2), maxy, white)
        drawbox ((maxx div 2) - 10, 0, (maxx div 2), maxy, black)
        View.Update
        drawfilloval (x, y, 5, 5, background)
        % BOUNDARIES
        if (x = 40) and (y >= dir and y <= dir2) then
            fork Sound
            dx := -dx
        elsif (x < 40) then
            score2P += 1
            fork DrawText
            fork WriteScore
            x := 550
            y := 300
        elsif (x >= maxx - 40) and (y >= dir3 and y <= dir4) then
            fork Sound
            dx := -dx
        elsif (x > maxx - 40) then
            score1P += 1
            fork DrawText
            fork WriteScore
            x := 70
            y := 300
        elsif (y = 0) or (y = maxy - 20) then
            fork Sound
            dy := -dy
        end if
    end loop
end BouncyBall

fork BouncyBall

loop
    Input.KeyDown (getKey)
    if getKey ('w') then
        MoveUp1P
    end if
    if getKey ('s') then
        MoveDown1P
    end if
    if getKey (KEY_UP_ARROW) then
        MoveUp2P
    end if
    if getKey (KEY_DOWN_ARROW) then
        MoveDown2P
    end if
    if getKey (KEY_ENTER) then
        Pause
    end if
    if getKey (KEY_ESC) then
        % Need to find out how to cancel a process
        % Take out fork writescore before you put this
        % open : scoresFile, "High Scores.txt", put, seek, mod
        % put : scoresFile, player1, " VS ", player2, " : ", score1P, " - ", score2P
        % seek : scoresFile, *
        % close : scoresFile
        quit
            MainMenu
    end if
    % 1P Boundaries
    if (dir <= 0) and (dir2 <= 80) then
        dir := 0
        dir2 := 80
    elsif (dir >= maxy - 80) and (dir2 >= maxy - 80) then
        dir2 := maxy
        dir := maxy - 80
    end if
    % 2P Boundaries
    if (dir3 <= 0) and (dir4 <= 80) then
        dir3 := 0
        dir4 := 80
    elsif (dir3 >= maxy - 80) and (dir4 >= maxy - 80) then
        dir3 := maxy - 80
        dir4 := maxy
    end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
do_pete




PostPosted: Thu May 11, 2006 8:34 pm   Post subject: (No subject)

Because you have a delay in the procs that move the paddles. Anyways, nice game overall. Very Happy
ohgeez_




PostPosted: Thu May 11, 2006 9:41 pm   Post subject: (No subject)

okk. not bad at all

but..

- avoid those processes at all costs. unless if its for the sound. the other parts are unnecessary or at least i believe can be avoided.

- i dont suggest u using 'quit' with your esc button. why cant u just use exit?

- i'd suggest u make the boards so that they cant go into the green area since the ball doesnt go there anyway. the green part kind of covers the board.

but its quite good a game overall
good job =D
Guest




PostPosted: Sat May 13, 2006 3:18 pm   Post subject: (No subject)

If I use exit, it still runs in the background because I am using a process. How can I kill a process? I like using processes because it's the easiest way I find to make things run in the background.
ohgeez_




PostPosted: Sat May 13, 2006 3:24 pm   Post subject: (No subject)

im not sure if there is a better syntax but one u can use is 'return'
it terminates a subprogram

oh and processes... avoid them
learn to use alternatives.
Guest




PostPosted: Sun May 28, 2006 9:28 am   Post subject: (No subject)

processes shorten the length of the code, in some cases dramatically. and in the end, with a program like pong in my case, would have the same result, almost.
ohgeez_




PostPosted: Sun May 28, 2006 12:55 pm   Post subject: (No subject)

are u sure that processes can shorten ur code?
because i think that if u were to put the process boucy ball in the loop, it wouldnt make a big difference.

using processes for music is ok though

although its true for your program, process or not process, it doesnt make a big difference.
MysticVegeta




PostPosted: Sun May 28, 2006 1:01 pm   Post subject: (No subject)

ohgeez_ wrote:
using processes for music is ok though

Not if you have turing 4.05 or +.
"Music.PlayFileLoop" needs no processes
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Sun May 28, 2006 1:12 pm   Post subject: (No subject)

What's this business about "processes" that I hear? Do my ears deceive me?

vahnx, there is absolutely no reason to use processes for pong that outweighs the consequences that come with process usage. Make sure you read the article outlining the reasons to avoid processes.
Guest




PostPosted: Sun May 28, 2006 1:32 pm   Post subject: (No subject)

There's nothing wrong with processes. I know they are randomly called "fork hi fork ho", they don't run (1,2,1,2). But in the case of pong it doesn't matter. It shortens the length of my pong game by a few lines. If Holtsoft didn't like processes, they wouldn't be in Turing. They probably put it in to help lazy people like me.
Andy




PostPosted: Sun May 28, 2006 3:19 pm   Post subject: (No subject)

processes are in turing not so people can abuse them. they're useful for playing music, and multi threaded programming. Pong was never intended to be done with processes. And learn to take constructive critisisms
Cervantes




PostPosted: Sun May 28, 2006 3:47 pm   Post subject: (No subject)

vahnx wrote:
There's nothing wrong with processes.


Yes, there is a lot wrong with processes. Read the article.

Tony said it well:
Tony wrote:

You essensially throw your code flow into a blender, mix it up and say. "Here I have a bunch of lines of code. They are in no particular order. If the computer happens to pick the right order - the results will be as expected. Otherwise I have no idea what might happen"


vahnx wrote:
They probably put it in to help lazy people like me.

If you understood how to do this without processes, you wouldn't see any difference in difficulty. The reason you used processes here has nothing to do with being lazy: it has to do with a fundamental eagerness -- accomplishing something by doing it the first way that pops into your head, without giving other altneratives a second thought.

vahnx wrote:
It shortens the length of my pong game by a few lines.

I don't think so.
Guest




PostPosted: Sun May 28, 2006 5:17 pm   Post subject: (No subject)

Fine you win. Why did Holtsoft include Processes in Turing? Except for the fact of Music.Sound.
HellblazerX




PostPosted: Sun May 28, 2006 6:11 pm   Post subject: (No subject)

For multi-threaded programs (threads are another name for processes) and sound. And no, Pong is not a multi-threaded program. An example of a multi-threaded program would be a web chat server, in which the server needs to handle each client separately in a process so that client won't affect the server's or other clients' performances.

And if Music.PlayFileLoop doesn't use processes, then how does it work?
Cervantes




PostPosted: Sun May 28, 2006 6:21 pm   Post subject: (No subject)

You use a process (or thread) when you need to continue running one piece of code while another piece is hanging. That is, that second piece of code is waiting on the get line. HellblazerX gave an excellent example of a web chat server. I'm going to reverse it, and explain why I needed threading in making an IRC client.

An IRC client needs to connect to multiple servers and get a line of data through a socket connection. The code hangs there at that get line until data is received. At that point, that data needs to be handled. This is impossible to do without threads because your code could be hanging waiting for one server to send a line while the other server has sent a whole bunch of lines that aren't being received.
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: