Kids can code too

How do you store scores of the game for deciding who is the winner post all the levels have been won against many players? You can use text files to store the scores on the hard disk.

The only change for this is in the code where the game over screen code is. We do this because we save only the number of footballs that disappear when the game is over, which is incidentally the number of walls we create.
This is just to illustrate creating and saving files and is a silly example for scores.
Code (the changes are in BOLD):

The “try/catch” is for exception handling. Means, when there is an error in the code, in the creation of the file etc, instead of the code crashing, the control of the code is passed to the “catch” block and the code there is executed.

Then we open the file in “w” mode – if the file is not present it creates it and if it does, overwrite it. Write the required text into the file and close the file.

Share this:

Like this:

Related

Published by diycomputerscienceandelectronics

A Computer Science engineer from BITS, Pilani, was an IT professional and now a self-professed teacher for my kids and anyone else who is interested.
Electronics intrigues me, now I sometimes wish, I had done my engineering in it. Such a powerful and captivating topic.
I love swimming and reading books and getting into long philosophical conversations.
I run a Computer Science and electronics course in Powai (www.facebook.com/kidscancodetoo) and work part time at Curiositygym, a wonderful places that dwells in technology.
View all posts by diycomputerscienceandelectronics