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

Username:   Password: 
 RegisterRegister   
 High Score Table: Paddle Ball Game?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
PrinceCooper




PostPosted: Tue Jun 14, 2011 12:47 pm   Post subject: High Score Table: Paddle Ball Game?

Problem:
Me and my friend are currently working on a ball and paddle type game and have run into a issue. We want to add a high score table option to the menu to keep track of the top scores.

Issue:
We currently have a score system in place so everytime the ball hits the paddle your score increases by 1 ponit. We want to be able have that score stored in a separate txt file or something so when the high score option is chosen from the menu the Top 10 Scores are set in place. As a bonus we want to be able to store names as well so we can tell who got which score.

Current Code:
Turing:


% VARIABLES
var x1, x2, x3, x4, x5 : int     % Lengths, X-Axis
var xdir, ydir : int             % Keeps Track of Speed of Ball
var y1 : int                     % Height,Y-axis
var key : string (1)             % Key Board Character
var num : int                    % Stores ASCII Numbers
var count : int                  % Keeps Track of Score
var choice : string (1)          % Choice for menu
var name : string                % Name for Highscore
var font1 : int
font1 := Font.New ("Agency FB:30")

% MENU
drawfill (10, 10, 225, 225)
locate (10, 24)
colorback (225)
Font.Draw ("WELCOME TO RYAN'S PADDLEBALL GAME!", 90, 320, font1, 31)
locate (11, 30)
color (31)
put "Created By: Ryan Covell" ..
locate (13, 28)
color (31)
put "PRESS (1) TO START A NEW GAME" ..
locate (15, 28)
color (31)
put "PRESS (2) TO SEE HIGH-SCORE TABLE" ..
locate (17, 28)
color (31)
put "PRESS (3) TO QUIT GAME" ..
color (31)
locate (20, 2)
put "Use the left (<--) and right (-->) arrows to direct the paddle. The point of the game is to try and keep the ball moving and stop it from hitting the ground!"

getch (choice)
case choice of

        % MAIN GAME
    label "1" :
        cls
        View.Set ("offscreenonly")
        loop
            cls
            count := 0                    %Setting the values for the variables
            num := 0
            x1 := 110
            xdir := 3
            ydir := 3
            y1 := 110
            x4 := 110
            x5 := 160
            x2 := 110
            x3 := 160


            loop
                drawfill (10, 10, 225, 225) % Background Colour
                locate (10, 2)
                colorback (225)
                color (31)
                put "SCORE ", count ..
                drawbox (110, -100, 635, 389, 31) % Background
                drawfillbox (x4, 0, x5, 10, 31) % Paddle
                drawfilloval (x1, y1, 5, 5, 31) % Ball
                delay (10)
                View.Update
                exit when y1 = -10
                drawfilloval (x1, y1, 5, 5, 225) % Erasing Ball Trail
                drawfillbox (x4, 0, x5, 10, 225) % Erasing Paddle Trail

                if hasch then
                    getch (key) % Get Key Pressed
                    num := ord (key) % Find ASCII number for key
                end if

                if num = 203 and x4 >= 120 then % Left arrow
                    x4 := x4 - 25
                    x5 := x5 - 25
                elsif num = 205 and x5 <= 620 then % Right arrow
                    x5 := x5 + 25
                    x4 := x4 + 25
                end if
                num := 0


                if y1 >= 10 and y1 <= 15 and x1 >= x4 and x1 <= x5 then % makes ball bounce of paddle
                    count := count + 1 % adds 1 point to the score each time it hits paddle
                    ydir := 3
                end if
                if x1 <= 101 then % makes ball not go past 101 on x axis
                    xdir := 3
                elsif x1 >= 620 then % makes ball not go past 620 on x axis
                    xdir := -3
                elsif y1 >= 380 then % makes ball not go past 380 on y axis
                    ydir := -3
                elsif y1 <= -60 then % makes ball not go past -60 on y axis
                    ydir := 3
                end if

                y1 := y1 + ydir
                x1 := x1 + xdir

            end loop

            % GAME OVER

            loop
                locate (10, 20)
                put "Do you want to try again? (Y=Yes / N=NO)" ..                   % This is the game over menu.
                View.Update
                getch (key)
                exit when key = "n" or key = "y"
                cls
                count := 0
            end loop
            exit when key = "n"  %Exit loop when 'n' is pressed.
        end loop
        View.Set ("nooffscreenonly")
        cls
        locate (10, 30)
        put "THANKS FOR PLAYING!"
        locate (12, 30)
        put "PLEASE ENTER YOUR NAME FOR HIGHSCORE TABLE!"
        get name

    label "2" :



    label "3" :
        quit

