Tutorial Subroutines

By Ian Finlayson

 

Issue 30

Nov/Dec 87

Next Article >>

<< Prev Article

 

 

3. HIGH SCORE SAVE

In the third of our Tutorial Subroutines Ian Finlayson provides a means of saving high scores in your program - not just for one game but as part of the actual program

Many games for your computer have a high score feature to ensure you play again and again to try an improve on your previous performance, but few games will remember your high score from one playing session to the next. The subroutines which follow will allow you to add a high score save feature to any Basic game. Before going any further I must first sound a warning – as the subroutines will write to your tape or disk do not add them to your original program, work on a copy!

FORCED READ MODE

The technique used is called 'Forced read' and is a very powerful feature of 8 bit Atari computers which allows a program to modify itself by automatically writing program lines on the screen and then reading them as if they were typed in manually. The forced read mode is used here to change line 31210 so that the variable TT7 is set equal to the highscore of your completed game. In a more complex application one of the early graphics programs for the Atari called Drawpic used this technique to store pictures by first transforming the screen data into ATASCI I characters in a long string and then adding the string to the program by using forced read.

THE SUBROUTINES

Two subroutines are required for the high score save. The first is very short, only two lines – 31210 is the line which will be modified and 31220 will set HIGHSCORE equal to the saved variable at the beginning of a game. You will have to change the variable name that I have called HIGHSCORE to the name that is used to store the high score value in your game program.

The second subroutine is the one which rewrites line 31210. As this also saves the game I have included versions for tape and disk. Keep line 31270 for use with disks or alternatively delete line 31270 and remove the REM from lines 31280 and 31290 for cassette use.

SUBROUTINE ANALYSIS

The first subroutine requires no explanation but I will expand on the second. When we call this subroutine game play is over and we wish to save the game complete with the new high score.

Line 31230 - In this line we check to see if the new high score (HIGHSCORE) is better than that saved last time (TT7). If it is not there is no need to save it and the subroutine returns immediately to the main program.

Line 31240 - First select Graphics 0 to allow the required text to be written to the screen. This is necessary as most games will be in other graphics modes. Then move the cursor down three lines and print line 31210 showing the variable TT7 equal to the new value of HIGHSCORE.

Line 31250 - Move the cursor down again and print "CONT"

Line 31260 - Put the cursor back to the top of the screen. Poke 842 with the value 13 to enable forced read mode and STOP the program to start the forced read. At this point the computer will scan the screen and implement what is there; so it will read the new line 31210, including it in the program, and then read the CONT command which restarts the program at line 31270. If you use this technique in your own programs remember to leave a blank line or two before the "CONT" statement or the forced read will sometimes skip over it and go into an endless read which can only be stopped with the RESET key.

Remember also that the program will restart at the line after the line where it stopped; any further statements after STOP in this line would be missed.

Line 31270 - POKEing 842 with 12 disables forced read, otherwise the computer will try to read the screen again next time the program is stopped (e.g. at the end, after the game save). Next we save the game back to disk complete with the new line 31210 and high score value.

Line 31280/90 - These lines replace 31270 for cassette users. I have included a prompt to rewind the cassette as this is often forgotten when your game is loaded initially.

If you do not want to return to the main program to finally exit the game then replace RETURN in line 31270 or 31290 with END.

USING THE SUBROUTINE

To use the high score save you will have to add two subroutine calls to your game program. The first will be GOSUB 31200 which must be in the initialisation sequence of your game. If your game program contains a statement which gives an initial value to your HIGHSCORE variable this GOSUB should replace it. The second will be GOSUB 31230 and this should be near the end of your program after a prompt such as "PLAY AGAIN? (Y/N)" so that you only save the game and highscore when you have finished playing and not after each game.

ALTERNATIVE HIGHSCORE SAVES

You can try other ways of saving your high score value. This is quite easy to do with the direct file access of disks, but the problem is greater with cassette tape because of the sequential nature of records. One way of shortening the time to save the high score would be to LIST "C:",31210. This would save only the line which has just been rewritten. You can then use forced read for an ENTER C: command near the beginning of your program and the whole will run automatically, but there will be a problem repositioning the tape to the end of the main game program after ENTERing line 31210 at the beginning of the game and before the LIST "C:",31210 at end of play.

I would be pleased to hear from anyone who develops further useful subroutines from those in this series, and from anyone who would like a particular topic addressed in these articles. If you want a personal reply please enclose a stamped envelope and I will try to write back. My address is Ian Finlayson, 60 Roundstone Crescent, East Preston, West Sussex BN16 1DQ.

AtariLister - requires Java

top