y.S • • • • • •v.v.va • • • ll I • • • • I 1 • * * • • • • • • • • • 108 • * • • • • • • •JLXt. * • • • m.M-». * .1 JLJLJt .1 t • • • » • * ' 1 1 1 l , r , * *-'-'. , T , .'.'.'r'- 1 H • • ".V .v. • • a a 1 .. . . ' . V . W • • • • • • I I • • • • • • • •' ll • • ••••• I • •••••••I ••••••II • • • • • • • II • • • • • * • I • ••••••II I • I I I I I I • • • ••••• •• •• • I• I ' • • • • • • • I • • • • • • • • • ••• •* ••••••• • I • • • • • • • • • • • • • • I I • • • « • * • • • .all • I • • if I • • • 11 • 11
Moving graphics Getting into the Movies What do you do when you want aliens and asteriods to burn around your TV screen? The PRINT AT on the <strong>ZX</strong>81 gives you one solution. You make an object move — or appear to move — on the TV screen by printing it in one position, holding the display for a moment, then unprinting the old position just as you reprint in a new position. The PRINT AT function on the <strong>ZX</strong>81, although it is slow, makes it easy to position an object just where you want it. For our first simple program, enter PROGRAM ONE and press RUN to see it in action. What you'll see, all being well, is a black square move down the screen diagonally. Note that you can 'chain' PRINT AT commands as in line 30. This program shows the simplest kind of moving graphics programming, and although far from satisfactory in terms of the result displayed on the screen, at least it should give you a clear idea of how to produce one kind of effect. Its main disadvantage is that the screen is blank while the computer is working out the new PRINT AT position for the black blob. As you can see from running this program, the RND function — when running the computer in SLOW — is particularly slow. As you begin to elaborate the program you're working on, and you want the computer to do more and more, you'll find this blank between subsequent PRINT ATs of the blob becomes intolerable. Try, for example, adding 35 LET Z = X' "Y and see how slow the program becomes. This can be overcome to some extent by assigning two other variables to the PRINT AT position which hold it for unprinting while the computer is working out new positions. Change the program so it reads as the listing for program two. Run this, and you'll see how much more satisfactory it is. If you want it to run forever, add 57 IF X greater than 19 THEN RUN. Now moving a blob down the screen in an irregular slide is not of much use to anyone. Enter and run program number three, which makes a ball bounce around the screen. Although this does not use a second set of variables to unprint, it is fairly satisfactory. You'll see that lines 70 and 80 check each time through the program to ensure that the ball has not 'hit the sides', and if it has done so, ensures that the variables which determine the next position of the ball (C and D) are changed. Program four does use new variables (E and F) to hold the 5 REM *MOUING GRAPHICS 5 REM *HOMING GRAPHIC PROGRAM ONE* PROGRAM TWO* 1® LET X = © 1 0 LET X=0 L.ET Y=0 20 LET Y=0 3 0 PRINT AT X..Y; ' AT X , Y ; *• * 25 LET A =X LET XsXfRND 27 L ET B =Y 5 0 LET Y=YfRND 3 0 PRINT AT X/Y;"B" 50 GOTO 30 4 0 LET X-X +RHD 5 0 LET Y=Y+RND 5 5 PRINT AT A,, 8; " " 60 GOTO 25 • Program one • Program two 20 LET A=UFiL "10*RND + 5 30 L E T B='vhL " A ^ R N D + S " 10 REM *BOUNCING BRLL -• • 1 * 40 LET' - A LEI A = 10*RNL>+5 45 LPT G-C +C 3 0 LET 6-P*RND+5 50 LFT D =C 4.0 L t£T C = 1 55 L^T E=R 5 3 LET D=1 56 L. fiT F s=B 5 0 PRINT f*T P . B; 0 60 PRINT RT R,B,"0' 7 0 Ir A 19 THEN LET C = - 70 IF R 19 THEN 3 0 IF 829 THEN LET D=- 60 IF B < G OR B>29 THEN 3 5 P R I N T RT A.. B.: " " 90 LET A = A 4-C 9 0 LET R=R*C 100 LET B=B+D 1 0 0 LET S = B + D 105 PRINT AT E F; ". " 1 10 GOTO 50 110 GOTO UAL "SS" Program three Program four o old position while the new one is being worked out, and these are used not as an 'unprint' but rather to print a trailing series of full stops, as you can see in diagram one. Run these programs a few times, and examine the listings, and you're sure to be able to work out ways to make them more effective. A second way of approaching the moving graphics problem is to use SCROLL to move the screen up a line, before reprinting what you need on the line it has just left. Enter and run program five, HAILSTORM, and you'll see how it works in practice. In this program, you are the black block, PRINT ATted in line 30. You control the position of the blob by using IN- KEY$, pressing on keys "8" (to move the blob right) and "5" (to move it left). The whole of line 80 ensures that the blob will not move off the screen, and saves valuable memory by placing the interpretation of both INKEY $ and checking the screen limits on either side, within one line. Line 990 prints LET LET D^- <strong>ZX</strong> COMPUTING SUMMER 1382 109