ebook img

Input - Vol 1 No 03 (1997)(Marshall Cavendish)(GB) PDF

36 Pages·1997·11.8 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Input - Vol 1 No 03 (1997)(Marshall Cavendish)(GB)

3 A MARSHALL CAVENDISH COMPUTER COURSE IN WEEKLY PARTS LEARN PROGRAMMING - FOR FUN AND THE FUTURE HOW TO ORDER YOUR BINDERS UK and Republic of Ireland: Send £4.95 (inc p & p) (1R£5.45) for each binder to the address below: Marshall Cavendish Services Ltd, Department 980, Newtown Road, Hove, Sussex BN3 7DN Australia: See inserts for details, or Vol 1 (cid:9) No 3 write to INPUT, Gordon and Gotch Ltd, PO Box 213, Alexandria, NSW MACHINE CODE 3 2015 New Zealand: See inserts for details, GET DOWN TO LOW-LEVEL LANGUAGES (cid:9) 65 or write to INPUT, Gordon and Gotch (NZ) Ltd, PO Box 1595, Wellington Malta: Binders are available from Learn why BASIC is limited for some jobs, and get to know local newsagents. machine code and assembly language GAMES PROGRAMMING 3 FUN-TO-PROGRAM MAZE GAMES (cid:9) 68 Create your own maze on the screen, complete with travelling 'eater BACK NUMBERS APPLICATIONS 2 -(cid:127)(cid:127)(cid:127)(cid:127) n(cid:127)n.- Copies of any part of INPUT can be obtained from the following addresses at the regular cover price, with no extra charge for postage and packing: STREAMLINE YOUR HOBBIES FILE-2 (cid:9) UK and Republic of Ireland: -1111M INPUT, Dept AN, Marshall Cavendish Services, Now you've set up your datafile, here's how to Newtown Road, Hove BN3 7DN search it, amend or delete your records Australia, New Zealand and Malta: Back numbers are available through your local newsagent MACHINE CODE 4 (cid:9) 8011 DRAW A FIRE-BREATHING DRAGON COPIES BY POST Another useful character to add to your stock Our Subscription Department can supply your copies direct to you regularly at £1.00 BASIC PROGRAMMING 5 each. For example the cost of 26 issues is £26.00; for any other quantity simply multiply the number of issues required by £1.00. These rates apply anywhere in the 84 world. Send your order, with payment to: HOW TO PLOT, DRAW, LINE AND PAINT (cid:9) Subscription Department, Marshall Cavendish Services Ltd, Newtown Road, Hove, Sussex BN3 7DN Get started on computer art Please state the title of the publication and the part from which you wish to start. BASIC PROGRAMMING 6 HOW TO PAY: Readers in UK and Republic of Ireland: All cheques orpostal orders for binders, back numbers and copies by post should be made payable to: [VARIABLES: THE MYSTERY EXPLAINED (cid:9) 92. Marshall Cavendish Partworks Ltd. QUERIES: When writing in,please give the make and model of your computer,as Learn to understand all those Xs and Ys well as the Part No., page and line where the program is rejected or where it does not work. We can only answer specific queries— and please do not telephone. Send your queries to INPUT Queries, Marshall Cavendish Partworks Ltd, 58 Old INDEX Compton Street, London W1V SPA. The last part of INPUT, Part 52, will contain a complete, cross-referenced index. For easy access to your growing collection, a cumulative index to the contents of each issue is contained on the inside back cover. INPUT IS SPECIALLY DESIGNED FOR: The SINCLAIR ZX SPECTRUM (16K, 48K,128 and +), COMMODORE 64 and 128, ACORN ELECTRON, BBC B PICTURE CREDITS and B+, and the DRAGON 32 and 64. . Front cover: Dave King. Pages 65-67, Malcolm Harrison. Pages 68-74, illustrations, Peter Bentley; photos, John Darling. Pages In addition, many of the programs and explanations are also 75-79, Dave King. Pages 80-83, Julek Heller. Pages 84-91, suitable for the SINCLAIR ZX81, COMMODORE VIC 20, and illustrations, Colin Hadley; photos, John Darling. Pages 92-96, Dave King. TANDY COLOUR COMPUTER in 32K with extended BASIC. Programs and text which are specifically for particular machines are indicated by the following symbols: © Marshall Cavendish Limited 1984/5/6 All worldwide rights reserved. SPECTRUM 16K, The contents of this publication including software, codes, listings, 4814128, and + COMMODORE 64 and 128 ‘: graphics, illustrations and text are the exclusive property and copyright of Marshall Cavendish Limited and may not be copied, reproduced, El ACORN ELECTR O1N, 4% transmitted, hired, lent, distributed, stored or modified in any form BBC B and B+ DRAGON 32 and 64 whatsoever without the prior approval of the Copyright holder. 11.. TANDY TRS80 Published by Marshall Cavendish Partworks Ltd, 58 Old Compton Street, London W1V 5PA, 7181 (cid:9) VIC 20 COLOUR COMPUTER England. Printed by Artisan Presss, Leicester and Howard Hunt Litho, London. n WHAT IS MACHINE CODE? n ADVANTAGES OF MACHINE CODE OVER BASIC n UNDERSTANDING OPCODES AND ASSEMBLY LANGUAGE Faster action and smoother thing to happen at a time. If a gun fires, for Machine code is a computer language that movement across the screen are just example, the rest of the action has to stop, if is composed entirely of numbers which are only for a split second. And if your program is the direct equivalent of those the computer two of the advantages you get from burdened down with IF ... THEN statements, uses. So when you use machine code you are machine code in games the pace will slow to an excruciating crawl. no longer expecting the computer to speak programming. First, though, you This is where the serious games pro- your language. You are, in effect, commu- need to know how machine code grammer leaves BASIC behind and moves nicating with it in its own language. actually affects your computer onto what the arcade-game programmers use, As one example (in this case from the machine code, which is—as near as makes no Spectrum), machine code looks like this: Most home computer users find BASIC more difference—the language of the computer B9 28 08 than adequate for all their programming itself. needs. It can be used to write simple games, In BASIC this is equivalent to: WHAT IS MACHINE CODE? business and household programs. What's 100 IF A=C THEN GOTO 190 more, it is fairly easy to learn. It is also The reason BASIC is slow is that it is a high flexible, simple to use and easy to adapt for level language. That means it uses English The machine code, as you'll see, is a series of different machines. words, plus operations that are familiar from two-digit numbers. The letter B in this line of The problem is that it is slow and cumber- ordinary arithmetic, which are easy for machine code is, in fact, a figure. It represents some. BASIC programs eat up a lot of human beings to learn and understand. 11 in hexadecimal arithmetic. memory. The computer finds these great But your computer does not think in These hexadecimal numbers are fed direct- wodges of programming unwieldy to handle. English, nor does it understand standard ly into the computer's memory. Operating Once you get into games programming human arithmetic symbols. In fact, it does not instructions, data, numbers, letters, words seriously, you will find that using BASIC think or understand anything. It operates on and memory addresses are all represented in makes movements jerky and allows only one electrical pulses which represent numbers. machine code by these two digit numbers. And the computer can tell the difference whole set of machine level instructions for one 140 CLSO between instructions, addresses and data only written command—just a simple fast, 150 PRINT ©0,"THIS IS MACHINE by the order in which they occur. For number-for-number conversion. CODE!" example, the first number in any program This means that you can write programs so 160 FOR N=1 TO 1000:NEXT must represent an instruction. If, by mistake, that they will be short and efficient to run. 170 N= USR0(0) you keyed in something as your first entry As an example of the comparative speeds of 180 FOR N =1 TO 2000:NEXT which contained a number, such as an address the two systems, try the following program 190 DATA 206, 136, 240, 142, 4, or some data, the computer would try to read which does the same thing using both BASIC 32,166,192,167,128,140,6,0, it as an instruction when the program was and machine code: 38,247,57 executed. Absolute accuracy is needed when you key in these numbers, otherwise the program will 1 MODE 6 10 PRINT "Dill-HIS IS SLOW BASIC": not work and it may even be wiped from the 10 FOR T= &1000 TO &101C POKE 53280,7:POKE 53281,7 memory when you try to RUN it. 20 READ A:?T= A 20 FOR Z=0 TO 959:POKE 55336 + Z,1: 30 NEXT POKE 1064 +Z,46:NEXT Z ADVANTAGES OF MACHINE CODE 40 PRINT"THIS IS SLOW BASIC" 30 FOR Z=1 TO 1000:NEXT Z When you key in a line of BASIC the 60 FOR T= &6100 TO &7FFF 40 PRINT "0" computer has to interpret it into its own 70 ?T=?(T+ &2000) 50 FOR Z=1 TO 1000:NEXT Z operating language before it can execute it. 80 NEXT 100 PRINT"RINOW THIS IS FAST MACHINE This is a cumbersome process. It takes up a 90 CLS CODE":POKE 53280,10:POKE lot of time and as BASIC statements rarely 100 PRINT"NOW THIS IS FAST MACHINE 53281,10 translate directly into a single machine code CODE" 105 FOR Z=1 TO 2000:NEXT Z procedure, it often results in much more 105 DUMMY= INKEY(100) 110 FOR Z=0 TO 19:READ X:POKE 832+Z, involved operating procedures than are really 110 CALL &1000 X:NEXT Z necessary. 120 END 120 SYS 832 When a whole program written in BASIC 200 DATA 162,96,160,0,185,0,128,153,0, 130 DATA 162,0,169,46,157,40,4,157, is RUN, each line has to be interpreted into the 96,200,208,247,232,138,24,105,32,141,6, 0,5,157,0,6 machine's operating numbers, then perfor- 16,142,9,16,224,128,208,230,96 135 DATA 157, 232,6,232,208,241,96 med, before the computer can go onto the NEI next line. If it goes round and round a loop in the BASIC program, it will have to interpret 10 CLEAR 29999 10 PRINT" (cid:9) iTHIs IS SLOW BASIC":POKE 36879,8 each line again each time it gets to it. 20 FOR n=30000 TO 30011 20 FOR Z=0 TO 483:POKE Everytime the computer comes across an 30 READ a 7702 + Z,46:NEXTZ instruction in BASIC it will have to perform 40 POKE n,a 30 FOR Z=1 TO 1000:NEXT Z the following procedure: 50 NEXT n 40 PRINT "a" (cid:127) Look at the instruction 60 PRINT "This is BASIC" 50 FOR Z=1 TO 1000:NEXT Z (cid:127) Translate the instruction into a series of 70 FOR n=16384 TO 22527 machine code instructions 80 POKE n,PEEK (n-16384) (cid:127) Perform the machine code instructions one 90 NEXT n by one 100 CLS (cid:127) Move onto the next instruction 110 PRINT "Now—MACHINE CODE" It is not difficult to see how slow this 120 PAUSE 100 process can be if it has to be performed over 130 RANDOMIZE USR 30000 and over again. 140 STOP It is possible to compile BASIC—that is 150 DATA 33,0,0,17,0,64,1,0,24,237, translate the whole program into machine 176, 201 code in one go before it is RUN—by the use of tgi commercially available compiling programs. But this still results in unnecessarily long, 10 CLEAR 200,31000 unwieldy programs. The most convenient 20 DEFUSRO= 31000 analogy for the difficulties this interpretation 30 FOR N=31000 TO 31015 process causes is the manual for a car or 40 READ A camera that has been written in Japanese and 50 POKE N,A translated into English. 60 NEXT In comparison, the step from machine code 70 CLSO into the computer's own operating process 80 PRINT 40,"THIS IS BASIC!" hardly takes any time at all. Each number in 90 FOR N=1 TO 500:NEXT machine code translates directly into a certain 100 FOR N=1056 TO 1535 configuration of the electrical pulses the 110 POKE N,PEEK(N +34000) computer operates on. There is no laborious 120 NEXT interpretation routine, no substitution of a 130 FOR N=1 TO 1000:NEXT 100 PRINTIR NOW FAST MACHINE CODE" One way round some of these problems 105 FOR Z=1 TO 2000:NEXT Z to step back and write your program in 1- 110 FOR Z=0 TO 13:READX:POKE language one level higher than machine code 10 FOR T=4047 TO 4123 832 +Z,X:NEXT Z This is known as assembly language, explainec 20 READ A:?T = A 120 SYS 832 in detail in later articles. 30 NEXT T: CALL 4047 130 DATA 162,0,169,46,157,22,30, In assembly language, machine code °per] 40 DATA 160,0,185,0,128,41,7,9,144,153, 157,0,31,232,208,245,96 ations are represented by abbreviations. Foi 0,124,200,169,255,153,0,124,200,192, example, LD means load and J means jumi 0,208,235 As you will see, all the program does is to fill (though the syntax of these instructions varie] 50 DATA 174,211,15,232,224,255,208,2,162, the screen with rubbish. But you will see how between microprocessors). 128,142,211,15,174,218,15,232,224, much quicker the machine code routine per- With some practice it is as easy to reac 128,240,8,142,218,15 forms the task. assembly language as it is to read BASIC 60 DATA 142,224,15,208,206,162,124,142, though understanding the structure of th( 218,15,142,224,15,24,173,217,15,105,1, ASSEMBLY LANGUAGE language is a bit more complicated than the 41,1,141,217,15,141,223,15,76,209,15 One disadvantage of machine code is that it equivalent BASIC. MI !HI is extremely difficult to write and debug. The disadvantage of assembly language Few people can remember the codes for the that the computer cannot read it directly. Ii 10 CLEAR 200,31000 various instructions. Even if you can, the has to be assembled by an assembler—which jE 20 DEFUSRO= 31000 operation codes—or opcodes—are indistin- a program in either BASIC or machim 30 FOR N =31000 TO 31020 guishable from the numbers that are fed in as code—before it will work. 40 READ A data or addresses. So you can't understand But this is a simple process compared witl- 50 POKE N,A any section of a machine code program unless interpreting BASIC. Assembly language 60 NEXT you read the whole program from the begin- equivalent to machine code—that is, ever) 70 N = USR0(0) ning, which is no help when you are trying assembly language instruction corresponch 80 POKE 32767,RND(256) —1 to pick out bugs. directly with a machine code operation. It car 90 FOR N=1 TO 100:NEXT The opcode numbers differ considerably be translated word for word, number b) 100 GOTO 70 between different microprocessors, so that number, instruction by instruction into th( 110 DATA 142,4,0,206,136,184,166,192, translating machine cdde programs from ma- computer's operating code. 184,127,255,138,129,167,128,140,6,0, chine to machine can be very difficult. The assembler also translates the whop 38,242,57 program in one go before the compute] executes it, rather than interpreting it line 1)) line while the program is RUNning. This give: 20 FOR Z=1 TO 26 "au it further speed advantages. 30 PRINT 0111111[110 CI 1=1 I=1 I=1 The BBC and Electron computers com( qqqq 0000qqqqqqq with an assembler program built in. For th( qq EIqq EOCI qqq 0qqq "; Spectrum, Dragon, Tandy and Commodore. 40 NEXT Z assemblers are available commercially; of 100 FOR Z=0 TO 25:READ X:POKE 832+ you can use those provided in a later issue o Z,X:NEXT Z Input. But for the moment, if you do not 110 SYS 832:FOR Z=1 TO 50:NEXT Z: have an assembler, you can hand-assemble GOTO 110 programs. Even on the simplest programs yot 120 DATA 162,0,200,192,7,208,2,160, will find it easier to write them in assembl 0,152,157,0,216,157,0,217,157,0,218 language, then translate them by hand fron 130 DATA 157,232,218,232,208,233,96 the machine code tables given in any machim code book for your machine. All this might seem like a lot of bother. Bu 20 FOR Z=1 TO 25 before you decide this is so, try out thes( 30PRINT"allI0CIOCIDEIDOEI machine code programs. Each is fed in a qqq DED qqqqqq "; DATA statements in BASIC programs. Thy 40 NEXT Z DATA is listed at the end. 100 FOR Z=0 TO 19:READ X:POKE 832+ Z,X:NEXT Z 110 SYS 832:FOR Z=1 TO 50:NEXT Z:GOTO 10 CLEAR 29999 110 20 FOR n =30000 TO 30020 120 DATA 162,0,200,192,7,208,2, 30 READ a 160,0,152,157,0,150 40 POKE n,a 130 DATA 157,0,151,232,208,239,96 50 NEXT n 60 RANDOMIZE USR 30000 Now—just for comparison—see if you can 70 GOTO 60 write a similar program in BASIC. We think 80 DATA 17,0,88,46,0,237,95,71,58,140, you will see our point. Later on, we shall look 92,128,230,63,103,1,0,3,237,176,201 at what is happening here. Sophisticated maze games require long programs. But you can design simple ones—and learn important principles—with not much more than a loop and a DATA statement Maze games have a perennial fascination for 9020 DATA"p.pp.pp.pp.pp.p" 50 LET x=10 computer owners, and variations on that well- 9030 DATA " p . p (cid:9) p p" 60 LET y=14 known gobbling man continue to pour from 9040 DATA"p...p.p.p.p (cid:9) p" 1000 PRINT PAPER 6; INK 2;AT x,y;"*" the software houses. 9050 DATA"p.ppp.p.p.ppp (cid:9) p" 1010 LET xx=x This article shows you how to jump on the 9060 DATA "p p p P" 1020 LET yy = y bandwagon in a small way by writing a maze 9070 DATA"pppp.pp.pp.pppp" 1030 IF 1NKEY$ = "" THEN GOTO 1030 game of your own. 9080 DATA "p p p P" 1040 IF 1NKEY$ ="p" AND ATTR (x —1,y) The maze in this first attempt includes no 9090 DATA"p.ppp.p.p.ppp.p" < >9 THEN LET x= x-1 `enemies' or obstacles, which would require a 9100 DATA"p...p.p.p.p p" 1050 IF INKEY$="I" AND ATTR (x +1,y) big lump of program. But it does show how to 9110 DATA " p . p (cid:9) p p" < >9 THEN LET x=x+1 program so that your main character cannot 9120 DATA "p. pp. pp. pp. pp (cid:9) p" 1060 IF 1NKEY$ = "z" AND ATTR (x,y — 1) walk through the walls—the basis of all maze 9130 DATA "p (cid:9) p" < > 9 THEN LET y= y-1 games. And it includes scoring and timing, 9140 DATA "PPPPPPPPPPPPPPP" 1070 IF INKEY$="x" AND ATTR (x,y + 1) and a 'best score' routine, to add competitive < >9 THEN LET y=y+1 interest. Lines 100, 120, 150 and 160, using a pair of 1080 PRINT INK 7;AT xx,yy;"q " the now-familiar FOR ... NEXT loops, set out a 1090 GOTO 1000 the boundaries of the maze. Line 130 PRINTS a full stop in each square. If you've already tried your hand at keyboard The easiest way to understand how the Then Lines 110 and 140 take over, control (pages 54 to 59), most of these lines Spectrum maze game works is to enter it in READing the DATA in Lines 9000 to 9140 and will already be familiar. Like all famous stages. So start by building the maze itself: replacing the full stop with a space—but in games characters, our asterisk friend is 100 FOR n=3 TO 17 blue ink on blue paper—everywhere that the hatched at x,y and is moved around—with 110 READ a$ DATA pattern shows the letter p. They can do your help—by a series of INKEY$ lines. 120 FOR m=7 T021 this because the Spectrum will not normally Here, however, there is a vital difference: 130 PRINT AT n,m;"." accommodate two characters at the same he can move onto a particular square only if it 140 IF a$(m-6)="p" THEN PRINT location, so the second one replaces the first. isn't blue. That is, if ATTR does not equal 9-9 PAPER 1; INK 1;AT n,m;"q " being the numerical value of the colours in CREATING THE 'EATER' 150 NEXT m which you have craftily PRINTed the lines of 160 NEXT n A maze is pretty useless, however, without your maze. 9000 DATA"ppppppppppppppp" someone or something to move around in it. 9010 DATA "p (cid:9) So RUN the program and then add: n THE PRINCIPLES OF CREATING n HOW TO MAKE A CHARACTER A MAZE GAME MOVE THROUGH THE MAZE n SETTING OUT A MAZE USING n MAKING YOUR MAZE WALLS LOOPS AND DATA STATEMENTS `CHARACTER PROOF' n SCORING AND TIMING n DESIGNING YOUR OWN MAZE In case you feel like creating a maze of a just left, and obliterate, by PRINTing a space, 23673)/50 different colour, here is how you work out the the full stops he has 'eaten'. These are useful 2010 PRINT AT 1,6;t;"q SECONDSq " value of ATTR: lines in any game, but note where they 2020 IF t < bt THEN LET bt = t 1 Take the code for the INK colour, as come—the first lines just before the INKEY$ 2030 PRINT AT 19,4;"BEST TIMED "; printed on the computer keyboard—in this `action', the final line straight after it. bt;" q SECONDS" case, 1. Result, 1. Note that Line 1090 is only temporary and 2 Take the code for the PAPER colour—in will be overwritten later. Its purpose is to To test this you need to add a temporary line. this case, 1—and multiply by 8. Result, 8. allow you to RUN the program and test it— Remember to delete it later: 3 Add 64 if the area concerned is BRIGHT. without the loop that this sets up, the charac- Result, in this case, 0. ter would only move one square. 1100 GOTO 1000 4 Add another 128 if the area concerned is SCORING AND TIMING in FLASH. Result, in this case, 0. The scoring bit is quite straightforward. Line Then you add all the numbers together. Now you have the makings of a simple game. 40 sets the initial score at zero. Line 1090 The result is the ATTR number. To make it playable—in the absence of adds 1 to the score each time your character ATTR on the Spectrum has other uses than `enemies', which would require a long lump eats a full stop, or rather, each time it crosses a preventing a character from entering a pro- of program—the best thing is a scoring and square whose INK and PAPER are not both. hibited area. In a bigger maze, for example, timing routine. So add these extra lines: white—ATTR, again. And Line 1025, which you could use the same idea to make a comes into action when all have been eaten, character explode or burn up if it did enter the 10 LET bt =100000 tells the computer to check out (at Line 2000) forbidden zone. For a red INK, red PAPER 40 LET s = 0 how long the player has taken. maze you would need a line starting 990 POKE 23672, 0: POKE 23673, 0 The timing part is a little more difficult 1025 IF S = 110 THEN GOTO 2000 IF ATTR (x,y) =18 THEN ... 1090 IF ATTR (x,y) < > 63 THEN LET In the meantime Lines 1010, 1020 and 1 U:0 s = s + 1: BEEP .005,10 define the position which your charactera* 2000 LET t= (PEEK 23672 + 256*PEEK until you understand PEEKs and POKES, ex- 590 DATA A . . . A ......... A . . . A plained fully in a later article. But, in brief, la 600 DATA A. AAA. AA. A. AA . AAA . A Line 990 sets the Spectrum 'clock' at zero by The Acorn maze program is built up in three 610 DATA A . . . A . A ..... A . A . . . A POKEing 0 into the relevant memory loca- parts. The first part draws the maze on the 620 DATA A . A AAA A A tions. Line 2000 counts the number of screen, the second part lets you move your 630 DATAA.AA AA (cid:9) AA.AA.A frames displayed on your TV screen since the `man' about, eating up all the dots, and the 640 DATA A (cid:9) AAA (cid:9) A game started, then divides by 50—frames third times the game and PRINTS the score. 650 DATA AAAAAAAAAAAAAAAAAAA per second on a British TV screen. Here is the first part. Type it in, then RUN The main part of this program is the list of At the same time, Line 10 sets the original it, and you should see a green maze on the left DATA statements. These define the shape of `best time' at 100,000—far longer than any- of the screen with yellow dots marking the the maze, with As representing the walls and one is likely to take, so that it must be beaten— pathways through the maze. each line of DATA representing a single line of while Line 2020 compares the newest time 10 MODE 1 the maze. This is a very convenient way of with 'best time'. 30 VDU 23,224,255,255,255,255,255, writing out the DATA, as you can see at a ANOTHER GO? 255,255,255 glance the shape of the maze and you can also 40 VDU 19,1,4;0;19,3,5;0; see mistakes more easily. To give the player the chance to try again, you 50 CLS All the rest of the program does is convert will need these extra lines. First enter 80 VDU 5 each A into a blue block and PRINT it in the 1CAPS SHIFT and 1BREAKI, then 'ENTER . 85 q right position on the screen. It sounds easy, 2040 PRINT AT 21,2;"PRESS ANY KEY 110 FOR row = 8 TO 23 but there are one or two complications. TO PLAY AGAIN" 120 READ A$ HOW IT WORKS 2050 IF 1NKEY$ < > "" THEN GOTO 2050 130 FOR column =1 TO 19 First of all there is no block character on the 2060 IF 1NKEY$ = "" THEN GOTO 2060 140 MOVE colume32, rowl2 keyboard so you have to make your own. The 2070 RESTORE 150 IF MI D$ (A$,column,1 ) = "A" THEN simplest way is to define a solid UDG 2080 GOTO 40 GCOL 0,1:PRINT CHR$224 ELSE GCOL character using VDU 23, as Line 30 does. 0,2: PRINT "." RESTORE resets to the beginning of the DATA The next problem is that the colours 160 NEXT column list. available in Mode 1 are black, red, yellow and 170 NEXT row 460 q white, whereas we want a green maze. Line 40 OTHER MAZES uses VDU 19 to redefine the colours so that red 500 DATA AAAAAAAAAAAAAAAAAAd If you want to try other mazes within the same 510 DATA A (cid:9) AAA (cid:9) appears as blue, but don't worry if you don't dimensions as this one, you can do so simply 520 DATA A . AA . AA (cid:9) AA . AA . understand how this works at the moment. by changing the pattern of letter p's in the 530 DATA A A AAA A d Working with colours is the subject of a whole DATA statements, Line 9000 onwards. For a 540 DATA A . A . A ..... A . A . . . separate article. bigger or smaller maze, you would also have 550 DATA. .AAA. A A. A . A .AA.AAA. 1 The last complication is the VDU 5 in Line to redefine the boundaries by altering the 560 DATA A . . . A ......... A . . . 50. This prints the text at the graphics numbers in Lines 100 and 120. 570 DATA AAA...A. AAA . A ...AA/ cursor so that all text is positioned using the If you do this, make sure you Have enough 580 DATA AAA . . . A . AAA . A . . . AA/ graphics coordinates. This means that, in- DATA to fill the whole maze, or you will get an error report before the maze even begins to appear on your screen. stead of using PRINT TAB(column,row), you 260 IF K$ = "L" AND POINT(X,Y — 32) < > 1 that may be there. (VDU 224 means the same as have to use MOVE column12,row*32 and then THEN Y = Y — 32 PRINT CHR$ 224 but in this case is more PRINT. (The number 32 converts the text 270 IF K$ = "P" AND POINT(X,Y + 32) < > 1 convenient.) The other number, 8, moves the coordinates into graphics coordinates. For THEN Y=Y+ 32 cursor back again so it is in the right position example, there are 40 columns in Mode 1. So 290 MOVE LX,LY :GCOL0,0 :VDU224 to PRINT the asterisk. Line 290 blanks out the if you multiply 40 by 32 you get 1280— 300 GOTO 200 asterisk's previous position. the width of the graphics screen.) If you RUN the program now you'll find the Now RUN the program. Z, X, P and L move the Now that the preliminaries are over, the asterisk moves rather slowly and hesitates asterisk as usual. program can get down to drawing out the each time it changes direction. The two other These lines are very similar to the routine maze. It is drawn half-way down on the left- extra lines, Lines 60 and 70, speed up the for moving a character given on pages 11 and hand side of the screen, between rows 8 and auto-repeat on the keys so the asterisk moves 12. But this time the program has to prevent 23 and columns 1 to 19. more quickly. *FX 11,15 sets the delay before the asterisk moving through the walls of the Lines 110 to 170 work like this. Line 110 auto-repeat starts to 15 hundredths of a maze. This is what the POINT function is there takes each row of the maze, then Line 120 second, while *FX 12,15 sets the rate of repeat for. reads the DATA for this row and calls it A$. to be the same. POINT(X,Y) checks the graphics colour at Line 130 takes each column in the row and There is one problem, though. When you the point X,Y. (It cannot check the text Line 140 moves the cursor to that position. finally press I ESCAPE you'll find the *FX colour—the computer has no function that Line 150 does all the work; if the character is commands are still in operation, making it does this. And that is why the text was printed an A it PRINTs a blue block; otherwise it more difficult to type because the keys repeat at the graphics cursor: so that the text PRINTs a yellow dot. MID$ is used to step too fast. LIST, for example, can come out as characters—the block and the dot—could be along the line of DATA one character at a time. LLIISSTT! The thing to do is press I BREAKI, then PRINTed in graphics colours and their num- type OLD and LIST. !BREAK' resets the FX to MOVING THROUGH THE MAZE bers checked.) normal. In this program, POINT works like this. So far all the program does is draw the maze. When a key is pressed, it looks at the colour of TIMING THE GAME Now enter these next few lines to move the the square you want to move to and lets you asterisk and eat up the dots. The program is not complete yet as there's no move as long as the colour is not blue—that timing and nothing happens when you've 60 *FX11,15 is, its number is not 1. If the square is blue, eaten all the dots. But the next few lines 70 *FX12,15 the line is ignored and the asterisk stays where remedy this: 190 LET X = 1012 : LET = 1612 it is. 200 MOVE X,Y: GCOL 0,0: VDU 224,8: Note the 32s creeping in again. Remember 20 LET besttime = 999999 GCOL 0,3: PRINT "*" that a jump of 32 is equivalent to moving by 90 LET dots = 0 220 LET LX = X: LET LY = Y one character position. 100 RESTORE 230 LET K$ = GET$ As for the other lines, Line -190 sets the 180 TIME= 0 240 IF K$ = "Z" AND POINT(X — 32,Y) < > 1 start position of the asterisk. Line'200 moves 210 IF dots = 154 THEN GOTO 310 THEN X= X-32 there and immediately PRINTs a black block, 280 IF POI NT(X + 12,Y— 20) = 2 THEN 250 IF K$ = "X" AND POINT(X + 32,Y) < > 1 using VDU 224 in order to wipe out any dot dots = dots + 1 : SOUND 0, — 15,1,1 THEN X=X+ 32 310 LET time = TIME/100 The next two lines are just 'housekeeping' 1030 FOR M = 0 TO 18 320 IF besttime > time THEN besttime = lines. *FX12,0 resets both the previous *FX 1040 IF PEEK(1024 + N*32 + M) = 65 time commands to normal and VDU 4 undoes the THEN POKE(1024 + N*32 + M),175 330 MOVE X,Y :GCOL0,0 :VDU 224 effects of VDU 5. 1050 NEXT M 340 *FX12,0 Finally, your time for this game and the 1060 NEXT N 350 VDU 4 best time are PRINTed out to the right of the 2000 DATA AAAAAAAAAAAAAAAAAA 360 VDU 23;8202;0;0;0; maze and you are invited to press RETURN to 2010 DATA A (cid:9) A A A (cid:9) 370 FOR delay = 1 TO 1000: NEXT play again. Line 450 waits for a key and if it is 2020 DATA A AA. AA (cid:9) AA . AA . 380 COLOUR3 RETURN l—code 13—then the game restarts. If 2030 DATA A . A AAA A 390 PRINT TAB(24,10) "TIMED =" any other key is pressed the program just 2040 DATA A . . . A . A ..... A . A . . . 400 PRINT TAB (24,11 );time;" El secs" keeps waiting and you'll have to press BREAK' 2050 DATA A. AAA. AA. A. AA. AAA. 410 PRINT TAB(24,13) "BEST TIME" when you've really finished. 2060 DATA A . . . A ......... A . . . 420 PRINT TAB(24,14);besttime;"q secs" 2070 DATA A A A . . . A . A A A . A . . . A A DESIGNING YOUR OWN MAZE 430 PRINT TAB(24,20) "PRESS RETURN" 2080 DATA A A A . . . A . A A A . A . . . A A 440 PRINT TAB(24,21) "TO PLAY AGAIN" To design your own maze, draw out the shape 2090 DATA A . . . A ......... A . . . 450 IF GET =13 THEN GOTO 50 ELSE on graph paper first. Then convert all the 2100 DATA A . AAA . AA . A . AA . AAA . GOTO 450 sections of wall to As and all the spaces to dots 2110 DATA A . . . A . A ..... A. A . . . and you've got your DATA statements for your 2120 DATA A . A AAA A First of all the best time is set to 999,999 own game. 2130 DATA A. AA. AA (cid:9) AA . AA . seconds and the number of dots eaten is set to If your new maze is larger or smaller than 2140 DATA A A A A zero. Line 100 is necessary to let the program the original one, you'll have to alter some lines 2150 DATA AAAAAAAAAAAAAAAAAA, reREAD the DATA each time the game is in the program as well. These are the lines played. Without it you would get an OUT OF The shape of the maze is contained in the which set up the FOR ... NEXT loop to READ DATA error after the first game. DATA lines—Lines 2000 to 2150. At this stage the DATA—Lines 110 and 130. Line 280 is the tricky one. This looks to see the boundaries are represented by As. The number of dots in the new maze will if you are moving over a dot. It uses the POINT Before the DATA is read, Line 30 clears the almost certainly be different, so change Line function again and this time checks the colour screen and colours it red, whose code number 210 as well. of the square where the full stop should be. If is 4. Line 40 sets the asterisk's start position. That should be all, but if the new maze is a it is colour 2 (yellow in this case) then the Lines 1000 to 1060 are the section which lot wider you may need to alter the position of player's score is increased by 1 and a munch- actually plots the maze on the screen. Each the text that PRINTs out your time. Play ing sound is made. If the asterisk is running time the FOR ... NEXT loop in Lines 1000 and around with the screen display until you get over a blank square then this line is ignored. 1060 is executed the program takes the next something that looks right. Fig 3 explains how to work out the position of line of DATA. Line 1010 READs the DATA and the dot. calls it A$, and Line 1020 displays it. As soon as all the dots have been eaten, But a collection of dots and As on the screen These first few lines set up the maze itself: Line 210 makes the program jump to Line doesn't look much like a proper maze. So 310 which stops the timer and divides TIME by 30 CLS4 Line 1040 examines the part of the machine's 100 to convert it to seconds. This time is then 40 LET P= 297 memory which corresponds to each screen compared with the best time, in Line 320, and 1000 FOR N = 0 TO 15 location. If the memory location contains the if it is faster it calls this new time the best 1010 READ A$ time. Line 330 then blanks out the asterisk. 1020 PRINT@ N*32,A$;

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.