
-----------------------------------
kpotter
Tue Mar 21, 2006 1:45 pm

my pong game for assignment
-----------------------------------

% pong
% Potter
% pong.t
% March 2,2006

var winMain : int := Window.Open ("Graphics:781;541")
setscreen ("nocursor")

var col1 : int := 7 %colour 1 background
var col2 : int := 0 %colour 2 paddles,balland score
var delaytime : int := 10 %delay time for ball moving cause of new comp systme

%----------------------------------------------------

%starting variables (are reused)
var diff : string (1) % difficulty chose
var stop : int := 250 % delay time for start and comp move

%---------------------------------------------------------

var leave : int := 0
%------------------------ball variables
%x - x coordinate of ball
%y - y coordinate of ball
%movex - movement of x
%movey - movement of y
%size - size of ball
var x, y, movex, movey : int
var size : int := 10

x := maxx div 2 %ball start position
randint (y, 20, maxy - 20) %ball start position
movex := 2 %ball speed
movey := 2 %ball speed

%----------------------paddle variables

%padx1 - paddle x
%pady1 - paddle y
%padsizey - size of paddle
%padsizex - size of paddle
%move1 - moving y axis of paddle
var padx1, pady1, padsizey, padsizex : int
var move1 : string (1)
var movey1 : int


padx1 := 1 %user paddle start
pady1 := (maxy div 2) - (110 div 2) %user paddle start
movey1 := 40 %user move amount

%-------------------------------------------------paddle2 variables

%padx2 - paddle x
%pady2 - paddle y
%padsizey2 - size of paddle
%padsizex2 - size of paddle
%move2 - moving y axis of paddle
var padx2, pady2, padsizey2, padsizex2 : int
var movey2 : int

padx2 := maxx - 2 %comp. paddle start
pady2 := (maxy div 2) - (110 div 2) %comp. paddle start
movey2 := 20 %comp. move amount

%---------------------------------------------------------------------------
%usescore - user score
%compscore - comp score
var usescore, compscore : int := 0

%----------------------------------------------------------------

put "              *****************************************************"
delay (stop)
put "              *               Welcome To Pong                     *"
delay (stop)
put "              *Please enter the diffuculty your would like to play*"
delay (stop)
put "              *                 1.Easy                            *"
delay (stop)
put "              *                 2.Medium                          *"
delay (stop)
put "              *                 3.Hard                            *"
delay (stop)
put "              *                 4.Impossible                      *"
delay (stop)
put "              *****************************************************"
delay (stop)

getch (diff)
loop
    if ord (diff) = 49 then
        put "Your Choose ", diff, ".Easy"
        stop := 81
        exit
    elsif ord (diff) = 50 then
        put "Your Choose ", diff, ".Medium"
        stop := 9
        exit
    elsif ord (diff) = 51 then
        put "Your Choose ", diff, ".Hard"
        stop := 3
        exit
    elsif ord (diff) = 52 then
        put "Your Choose ", diff, ".Impossible"
        stop := 1
        exit
    else
        put "Invaild Entry"
        put "Please enter the diffuculty your would like to play"
        getch (diff)
    end if
end loop
put "Press Any Key to Continue"
getch (diff)

%----------------------------------------------------------------------------------------

setscreen ("offscreenonly")

%---------------------------------------------------------------------------------------

%bouncing the ball off the sides
process ball %moving ball
    loop
        %put "Press Any Key To Begin"
        %getch (diff)
        delay (100)

        loop
            delay (delaytime)
            x := x + movex %moves ball
            y := y + movey %moves ball

            %ball bounces off screen
            if x - size = padx2 then % if ball in line with comp. paddle
                if y - size = pady2 then %if ball hits paddle
                    movex := -movex
                    movex := movex - 1
                else % if ball misses paddle
                    leave := 2
                end if
            end if

            if y >= maxy - 1 - 5 then %if ball hits top
                movey := -movey
                movey := movey - 1
            elsif y  padsizey2 - 55 then % if ball above middle paddle
                pady2 := pady2 + movey2
            end if

            if leave = 1 or leave = 2 then
                exit
            end if
        end loop
        delay (1000)
        exit when usescore = 5 or compscore = 5
    end loop
end paddle2

%-------------------------------------------------------------------

