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

Username:   Password: 
 RegisterRegister   
 my pong game for assignment
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
kpotter




PostPosted: Tue Mar 21, 2006 1:45 pm   Post subject: my pong game for assignment

code:

% 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 <= padsizex then % if ball in line with user paddle
                if y - size <= padsizey and y + size >= pady1 then %if ball hits paddle
                    movex := -movex
                    movex := movex + 1
                else % if ball misses user paddle
                    leave := 1
                end if
            end if

            if x + size >= padx2 then % if ball in line with comp. paddle
                if y - size <= padsizey2 and 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 <= 1 + 5 then %if ball hits bottom
                movey := -movey
                movey := movey + 1
            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 ball

%___________________________________________________________________________________________

%moving the paddle1

process paddle1
    loop
        loop
            padsizex := padx1 + 5 %paddle x and y 2 coordinates
            padsizey := pady1 + 110
            getch (move1) % paddle move command

            %moves user paddle
            if ord (move1) = 200 and padsizey <= maxy then %move up
                pady1 := pady1 + movey1
            elsif ord (move1) = 208 and pady1 >= 0 then %move down
                pady1 := pady1 - movey1
            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 paddle1

%________________________________________________________________________

%moving comp. paddle2

process paddle2
    loop
        loop
            padsizex2 := padx2 + 5 %paddle x and y 2 coordinates
            padsizey2 := pady2 + 110
            delay (stop) % delay comp paddle


            if y < pady2 + 55 then %if ball below middle of paddle
                pady2 := pady2 - movey2
            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
Sponsor
Sponsor
Sponsor
sponsor
person




PostPosted: Tue Mar 21, 2006 2:59 pm   Post subject: (No subject)

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




PostPosted: Tue Mar 21, 2006 3:19 pm   Post subject: (No subject)

#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 Smile I like how you separated all the process by comment lines (Though you should be using procedures instead Razz).



@ an admin: Someone should probably make a "Pong Programs here" sticky, where any pong programs should be posted.
person




PostPosted: Tue Mar 21, 2006 3:41 pm   Post subject: (No subject)

TheOneTrueGod wrote:
not to use processes for everything.


Processes are sometimes evil Twisted Evil .

I remember Cervantes made a post explaining why...although I can't find it.
Dan




PostPosted: Tue Mar 21, 2006 3:54 pm   Post subject: (No subject)

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.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Cervantes




PostPosted: Tue Mar 21, 2006 6:44 pm   Post subject: (No subject)

Tutorials section. Wink
http://www.compsci.ca/v2/viewtopic.php?t=7842
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 1  [ 6 Posts ]
Jump to:   


Style:  
Search: