Random Walk and Random Squawk - PROC, POINT(x,y)
Tutorial 008
Here we introduce use of a PROC (Procedure). This program is a development
of one by Alan Thomas in his book "Further Programming for the BBC Micro"
which is full of good ideas. Here I have removed his GOTOs and used instead
a REPEAT ... UNTIL loop. (The use of GOTO in all but the smallest programs
can cause programs to be almost unreadable as their structure soon becomes
unclear. If we avoid GOTO then we can dispense with line numbers, which makes
program development much easier and quicker).
Although the PROC in this program is not strictly necessary it does make
for readability, which is important for the author as well as for readers.
Its easy to forget how one programmed something, so its good to choose
"meaningful" variable names - thus in the following program "jump" is better
than just "j".
Select/Copy/Paste the following program into BB4W,
run and perhaps save it to disc (hard or floppy). Then read the annotated
listing to see how it all works
REM "Random Squawk"
by RG Weston
REM 19 Feb 2003
REM After Alan Thomas p.11 but...
REM structured, coloured, made audible etc.
REM ** Models a gas molecule's motion **
:
MODE8:OFF
PRINT'TAB(30)"Random Walk"
ORIGIN 640,512
MOVE 0,0
x=0 : y=0
jump=50
REPEAT
PROCwalk
UNTIL POINT(x,y) = -1
RUN
END
:
DEF PROCwalk
dx=RND(jump):dy=RND(jump)
R=RND(2):S=RND(2)
IF R=1 THEN x=x+dx ELSE x=x-dx
IF S=1 THEN y=y+dy ELSE y=y-dy
DRAW x,y
GCOL0,RND(15)
delay=INKEY(RND(10))
SOUND 1,-(7+RND(8)),y,1
REM PRINTTAB(30,30)POINT(x,y)
ENDPROC
Annotated listing :
100 REM "Random Squawk" by RG Weston
110 REM 19 Feb 2003
120 REM After Alan Thomas p.11 but...
130 REM structured, coloured, made audible etc.
140 REM ** Models a gas molecule's motion **
150 :
160 MODE8:OFF
170 PRINT'TAB(30)"Random Walk"
180 ORIGIN 640,512 *** sets graphics origin to centre of screen
***
190 MOVE 0,0 *** Moves graphics cursor to
centre of screen ****
200 x=0 : y=0 *** sets DRAW parameters for line
330 intitially to zero
210 jump=50 *** max jump size for line 290 ***
220 REPEAT *** main program loop ends at line 240 ***
230 PROCwalk *** This procedure is defined at line
280 ***
240 UNTIL POINT(x,y)= -1 *** main loop repeats until the graphics
cursor moves off screen.... ***
250 RUN *** When the program exits the main loop RUN
instructs the computer to start the program again ***
260 END *** Shows end of program - put any Procedure definitions beyond
here.
270 : *** just a spacer ***
280 DEF PROCwalk *** Definition of PROCwalk ***
290 dx=RND(jump):dy=RND(jump) *** dx and dy are the changes to the
value of the graphics co-ordinates x and y ***
300 R=RND(2):S=RND(2) ***decide which way to jump R=1 for right, R=2
for left, S=1 for up, S=2 for down... ***
310 IF R=1 THEN x=x+dx ELSE x=x-dx
320 IF S=1 THEN y=y+dy ELSE y=y-dy
330 DRAW x,y *** Draws the line to the new position ***
340 GCOL0,RND(15) *** changes graphics colour to make it easier to
follow the jumps ***
350 delay=INKEY(RND(10)) *** Random delay between jumps is more realistic
for molecular collisions than a fixed one ***
360 SOUND 1,-(7+RND(8)),y,1 *** hear the "y" co-ordinate as a sound!
***
370 REM PRINTTAB(30,30)POINT(x,y) *** the REM here prevents the instruction
working ***
380 ENDPROC *** signals the end of the procedure.
Note :
At line 370, if you delete just the start of the line so as to remove only
the three letters REM, so that the line reads
PRINTTAB(30,30)POINT(x,y)
then the line will then be able to be active in your listing in the BB4W
editing pane (window). If you run the program now, you will then see it printing
the values returned by the POINT function which shows you the colour of the
screen at the position (x,y).
Only when (x,y) wanders off the screen does it return a value -1 and the
program restarts.
Next Tutorial
Richard Weston's Homepage