Chapter 8. Trying Out Some Programs

Having seen something of what the Electron can do, and having got used to typing on the keyboard, it's now your turn to tell the Electron to do things. Because the computer does exactly what it is told, remember to type in the examples given exactly as they appear in this chapter. You'll find that you can sometimes get away with adding an extra space here and there or leaving one out, but rather wait until you are more familiar with the Electron BASIC language before experimenting! After you have typed in each program, type

RUN  RETURN

to execute it. If the program doesn't appear to work, the computer may help you by displaying an error message, and telling you in which line the mistake appears. Press SHIFT and list the program as described in the chapter on editing. Make the necessary alterations, then run the program again.

Before you start, press the key marked BREAK on the keyboard. This will get the computer ready for you, and also start you off with an empty screen.

PERSIAN

This program produces a pattern by drawing hundreds of lines. Random colours are selected by lines 40 and 50. Line 60 moves the origin (middle) of the picture to the centre of the screen. The program stops after a while, so run it again to repeat the patterns.

 10 REM PERSIAN
 20 MODE 1
 30 D%=4
 40 VDU 19,2,RND(3)+1,0,0,0
 50 VDU 19,3,RND(3)+4,0,0,0
 60 VDU 29,640;512;
 70 J1%=0
 80 FOR K%=500 TO 380 STEP -40
 90 REPEAT J2%=RND(2): UNTIL J2%<> J1%
100 J1%=J2%
110 GCOL 3,J1%
120 FOR I%=-K% TO K% STEP D%
130 MOVE K%,I%
140 DRAW -K%,-I%
150 MOVE I%,-K%
160 DRAW -I%,K%
170 NEXT
180 NEXT

POLYGON

This program draws polygons (many sided shapes) in random colours.

Lines 90 to 150 select a random place on the screen which will be the centre (origin) of the next shape.

Lines 170 to 250 calculate the X and Y coordinates of each corner of the polygon and store the values in two 'arrays' for future use.

Lines 220 and 160 fill the shape with black triangles which make it appear as if the new polygon is in front of the older ones.

Lines 260 to 310 draw all the lines that make up the polygon.

Lines 30 to 50 set the actual colours of logical colours 1, 2 and 3 to red, blue and yellow. You can change these to use other colours if you like.

Unlike the PERSIAN program, this one carries on until you stop it yourself. To do this, either press SHIFT or BREAK.

 10 REM POLYGON
 20 MODE 1
 30 VDU 19,1,1,0,0,0
 40 VDU 19,2,4,0,0,0
 50 VDU 19,3,3,0,0,0
 60 DIM X(10)
 70 DIM Y(10)
 80 FOR C=1 TO 2500
 90 xorigin=RND(1200)
100 yorigin=RND(1000)
110 VDU 29,xorigin;yorigin;
120 radius=RND(300)+50
130 sides=RND(8)+2
140 MOVE radius,0
150 MOVE 10,10
160 GCOL 0,0
170 FOR SIDE=1 TO sides
180 angle=(SIDE-1)*2*PI/sides
190 X(SIDE)=radius*COS(angle)
200 Y(SIDE)=radius*SIN(angle)
210 MOVE 0,0
220 PLOT 85,X(SIDE),Y(SIDE)
230 NEXT SIDE
240 MOVE 0,0
250 PLOT 85,radius,0
260 GCOL 0,RND(3)
270 FOR SIDE=1 TO sides
280 FOR line=SIDE TO sides
290 MOVE X(SIDE),Y(SIDE)
300 DRAW X(line),Y(line)
310 NEXT line
320 NEXT SIDE
330 NEXT C

DRAW

This program is a simpler version of the SKETCH program on the Introductory Cassette. The main part of the program is between lines 20 and 220, and two procedures are called from here.

Lines 240 to 290 print the instructions at the bottom of the screen.

Lines 320 to 360 limit the graphics area you can draw in, and contain the DRAW instruction.

Lines 130 to 200 define which keys on the keyboard are 'drawing' keys, and set the values for X and Y for each key. These values are used later on by line 360.

Note that line 40 turns the cursor off, so when you've finished drawing masterpieces, press SHIFT and type:

VDU 23,1,1;0;0;0;  RETURN

Alternatively, just press BREAK.

 10 REM BREAK
 20 MODE1
 30 PROCKEY
 40 VDU 23,1,0;0;0;0;
 50 GCOL 0,1
 60 VDU 19,1,2,0,0,0
 90 X=640
100 Y=512
110 MOVE X,Y:DRAW X,Y
120 REPEAT
130 L=INKEY(-98)
140 R=INKEY(-67)
150 U=INKEY(-73)
160 D=INKEY(-105)
170 IF L=-1 THEN X=X-4:PROCDRAW(X,Y)
180 IF R=-1 THEN X=X+4:PROCDRAW(X,Y)
190 IF U=-1 THEN Y=Y-4:PROCDRAW(X,Y)
200 IF D=-1 THEN Y=Y+4:PROCDRAW(X,Y)
210 UNTIL FALSE
220 END
230 DEF PROCKEY
240 PRINT TAB(0,25) "Your Drawing keys are:"
250 PRINT TAB(0,26) "Z or z = Left"
260 PRINT TAB(0,27) "X or x = Right"
270 PRINT TAB(0,28) "* or : = Up"
280 PRINT TAB(0,29) "? or / = Down"
290 PRINT TAB(0,30) "Press two keys for a diagonal"
300 ENDPROC
310 DEF PROCDRAW(X,Y)
320 IF X<0 THEN X=0
330 IF X>1279 THEN X=1279
340 IF Y<250 THEN Y=250
350 IF Y>1023 THEN Y=1023
360 DRAW X,Y
370 ENDPROC