end case






Version:
4.0.4.C
Sponsor
Sponsor
Sponsor
sponsor
apython1992




PostPosted: Tue Jun 14, 2011 1:08 pm   Post subject: RE:High Score Table: Paddle Ball Game?

If you want to keep the high scores, you will need to output to a file for permanent storage. See this tutorial if you haven't done any file I/O before.

If you would like to add names to the table as well, this is easy once you can write stuff to your scores file. Just make sure you ask for name input.
PrinceCooper




PostPosted: Wed Jun 15, 2011 9:35 am   Post subject: Re: High Score Table: Paddle Ball Game?

I was able to write a sentence to the external text file, but only if the sentence was predetermined

var stream : int
var myText : string := "This is how to write to a file"
open : stream, "myText.txt", write
write : stream, myText

Question: What I can't seem to do Is have the program display said value when I press (2),

label "2" :
cls
var stream : int
var myText : string
open : stream, "myText.txt", read
read : stream, myText

What I want to be able to do is replace the sentence with the 'count' variable, which is the score from the game, and be able to display when I chose to from the menu. No matter what I have tried nothing has succeed. I would greatly appreciate help with this problem, while the tutorial is helpful for those who are somewhat fluent in Turing I am not, my friend is but I can't get his help for another couple weeks and I want it solved before than. I really like Turing and I wish to learn as much as I can.
apython1992




PostPosted: Wed Jun 15, 2011 10:42 am   Post subject: RE:High Score Table: Paddle Ball Game?

Can you post the code that you have tried with getting to write that count variable to the file? And perhaps going through a quick overview of Turing might be good for you to learn the syntax and in general how programs work, if you feel that you aren't really getting it.
PrinceCooper




PostPosted: Thu Jun 16, 2011 3:08 pm   Post subject: Re: High Score Table: Paddle Ball Game?

Another friend helped and we (mostly him) cooked up this code that allowed us to get and store names at the end of that game and associate that name with the score earned in the last game.
Than at the menu you can pick the 2nd choice and Enter the name you want the score for and It displays the score associated with that name. While it can store multiple names and scores. it creates separate txt files for each name. What I want to be able to do is have the game only store and display the Top 10 Scores from one txt file. I do know that you have to use an array I just don't know how to implement it. I have posted the game again with the changes and some others here and there. As extra I was wondering if there was a way so the game won't error out when something at the menu is pressed that isn't 1, 2, or 3.


% VARIABLES
var x1, x2, x3, x4, x5 : int % Lengths, X-Axis
var xdir, ydir : int % Keeps Track of Speed of Ball
var y1 : int % Height,Y-axis
var key : string (1) % Key Board Character
var num : int % Stores ASCII Numbers
var count : int % Keeps Track of Score
var choice : string (1) % Choice for menu
var name : string % Name for Highscore
var font1 : int
font1 := Font.New ("Agency FB:30")

% MUSIC
process DoMusic % These lines are responsable for adding backround music to the game.
loop
Music.PlayFile ("Davros.mp3")
end loop
end DoMusic

fork DoMusic


loop
% MENU
drawfill (10, 10, 225, 225)
locate (10, 24)
colorback (225)
Font.Draw ("WELCOME TO RYAN'S PADDLEBALL GAME!", 90, 320, font1, 31)
locate (11, 30)
color (31)
put "Created By: Ryan Covell" ..
locate (13, 28)
color (31)
put "PRESS (1) TO START A NEW GAME" ..
locate (15, 28)
color (31)
put "PRESS (2) TO SEE HIGH-SCORE TABLE" ..
locate (17, 28)
color (31)
put "PRESS (3) TO QUIT GAME" ..
color (31)
locate (20, 2)
put "Use the left (<--) and right (-->) arrows to direct the paddle. The point of the game is to try and keep the ball moving and stop it from hitting the ground!"

getch (choice)
case choice of

% MAIN GAME
label "1" :
cls
View.Set ("offscreenonly")
loop
cls
count := 0 %Setting the values for the variables
num := 0
x1 := 110
xdir := 3
ydir := 3
y1 := 110
x4 := 110
x5 := 160
x2 := 110
x3 := 160