%all animations (ball,paddles,etc.)
process pics
    loop
        loop
            drawfill (5,5,col1,col1) %background colour
            
            %draws score
            Text.LocateXY (maxx div 2 - 4, 40)
            Text.ColourBack (col1)
            Text.Colour (col2)
            put "SCORE!!!"
            Text.LocateXY (maxx div 2 - 4, 30)
            Text.ColourBack (col1)
            Text.Colour (col2)
            put "USER - ", usescore
            Text.LocateXY (maxx div 2 - 10, 20)
            Text.ColourBack (col1)
            Text.Colour (col2)
            put "COMP. - ", compscore
            
            %drawing any object goes here
            drawfilloval (x, y, size, size, col2) %draws ball
            drawfillbox (padx1, pady1, padsizex, padsizey, col2) % draws paddle
            drawfillbox (padx2, pady2, padsizex2, padsizey2, col2) % draw comp
            %paddle
            View.Update
            if leave = 1 or leave = 2 then
                exit
            end if
            cls
        end loop
        delay (999)
        exit when usescore = 5 or compscore = 5
    end loop
end pics

%-----------------------------------------------------------------------------------

process score
    loop
        loop
            if leave = 1 then % if user scores
                compscore := compscore + 1
                delay (1000)
                leave := 0
            elsif leave = 2 then % if comp scores
                usescore := usescore + 1
                delay (1000)
                leave := 0
            end if
            exit when usescore = 5 or compscore = 5
        end loop
        exit when usescore = 5 or compscore = 5
    end loop
end score

%-----------------------------------------------------------------------------

process reset
    loop
        delay (500)
        if leave = 1 or leave = 2 then
            var size : int := 10

            x := maxx div 2 %ball start position
            randint (y, 20, maxy - 20) %ball start position
            movex := 2 %ball speed
            movey := 2 %ball speed

            padx1 := 1 %user paddle start
            pady1 := (maxy div 2) - (110 div 2) %user paddle start

            padx2 := maxx - 2 %comp. paddle start
            pady2 := (maxy div 2) - (110 div 2) %comp. paddle start
        end if
        exit when usescore = 5 or compscore = 5
    end loop
end reset

fork ball
fork paddle1
fork paddle2
fork pics
fork score
fork reset



Text.LocateXY (maxx div 2 - 4, 40)
put "SCORE!!!"
Text.LocateXY (maxx div 2 - 4, 30)
put "USER - ", usescore
Text.LocateXY (maxx div 2 - 10, 20)
put "COMP. - ", compscore
View.Update


-----------------------------------
person
Tue Mar 21, 2006 2:59 pm


-----------------------------------
1) Use Code Tags...Please?
2) You Could Make the Paddle Movement a Little Bit Smother
3) Awkward Acceleration of the Ball
4) I really liek how this looks pretty similar to the originalpong game.

-----------------------------------
TheOneTrueGod
Tue Mar 21, 2006 3:19 pm


-----------------------------------
#1) Wrong forum (Theres a source code forum in the turing section)
#2) Investigate Input.KeyDown
#3) Its a good idea not to use processes for everything.  Instead, have one main loop.  Theres a thread somewhere that says why you shouldn't use processes in turing.  (Especially because pong can be done quite easily, and probably more efficiently without processes)
#4) Nice work with commenting :) I like how you separated all the process by comment lines (Though you should be using procedures instead :P).



@ an admin: Someone should probably make a "Pong Programs here" sticky, where any pong programs should be posted.

-----------------------------------
person
Tue Mar 21, 2006 3:41 pm


-----------------------------------
not to use processes for everything.

Processes are sometimes evil  :twisted: .

I remember Cervantes made a post explaining why...although I can't find it.

-----------------------------------
Dan
Tue Mar 21, 2006 3:54 pm


-----------------------------------
Process are a good thing when used right. Unfrontly with a lange with as limited control over them as turing they are hardly ever used right.

Process or treads (like the rest of the world calls them) are an antmented to make a computer do 2 things at once. How ever unless you have a muptial cpus or a hyperthread one this dose not happen. Insted the progame jumps back and forth between the threads almost at random. In other langues there are means to controll thess threads and keep them synorized, turing simple dose not have this. 

So what dose this mean for turing progames that use them? Well it means that the code in your process are going to be runing at random invteravals and if there is code in them at is needed by other threads it could happend b4 it needs them, at the same time, or affter it dose. This can causes errors, bugs and just out right odd things to happen.

Also difrent computers deal with threads difrent ways. So your progame could do somthing complty difrent on a difrent computer. 

In turing it is ushely a good idea to only use process for things that are not crital to the main part of your progame or that do not coantin values, ect that are needed by the main or any other part. One example of when to use a process used to be to play musick, but with the new turing verson playing muscik is not a blocking comand (ie. it dose not stop ecution of the progame).

The only need for process in turing and most langs should be when you have a comand that stops exucation of the progame and you need to do somthing well you wait for input or the comand to be done.

-----------------------------------
Cervantes
Tue Mar 21, 2006 6:44 pm


-----------------------------------
Tutorials section. ;)
http://www.compsci.ca/v2/viewtopic.php?t=7842
