HISCORES IN STOS This article is from ST USER iss.3 1990 and itºs written to this TPS magazine by...Sorcerer of The Digital Dictators (who else?)!! This article is devoted to high-score routines,displaying the scores and entering playersº names. First,youºll have to design a screen and squash it with the COMPACT.ACB utility. To help you,Program I will produce the screen I have used throughout this article.It should be compacted and stored in bank 5. You will also have to create a sprite bank of letters and a full stop.This can easily be done with the SPRITE.ACB accessory by grabbing the letters from the picture created with Program I, where sprites 1 to 26 should be the letters A to Z,and sprite 27 being the full stop: 5 REM Program I 10 MODE 0:KEY OFF:HIDE 20 WINDOPEN 1,0,0,40,12,0,3 30 CURS OFF:RESTORE 100 40 FOR a=1 to 4:READ mess$ 50 LOCATE 0,(a*2):CENTRE mess$ 60 NEXT a 70 WAIT 100 80 SAVE "HIPIC.PI1",BACK 90 GOTO 90 100 DATA "A B C D E F G H I" 110 DATA "J K L M N O P Q R" 120 DATA "S T U V W X Y Z ." 130 DATA "Enter Delete Space" No matter how you decide to display the table and enter the playersº names,you first have to produce a dummy table which contains a few high scores for the players to try and beat which can give the player an incentive. Lines 21040 and 21050 of Program II set up this initial table, which is displayed with lines 21060 to 21100.Iºve used a simple print loop,but you could easily substitute this for one of the routines in the last monthºs number of ST USER: 21000 REM Program II 21010 MODE 0:KEY OFF 21020 WINDOPEN 1,0,0,40,12,0,3 21030 CURS OFF 21040 DIM hs(10),hs$(10) 21050 FOR a=1 TO 10:h$(a)="Atari ST User":hs(a)=(11-a)*1000:NEXT a 21055 sc=6543:GOSUB 21200 21060 LOCATE 0,0:CENTRE"High Score Table" 21070 FOR a=1 TO 10:LOCATE 18-LEN(hs$(a)),a+1 21080 PRINT hs$(a);" ........";hs(a); 21090 NEXT a 21100 GOTO 21100 21200 IF schs(a) THEN SWAP hs$(a),hs$(a+1):SWAP hs(a),hs(a+1) 21250 NEXT a:RETURN 22000 n$="T D D":RETURN The rest of the program-lines 21200 to 22000-is concerned with reading the playerºs name and placing it into the correct high-score position.This section of code is called from line 21055,which also sets a dummy score so you can see the routines working. Running the above program will show that TDDºs score of 6,543 is placed into position in the table.Looking at lines 21200 to 21250 will show how the program decides exactly where the new score should appear.The simplest way of doing this is to use a technique called bubble sorting. First,the playerºs score is checked with the tenth or lowest score.If the player hasnºt beaten this value,he hasnºt made it to the table and the program will return to line 21060.Try changing the value of sc to 900 in line 21055 to see what I mean. Line 21210 jumps to the routine starting at 22000 to get the playerºs name.For the time being Iºve placed a dummy line at this position,but weºll be exploring different ways odf doing this in a moment. The important lines are 21220 to 21250,which first places the new score at position 10.We now check if this score is greater than the next one at position 9. If it is,the program makes use of the SWAP command,which just exchanges the contents of two variables or strings.For example, enter the following commands: A=10:B=20 A$="YES":B$="NO" SWAP A,B SWAP A$,B$ PRINT A,B PRINT A$,B$ Youºll see that the value of a is 20,b is 10,a$ is"NO" and b$ is "YES". When the playerºs score is less than the next one in the table, the new name and score will be in the correct place.What happens when two scores are the same? Try changing the value of sc to 6000 and run the program again.Youºll notice that TDDºs score is placed immediately below the existing one.This is how it should be, as you can only pass a score if you beat it. Notice that in line 20170 the LOCATE command uses the length of each name.This is beacause the player may only enter a short name,or just his initials,so leaving the high-score table looking rather messy.Try replacing line 21070 with the following line to see this untidy effect: 21070 FOR a=1 TO 10:LOCATE 5,a+1 WHATºS IN A NAME? The first method of inputting a playerºs name that weºll examine is probably the most common,where the letters of the alphabet are displayed on the screen,and the player chooses which ones to use with the mouse.Add the following lines to Program II: 22000 REM Hiscore 3 22010 REM --------- 22020 RESERVE AS SCREEN 10:UNPACK 5,10 22030 SCREEN COPY 10 TO PHYSIC:SCREEN COPY 10 TO BACK 22040 x=1 22050 FOR a=1 TO 3:FOR b=1 TO 9 22060 SET ZONE x,b*24+30,a*32 TO b*24+42,a*32+16 22070 BOX b*24+30,a*32 TO b*24+42,a*32+16 22080 INC x:NEXT b:NEXT a 22090 BOX 70,127 TO 114,145 22100 SET ZONE 28,70,127 TO 114,145 22110 BOX 134,127 TO 186,45 22120 SET ZONE 29,134,127 TO 186,145 22130 BOX 206,127 TO 250,145 22140 SET ZONE 30,206,127 TO 250,145 22150 SHOW:LOCATE 14,10:n$=" ":PRINT n$ 22160 done=0:c=1:WHILE done=0 22170 z=0:IF MOUSE KEY=0 THEN 22170 22180 z=ZONE(0):IF z=0 THEN 22170 22190 c$="":IF z<27 THEN c$=CHR$(z+64) 22200 IF z=27 THEN c$="." 22210 IF z=28 THEN done=1 22220 IF z=30 THEN c$=" " 22230 IF c$<>"" AND c<>14 THEN GOSUB 22300:INC c 22240 IF z=29 AND c>1 THEN DEC c:c$=" ":GOSUB 22300 22250 WEND 22260 FOR a=1 TO 13:IF MID$(n$,a,1)=" " THEN MID$(n$,a,1)=" " 22270 NEXT a 22280 IF RIGHT$(n$,1)=" " THEN n$=LEFT$(n$,LEN(n$)-1):GOTO 22280 22290 CLW:RETURN 22300 MID$(n$,c,1)=c$ 22310 LOCATE 14,10:PRINT n$ 22320 WHILE MOUSE KEY<>0:WEND 22330 RETURN Lines 22000 to 22150 are concerned with displaying the screen,defining a zone and drwing a box around each character and option. Running the program is probably the best way to see how it works. Whenever you click on a letter or full stop,that character is placed in the name and displayed on the screen.Spaces can also be entered by clicking on the SPACE box.You should be able to follow the program with the aid of these variables: c = Position counter within name z = Which zone was clicked on done = 1 if enter button pressed n$ = The entered name c$ = Character to add to n$ The two remaining boxes are Delete and Enter.The first removes the last chosen character,while Enter returns to the line which called the routine,with the name stored in the string n$.Since the name started off being 13 underline characters (-),lines 22260 to 22280 change these and remove unnecessary spaces at the end of the name. Instead of the chosen letter automatically appearing at the next position in the name,this alternative version of Program III allows us to pick up a character and place it wherever we wish. Before entering these lines type: DELETE 22160-22330 You can now add the new lines which give a more pleasing effect. 22000 REM Hiscore 3b 22160 done=0:WHILE done=0 22170 z=0:WHILE z=0:WHILE MOUSE KEY<>1:WEND:z=ZONE(0):WEND 22180 IF z<28 THEN CHANGE MOUSE 3+z:WHILE MOUSE KEY<>0:WEND:GOSUB 22260 22190 IF z=28 THEN done=1 22200 IF z=29 THEN n$=" ":LOCATE 14,10:PRINT n$ 22210 WEND 22220 FOR a=1 TO 13:IF MID$(n$,a,1)=" " THEN MID$(n$,a,1)=" " 22230 NEXT a 22240 IF RIGHT$(n$,1)=" " THEN n$=LEFT$(n$,LEN(n$)-1):GOTO 22240 22250 RETURN 22260 IF z=27 THEN c$="." ELSE c$=CHR$(64+z) 22270 WHILE MOUSE KEY=0:WEND 22280 IF MOUSE KEY=2 THEN CHANGE MOUSE 1:RETURN 22290 x=X MOUSE:y=Y MOUSE 22300 IF x<110 OR x>210 OR y<157 OR y>172 THEN 22260 22310 c=(x-100)/8:MID$(n$,c,1)=c$:LOCATE 14,10:PRINT n$ 22320 GOTO 22270 Whenever you click on a letter,the mouse pointer becomes that letter, which can be placed anywhere within the name.Line 22300 makes sure the mouse/letter is within range of the name grid and line 22310 calculates exactly where in the name the new letter should appear. The final program in this article only allows the player to enter his or her initials.This technique was widely used on arcade machines and many computer games back in the early º80s, but has now become quite rare.After examining the code you should be able to expand it to take 10 or more characters: 22000 REM Program IV 22010 MODE 0:KEY OFF 22020 CURS OFF:HIDE 22030 FOR a=1 TO 3 22040 BOX a*30+80,70 TO a*30+100,90 22050 NEXT 22060 n$="" 22070 FOR a=1 TO 3 22080 sp=1:done=0:WHILE done=0 22090 SPRITE 1,a*30+88,75,sp 22100 IF JOY=4 THEN DEC sp ELSE IF JOY=8 THEN INC sp 22110 IF JOY=0 THEN i$=INKEY$:s=SCANCODE 22120 IF s=75 THEN DEC sp:CLEAR KEY:ELSE IF s=77 THEN INC sp:CLEAR KEY 22130 IF sp<1 THEN sp=27 ELSE IF sp>27 THEN sp=1 22140 IF FIRE=-1 OR s=57 THEN done=1:PUT SPRITE 1:WAIT VBL:IF sp=27 THEN n$=n$+"." ELSE n$=n$+CHR$(64+sp) 22150 WAIT 5:WEND 22160 NEXT:SPRITE OFF:CLW:RETURN To enter a character either use the joystick or left/right cursor keys to scroll through the available letters.When you find the one you want,press the fire button or the spacebar. THE END!!