loop
drawfill (10, 10, 225, 225) % Background Colour
locate (10, 2)
colorback (225)
color (31)
put "SCORE ", count ..
drawbox (110, -100, 635, 389, 31) % Background
drawfillbox (x4, 0, x5, 10, 31) % Paddle
drawfilloval (x1, y1, 5, 5, 31) % Ball
delay (10)
View.Update
exit when y1 = -10
drawfilloval (x1, y1, 5, 5, 225) % Erasing Ball Trail
drawfillbox (x4, 0, x5, 10, 225) % Erasing Paddle Trail

if hasch then
getch (key) % Get Key Pressed
num := ord (key) % Find ASCII number for key
end if

if num = 203 and x4 >= 120 then % Left arrow
x4 := x4 - 25
x5 := x5 - 25
elsif num = 205 and x5 <= 620 then % Right arrow
x5 := x5 + 25
x4 := x4 + 25
end if
num := 0


if y1 >= 10 and y1 <= 15 and x1 >= x4 and x1 <= x5 then % makes ball bounce of paddle
count := count + 1 % adds 1 point to the score each time it hits paddle
ydir := 3
end if
if x1 <= 101 then % makes ball not go past 101 on x axis
xdir := 3
elsif x1 >= 620 then % makes ball not go past 620 on x axis
xdir := -3
elsif y1 >= 380 then % makes ball not go past 380 on y axis
ydir := -3
elsif y1 <= -60 then % makes ball not go past -60 on y axis
ydir := 3
end if

y1 := y1 + ydir
x1 := x1 + xdir

end loop

% GAME OVER

loop
locate (10, 20)
put "Do you want to try again? (Y=Yes / N=NO)" .. % This is the game over menu.
View.Update
getch (key)
exit when key = "n" or key = "y"
cls
count := 0
end loop
exit when key = "n" %Exit loop when 'n' is pressed.
end loop
View.Set ("nooffscreenonly")
cls
locate (10, 30)
put "THANKS FOR PLAYING!"
locate (12, 30)
put "PLEASE ENTER YOUR NAME FOR HIGHSCORE TABLE!"
get name

var stream : int
open : stream, name + ".txt", put
put : stream, count

name := ""
count := 0
close : stream
cls

label "2" :
cls

var stream : int
put "Enter Player Name."
get name

open : stream, name + ".txt", get %notice the file has to be in the "get" mode
get : stream, count
put count
delay (4000)
cls

name := ""
count := 0
close : stream

label "3" :
quit


end case
end loop
apython1992




PostPosted: Thu Jun 16, 2011 4:54 pm   Post subject: RE:High Score Table: Paddle Ball Game?

Do you know how to loop, and exit on a certain condition? Use that strategy for this case.
PrinceCooper




PostPosted: Thu Jun 16, 2011 5:53 pm   Post subject: Re: High Score Table: Paddle Ball Game?

Loop what exactly? I want to be able to pull multiply values from a external document to create the high score table but I don't know how to achieve this, the tut. you gave me earlier mentioned it but nothing I tried worked >.< I believe it was easy as replacing those var names and text names with the ones that I'm using but alas it doth not work. I do know how conditional loops work I just don't know where I would put it, in this particular case for the desired effect.
apython1992




PostPosted: Fri Jun 17, 2011 7:49 am   Post subject: RE:High Score Table: Paddle Ball Game?

I was referring to your last remark about how to handle situations where an option 1, 2, or 3 is not selected. In that case, you could throw that input into a loop that won't exit *until* one of those options is selected. As far as your high scores are concerned, you're seeing them in separate files because you are using a name that depends on the player's name:

code:
open : stream, name + ".txt", put


It shouldn't be hard to see why this causes each player's score to be written into their own file. We'll address the next problem of reading multiple values from the file once you can get everything into the same file.
Sponsor
Sponsor
Sponsor
sponsor
apython1992




PostPosted: Fri Jun 17, 2011 7:51 am   Post subject: RE:High Score Table: Paddle Ball Game?

Also, try using syntax tags so that I can read your code more easily Smile

code:
[syntax="turing"]
var age : int
put "How old are you?"
get age
[/syntax]


would give you:
Turing:

var age : int
put "How old are you?"
get age
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: