Sinclair ZX Spectrum BASIC programming

Embed Size (px)

Citation preview

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    1/116

    SINCLAIR ZX SPECTRUMBASIC Programming

    By Steven VickersEdited by Robin Brabeer

    Converted to electronic text byC!ris "#en

    (Internet email: co$nvg%&nit%no)

    _______________________________________________

    Original edition published in 1982-83by Sinclair Research Limited

    This version published in 199by !hris O"en on behal# o# $mstrad plc

    !over illustration by%ohn &arris o# 'oung $rtists

    ________________________________________________

    ' Amstra ()c *++, - a)) rig!ts reserve

    ________________________________________________

    This manual is freely distributable but maynot be distributed without this notice.

    C"NTENTS

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    2/116

    Chapter 1: Intro&ction $ guide to the () Spectrum *eyboard and a description o# the display+

    Chapter 2: Basic (rogramming conce(ts ,rograms line numbers editing programs usingandEDITRUNLIST

    GO TO ONTINUEIN!UTNE"RE#!RINTSTO! inIN!UTdata

    $RE%&

    Chapter 3: .ecisions I'STO!, =,

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    3/116

    Chapter 1#: Co)o&rs IN&!%!ER 'L%S$RIGTIN+ERSE O+ER$ORDER

    Chapter 1$: 4ra(!ics !LOTDR%" IRLE!OINT

    Chapter 1%: Motion $nimated graphics using!%USEIN&E2*and!EE&

    Chapter 1&: BEEP The sound capabilities o# the () Spectrum using$EE!

    Chapter 2': Ta(e Storage &o" to store your programs on cassette tape5S%+ELO%D +ERI'2#ERGE

    Chapter 21: T!e ZX Printer

    LLISTL!RINT O!2

    Chapter 22: "t!er e5&i(ment !onnecting the () Spectrum to other machines and devices+

    Chapter 23: IN an "UT .nputOutput ports and their uses5IN OUT

    Chapter 2!: T!e memor3 $ loo* at the internal "or*ings o# the () Spectrum5 LE%R

    Chapter 2": T!e s3stem variab)es

    Chapter 2#: Using mac!ine coe .ntroducing USR"ith a numeric argument

    *++E-I. *: T!e c!aracter set

    *++E-I. B: Re(orts

    *++E-I. C: A escri(tion o2 t!e ZX S(ectr&m 2or re2erence

    *++E-I. -: E0am()e (rograms

    *++E-I. E: Binar3 an !e0aecima)

    C6APTER *

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    4/116

    Introduction

    /hether yo0 read the Introd0ctory boo irt, or came trai4ht here, yo0 ho0ld be a5are that command are obeyed

    trai4ht a5ay, and intr0ction be4in 5ith a line n0mber and are tored a5ay or later 6o0 ho0ld alo be a5are o

    the command: +7I8, 9E8, and I+8 (5hich can be 0ed on all machine that 0e B*;IC), and B7-E7,

    +*+E7 and BEE+ (5hich are 0ed on the ;pectr0m)

    8hi B*;IC man0al tart by repeatin4 ome thin4 4iven in the introd0ctory boolet, b0t in m0ch more detail,

    tellin4 yo0 exactly 5hat yo0 can and cannot do 6o0 5ill alo ind ome exercie at the end o each chapter -ont

    i4nore thee> many o them ill0trate point that are hinted at in the text 9oo thro04h them, and do any that interet

    yo0, or that eem to cover 4ro0nd that yo0 dont 0ndertand properly

    /hatever ele yo0 do, eep 0in4 the comp0ter I yo0 have the ?0etion @5hat doe it do i I tell it 0ch and 0chA@

    then the an5er i eay: type it in and ee /henever the man0al tell yo0 to type omethin4 in, al5ay a yo0rel,

    @5hat co0ld I type inteadA@, and try o0t yo0r replie 8he more o yo0r o5n pro4ram yo0 5rite, the better yo0 5ill

    0ndertand the comp0ter

    *t the end o thi pro4rammin4 man0al are ome appendice 8hee incl0de ection on the 5ay the memory i

    or4anied, ho5 the comp0ter manip0late n0mber, and a erie o example pro4ram ill0tratin4 the po5er o the .;pectr0m

    T!e ke3boar

    . ;pectr0m character comprie not only the in4lesymbols(letter, di4it, etc), b0t alo the compo0nd to*ens

    (ey5ord, 0nction name, etc) and all thee are entered rom the eyboard rather than bein4 pelled o0t 8o obtain

    all thee 0nction and command, ome ey have ive or more

    ditinct meanin4, 4iven partly by hitin4 the ey (ie prein4 either the C*+; ;ID8 ey or the ;6B9

    ;ID8 ey at the ame time a the re?0ired one) and partly by havin4 the machine in dierent mode

    8he mode i indicated by the cursor, a lahin4 letter that ho5 5here the next character rom the eyboard 5ill beinerted:

    7(or ey5ord) mode a0tomatically replace Lmode 5hen the machine i expectin4 a command orpro4ram line (rather than I+8 data), and rom it poition on the line it no5 it ho0ld expect a line n0mber or a

    ey5ord 8hi i at the be4innin4 o the line, or F0t ater 8E, or F0t ater: (except in a trin4) I 0nhited, the

    next ey 5ill be interpreted a either a ey5ord (5ritten on the ey), or a di4it

    L(or letter) mode normally occ0r at all other time I 0nhited, the next ey 5ill be interpreted a themain ymbol on that ey, in lo5er cae or letter

    In both 7and Lmode, ;6B9 ;ID8 and a ey 5ill be interpreted a the 0bidiary red character onthe ey and C*+; ;ID8 5ith a di4it ey 5ill be interpreted a the control 0nction 5ritten in 5hite above the ey

    C*+; ;ID8 5ith other ey doe not aect the ey5ord in 7mode, and in L mode it convert lo5er cae tocapital

    C(or capital) mode i a variant o Lmode in 5hich all letter appear a capital C*+; 9CG ca0e achan4e rom Lmode to Cmode or bac a4ain

    E(or extended) mode i 0ed or obtainin4 0rther character, motly toen It occ0r ater both hit eyare preed to4ether, and lat or one ey depreion only In thi mode, a letter 4ive one character or toen (ho5n

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    5/116

    in 4reen above it) i 0nhited, and another (ho5n in red belo5 it) i preed 5ith either hit * di4it ey 4ive a

    toen i preed 5ith ;6B9 ;ID8> other5ie it 4ive a colo0r control e?0ence

    4(or 4raphic) mode occ0r ater H7*+IC; (C*+; ;ID8 and &) i preed, and lat 0ntil it i preeda4ain or & i preed on it o5n * di4it ey 5ill 4ive a moaic 4raphic, ?0it H7*+IC; or -E9E8E, and each o

    the letter ey apart rom , /, ., 6 and , 5ill 4ive a 0erJdeined 4raphic

    I any ey i held do5n or more than abo0t 2 or 3 econd, it 5ill tart a0toJrepeatin4

    Geyboard inp0t appear in the bottom hal o the creen a it i typed, each character (in4le ymbol or compo0nd

    toen) bein4 inerted F0t beore the c0ror 8he c0ror can be moved let 5ith C*+; ;ID8 and ", or ri4ht 5ith

    C*+; ;ID8 and % 8he character beore the c0ror can be deleted 5ith -E9E8E (C*+; ;ID8 and &) (ote: the

    5hole line can be deleted by typin4 E-I8 (C*+; ;ID8 and 1) ollo5ed by E8E7)

    /hen E8E7 i preed, the line i exec0ted, entered into the pro4ram, or 0ed a I+8 data a appropriate,

    0nle it contain a yntax error In thi cae a lahin4 8appear next to the error

    * pro4ram line are entered, a litin4 i diplayed in the top hal o the creen 8he lat line entered i called the

    c0rrent line and i indicated by the ymbol 9> thi can be moved by 0in4 the ey(C*+; ;ID8 and #) and

    (C*+; ;ID8 and $) I E-I8 (C*+; ;ID8 and 1) i preed, the c0rrent line i bro04ht do5n to the bottom parto the creen and can be edited

    /hen a command i exec0ted or a pro4ram r0n, o0tp0t i diplayed in the top hal o the creen and remain 0ntil a

    pro4ram line i entered, or E8E7 i preed 5ith an empty line, orori preed In the bottom part appear areport 4ivin4 a code (di4it or letter) reerred to in$ppendi0 6%8he report remain on the creen 0ntil a ey i preed

    (and indicate 7mode)

    In certain circ0mtance, C*+; ;ID8 5ith the ;+*CE ey act a a B7E*G, toppin4 the comp0ter 5ith report -

    or 9 8hi i reco4nied

    (i) at the end o a tatement 5hile a pro4ram i r0nnin4, or

    (ii) 5hile the comp0ter i 0in4 the caette recorder or printer

    T!e te)evision screen

    8hi ha 2! line, each 32 character lon4, and i divided into t5o part 8he top part i at mot 22 line and diplay

    either a litin4 or pro4ram o0tp0t /hen printin4 in the top part ha reached the bottom, it all croll 0p one line> i

    thi 5o0ld involve loin4 a line that yo0 have not had a chance to ee yet, then the comp0ter top 5ith the mea4e

    crollA +rein4 the ey , ;+*CE or ;8+ 5ill mae the pro4ram top 5ith report . BREA7 - C"NT re(eats>

    any other ey 5ill let the crollin4 contin0e 8he bottom part i 0ed or inp0ttin4 command, pro4ram line, and

    I+8 data, and alo or diplayin4 report 8he bottom part tart o a t5o line (the 0pper one blan), b0t it

    expand to accommodate 5hatever i typed in /hen it reache the c0rrent print poition in the top hal, 0rther

    expanion 5ill mae the top hal croll 0p

    C6APTER :6asic ,rogramming !oncepts

    S&mmar3

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    6/116

    Programs

    Line n&mbers

    Eiting (rograms &sing,/ an E.IT RUN/ LIST

    4" T"/ C"NTINUE/ INPUT/ NE;/ REM/ PRINT

    ST"P in INPUT ata

    BREA7

    8ype in thee t5o line o a comp0ter pro4ram to print o0t the 0m o t5o n0mber:

    20 PRINT a10 LET a=10

    o that the creen loo lie thi:

    * yo0 already no5, beca0e thee line be4an 5ith n0mber, they 5ere not obeyed immediately b0t tored a5ay,

    a pro4ram line 6o0 5ill alo have noticed here that the line n0mber 4overn the order o the line 5ithin the

    pro4ram: thi i mot important 5hen the pro4ram i r0n, b0t it i alo relected in the order o the line in the litin4

    that yo0 can ee on the creen no5

    ;o ar yo0 have only entered one n0mber, o type

    15 LET b=15

    and in it 4oe It 5o0ld have been impoible to inert thi line bet5een the irt t5o i they had been n0mbered 1 and

    2 intead o 1' and 2' (line n0mber m0t be 5hole n0mber bet5een 1 and &&&&), o that i 5hy, 5hen irt typin4

    in a pro4ram, it i 4ood practice to leave 4ap bet5een the line n0mber

    o5 yo0 need to chan4e line 2' to

    20 PRINT a+b

    6o0 co0ld type o0t the replacement in 0ll, b0t it i eaier to 0e the E-I8 acility decribed in the introd0ctory

    boolet 8he >by line 1" i called the pro4ram c0ror, and the line it point to i the c0rrent line 8hi i 00ally thelat line that yo0 entered, b0t yo0 can 0e theorey to move the pro4ram c0ror do5n or 0p (8ry it, leavin4the pro4ram c0ror event0ally at line 2')

    /hen yo0 pre the E-I8 ey, then a copy o the c0rrent line 5ill be diplayed at the bottom o the creen J in yo0r

    cae, a copy o line 2' old do5n theey 0ntil theLc0ror move to the end o the line, and then type

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    7/116

    +b (5itho0t E8E7)

    8he line at the bottom ho0ld no5 read

    20 PRINT a+b

    8ype E8E7 and it 5ill replace the old line 2', o that the creen loo lie thi:

    70n thi pro4ram 0in4 7 and E8E7 and the 0m 5ill be diplayed

    70n the pro4ram a4ain and then type

    PRINT a, b

    8he variable are till there, even tho04h the pro4ram ha inihed

    8here i a 0e0l method 0in4 E-I8 to 4et rid o the bottom part o the creen 8ype in a load o r0bbih (5itho0t

    E8E7) and then decide that yo0 dont 5ant it ater all ne 5ay to delete it i to hold the -E9E8E ey do5n 0ntil

    the line i 4one> b0t another 5ay i a ollo5 I yo0 pre E-I8, the r0bbih at the bottom o the creen 5ill be

    replaced by a copy o the c0rrent line I yo0 no5 pre E8E7, the c0rrent line 5ill be p0t bac in the pro4ram0naltered, leavin4 the bottom part o the creen clear

    I yo0 enter a line by mitae, ay

    12 LET b=8

    it 5ill 4o 0p into the pro4ram and yo0 5ill realie yo0r mitae 8o delete thi 0nneceary line, type

    12 (5ith E8E7 o co0re)

    6o0 5ill notice 5ith 0rprie that the pro4ram c0ror ha 4one 6o0 ho0ld ima4ine it a bein4 hidden in bet5een

    line 1' and 1", o i yo0 preit 5ill move 0p to line 1', 5hile i yo0 preit 5ill move do5n to line 1"

    8ype

    12 (and E8E7)

    *4ain, the pro4ram c0ror 5ill be hidden bet5een line 1' and 1" o5 pre E-I8 and line 1" 5ill come do5n:

    5hen the pro4ram c0ror i hidden bet5een t5o line, E-I8 brin4 do5n the next line ollo5in4 the ne5 line

    n0mber 8ype E8E7 to clear the bottom part o the creen

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    8/116

    o5 type

    30 (and E8E7)

    8hi time, the pro4ram c0ror i hidden ater the end o the pro4ram: and i yo0 pre E-I8, then line 2' 5ill be

    bro04ht do5n

    9atly, type

    LIST 15

    6o0 5ill no5 ee on the creen

    15 LET b=1520 PRINT a+b

    9ine 1' ha vanihed rom the creen b0t it i till in yo0r pro4ram J 5hich yo0 can prove by prein4 E8E7 8he

    only eect o 9I;8 1" are to prod0ce a litin4 that tart at line 1" and to p0t the pro4ram c0ror at line 1" I yo0

    have a very lon4 pro4ram, then 9I;8 5ill probably be a more 0e0l 5ay o movin4 the pro4ram c0ror thanand

    8hi ill0trate another 0e o line n0mber: they act a name or the pro4ram line o that yo0 can reer to them,

    rather lie the 5ay in 5hich variable have name

    9I;8 on it o5n mae the litin4 tart at the be4innin4 o a pro4ram

    *nother command een in the introd0ctory boolet i:

    NEW

    8hi erae any old pro4ram and variable in the comp0ter o5 care0lly type in thi pro4ram, 5hich chan4e

    Dahrenheit temperat0re to Centi4rade

    10 REM temperature co!er"#o20 PRINT $%e& '$, $%e& ($30 PRINT)0 INP*T $Eter %e& '$, '50 PRINT ','32-.5/0 T )0

    6o0 5ill need to type the 5ord in line 1' *lo, altho04h H 8 ha a pace in it, it i really all one ey5ord (on

    H)

    o5 r0n it 6o0 5ill ee the headin4 printed on the creen by line 2', b0t 5hat happened to line 1'A *pparently

    the comp0ter ha completely i4nored it /ell, it ha 7E in line 1' tand or remar, or reminder, and i there

    olely to remind yo0 o 5hat the pro4ram doe * 7E command conit o 7E ollo5ed by anythin4 yo0 lie,

    and the comp0ter 5ill i4nore it ri4ht 0p to the end o the line

    By no5, the comp0ter ha 4ot to the I+8 command on line !' and i 5aitin4 or yo0 to type in a val0e or the

    variable D J yo0 can tell thi beca0e 5here yo0 mi4ht have expected a 7c0ror there i intead an Lc0ror Enter a

    n0mber> remember E8E7 o5 the comp0ter ha diplayed the re0lt and i 5aitin4 or another n0mber 8hi i

    beca0e o line #', H 8 !', 5hich mean exactly 5hat it ay Intead o r0nnin4 o0t o pro4ram and toppin4, the

    comp0ter F0mp bac to line !' and tart a4ain ;o, enter another temperat0re *ter a e5 more o thee yo0 mi4ht

    be 5onderin4 i the machine 5ill ever 4et bored 5ith thi, it 5ont ext time it a or another n0mber, type ;8+

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    9/116

    8he comp0ter come bac 5ith a report 4 STP # INP*T # #e )061, 5hich tell yo0 5hy it topped, and 5here(in the irt command o line !')

    I yo0 5ant to contin0e the pro4ram type

    (NTIN*E

    and the comp0ter 5ill a yo0 or another n0mber

    /hen C8IE i 0ed the comp0ter remember the line n0mber in the lat report that it ent yo0, a lon4 a it

    5a not0 7, and F0mp bac to that line: in o0r cae, thi involve F0mpin4 to line !', the I+8 command

    7eplace line #' by H 8 31 J it 5ill mae no perceptible dierence to the r0nnin4 o the pro4ram I the line

    n0mber in a H 8 command reer to a nonJexitent line, then the F0mp i to the next line ater the 4iven n0mber

    8he ame 4oe or 7> in act 7 on it o5n act0ally mean 7 '

    o5 type in n0mber 0ntil the creen tart 4ettin4 0ll /hen it i 0ll, the comp0ter 5ill move the 5hole o the top

    hal o the creen 0p one line to mae room, loin4 the headin4 o the top 8hi i called crollin4

    /hen yo0 are tired o thi, top the pro4ram 0in4 ;8+ and 4et the litin4 by prein4 E8E7

    9oo at the +7I8 tatement on line "' 8he p0nct0ation in thi J the comma (,) i very important, and yo0 ho0ld

    remember that it ollo5 m0ch more deinite r0le than the p0nct0ation in En4lih

    Comma are 0ed to mae the printin4 tart either at the let hand mar4in, or in the middle o the creen, dependin4

    on 5hich come next 8h0 in line "', the comma ca0e the centi4rade temperat0re to be printed in the middle o the

    line /ith a emicolon, on the other hand, the next n0mber or trin4 i printed immediately ater the precedin4 one

    6o0 can ee thi in line "', i the comma i replaced by a emicolon

    *nother p0nct0ation mar yo0 can 0e lie thi in +7I8 command i the apotrophe () 8hi mae 5hatever i

    printed next appear at the be4innin4 o the next line on the creen b0t thi happen any5ay at the end o each +7I8

    command, o yo0 5ill not need the apotrophe very m0ch 8hi i 5hy the +7I8 command in line "' al5ay tart

    it printin4 on a ne5 line, and it i alo 5hy the +7I8 command in line 3' prod0ce a blan line

    I yo0 5ant to inhibit thi, o that ater one +7I8 command the next one carrie on on the ame line, yo0 can p0t a

    comma or emicolon at the end o the irt 8o ee ho5 thi 5or, replace line "' in t0rn by each o

    50 PRINT ',50 PRINT '

    and

    50 PRINT '

    and r0n each verion J or 4ood mea0re yo0 co0ld alo try

    50 PRINT '9

    8he one 5ith the comma pread everythin4 o0t in t5o col0mn, that 5ith the emicolon cram everythin4 to4ether,

    that 5itho0t either allo5 a line or each n0mber and o doe that 5ith the apotrophe J the apotrophe 4ive a ne5

    line o it o5n, b0t inhibit the a0tomatic one

    7emember the dierence bet5een comma and emicolon in +7I8 command> alo, do not con0e them 5ith the

    colon (:) that are 0ed to eparate command in a in4le line

    o5 type in thee extra line:

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    10/116

    100 REM t:#" po#te pro&ram remember" ;our ame110 INP*T here H 8 5o0ld be neceary

    and 7 co0ld be diatro0, o it i better not to 4et into the habit o a0tomatically typin4 7 to r0n a pro4ram

    *nother dierence i that yo0 can type 7 5itho0t a line n0mber, and it tart o at the irt line in the pro4ram

    H 8 m0t al5ay have a line n0mber

    Both thee pro4ram topped beca0e yo0 typed ;8+ in the inp0t line> ometime J by mitae J yo0 5rite a

    pro4ram that yo0 cant top and 5ont top itel 8ype

    200 T 200R*N 200

    8hi loo all et to 4o on or ever 0nle yo0 p0ll the pl04 o0t> b0t there i a le dratic remedy +re C*+;

    ;ID8 5ith the ;+*CE ey, 5hich ha B7E*G 5ritten above it 8he pro4ram 5ill top, ayin4 L RE?7 #topro&ram

    *t the end o every tatement, the pro4ram loo to ee i thee ey are preed> and i they are, then it top 8he

    B7E*G ey can alo be 0ed 5hen yo0 are in the middle o 0in4 the caette recorder or the printer, or vario0

    other bit o machinery that yo0 can attach to the comp0ter J F0t in cae the comp0ter i 5aitin4 or them to do

    omethin4 b0t theyre not doin4 it

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    11/116

    In thee cae there i a dierent report, @ RE?7 (NT repeat" C8IE in thi cae (and in act in motother cae too) repeat the tatement 5here the pro4ram 5a topped> b0t ater the report L RE?7 #topro&ram, C8IE carrie trai4ht on 5ith the next tatement ater allo5in4 or any F0mp to be made

    70n the name pro4ram a4ain and 5hen it a yo0 or inp0t type

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    12/116

    23 REM

    and yo0 4et line 2 to 23 on the creen> type

    28 REM

    and yo0 4et line $ to 2% (In both cae, by typin4 a ne5 line, yo0 have moved the pro4ram c0ror o that a ne5

    litin4 ha been made)

    aybe thi loo a little arbitrary to yo0 It i act0ally tryin4 to 4ive yo0 exactly 5hat yo0 5ant, altho04h, h0man

    bein4 0npredictable creat0re, it doent al5ay 40e ri4ht

    8he comp0ter eep a record not only o the c0rrent line, the one that ha to appear on the creen, b0t alo the top

    line on the creen /hen it trie to mae a litin4, the irt thin4 it doe i compare the top line to the c0rrent line I

    the top line come ater, then there i no point in tartin4 there, o it 0e the c0rrent line or a ne5 top line and mae

    it litin4 ther5ie, it method i to tart main4 the litin4 rom the top line, and carry on 0ntil it ha lited the

    c0rrent line, crollin4 i neceary

    o5ever, it irt doe a ro04h calc0lation to ee ho5 lon4 thi 5o0ld tae, and i the an5er i m0ch too lon4, thenit move the top line do5n to be a lot cloer to the c0rrent line o5, havin4 5ored o0t it top line, it tart litin4

    rom there I, 5hen it reache the end o the pro4ram or the bottom o the creen, the c0rrent line ha been lited,

    then it top ther5ie, it croll 0ntil the c0rrent line i on the creen, and or each extra line that it lit it move the

    top line do5n one o that the top line drit into the nei4hbo0rhood o the c0rrent line

    Experiment 5ith movin4 the c0rrent line abo0t by typin4

    line n0mberREM

    9I;8 move the c0rrent line b0t not the top line, o 0be?0ent litin4 mi4ht be dierent Dor intance, type

    LIST

    to 4et the 9I;8 litin4 and then pre E8E7 a4ain to mae line ' the top line 6o0 ho0ld have line 1 to 22 on the

    creen 8ype

    LIST 22

    5hich 4ive yo0 line 22 to !3> 5hen yo0 pre E8E7 a4ain, yo0 4et bac line 1 to 22 8hi tend to be more

    0e0l or hort pro4ram than or lon4 one

    in4 the pro4ram 0ll o 7E above, type

    LIST

    and then n 5hen it a yo0 "croC o5 type

    (NTIN*E

    C8IE i a bit ?0iry here, beca0e the bottom part o the creen 4oe blan> b0t yo0 can retore normality

    5ith B7E*G 8he reaon i that 9I;8 5a the irt command in the line, o C8IE repeat thi command

    nort0nately, the irt command in the line i no5 C8IE itel o the comp0ter F0t it there doin4

    C8IE over and over a4ain 0ntil yo0 top it

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    13/116

    6o0 can vary thi by replacin4 9I;8 5ith

    6 LIST

    or 5hich C8IE 4ive ' G (beca0e C8IE F0mp to the econd command in the line, 5hich i taen to

    be it end) or

    66 LIST

    or 5hich C8IE 4ive N Statemet o"t(beca0e C8IE F0mp to the third command in the line,5hich no lon4er exit)

    6o0 have no5 een the tatement +7I8, 9E8, I+8, 7, 9I;8, H 8, C8IE, E/ and 7E, and

    they can all be 0ed either a direct command or in pro4ram line J thi i tr0e o almot all command in .

    ;pectr0m B*;IC 7, 9I;8, C8IE and E/ are not 00ally o m0ch 0e in a pro4ram, b0t they can be

    0ed

    E0ercises

    1 +0t a 9I;8 tatement in a pro4ram, o that 5hen yo0 r0n it, it lit itel

    2 /rite a pro4ram to inp0t price and print o0t the tax d0e (at 1" per cent) +0t in +7I8 tatement o that the

    comp0ter anno0nce 5hat it i 4oin4 to do, and a or the inp0t price 5ith extrava4ant

    politene odiy the pro4ram o that yo0 can alo inp0t the tax rate (to allo5 or Lero ratin4 or 0t0re chan4e)

    3 /rite a pro4ram to print a r0nnin4 total o n0mber yo0 inp0t (;044etion: have t5o variable called totalJ et to

    ' to be4in 5ith J and item Inp0t item, add it to total, print them both, and 4o ro0nd a4ain)

    ! /hat 5o0ld C8IE and E/ do in a pro4ramA Can yo0 thin o any 0e at all or thiA

    C6APTER a T4EN PRINT $T:at #" too b#&, tr; a&a#$0 T 30

    6o0 can ee that an ID tatement tae the orm

    I'condition T4EN F F F

    5here the tand or a e?0ence o command, eparated by colon in the 00al 5ay 8he condition i omethin4

    that i 4oin4 to be 5ored o0t a either tr0e or ale: i it come o0t a tr0e then the tatement in the ret o the line

    ater 8E are exec0ted, b0t other5ie they are ipped over, and the pro4ram exec0te the next intr0ction

    8he implet condition compare t5o n0mber or t5o trin4: they can tet 5hether t5o n0mber are e?0al or

    5hether one i bi44er than the other> and they can tet 5hether t5o trin4 are e?0al, or (ro04hly) one come beore

    the other in alphabetical order 8hey 0e the relation =,

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    15/116

    athematician 00ally 5rite I'condition T4ENline n0mber

    8hi mean the ame a

    I'condition T4EN Tline n0mber

    E0ercises

    1 8ry thi pro4ram:

    10 PRINT $G$6 STP6 PRINT A

    /hen yo0 r0n it, it 5ill diplay x and top 5ith report STP "tatemet, 1062 o5 type

    (NTIN*E

    6o0 mi4ht expect thi to F0mp bac to the ;8+ command J C8IE 00ally repeat the tatement reerred to in

    the report o5ever, here thi 5o0ld not be very 0e0l, beca0e the comp0ter 5o0ld F0t top a4ain 5itho0t

    diplayin4 y 8hereore, thin4 are arran4ed o that ater report & C8IE F0mp to the command ater the ;8+

    command J o in o0r example, ater C8IE, the comp0ter print and reache the end o the pro4ram

    C6APTER ?

    LoopingS&mmar3

    1"R/ NEXT

    T"/ STEP

    ;0ppoe yo0 5ant to inp0t ive n0mber and add them to4ether ne 5ay (dont type thi in 0nle yo0 are eelin4

    d0ti0l) i to 5rite

    10 LET tota=020 INP*T a

    30 LET tota=tota+a)0 INP*T a50 LET tota=tota+a0 INP*T a0 LET tota=tota+a80 INP*T a0 LET tota=tota+a100 INP*T a110 LET tota=tota+a

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    16/116

    120 PRINT tota

    8hi method i not 4ood pro4rammin4 practice It may be F0t abo0t controllable or ive n0mber, b0t yo0 can

    ima4ine ho5 tedio0 a pro4ram lie thi to add ten n0mber 5o0ld be, and to add a h0ndred 5o0ld be F0t

    impoible

    0ch better i to et 0p a variable to co0nt 0p to " and then top the pro4ram, lie thi (5hich yo0 ho0ld type in):

    10 LET tota=020 LET cout=130 INP*T a)0 REM cout=umber oB t#me" t:at a :a" bee #put "o Bar50 LET tota=tota+a0 LET cout=cout+10 I' coutD=5 T4EN T 3080 PRINT tota

    otice ho5 eay it 5o0ld be to chan4e line $' o that thi pro4ram add ten n0mber, or even a h0ndred

    8hi ort o co0ntin4 i o 0e0l that there are t5o pecial command to mae it eaier: the D7 command and theE.8 command 8hey are al5ay 0ed to4ether in4 thee, the pro4ram yo0 have F0t typed in doe exactly the

    ame a

    10 LET tota=020 'R c=1 T530 INP*T a)0 REM c=umber oB t#me" t:at a :a" bee #put "o Bar50 LET tota=tota+a0 NEHT c80 PRINT tota

    (8o 4et thi pro4ram rom the previo0 one, yo0 F0t have to edit line 2', !', #', and $' 8 i ;6B9 ;ID8

    5ith D)

    ote that 5e have chan4ed co0nt to c 8he co0ntin4 variable J or control variable o a D7 J E.8 loop m0t have

    a in4le letter or it name

    8he eect o thi pro4ram i that c r0n thro04h the val0e 1 (the initial val0e), 2, 3, ! and " (the limit), and or each

    one, line 3', !' and "' are exec0ted 8hen, 5hen c ha inihed it ive val0e, line %' i exec0ted

    *n extra 0btlety to thi i that the control variable doe not have to 4o 0p by 1 each time: yo0 can chan4e thi 1 to

    anythin4 yo0 lie by 0in4 a ;8E+ part in the D7 command 8he mot 4eneral orm or a D7 command i

    'Rcontrol variable = initial val0e Tlimit STEPtep

    5here the control variable i a in4le letter, and the initial val0e, limit and tep are all thin4 that the comp0ter can

    calc0late a n0mber J lie the act0al n0mber themelve, or 0m, or the name o

    n0meric variable ;o, i yo0 replace line 2' in the pro4ram by

    20 'R c=1 T 5 STEP 3/2

    then c 5ill r0n thro04h the val0e 1, 2" and ! otice that yo0 dont have to retrict yo0rel to 5hole n0mber, and

    alo that the control val0e doe not have to hit the limit exactly J it carrie on loopin4 a

    lon4 a it i le than or e?0al to the limit

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    17/116

    8ry thi pro4ram, to print o0t the n0mber rom 1 to 1' in revere order

    10 'R =10 T 1 STEP 120 PRINT 30 NEHT

    /e aid beore that the pro4ram carrie on loopin4 a lon4 a the control variable i le than or e?0al to the limit I

    yo0 5or o0t 5hat thi 5o0ld mean in thi cae yo0 5ill ee that it 4ive nonene 8he normal r0le ha to be

    modiied: 5hen the tep i ne4ative, the pro4ram carrie on loopin4 a lon4 a the control variable i 4reater than or

    e?0al to the limit

    6o0 m0t be care0l i yo0 are r0nnin4 t5o D7 J E.8 loop to4ether, one inide the other 8ry thi pro4ram,

    5hich print o0t the n0mber or a complete et o ix pot dominoe

    10 'R m=0 T O 20 'R =0 T m O O 30 PRINT m$6$$ $ O nJloop O mJloop

    )0 NEHT O O

    50 PRINT O0 NEHT m O

    6o0 can ee that the nJloop i entirely inide the mJloop J they are properly neted /hat m0t be avoided i havin4

    t5o D7 J E.8 loop that overlap 5itho0t either bein4 entirely inide the other, lie thi:

    5 REM t:#" pro&ram #" ro&10 'R m=0 T O20 'R =0 T m O mJloop O30 PRINT m$6$$ $ O O)0 NEHT m O O nJloop50 PRINT O0 NEHT O

    85o D7 J E.8 loop m0t either be one inide the other, or be completely eparate

    *nother thin4 to avoid i F0mpin4 into the middle o a D7 J E.8 loop rom the o0tide 8he control variable i

    only et 0p properly 5hen it D7 tatement i exec0ted, and i yo0 mi thi o0t the E.8 tatement 5ill con0e

    the comp0ter 6o0 5ill probably 4et an error report ayin4 NEHT #t:out 'Ror !ar#abe ot Bou%

    8here i nothin4 5hatever to top yo0 0in4 D7 and E.8 in a direct command Dor example, try:

    'R m=0 T 106 PRINT m6 NEHT m

    6o0 can ometime 0e thi a a (ome5hat artiicial) 5ay o 4ettin4 ro0nd the retriction that yo0 cannot H 8

    any5here inide a command J beca0e a command ha no line n0rrber Dor intance,

    'R m=0 T 1 STEP 06 INP*T a6 PRINT a6 NEHT m

    8he tep o Lero here mae the command repeat itel orever

    8hi ort o thin4 i not really recommended, beca0e i an error crop 0p then yo0 have lot the command and 5ill

    have to type it in a4ain J and C8IE 5ill not 5or

    E0ercises

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    18/116

    1 * control variable ha not F0t a name and a val0e, lie an ordinary variable, b0t alo a limit, a tep, and a

    reerence to the tatement ater the correpondin4 D7 tatement +er0ade yo0rel that 5hen the D7 tatement i

    exec0ted all thi inormation i available (0in4 the initial val0e a the irt val0e the variable tae), and alo that thi

    inormation i eno04h or the E.8 tatement to no5 by ho5 m0ch to increae the val0e, 5hether to F0mp bac,

    and i o 5here to F0mp bac to

    2 70n the third pro4ram above and then type

    PRINT c

    /hy i the an5er #, and not "A

    (*n5er: the E.8 command in line #' i exec0ted ive time, and each time 1 i added to c 8he lat time, c

    become #> and then the E.8 command decide not to loop bac, b0t to carry on, c bein4 pat it limit)

    /hat happen i yo0 p0t ;8E+ 2 in line 2'A

    3 Chan4e the third pro4ram o that intead o a0tomatically addin4 ive n0mber, it a yo0 to inp0t ho5 many

    n0mber yo0 5ant addin4 /hen yo0 r0n thi pro4ram, 5hat happen i yo0 inp0t ', meanin4 that yo0 5ant non0mber addin4A /hy mi4ht yo0 expect thi to ca0e problem or the

    comp0ter, even tho04h it i clear 5hat yo0 meanA (8he comp0ter ha to mae a earch or the

    command E.8 c, 5hich i not 00ally neceary) In act thi ha all been taen care o

    ! In line 1' o the o0rth pro4ram above, chan4e 1' to 1'' and r0n the pro4ram It 5ill print the n0mber rom 1''

    to $& on the creen, and then ay "croCat the bottom 8hi i to 4ive yo0 a chance to ee the n0mber that are abo0tto be crolled o the top I yo0 pre n, ;8+ or the B7E*G ey, the pro4ram 5ill top 5ith the report @ RE?7(NT repeat" I yo0 pre any other ey, then it 5ill print another 22 line and a yo0 a4ain

    " -elete line 3' rom the o0rth pro4ram /hen yo0 r0n the ne5 c0rtailed pro4ram, it 5ill print the irt n0mber and

    top 5ith the mea4e0 7 I yo0 type

    NEHT

    the pro4ram 5ill 4o once ro0nd the loop, printin4 o0t the next n0mber

    C6APTER ,Subroutines

    S&mmar3

    4" SUB/ RETURN

    ;ometime dierent part o the pro4ram 5ill have rather imilar Fob to do, and yo0 5ill ind yo0rel typin4 the

    ame line in t5o or more time> ho5ever thi i not neceary 6o0 can type the line in once, in a orm no5n a a

    0bro0tine, and then 0e, or call, them any5here ele in the pro4ram 5itho0t havin4 to type them in a4ain

    8o do thi, yo0 0e the tatement H ;B (H to ;Bro0tine) and 7E87 8hi tae the orm

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    19/116

    S*n

    5here n i the line n0mber o the irt line in the 0bro0tine It i F0t lie H 8 n except that the comp0ter

    remember 5here the H ;B tatement 5a o that it can come bac a4ain ater doin4 the 0bro0tine It doe thi

    by p0ttin4 the line n0mber and the tatement n0mber 5ithin the line (to4ether thee contit0te the ret0m addre) on

    top o a pile o them (the H ;B tac)>

    RET*RN

    tae the top return addresso the H ;B tac, and 4oe to the tatement ater it

    * an example, let loo at the n0mber 40ein4 pro4ram a4ain 7etype it a ollo5:

    10 REM $? rearra&e% &ue""#& &ame$ 20 INP*T a6 (LS 30 INP*T $ue"" t:e umber $,b )0 I' a=b T4EN PRINT $(orrect$6 STP 50 I' aDb T4EN S* 100 0 I' a>b T4EN S* 100

    0 T 30 100 PRINT $Tr; a&a#$ 110 RET*RN

    8he H 8 tatement in line $' i very important beca0e other5ie the pro4ram 5ill r0n on into the 0bro0tine

    and ca0e an error ( RET*RN #t:out S*) 5hen the 7E87 tatement i reached

    ere i another rather illy pro4ram ill0tratin4 the 0e o H ;B

    100 LET G=10 110 S* 500 120 PRINT " 130 LET G=G+)

    1)0 S* 500 150 PRINT " 10 LET G=G+2 10 S* 500 180 PRINT " 10 STP 500 LET "=0 510 'R ;=1 T G 520 LET "="+; 530 NEHT ; 5)0 RET*RN

    /hen thi pro4ram i r0n, ee i yo0 can 5or o0t 5hat i happenin4 8he 0bro0tine tart at line "''

    * 0bro0tine can happily call another, or even itel (a 0bro0tine that call itel irecursive), o dont be araid o

    havin4 everal layer

    C6APTER @

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    20/116

    R$7 7$T$ RSTOR

    S&mmar3

    REA./ .ATA/ REST"RE

    In ome previo0 pro4ram 5e a5 that inormation, or data, can be entered directly into the comp0ter 0in4 the

    I+8 tatement ;ometime thi can be very tedio0, epecially i a lot o the data i repeated every time the

    pro4ram i r0n 6o0 can ave a lot o time by 0in4 the 7E*-, -*8* and 7E;87E command Dor example:

    10 RE?@ a,b,c 20 PRINT a,b,c 30 @?T? 10,20,30 )0 STP

    * 7E*- tatement conit o 7E*- ollo5ed by a lit o the name o variable, eparated by comma It 5or

    rather lie an I+8 tatement, except that intead o 4ettin4 yo0 to type in the val0e to 4ive to the variable, the

    comp0ter loo 0p the val0e in the -*8* tatement

    Each -*8* tatement i a lit o expreion J n0meric or trin4 expreion eparated by comma 6o0 can p0t

    them any5here yo0 lie in a pro4ram, beca0e the comp0ter i4nore them except 5hen it i doin4 a 7E*- 6o0 m0t

    ima4ine the expreion rom all the -*8* tatement in the pro4ram a bein4 p0t to4ether to orm one lon4 lit o

    expreion, the -*8* lit 8he irt time the comp0ter 4oe to 7E*- a val0e, it tae the irt expreion rom the

    -*8* lit> the next time, it tae the econd> and th0 a it meet 0cceive 7E*- tatement, it 5or it 5ay

    thro04h the -*8* lit (I it trie to 4o pat the end o the -*8* lit, then it 4ive an error)

    ote that it a 5ate o time p0ttin4 -*8* tatement in a direct command, beca0e 7E*- 5ill not ind them

    -*8* tatement have to 4o in the pro4ram 9et ee ho5 thee it to4ether in the pro4ram yo0ve F0t typed in 9ine

    1' tell the comp0ter to read three piece o data and 4ive them the variable a, b and c 9ine 2' then ay +7I8

    thee variable 8he -*8* tatement in line 3' 4ive the val0e o a, b and c 9ine !' top the pro4ram 8o ee the

    order in 5hich thin4 5or chan4e line 2' to:

    20 PRINT b,c,a

    8he inormation in -*8* can be part o a D7 E.8 loop 8ype in

    10 'R =1 T 20 RE?@ @ 30 @?T? 2,),,8,10,12 )0 PRINT @ 50 NEHT 0 STP

    /hen thi pro4ram i 7 yo0 can ee the 7E*- tatement movin4 thro04h the -*8* lit -*8* tatement canalo contain trin4 variable Dor example:

    10 RE?@ %< 20 PRINT $T:e %ate #"$,%< 30 @?T? $Jue 1"t, 182$ )0 STP

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    21/116

    8hi i the imple 5ay o etchin4 expreion rom the -*8* lit: tart at the be4innin4 and 5or thro04h 0ntil yo0

    reach the end o5ever, yo0 can mae the comp0ter F0mp abo0t in the -*8* lit, 0in4 the 7E;87E tatement

    8hi ha 7E;87E, ollo5ed by a line n0mber, and mae 0be?0ent 7E*- tatement tart 4ettin4 their data rom

    the irt -*8* tatement at or ater the 4iven line n0mber (6o0 can mi o0t the line n0mber, in 5hich cae it i a

    tho04h yo0 had typed the line n0mber o the irt line in the pro4ram)

    8ry thi pro4ram:

    10 RE?@ a,b 20 PRINT a,b 30 RESTRE 10 )0 RE?@ G,;,K 50 PRINT G,;,K 0 @?T? 1,2,3 0 STP

    In thi pro4ram the data re?0ired by line 1' made a=1 and b=2 8he 7E;87E 1' intr0ction reet the variable,

    and allo5ed x, y and L to be 7E*- tartin4 rom the irt n0mber in the -*8* tatement 7er0n thi pro4ram

    5itho0t line 3' and ee 5hat happen

    C6APTER 0pressions

    S&mmar3

    "(erations / -/ D/

    E0(ressions/ scienti2ic notation/ variab)e names

    6o0 have already een ome o the 5ay in 5hich the . ;pectr0m can calc0late 5ith n0mber It can perorm the

    o0r arithmetic operation P, J, Q and R (remember that Q i 0ed or m0ltiplication, and R i 0ed or diviion), and it

    can ind the val0e o a variable, 4iven it name

    8he example:

    LET taG="um.15/100

    4ive F0t a hint o the very important act that thee calc0lation can be combined ;0ch a combination Iie

    0mQ1"R1'', i called an expreion: o an expreion i F0t a hortJhand 5ay o tellin4 the comp0ter to do everal

    calc0lation, one ater the other In o0r example, the expreion 0mQ1"R1'' mean loo 0p the val0e o the variable

    called @0m@, m0ltiply it by 1", and divide by 1''

    I yo0 havent yet done o, 5e recommend that yo0 loo thro04h the introd0ctory boolet to ee ho5 the .

    ;pectr0m handle n0mber, and the order in 5hich it eval0ate mathematical expreion

    8o recap:

    0ltiplication and diviion are done irt 8hey have higher prioritythan addition and 0btraction 7elative to

    each other, m0ltiplication and diviion have the ame priority, 5hich mean that the m0ltiplication and diviion are

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    22/116

    done in order rom let to ri4ht /hen they are dealt 5ith, the addition and 0btraction come next J thee a4ain have

    the ame priority a each other, o 5e do them in order rom let to ri4ht

    *ltho04h all yo0 really need to no5 i 5hether one operation ha a hi4her or lo5er priority than another, the

    comp0ter doe thi by havin4 a n0mber bet5een 1 and 1# to repreent the priority o each operation:

    Q and R have priority %, and P and J have priority #

    8hi order o calc0lation i abol0tely ri4id, b0t yo0 can circ0mvent it by 0in4 bracet: anythin4 in bracet i

    eval0ated irt and then treated a a in4le n0mber

    Expreion are 0e0l beca0e, 5henever the comp0ter i expectin4 a n0mber rom yo0, yo0 can 4ive it an

    expreion intead and it 5ill 5or o0t the an5er 8he exception to thi r0le are o e5 that they 5ill be tated

    explicitly in every cae

    6o0 can add to4ether a many trin4 (or trin4 variable) a yo0 lie in a in4le expreion, and i yo0 5ant, yo0

    can even 0e bracet

    /e really o04ht to tell yo0 5hat yo0 can and cannot 0e a the name o variable * 5e have already aid, the

    name o a trin4 variable ha to be a in4le letter ollo5ed by K> and the name o the control variable o a D7JE.8

    loop m0t be a in4le letter> b0t the name o ordinary n0meric variable are m0ch reer 8hey can 0e any letter ordi4it a lon4 a the irt one i a letter 6o0 can p0t pace in a 5ell to mae it eaier to read, b0t they 5ont co0nt

    a part o the name *lo, it doent mae any dierence to the name 5hether yo0 type it in capital or lo5er cae

    letter

    ere are ome example o the name o variable that are allo5ed:

    0

    t?:

    t!is name is so )ong t!at I s!a)) never be ab)e to t3(e it o&t again

    #it!o&t making a mistake

    no# #e are si0 Sthee lat t5o name are conidered the ame, and

    n";;eaReSiX reer to the ame variableT

    8hee are not allo5ed to be the name o variable:

    :FF* Sit be4in 5ith a di4itT

    < bears Sbe4in 5ith a di4itT

    MDADSD6 SQ i not a letter or a di4itT

    1ot!erington-T!omas SJ i not a letter or a di4itT

    0merical expreion can be repreented by a n0mber and exponent: a4ain reer to the introd0ctory boolet 8ry

    the ollo5in4 to prove the point:

    PRINT 2F3)e0 PRINT 2F3)e1 PRINT 2F3)e2

    and o on 0p to

    PRINT 2F3)e15

    6o0 5ill ee that ater a 5hile the comp0ter alo tart 0in4 cientiic notation 8hi i beca0e no more than

    o0rteen character can be 0ed to 5rite a n0mber ;imilarly, try

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    23/116

    PRINT 2F3)e1 PRINT 2F3)e2

    and o on

    +7I8 4ive only ei4ht i4niicant di4it o a n0mber 8ry

    PRINT )2)25, )2)25)2e

    8hi prove that the comp0ter can hold the di4it o !2&!$2&", even tho04h it i not prepared to diplay them all

    at once

    8he . ;pectr0m 0e loatin4 point arithmetic, 5hich mean that it eep eparate the di4it o a n0mber (it

    mantia) and the poition o the point (the exponentt 8hi i not al5ay exact, even or 5hole n0mber 8ype

    PRINT 1e10+11e10,1e101e10+1

    0mber are held to abo0t nine and a hal di4it acc0racy, o 1 e1' i too bi4 to be held exactly ri4ht 8he

    inacc0racy (act0ally abo0t 2) i more than 1, o the n0mber 1e1' and 1e1'P1 appear to the comp0ter to be e?0al

    Dor an even more pec0liar example, type

    PRINT 5e+15e

    ere the inacc0racy in "e& i only abo0t 1, and the 1 to be added on in act 4et rounded upto 2 8he n0mber

    "e&P1 and "e&P2 appear to the comp0ter to be e?0al

    8he lar4et inte4er (5hole n0mber) that can be held completely acc0rately i 1 le than 32 2 m0ltiplied to4ether

    (or !,2&!,$,2&")

    8he trin4 @@ 5ith no character at all i called the empty or n0ll trin4 7emember that pace are i4niicant and an

    empty trin4 i not the ame a one containin4 nothin4 b0t pace

    8ry:

    PRINT $4a!e ;ou B##":e% $'#e&a" Wae$ ;etC$

    /hen yo0 pre E8E7, yo0 5ill 4et the lahin4 ?0etion mar that ho5 there i a mitae ome5here in the

    line /hen the comp0ter ind the do0ble ?0ote at the be4innin4 o @Dinne4an /ae@, it ima4ine that thee mar

    the end o the trin4 @have yo0 inihed@, and it then cant 5or o0t 5hat Dinne4an /ae mean

    8here i a pecial device to 4et over thi: 5henever yo0 5ant to 5rite a trin4 ?0ote ymbol in the middle o a trin4,

    yo0 m0t 5rite it t5ice, lie thi:

    PRINT $4a!e ;ou B##":e% $$'#e&a" Wae$$ ;etC$

    * yo0 can ee rom 5hat i printed on the creen, each do0ble ?0ote i only really there once> yo0 F0t have to type

    it t5ice to 4et it reco4niLed

    C6APTER GStrings

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    24/116

    S&mmar3

    S)icing% &sing T"% Note t!at t!is notation is not stanar BASIC%

    Hiven a trin4, a 0btrin4 o it conit o ome conec0tive character rom it, taen in e?0ence 8h0 @trin4@ i a

    0btrin4 o @bi44er trin4@, b0t @b tin4@ and @bi4 re4@ are not

    8here i a notation calledslicingor decribin4 0btrin4, and thi can be applied to arbitrary trin4 expreion

    8he 4eneral orm i

    string e0(ression HstartT 2inis!

    o that, or intance,

    Jabce2JH: T" ,=JbceJ

    I yo0 omit the tart, then 1 i a0med> i yo0 omit the inih then the len4th o the trin4 i a0med 8h0

    Jabce2JH T" ,=Jabce2JH* T" ,=JabceJJabce2JH: T" =Jabce2JH: T" @=Jbce2J

    Jabce2JH T" =Jabce2JH* T" @=Jabce2J

    (6o0 can alo 5rite thi lat one a @abcde@(), or 5hat it 5orth)

    * li4htly dierent orm mie o0t the 8 and F0t ha one n0mber

    Jabce2JH

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    25/116

    20 'R =1 T 10 30 PRINT a

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    26/116

    S&mmar3

    .E1

    LEN/ STRK% VAL/ S4N% ABS/ INT/ SR

    1N

    Conider the a0a4e machine 6o0 p0t a l0mp o meat in at one end, t0rn a handle, and o0t come a a0a4e at theother end * l0mp o por 4ive a por a0a4e, a l0mp o ih 4ive a ih a0a4e, and a load o bee a bee a0a4e

    D0nction are practically inditin40ihable rom a0a4e machine b0t there i a dierence: they 5or on n0mber

    and trin4 intead o meat 6o0 0pply one val0e (called the argument), mince it 0p by doin4 ome calc0lation on

    it, and event0ally 4et another val0e, the result

    Meat in Sa&sage Mac!ine Sa&sage o&t

    Arg&ment in 1&nction Res&)t o&t

    -ierent ar40ment 4ive dierent re0lt, and i the ar40ment i completely inappropriate the 0nction 5ill top and

    4ive an error report

    U0t a yo0 can have dierent machine to mae dierent prod0ct J one or a0a4e another or dih cloth, and a

    third or ihJin4er and o on, dierent 0nction 5ill do dierent calc0lation Each 5ill have it o5n val0e to

    ditin40ih it rom the other

    6o0 0e a 0nction in expreion by typin4 it name ollo5ed by the ar40ment, and 5hen the expreion i

    eval0ated the re0lt o the 0nction 5ill be 5ored o0t

    * an example, there i a 0nction called 9E, 5hich 5or o0t the len4th o a trin4 It ar40ment i the trin4

    5hoe len4th yo0 5ant to ind, and it re0lt i the len4th, o that i yo0 type

    PRINT LEN $S#ca#r$

    the comp0ter 5ill 5rite the an5er % the n0mber o letter in ;inclair (8o 4et 9E a 5ith mot 0nction name,

    yo0 m0t 0e extended mode: pre C*+; ;ID8 and ;6B9 ;ID8 at the ame time to chan4e the c0ror rom

    Lto E, and then pre the G ey)

    I yo0 mix 0nction and operation in a in4le expreion, then the 0nction 5ill be 5ored o0t beore the

    operation *4ain, ho5ever, yo0 can circ0mvent thi r0le by 0in4 bracet Dor intance, here are t5o expreion

    5hich dier only in the bracet, and yet the calc0lation are perormed in an entirely dierent order in each cae

    (altho04h, a it happen, the end re0lt are the ame)

    LEN $'re%$+ LEN $o&&"$ LEN $'re%$+$o&&"$-

    )+LEN $o&&"$ LEN $'re%o&&"$-

    )+ LEN $'re%o&&"$

    10 10

    ere are ome more 0nction

    ;87K convert n0mber into trin4: it ar40ment i a n0mber, and it re0lt i the trin4 that 5o0ld appear on the

    creen i the n0mber 5ere diplayed by a +7I8 tatement ote ho5 it name end in a K i4n to ho5 that it re0lt

    i a trin4 Dor example, yo0 co0ld ay

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    27/116

    LET a

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    28/116

    and print o0t all o the ollo5in4: *9 aK, *9 @aK@, *9 @@@aK@@@, *9K aK, *9K @aK@ and *9K @@@aK@@@

    ;ome o thee 5ill 5or, and ome o them 5ont> try to explain all the an5er (Geep a cool head)

    ;H i thesign0nction (ometime calledsignum) It i the irt 0nction yo0 have een that ha nothin4 to do 5ith

    trin4, beca0e both it ar40ment and it re0lt are n0mber 8he re0lt i P1 i the ar40ment

    i poitive, ' i the ar40ment i Lero, and J1 i the ar40ment i ne4ative

    *B; i another 0nction 5hoe ar40ment and re0lt are both n0mber It convert the ar40ment into a poitive

    n0mber (5hich i the re0lt) by or4ettin4 the i4n, o that or intance

    ?S 3F2 = ?S 3F2 = 3F2

    I8 tand or inte4er part J an integeri a 5hole n0mber, poibly ne4ative 8hi 0nction convert a ractional

    n0mber into an inte4er by thro5in4 a5ay the ractional part, o that or intance,

    INT 3F=3

    Be care0l 5hen yo0 are applyin4 it to ne4ative n0mber, beca0e it al5ay ro0nd do5n: th0, or intance,

    INT 3F=)

    ;N7 calc0late thes:uare rooto a n0mber J the re0lt that, 5hen m0ltiplied by itel, 4ive the ar40ment Dor

    intance,

    SR ? = :beca0e 2Q2=!

    SR F%:, = F%,beca0e '"Q'"='2"

    SR : = *%?*?:*

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    29/116

    /hen yo0 have entered thi line, yo0 can invoe the 0nction F0t lie one o the comp0ter o5n 0nction, by

    typin4 it name, D , ollo5ed by the ar40ment 7emember that 5hen yo0 have deined a 0nction yo0rel, the

    ar40ment m0t be encloed in bracet 8ry it o0t a e5 time:

    PRINT 'N "2-PRINT 'N "3+)-

    PRINT 1+1NT 'N " LEN $c:#ce$/2+3-

    nce yo0 have p0t the correpondin4 -ED tatement into the pro4ram, yo0 can 0e yo0r o5n 0nction in

    expreion F0t a reely a yo0 can 0e the comp0ter

    ote: in ome dialect o B*;IC yo0 m0t even encloe the ar40ment o one o the comp0ter 0nction in bracet

    8hi i not the cae in . ;pectr0m B*;IC

    I8 al5ay ro0nd do5n 8o ro0nd to the nearet inte4er, add " irt J yo0 co0ld 5rite yo0r o5n 0nction to do thi

    20 @E' 'N rG-=NT G+F5-6 REM !e" G rou%e% to t:e eare"t #te&erF

    6o0 5ill then 4et, or intance,

    'N r2F- = 3 'N r2F)- = 2'N r2F- = 3 'N r2F)- = 2

    Compare thee 5ith the an5er yo0 4et 5hen yo0 0e I8 intead o D r 8ype in and r0n the ollo5in4:

    10 LET G=06 LET ;=06 LET a=1020 @E' 'N pG,;-=a+G30 @E' 'N O-=a+G.;)0 PRINT 'N p2,3-,'N O-

    8here are a lot o 0btle point in thi pro4ram

    Dirt, a 0nction i not retricted to F0t one ar40ment: it can have more, or even none at all J b0t yo0 m0t tillal5ay eep the bracet

    ;econd, it doent matter 5hereabo0t in the pro4ram yo0 p0t the -ED tatement *ter the comp0ter ha exec0ted

    line 1', it imply ip over line 2' and 3' to 4et to line !' 8hey do, ho5ever, have to be ome5here in the

    pro4ram 8hey cant be in a command

    8hird, x and y are both the name o variable in the pro4ram a a 5hole, and the name o ar40ment or the

    0nction D p D p temporarily or4et abo0t the variable called x and y, b0t ince it ha no ar40ment called a, it

    till remember the variable a 8h0 5hen D p(2,3) i bein4 eval0ated, a ha the val0e 1' beca0e it i the variable, x

    ha the val0e 2 beca0e it i the irt ar40ment, and y ha the val0e 3 beca0e it i the econd ar40ment 8he re0lt i

    then, 1'P2Q3=1# /hen D ?() i bein? eval0ated, on the other hand there are no ara0ment o a x and v all till

    reer to the variable and have val0e 1', ' and ' repectively 8he an5er in thi cae i 1'P'Q'=1'

    o5 chan4e line 2' to

    20 @E' 'N pG,;-='N O-

    8hi time, D p(2,3) 5ill have the val0e 1' beca0e D ? 5ill till 4o bac to the variable x and y rather than 0in4

    the ar40ment o D p

    ;ome B*;lC (not the . ;pectr0m B*;IC) have 0nction called 9ED8K, 7IH8K, I-K and 89K

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    30/116

    9ED8K (aK,n) 4ive the 0btrin4 o aK conitin4 o the irt ncharacter

    7IH8K (aK,n) 4ive the 0btrin4 o aK conitin4 o the character romnth on

    I-K (aK, n1, n2) 4ive the 0btrin4 o aK conitin4 o n2 character tartin4 at the n1th

    89K (aK) 4ive the 0btrin4 o aK conitin4 o all it character except the irt

    6o0 can 5rite ome 0erJdeined 0nction to do the ame: e4

    10 @E' 'N t b0t obvio0ly thi 5o0ld be diic0lt on a comp0ter o 5e 0e the ymbol intead Dor example, the po5ero 2 are

    :*=:::=:D:=? (2 ?0ared):

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    31/116

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    32/116

    8hi ort o behavio0r, 5here ater a ixed interval o time ome ?0antity m0ltiplie itel by a ixed proportion, i

    called exponential 4ro5th, and it i calc0lated by raiin4 a ixed n0mber to the po5er o the time

    ;0ppoe yo0 did thi:

    10 @E' 'N aG-=aG

    ere, ai more or le ixed, by 9E8 tatement: it val0e 5ill correpond to the interet rate, 5hich chan4e only

    every o oten

    8here i a certain val0e orathat mae the 0nction D a loo epecially pretty to the trained eye o a

    mathematician: and thi val0e i called e 8he . ;pectr0m ha a 0nction called E.+ deined by

    EHP G=eG

    nort0nately, eitel i not an epecially pretty n0mber: it i an ininite nonJrec0rrin4 decimal 6o0 can ee it irt

    e5 decimal place by doin4

    PRINT EHP 1

    beca0e E.+ 1 = e1 = e co0re, thi i F0t an approximation 6o0 can never 5rite do5n eexactly

    LN

    8he invere o an exponential 0nction i a logarithmic0nction: the lo4arithm (to bae a) o a n0mber0i the

    po5er to 5hich yo0 have to raie a to 4et the n0mber0, and it i 5ritten lo4ax 8h0 by deinition alo4

    ax=x> and it i

    alo tr0e that lo4 (ax)=x

    6o0 may 5ell already no5 ho5 to 0e bae 1' lo4arithm or doin4 m0ltiplication> thee are called common

    lo4arithm 8he . ;pectr0m ha a 0nction 9 5hich calc0late lo4arithm to the bae e> thee are called nat0ral

    lo4arithm 8o calc0late lo4arithm to any other bae, yo0 m0t divide the nat0ral lo4arithm by the nat0ral lo4arithm

    o the bae:

    lo4ax = LN 0 LN a

    PI

    Hiven any circle, yo0 can ind itperimeter(the ditance ro0nd it ed4e> oten called it circum#erence) by

    m0ltiplyin4 it diameter(5idth) by a n0mber called (i a Hree p, and it i 0ed beca0e it tand or perimeterIt name i pi)

    9ie e, i an ininite nonJrec0rrin4 decimal> it tart o a 31!1"&2#"3"%&8he 5ord +I on the ;pectr0m(extended mode, then ) i taen a tandin4 or thi n0mber J try +7I8 +I

    SIN/ C"S an TAN ASN/ ACS an ATN 8he trigonometrical0nction mea0re 5hat happen 5hen a point move ro0nd a circle ere i a circle o radi0 1

    (1 5hatA It doent matter, a lon4 a 5e eep to the ame 0nit all the 5ay thro04h 8here i nothin4 to top yo0

    inventin4 a ne5 0nit o yo0r o5n or every circle that yo0 happen to be intereted in) and a point movin4 ro0nd it

    8he point tarted at the 3 ocloc poition, and then moved ro0nd in an antiJcloc5ie direction

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    33/116

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    34/116

    ote that i the point 4oe to the let o the yJaxi, then the coine become ne4ative> and i the point 4oe belo5 the

    xJaxi, the ine become ne4ative

    *nother property i that once aha 4ot 0p to 2, the point i bac 5here it tarted and the ine and coine tarttain4 the ame val0e all over a4ain:

    SIN a+2.PI- = SIN a (S a+2.PI- = (S a

    8he tangento ai deined to be the ine divided by the coine> the correpondin4 0nction on the comp0ter i

    called 8*

    ;ometime 5e need to 5or thee 0nction o0t in revere, indin4 the val0e o a that ha 4iven ine, coine or

    tan4ent 8he 0rction to do thi are called arcsine(*; on the comp0ter), arccosine(*C;) and arctangent(*8)

    In the dia4ram o the point movin4 ro0nd the circle, loo at the radi0 Foinin4 the centre to the point 6o0 ho0ld be

    able to ee that the ditance 5e have called a, the ditance that the point ha moved ro0nd the ed4e o the circle, i a

    5ay o mea0rin4 the an4le thro04h 5hich the radi0 ha moved a5ay rom the xJaxi /hen a=2, the an4le i &'de4ree> 5hen a=the an4le i 1%' de4ree> and o ro0nd to 5hen a=2, and the an4le i 3#' de4ree 6o0 mi4ht F0t

    a 5ell or4et abo0t de4ree, and mea0re the an4le in term o aalone: 5e ay then that 5e are mea0rin4 the an4lein radians 8h0 R2 radian=&' de4ree and o on

    6o0 m0t al5ay remember that on the . ;pectr0m ;I, C; and o on 0e radian and not de4ree 8o convert

    de4ree to radian, divide by 1%' and m0ltiply by > to convert bac rom radian to de4ree, yo0 divide by andm0ltiply by 1%'

    C6APTER **Random numbers

    S&mmar3

    RAN."MIZE

    RN.

    8hi chapter deal 5ith the 0nction 7- and the ey5ord 7*-IE 8hey are both 0ed in connection 5ith

    random n0mber, o yo0 m0t be care0l not to 4et them mixed 0p 8hey are both on the ame ey (8)>

    7*-IE ha had to be abbreviated to 7*-

    In ome 5ay 7- i lie a 0nction: it doe calc0lation and prod0ce a re0lt It i 0n00al in that it doe not need

    an ar40ment

    Each time yo0 0e it, it re0lt i a ne5 random n0mber bet5een ' and 1 (;ometime it can tae the val0e ', b0tnever 1)

    8ry

    10 PRINT RN@ 20 T 10

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    35/116

    to ee ho5 the an5er varie Can yo0 detect any pattern$ 6o0 ho0ldnt be able to> random mean that there i no

    pattern

    *ct0ally, 7- i not tr0ly random, beca0e it ollo5 a ixed e?0ence o #""3# n0mber o5ever, thee are o

    thoro04hly F0mbled 0p that there are at leat no obvio0 pattern and 5e ay that 7- ipseudo-random

    7- 4ive a random n0mber bet5een ' and 1, b0t yo0 can eaily 4et random n0mber in other ran4e Dor intance,"Q7- i bet5een ' and ", and 13P'$Q7- i bet5een 13 and 2 8o 4et 5hole n0mber, 0e I8 (rememberin4

    that I8 al5ay ro0nd do5n) a in 1 PI8 (7-Q#), 5hich 5e hall 0e in a pro4ram to im0late dice 7-Q# i in

    the ran4e ' to #, b0t ince it never act0ally reache #, I8 (7-Q#) i ',1,2,3,! or "

    ere i the pro4ram:

    10 REM %#ce t:ro#& pro&ram 20 (LS 30 'R =1 T 2 )0 PRINT 1+1NT [email protected]$ $ 50 NEHT 0 INP*T a

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    36/116

    50 PRINT :ea%"$,$ta#", 0 I' ta#"D> T4EN PRINT :ea%"/ta#" 0 PRINT6 T 20

    8he ratio o head to tail ho0ld become approximately 1 i yo0 4o on lon4 eno04h, beca0e in the lon4 r0n yo0

    expect approximately e?0al n0mber o head and tail

    E0ercises

    1 8et thi r0le:

    ;0ppoe yo0 chooe a n0mber bet5een 1 and %$2 and type

    R?N@MIEyo0r n0mber

    8hen the next val0e o 7- 5ill be

    H,DH3o&r n&mber* -* @,,

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    37/116

    the variable Blo44 1, Blo44 2, and o on 0p to Blo44 1', b0t the pro4ram to et 0p thee ten n0mber 5o0ld be

    rather lon4 and borin4 to type in

    o5 m0ch nicer it 5o0ld be i yo0 co0ld type thi:

    5 REM t:#" pro&ram # ot or

    10 'R =1 T 1020 RE?@ o&&" 30 NEHT )0 @?T? 10,2,5,1,1,3,11,1,0,

    /ell yo0 cant

    o5ever, there i a mechanim by 5hich yo0 can apply thi idea, and it 0e arrays *n array i a et o variable,

    it element, all 5ith the ame name, and ditin40ihed only by a n0mber (the 0bcript) 5ritten in bracet ater the

    name In o0r example the name co0ld be b(lie control variable o D7 J E.8 loop, the name o an array m0t

    be a in4le letter), and the ten variable 5o0ld then be b(l), b(2), and o on 0p to b(l')

    8he element o an array are calledsubscriptedvariable, a oppoed to the imple variable that yo0 are already

    amiliar 5ith

    Beore yo0 can 0e an array, yo0 m0t reerve ome pace or it inide the comp0ter, and yo0 do thi 0in4 a -I

    (or dimenion) tatement

    @IM b10-

    et 0p an array called b5ith dimenion 1' (ie there are 1' 0bcripted variable b(1),,b(1')) and initialiLe the 1'

    val0e to ' It alo delete any array called b that exited previo0ly (B0t not a imple variable *n array and a

    imple n0merical variable 5ith the ame name can coexit, and there ho0ldnt be any con0ion bet5een them

    beca0e the array variable al5ay ha a 0bcript)

    8he 0bcript can be an arbitrary n0merical expreion, o no5 yo0 can 5rite

    10 'R =1 T 1020 RE?@ b-30 NEHT )0 @?T? 10,2,5,1,1,3,11,1,0,

    6o0 can alo et 0p array 5ith more than one dimenion In a t5o dimenional array yo0 need t5o n0mber to

    peciy one o the element J rather lie the line and col0mn n0mber to peciy a character poition on the televiion

    creen J o it ha the orm o a table *lternatively, i yo0 ima4ine the line and col0mn n0mber (t5o dimenion) a

    reerrin4 to a printed pa4e, yo0 co0ld have an extra dimenion or the pa4e n0mber co0re, 5e are talin4 abo0t

    n0meric array> o the element 5o0ld not be printed character a in a boo, b0t n0mber 8hin o the element o a

    threeJdimenional array va bein4 peciied by v(pa4e n0mber, line n0mber, col0mn n0mber)

    Dor example, to et 0p a t5oJdimenional array c 5ith dimenion 3 and #, yo0 0e a -I tatement

    @IM c3,-

    8hi then 4ive yo0 3Q#=1% 0bcripted variable

    cH* /*/cH* /:/ % % %/cH*/@

    cH:/ * /cH:/:/ % % %/cH:/@

    cH

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    38/116

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    39/116

    PRINT $o #" t:e mot: oB $m0 T4EN PRINT G

    in 5hich x only 4et printed i aK=ye@ and x' 8he B*;IC here i o cloe to En4lih that it hardly eem 5orth

    pellin4 o0t the:detail * in En4lih, yo0 can Foin lot o relation to4ether 5ith *-, and then the 5hole lot i tr0e

    i all the individ0al relation are

    ne relation 7 another i tr0e 5henever at leat one o the t5o relation i tr0e (7emember that it i till tr0e i

    both the relation are tr0e> thi i omethin4 that En4lih doent al5ay imply)

    8he 8 relationhip t0rn thin4 0pide do5n 8he 8 relation i tr0e 5henever the relation i ale, and ale

    5henever it i tr0eM

    9o4ical expreion can be made 5ith relation and *-, 7 and 8, F0t a n0merical expreion can be made

    5ith n0mber and P, J and o on> yo0 can even p0t them in bracet i neceary 8hey have prioritie in the ame

    5ay a the 00al operation P, J, Q, R and , do: 7 ha the lo5et priority, then *-, then 8, then the relation,and the 00al operation

    8 i really a 0nction, 5ith an ar40ment and a re0lt, b0t it priority i m0ch lo5er than that o other 0nction

    8hereore it ar40ment doe not need bracet 0nle it contain *- or 7 (or both) 8 a=b mean the ame a

    8 (a=b) (and the ame a a9bi the ame a a=b

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    40/116

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    41/116

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    42/116

    E0ercise

    1 B*;IC can ometime 5or alon4 dierent line rom En4lih Conider, or intance, the En4lih cla0e I a

    doent e?0al bor c o5 5o0ld yo0 5rite thi in B*;ICA 8he an5er i not

    I1 A>9B "R C

    nor i itI1 A>9B "R A>9C

    C6APTER *?The !haracter Set

    S&mmar3

    C".E/ C6RK

    P"7E/ PEE7

    USR

    BIN

    8he letter, di4it, p0nct0ation mar and o on that can appear in trin4 are called character, and they mae 0p the

    alphabet, or character set, that the . ;pectr0m 0e ot o thee character are in4lesymbols, b0t there are ome

    more, called to*ens, that repreent 5hole 5ord, 0ch a +7I8, ;8+,

    *;CII alo ai4n n0meric code to thee character, and thee are the code that the . ;pectr0m 0e

    8he ret o the character are not part o *;CII, and are pec0liar to the . ;pectr0m Dirt amon4t them are a

    pace and 1" pattern o blac and 5hite blob 8hee are called thegraphicsymbol and can be 0ed or dra5in4

    pict0re 6o0 can enter thee rom the eyboard, 0in4 5hat i called 4raphic mode I yo0 pre H7*+IC; (C*+;;ID8 5ith &) then the c0ror 5ill chan4e to 4 o5 the ey or the di4it 1 to % 5ill 4ive the 4raphic ymbol: on

    their o5n they 4ive the ymbol dra5n on the ey> and 5ith either hit preed they 4ive the ame ymbol b0t

    inverted, ie blac become 5hite, and vice vera

    7e4ardle o hit, di4it & tae yo0 bac to normal (L) mode and di4it ' i -E9E8E

    ere are the ixteen 4raphic ymbol:

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    43/116

    *ter the 4raphic ymbol, yo0 5ill ee 5hat appear to be another copy o the alphabet rom * to 8hee are

    character that yo0 can redeine yo0rel altho04h 5hen the machine i irt 5itched on they are et a letter J they

    are called user-de#ined4raphic 6o0 can type thee in rom the eyboard by 4oin4 into 4raphic mode, and then

    0in4 the letter ey rom * to

    8o deine a ne5 character or yo0rel, ollo5 thi recipe J it deine a character to ho5

    (i) /or o0t 5hat the character loo lie Each character ha an %x% ?0are o dot, each o 5hich can ho5 either

    the paper colo0r or the in colo0r (ee the introd0ctory boolet) 6o0d dra5 a dia4ram omethin4 lie thi, 5ith

    blac ?0are or the in colo0r:

    /eve let a 1 ?0are mar4in ro0nd the ed4e beca0e the other letter all have one (except or lo5er cae letter 5ith

    tail, 5here the tail 4oe ri4ht do5n to the bottom)

    (ii) /or o0t 5hich 0erJdeined 4raphic i to ho5 nJ let ay the one correpondin4 to +, o that i yo0 pre + in

    4raphic mode yo0 4et

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    44/116

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    45/116

    PRINT 1,2

    bvio0ly thi i not a very clear 5ay o 0in4 it * more 0btle 5ay i to ay

    LET a

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    46/116

    JT!e same stringJ>=JT!e same stringJ

    i tr0e, b0t

    JT!e same stringJ>JT!e same stringJ

    i ale

    Experiment on all thi 0in4 the pro4ram here, 5hich inp0t t5o trin4 and p0t them in order

    10 INP*T $T;pe # to "tr#&"6$, a

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    47/116

    220 REM pa230 @?T? $p$,0,0,%,c2)0 @?T? c,%,b,0250 REM #&:t20 @?T? $$,0,%,c, IN 0111100020 @?T? IN 00011000,c,b,0

    ote that ' can be 0ed intead o BI ''''''''

    /hen yo0 have r0n the pro4ram, loo at the piece by 4oin4 into 4raphic mode

    E0ercises

    1 Ima4ine the pace or one ymbol divided 0p into o0r ?0arter lie a Battenb0r4 cae 8hen i each ?0arter can be

    either blac or 5hite, there are 2x2x2x2=1# poibilitie Dind them all in the character et

    2 70n thi pro4ram:

    10 INP*T a

    20 PRINT (4R< a30 T 10

    I yo0 experiment 5ith it, yo0ll ind that C7K a i ro0nded to the nearet 5hole n0mber> and i a i not in the ran4e

    ' to 2"" then the pro4ram top 5ith error report #te&er out oB ra&e

    3 /hich o thee t5o i the leerA

    JEVILJ

    Jevi)J

    ! /or o0t ho5 to modiy the pro4ram to et 0p 0erJdeined 4raphic o that it 0e 7E*- and -*8* tatement

    intead o the I+8 tatement

    C6APTER *,/ore about ,R.;T and .;,4T

    S&mmar3

    CLS

    PRINT items not!ing at a))

    E0(ressions Hn&meric or string t3(e TAB n&meric e0(ression/ AT n&meric e0(ression/ n&meric

    e0(ression PRINT se(arators / Q

    INPUT items variab)es Hn&meric or string t3(e

    LINE string variab)e

    An3 PRINT item not beginning #it! a )etter% HTokens are not consiere as beginning #it! a )etter%

    Scro))ing%

    SCREENK

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    48/116

    6o0 have already een +7I8 0ed ?0ite a lot, o yo0 5ill have a ro04h idea o ho5 it i 0ed Expreion 5hoe

    val0e are printed are called +7I8 items, and they are eparated by comma or emicolon, 5hich are called +7I8

    separators * +7I8 item can alo be nothin4 at all, 5hich i a 5ay o explainin4 5hat happen 5hen yo0 0e t5o

    comma in a ro5

    8here are t5o more ind o +7I8 item, 5hich are 0ed to tell the comp0ter not 5hat, b0t 5here to print Dor

    example +7I8 *8 11,1#>@Q@ print a tar in the middle o the creen

    AT )ine/ co)&mn

    move the +7I8 poition (the place 5here the next item i to be printed) to the line and col0mn peciied 9ine are

    n0mbered rom ' (at the top) to 21, and col0mn rom ' (on the let) to 31

    ;C7EEK i the revere 0nction to +7I8 *8, and 5ill tell yo0 (5ithin limit) 5hat character i at a partic0lar

    poition on the creen It 0e line and col0mn n0mber in the ame 5ay a +7I8 *8, b0t encloed in bracet: or

    intance

    PRINT S(REEN< 11,1-

    5ill retrieve the tar yo0 printed in the para4raph above

    Character taen rom toen print normally, a in4le character, and pace ret0rn a pace 9ine dra5n by

    +98, -7*/ or CI7C9E, 0erJdeined character and 4raphic character ret0rn a a n0ll (empty) trin4, ho5ever

    8he ame applie i E7 ha been 0ed to create a compoite character

    TAB co)&mn

    print eno04h pace to move the +7I8 poition to the col0mn peciied It tay on the ame line or, i thi 5o0ld

    involve bacpacin4, move on to the next one ote that the comp0ter red0ce the col0mn n0mber mod0lo 32 (it

    divide by 32 and tae the remainder)> o 8*B 33 mean the ame a 8*B 1

    * an example,

    PRINT T? 301T? 12$(otet"$ ?T 3,1$(4?PTER$T? 2)$pa&e$

    i ho5 yo0 mi4ht print o0t the headin4 o a content pa4e on pa4e 1 o a boo

    8ry r0nnin4 thi:

    10 'R =8 T 2320 PRINT T? 8.30 NEHT

    8hi ho5 5hat i meant by the 8*B n0mber bein4 red0ced mod0lo 32

    Dor a more ele4ant example, chan4e the % in line 2' to a #

    ;ome mall point:

    (i) 8hee ne5 item are bet terminated 5ith emicolon, a 5e have done above 6o0 can 0e comma (or nothin4, at

    the end o the tatement), b0t thi mean that ater havin4 care0lly et 0p the +7I8 poition yo0 immediately move

    it on a4ain not 00ally terribly 0e0l

    (ii) 6o0 cannot print on the bottom t5o line (22 and 23) on the creen beca0e they are reerved or command,

    I+8 data, report and o on 7eerence to the bottom line 00ally mean line 21

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    49/116

    (iii) 6o0 can 0e *8 to p0t the +7I8 poition even 5here there i already omethin4 printed> the old t0 5ill be

    obliterated 5hen yo0 print more

    *nother tatement connected 5ith +7I8 i C9; 8hi clear the 5hole creen, omethin4 that i alo done by

    C9E*7 and 7

    /hen the printin4 reache the bottom o the creen, it tart to croll 0p5ard rather lie a type5riter 6o0 can eethi i yo0 do

    (LS6 'R =1 T 226 PRINT 6 NEHT

    and then do

    PRINT

    a e5 time

    I the comp0ter i printin4 o0t ream and ream o t0, then it tae 4reat care to mae 0re that nothin4 i crolled

    o the top o the creen 0ntil yo0 have had a chance to loo at it properly 6o0 can ee thi happenin4 i yo0 type

    (LS6 'R =1 T 1006 PRINT 6 NEHT

    /hen it ha printed a creen 0ll, it 5ill top, 5ritin4 "croCat the bottom o the creen 6o0 can no5 inpect theirt 22 n0mber at yo0r lei0re /hen yo0 have inihed 5ith them, pre y (or ye) and the comp0ter 5ill 4ive yo0

    another creen 0ll o n0mber *ct0ally, any ey 5ill mae the comp0ter carry on except n (or no), ;8+

    (;6B9 ;ID8 and a), or ;+*CE (the B7E*G ey) 8hee 5ill mae the comp0ter top r0nnin4 the pro4ram

    5ith a report @ RE?7 (NT repeat"

    8he I+8 tatement can do m0ch more than 5e have told yo0 o ar 6o0 have already een I+8 tatement

    lie

    INP*T $4o o% are ;ouC$, a&e

    in 5hich the comp0ter print the caption o5 old are yo0A at the bottom o the creen, and then yo0 have to type in

    yo0r a4e

    In act, an I+8 tatement i made 0p o item and eparator in exactly the ame 5ay a a +7I8 tatement i, o

    o5 old are yo0A and a4e are both I+8 item I+8 item are 4enerally the ame a +7I8 item, b0t there are

    ome very important dierence

    Dirt, an obvio0 extra I+8 item i the variable 5hoe val0e yo0 are to type in a4e in o0r example above 8he

    r0le i that i an I+8 item be4in 5ith a letter, it m0t be a variable 5hoe val0e i to be inp0t

    ;econd, thi 5o0ld eem to mean that yo0 cant print o0t the val0e o variable a part o a caption> ho5ever, yo0

    can 4et ro0nd thi by p0ttin4 bracet ro0nd the variable *ny expreion that tart 5ith a letter m0t be encloed in

    bracet i it i to be printed a part o a caption

    *ny ind o +7I8 item that i not aected by thee r0le i alo an I+8 item ere i an example to ill0trate

    5hat 4oin4 on:

    LET m; a&e = INT RN@ . 100-6 INP*T $I am $m; a&e $F $-$4o o% are ;ouC$, ;our a&e

    my agei contained in bracet, o it val0e 4et printed o0tyour agei not contained in bracet, o yo0 have to

    type it val0e in

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    50/116

    Everythin4 that an I+8 tatement 5rite 4oe to the bottom part o the creen, 5hich act ome5hat

    independently o the top hal In partic0lar, it line are n0mbered relative to the top line o the bottom hal, even i

    thi ha moved 0p the act0al televiion creen (5hich it doe i yo0 type lot and lot o I+8 data)

    8o ee ho5 *8 5or in I+8 tatement, try r0nnin4 thi:

    10 INP*T $T:#" #" #e 1F$,a

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    51/116

    PRINT (4RS 23+(4RS a+(4RS b

    ha the ame eect a

    PRINT T? a+25.b

    6o0 can 0e +GE to top the comp0ter ain4 yo0 "croCby doin4

    P7E 232,255

    every o oten *ter thi it 5ill croll 0p 2"" time beore toppin4 5ith "croC * an example, try

    10 'R =0 T 1000020 PRINT 6 P7E 232,25530 NEHT

    and 5atch everythin4 5hiLL o the creenM

    E0ercises

    1 8ry thi pro4ram on ome children, to tet their m0ltiplication table

    10 LET m

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    52/116

    10 'R m=) T 16 RI4T m 20 'R =1 T 1) 30 'R c=) T )0 P?PER c6 PRINT $ $6 REM ) cooure% "pace" 50 NEHT c6 NEHT 6 NEHT m

    0 'R m=0 T 16 RI4T m6 P?PER 0 'R c=0 T 3 80 IN7 c6 PRINT c$ $ 0 NEHT c6 P?PER 0

    100 'R c=) T0 110 IN7 c6 PRINT c$ $120 NEHT c6 NEHT m130 P?PER 6 IN7 06 RI4T 0

    8hi ho5 the ei4ht colo0r (incl0din4 5hite and blac) and the t5o level o bri4htne that the . ;pectr0m can

    prod0ce on a colo0r televiion (I yo0r televiion i blac and 5hite, then yo0 5ill F0t ee vario0 hade o 4rey)

    ere i a lit o them or reerence> they are alo 5ritten over the appropriate n0mber ey

    ' J blac1 J bl0e

    2 J red

    3 J p0rple, or ma4enta

    ! J 4reen

    " J pale bl0e, technically called cyan

    # J yello5

    $ J 5hite

    n a blac and 5hite televiion, thee n0mber are in order o bri4htne

    8o 0e thee colo0r properly, yo0 need to 0ndertand a bit abo0t ho5 the pict0re i arran4ed

    8he pict0re i divided 0p into $#% (2! line o 32) poition 5here character can be printed, and each character iprinted a an %x% ?0are o dot lie that belo5 or a 8hi ho0ld remind yo0 o the 0erJdeined 4raphic in Chapter

    1!, 5here 5e had ' or the 5hite dot and 1 or the blac dot

    8he character poition alo ha aociated 5ith it t5o colo0r: the in, or ore4ro0nd colo0r, 5hich i the colo0r or

    the blac dot in o0r ?0are, and the paper, or bac4ro0nd colo0r, 5hich i 0ed or the 5hite dot 8o tart o 5ith,

    every poition ha blac in and 5hite paper o 5ritin4 appear a blac on 5hite

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    53/116

    8he character poition alo ha a bri4htne (normal or extra bri4ht) and omethin4 to ay 5hether it lahe or not J

    lahin4 i done by 5appin4 the in and paper colo0r 8hi can all be coded into n0mber, o a character poition

    then ha

    (i) an %x% ?0are o ' and 1 to deine the hape o the character, 5ith ' or paper and 1 or in,

    (ii) in and paper colo0r, each coded into a n0mber bet5een ' and $,

    (iii) a bri4htne J ' or normal, 1 or extra bri4ht and

    (iv) a lah n0mber J ' or teady, 1 or lahin4

    ote that ince the in and paper colo0r cover a 5hole character poition, yo0 cannot poibly have more than t5o

    colo0r in a 4iven bloc o #! dot 8he ame 4oe or the bri4htne and lah n0mber: they reer to the 5hole

    character poition, not individ0al dot 8he colo0r, bri4htne and lah n0mber at a 4iven poition are called

    attrib0te

    /hen yo0 print omethin4 on the creen, yo0 chan4e the dot pattern at that poition> it i le obvio0, b0t till tr0e,

    that yo0 alo chan4e the attrib0te at that poition 8o tart o 5ith yo0 do not notice thi beca0e everythin4 i

    printed 5ith blac in on 5hite paper (and normal bri4htne and no lahin4), b0t yo0 can vary thi 5ith the IG,+*+E7, B7IH8 and D9*; tatement 8ry

    P?PER 5

    and then print a e5 thin4: they 5ill all appear on cyan paper, beca0e a they are printed the paper colo0r at the

    poition they occ0py are et to cyan (5hich ha code ")

    8he other 5or the ame 5ay, o ater

    PAPERn0mber bet5een ' and $

    IN7n0mber bet5een ' and $

    BRI46T' or 1 O 8hin o ' a o

    or O and 1 a on

    1LAS6' or 1 O

    any printin4 5ill et the correpondin4 attrib0te at all the character poition it 0e 8ry ome o thee o0t 6o0

    ho0ld no5 be able to ee ho5 the pro4ram at the be4innin4 5ored (remember that a pace i a character that ha

    IG and +*+E7 the ame colo0r)

    8here are ome more n0mber yo0 can 0e in thee tatement that have le direct eect

    % can be 0ed in all o0r tatement, and mean tranparent in the ene that the old attrib0te ho5 thro04h

    ;0ppoe, or intance, that yo0 do

    P?PER 8

    o character poition 5ill ever have it paper colo0r et to % beca0e there i no 0ch colo0r> 5hat happen i that

    5hen a poition i printed on, it paper colo0r i let the ame a it 5a beore IG %, B7IH8 % and D9*; %

    5or the ame 5ay or the other attrib0te

    & can be 0ed only 5ith +*+E7 and IG, and mean contrat 8he colo0r (in or paper) that yo0 0e it 5ith i

    made to contrat 5ith the other by bein4 made 5hite i the other i a dar colo0r (blac, bl0e, red or ma4enta), and

    blac i the other i a li4ht colo0r (4reen, cyan, yello5 or 5hite)

    8ry thi by doin4

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    54/116

    IN7 6 'R c=0 T 6 P?PER c6 PRINT c6 NEHT c

    * more impreive diplay o it po5er i to r0n the pro4ram at the be4innin4 to mae colo0red tripe, and then

    doin4

    IN7 6 P?PER 86 PRINT ?T 0,86 'R = T 10806 PRINT 6 NEXT n

    8he in colo0r here i al5ay made to contrat 5ith the old paper colo0r at each poition

    Colo0r televiion relie on the rather c0rio0 act that the h0man eye can only really ee three colo0r J the primary

    colo0r, bl0e, red and 4reen 8he other colo0r are mixt0re o thee Dor intance, ma4enta i made by mixin4 bl0e

    5ith red J 5hich i 5hy it code, 3, i the 0m o the code or bl0e and red

    8o ee ho5 all ei4ht colo0r it to4ether, ima4ine three rectan40lar potli4ht, colo0red bl0e, red and 4reen, hinin4

    at not ?0ite the ame place on a piece o 5hite paper in the dar /here they overlap yo0 5ill ee mixt0re o

    colo0r, a ho5n by thi pro4ram (note that in pace are obtained by 0in4 either ;ID8 5ith % 5hen in H mode):

    10 R@ER 06 P?PER 06 IN7 6 (LS

    20 'R a=1 T 30 PRINT T? IN7 1 $$6 REM 18 # "Ouare")0 NEHT a50 LET %ata#e=200 S* 10000 LET %ata#e=21080 S* 10000 STP200 @?T? 2,3,,5,)210 @?T? 2,2,,),)1000 'R a=1 T 1010 RESTRE %ata#e1020 'R b=1 T 5

    1030 RE?@ c6 PRINT IN7 c$$6 REM # "Ouare"10)0 NEHT b6 PRINT6 NEHT a1050 RET*RN

    8here i a 0nction called *887 that ind o0t 5hat the attrib0te are at a 4iven poition on the creen It i a airly

    complicated 0nction, o it ha been rele4ated to the end o thi chapter

    8here are t5o more tatement, IE7;E and E7, 5hich control not the attrib0te, b0t the dot pattern that i

    printed on the creen 8hey 0e the n0mber ' or o and 1 or on in the ame 5ay a D9*; and B7IH8 do, b0t

    thoe are the only poibilitie I yo0 do IE7;E 1, then the dot pattern printed 5ill be the invere o their 00al

    orm: paper dot 5ill be replaced by in dot and vice vera 8h0 a 5o0ld be printed a

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    55/116

    I (a at 5itchJon) 5e have blac in and 5hite paper, then thi a 5ill appear a 5hite on blac J b0t 5e till have

    blac in and 5hite paper at that character poition It i the dot that have chan4ed

    8he tatement

    AER 1

    et into action a partic0lar ort o overprintin4 ormally 5hen omethin4 i 5ritten into a character poition it

    completely obliterate 5hat 5a there beore> b0t no5 the ne5 character 5ill imply be added in on top o the old one

    (b0t ee Exercie 1) 8hi can be partic0larly 0e0l or 5ritin4 compoite character, lie letter 5ith accent on

    them, a in thi pro4ram to print o0t Herman letter J an o 5ith an 0mla0t above it (-o E/ irt)

    10 AER 120 'R =1 T 3230 PRINT $o$ (4RS 8$$$$)0 NEHT

    (notice the control character C7; % 5hich bac 0p one pace)

    8here i another 5ay o 0in4 IG, +*+E7 and o on 5hich yo0 5ill probably ind more 0e0l than havin4 them a

    tatement 6o0 can p0t them a item in a +7I8 tatement (ollo5ed by >), and they then do exactly the ame athey 5o0ld have done i they had been 0ed a tatement on their o5n, except that their eect i only temporary: it

    lat a ar a the end o the +7I8 tatement that contain them 8h0 i yo0 type

    PRINT P?PER $G$6 PRINT $;$

    then only the x 5ill be on yello5

    IG and the ret 5hen 0ed a tatement do not aect the colo0r o the lo5er part o the creen, 5here command

    and I+8 data are typed in 8he lo5er part o the creen 0e the colo0r o the border or it paper colo0r and code

    & or contrat or it IG colo0r, ha lahin4 o, and everythin4 at normal bri4htne 6o0 can chan4e the border

    colo0r to any o the ei4ht normal colo0r (not % or &) 0in4 tatement

    R@ERcolo0r

    /hen yo0 type in I+8 data, it ollo5 thi r0le o 0in4 contratin4 in on border colo0red paper> b0t yo0 can

    chan4e the colo0r o the caption 5ritten by the comp0ter by 0in4 IG and +*+E7 (and o on) item in the I+8

    tatement, F0t a yo0 5o0ld in a +7I8 tatement 8heir eect lat either to the end o the tatement, or 0ntil ome

    I+8 data i typed in, 5hichever come irt 8ry

    INP*T 'L?S4 1 IN7 1$W:at #" ;our umberC$

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    56/116

    8here i one more 5ay o chan4in4 the colo0r by 0in4 control character J rather lie the control character or *8

    and 8*B in Chapter 1"

    C6RK *@correpond to IG

    C6RK *correpond to +*+E7

    C6RK *Gcorrepond to D9*;

    C6RK *+correpond to B7IH8C6RK :Fcorrepond to IE7;E

    C6RK :*correpond to E7

    8hee are each ollo5ed by one character that ho5 a colo0r by it code: o (or intance)

    PRINT (4R< 1+(4R< F F F

    ha the ame eect a

    PRINT IN7 F F F

    n the 5hole, yo0 5o0ld not bother to 0e thee control character beca0e yo0 mi4ht F0t a 5ell 0e the colo0r

    item o5ever, one very 0e0l thin4 yo0 can do 5ith them i p0t them in pro4ram: thi re0lt in dierent partbein4 lited in dierent colo0r, to et them apart rom each other or even F0t to loo pretty 6o0 m0t p0t them in

    ater the line n0mber, or they 5ill F0t 4et lot

    8o 4et thee into the pro4ram, yo0 have to enter them rom the eyboard, motly 0in4 extended mode 5ith the

    di4it

    8he di4it ' to $ et the correpondin4 colo0rJ in i C*+; ;ID8 i alo preed, paper i it i not ore preciely,

    i yo0 are in E mode and yo0 pre a di4it (let 0 ay # or yello5> at any rate it ha to be bet5een ' and $ J not % or

    &) then t5o character are inerted: irt C7; 1$ or +*+E7, and C7K # meanin4 et it to yello5 I yo0 had been

    prein4 C*+; ;ID8 5hen yo0 preed the di4it, yo0 5o0ld have 4ot C7; 1# meanin4 et in colo0r intead o

    C7; 1$

    ;ince thee are t5o character yo0 can 4et ome odd eect 5hen yo0 r0b them o0t J yo0 m0t pre -E9E8Et5ice, and ater the irt time yo0 5ill oten 4et a ?0etion mar or even odder thin4 appearin4 -o not 5orry> F0t

    pre -E9E8E a4ain

    andcan alo behave tran4ely 5hile the c0ror i movin4 pat the control character

    ;till in extended mode,

    % 4ive C7K 1& and C7K ' or normal bri4htne

    & 4ive C7K 1& and C7K 1 or extra bri4htne

    C*+; ;ID8 5ith % 4ive C7K 1% and C7K ' or no lahin4

    C*+; ;ID8 5ith & 4ive C7K 1% and C7K 1 or lahin4

    8here are a co0ple more in ordinary (9) mode:

    C*+; ;ID8 5ith 3 4ive C7; 2' and C7K ' or normal character

    C*+; ;ID8 5ith ! 4ive C7K 2' and C7; 1 or invere character

    8o 0mmariLe here i a complete decription o the top ro5 on the eyboard:

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    57/116

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    58/116

    Dor intance, i the character poition i lahin4 and normal 5ith yello5 paper and bl0e in then the o0r n0mber

    that 5e have to add to4ether are 12%, ', %Q#=!% and 1, main4 1$$ alto4ether 8et thi 5ith

    PRINT ?T 0,0 'L?S4 1 P?PER IN7 1$ $ ?TTR 0,0

    E0ercises

    1 8ry

    PRINT $$ (4R< 8 AER 1$/$

    /here the R ha c0t thro04h the B, it ha let a 5hite dot 8hi i the 5ay overprintin4 5or on the . ;pectr0m:

    t5o paper or t5o in 4ive a paper, one o each 4ive an in 8hi ha the interetin4 property that i yo0 overprint

    5ith the ame thin4 t5ice yo0 4et bac 5hat yo0 tarted o 5ith I yo0 no5 type

    PRINT (4R< 8 AER 1$r9

    5hy do vo0 recover an 0nblemihed BA

    P?PER 06 IN7 0

    J int it F0t a 5ell that thee dont aect the lo5er part o the creenA o5 type

    R@ER 0

    and ee ho5 5ell the comp0ter loo ater yo0M

    3 70n thi pro4ram:

    10 P7E [email protected]), [email protected] T 10

    ever mind ho5 thi 5or> it i chan4in4 the colo0r o ?0are on the televiion creen and the 7- ho0ld

    en0re that thi happen randomly 8he dia4onal tripe that yo0 event0ally ee are a manietation o the hidden

    pattern in 7- J the pattern that mae it pe0dorandom intead o tr0ly random

    ! 8ype or 9*- in the che piece character in Chapter 1!, and then type in thi pro4ram 5hich dra5 a dia4ram

    o a che poition 0in4 them

    5 REM %ra ba boar%10 LET bb=16 LET b=26 REM re% a% bue Bor boar%15 P?PER b6 IN7 bb6 (LS20 PLT ,1286 REM bor%er30 @R?W 5,06 @R?W 0,5

    )0 @R?W 5,06 @R?W 0,550 P?PER bb0 REM boar%0 'R =0 T 36 'R m=0 T 380 PRINT ?T +2., 11+2.m$ $0 PRINT ?T +2., 10+2.m$ $100 NEHT m6 NEHT 110 P?PER 8120 LET p=6 LET pb=56 REM coour" oB :#te a% bac p#ece"200 @IM b< 8,8-6 REM po"#t#o" oB p#ece"

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    59/116

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    60/116

    30 NEHT

    otice that pixel coordinate are rather dierent rom the line and col0mn in an *8 item 6o0 may ind the dia4ram

    in Chapter 1" 0e0l 5hen 5orin4 o0t pixel coordinate and line and col0mn n0mber

    8o help yo0 5ith yo0r pict0re, the comp0ter 5ill dra5 trai4ht line, circle and part o circle or yo0, 0in4 the

    -7*/ and CI7C9E tatement

    8he tatement -7*/ to dra5 a trai4ht line tae the orm

    @R?W0,y

    8he tartin4 place o the line i the pixel 5here the lat +98, -7*/ or CI7C9E tatement let o (thi i called

    the +98 poition> 7, C9E*7, C9; and E/ reet it to the bottom let hand corner, at (',')), and the inihin4

    place i0pixel to the ri4ht o that andypixel 0p 8he -7*/ tatement on it o5n determine the len4th and

    direction o the line, b0t not it tartin4 point

    Experiment 5ith a e5 +98 and -7*/ command, or intance

    PLT 0,1006 @R?W 80,35PLT 0,1506 @R?W 80,35

    otice that the n0mber in a -7*/ tatement can be ne4ative, altho04h thoe in a +98 tatement cant

    6o0 can alo plot and dra5 in colo0r, altho04h yo0 have to bear in mind that colo0r al5ay cover the 5hole o a

    character poition and cannot be peciied or individ0al pixel /hen a pixel i plotted, it i et to ho5 the 0ll in

    colo0r, and the 5hole o the character poition containin4 it i 4iven the c0rrent in colo0r 8hi pro4ram

    demontrate thi:

    10 R@ER 06 P?PER 06 IN7 6 (LS6 REM bac oue "cree20 LET G1=06 LET ;1=06 REM "tart oB #e30 LET c=16 REM Bor # coour, "tart#& bue

    )0 LET G2=NT [email protected] LET ;2=NT [email protected] ra%om B##": oB #e

    50 @R?W IN7 cG2G1,;2;10 LET G1=G26 LET ;1=;26 REM eGt #e "tart" :ere a"t oe B##":e%0 LET c=c+16 I' c=8 T4EN LET c=16 REM e coour80 T )0

    8he line eem to 4et broader a the pro4ram 4oe on, and thi i beca0e a line chan4e the colo0r o all the ined

    in pixel o all the character poition that it pae thro04h ote that yo0 can embed +*+E7, IG, D9*;,

    B7IH8, IE7;E and E7 item in a +98 or -7*/ tatement F0t a yo0 co0ld 5ith +7I8 and I+8

    8hey 4o bet5een the ey 5ord and the coordinate, and are terminated by either emicolon or comma

    *n extra rill 5ith -7*/ i that yo0 can 0e it to dra5 part o circle intead o trai4ht line, by 0in4 an extra

    n0mber to peciy an an4le to be t0rned thro04h: the orm i

    @R?Wx,y,a

    0andyare 0ed to peciy the inihin4 point o the line F0t a beore and ai the n0mber o radian that it m0t t0rn

    thro04h a it 4oe J iai a poitive it t0rn to the let, 5hile i ai a ne4ative it t0rn to the ri4ht *nother 5ay o

    eein4 ai a ho5in4 the raction o a complete circle that 5ill be dra5n: a complete circle i 2n radian, o i a= it5ill dra5 a emicircle, i a=' "Qa ?0arter o a circle, and o on

  • 8/11/2019 Sinclair ZX Spectrum BASIC programming

    61/116

    Dor intance 0ppoe a= 8hen 5hatever val0e0andytae, a emicircle 5ill be dra5n 70n

    10 PLT 100,1006 @R?W 50,50, P

    5hich 5ill dra5 thi:

    inih at (1"',1"')

    tart at (1'',1'')

    8he dra5in4 tart o in a o0thJeaterly direction, b0t by the time it top it i 4oin4 northJ5et: in bet5een it ha

    t0rned ro0nd thro04h 1%' de4ree, or radian (the val0e o a)

    70n the pro4ram everal time, 5ith +l replaced by vario0 other expreion e4 J+I, +IR2, 3Q+IR2, +IR!, 1,'

    8he lat tatement in thi chapter i the CI7C9E tatement, 5hich dra5 an entire circle 6o0 peciy the

    coordinate o the centre and the radi0 o the circle 0in4

    (IR(LEx coordinate, y coordinate, radi0

    U0t a 5ith +98 and -7*/, yo0 can p0t the vario0 ort o colo0r item in at the be4innin4 o a CI7C9E