index-bas.htm wywtk.com homepage What You Want To Know  Index to my pages about BBC BASIC

More drawing with BBC BASIC

And, near the end...
Subroutines- and important tool

(Page's URL: bas4-clever-move-draw.htm)

In the previous tutorial we learned a way to draw a square....

MODE 12
PRINT "One plain"

GCOL 14
MOVE 500,1000
DRAW 600,1000
DRAW 600,1100
DRAW 500,1100
DRAW 500,1000

That "works". but it is very pedestrian. We could do that as follows...

If we get clever...

We can write code to draw the first square in a very different way... and yet it draws exactly the same squares! Here's the code. (Don't worry about all the spaces I put it to make things line up. They don't change what happens... but it is always good to make your code easy to read.)

MODE 12
PRINT "One fancy"

x%=500
y%=1000
side%=100
GCOL 14

MOVE x%,            y%
DRAW x%+side%,y%
DRAW x%+side%,y%+side%
DRAW x%,           y%+side%
DRAW x%,           y%

Save that. I suggest you call it bas4-one-fancy.bbc.

Study the code carefully until you think you really understand how it works.

When you think you understand how it works, change one of the numbers above one of the MOVE lines. Does your change have the effect you thought it would? Good! (If not, figure out why the change you made had the result it did! The computer doesn't make mistakes. It does what you told it to do. Not what your MEANT you wanted when you said what you did!)

Repeat that a few times, until you are sure, sure, sure you understand how this square drawer works.

Take a break!

Shut down you BASIC BBC IDE. Do some chores. Take a walk.

Then come back to the computer without looking at this, without looking at what you saved before your break... and try to write the version we finished with before the break from scratch!

You shouldn't try to "remember" it, exactly. You do need to remember The Ways To Do Things that you saw, and think about what you want... step by step... and then you can rebuild the code! (^_^)

IF you really understood it! Which will be true for very few people!

When you've given it your best shot, go back, re-read the stuff above here, and maybe try again, maybe press on.

TWO squares! Whoop-de-doo!

What if you wanted code to draw TWO squares.

You could do it the boring and bad way...

MODE 12
PRINT "Two plain"

GCOL 14
MOVE 500,1000
DRAW 600,1000
DRAW 600,1100
DRAW 500,1100
DRAW 500,1000

GCOL 12
MOVE 220,300
DRAW 1220,300
DRAW 1220,1300
DRAW 220,1300
DRAW 220,300

But that would be a BAD way to do it. The second square needs adjustments to 5 lines... and each time you make an adjustment, a chance to make a mistake is created.

Instead, add to "One Fancy", the code we did a moment ago. Turn that into what you see below...

MODE 12
PRINT "Two fancy"

REM First square...
x%=500
y%=1000
side%=100
GCOL 14

MOVE x%,            y%
DRAW x%+side%,y%
DRAW x%+side%,y%+side%
DRAW x%,           y%+side%
DRAW x%,           y%

REM Second square...
x%=220
y%=300
side%=1000
GCOL 12

MOVE x%,            y%
DRAW x%+side%,y%
DRAW x%+side%,y%+side%
DRAW x%,           y%+side%
DRAW x%,           y%

Notice that a big chunk of the new code is just a copy of the code we used to make the first square. If you've figured out doing "copy/paste" (see the first tutorial about doing squares), adding the "new" bits will be easy!

This really is a lot better!

Not convinced?

Well... if that's all your going to do, I admit the difference isn't huge.

But! Now that you understand that much, I can show you something really cool... though you may not see that yet. It is an example of a very, very important skill in coding: Using subroutines.

Add the following to the end of the code, and run it again.

PRINT "Bit at End"

When you run it, you will get the two squares again, of course, and "Bit at End" up in the top right hand corner.

Now... even though it will make no sense to you for the moment, add...

END

... just before the PRINT "Bit at End"... and run the code again.

As you possibly guessed, even though it was still a mystery as to why you would want to do such a thing, this time you got the two squares still, but you did not get the PRINT "Bit at End". Don't you like it when things are simple? END measn... "end"! "Stop", "Go no further".

Pressing on...

Now replace the END with

DEF PROC_DoSquare
MOVE x%,            y%
DRAW x%+side%,y%
DRAW x%+side%,y%+side%
DRAW x%,           y%+side%
DRAW x%,           y%
ENDPROC

... and change the heart of the progam, leaving you with...

MODE 12
PRINT "Two by subroutine"

REM First square...
x%=500
y%=1000
side%=100
GCOL 14
PROC_DoSquare

REM Second square...
x%=220
y%=300
side%=1000
GCOL 12
PROC_DoSquare

END

REM  The rest of this is "details"....

DEF PROC_DoSquare
   MOVE x%,            y%
   DRAW x%+side%,y%
   DRAW x%+side%,y%+side%
   DRAW x%,           y%+side%
   DRAW x%,           y%
ENDPROC

Do you see how much better that is that our early versions? I think the more you see subroutines (PROC_DoSquare is a subroutine), the more you will like them.

Among other things, they let you "make up new words" for the language! (like "PROC_DoSquare"!)

what happens in heart of the program...

REM First square...
x%=500
y%=1000
side%=100
GCOL 14
PROC_DoSquare

REM Second square...
x%=220
y%=300
side%=1000
GCOL 12
PROC_DoSquare

... is now easier to see, isn't it? And of course, you don't have to worry about typos in endless MOVE/ DRAW statements.

On to something... amusing?

One person's "amusing" is another person's "yawn", but I hope you will enjoy...

Save the final version of the two squares program, then start a new program, and make it...

SquaresToDoStill%=50
MODE 12

REPEAT
  x%=RND(1900)
  y%=RND(1400)
  side%=25*RND(18)+30
  GCOL RND(14)+1
  PROC_DoSquare

  WAIT(5)
  SquaresToDoStill%=SquaresToDoStill%-1
UNTIL SquaresToDoStill%=0
PRINT "Pretty? Goodbye."
WAIT(200)
QUIT

END

REM  The rest of this is "details"....

DEF PROC_DoSquare
MOVE x%,            y%
DRAW x%+side%,y%
DRAW x%+side%,y%+side%
DRAW x%,           y%+side%
DRAW x%,           y%
ENDPROC

Minor bonus...

There's no need to try this, but it might amuse...

Revise the bit above and including the WAIT, so it looks like the following. You're only adding one line, but you're also putting some blank lines in to make things more readable.

GCOL RND(14)+1

PROC_DoSquare
CIRCLE FILL x%,y%, 5*RND(12)+15

WAIT(5)

"CIRCLE FILL" is a built in word... just one of many you can use. For now, though, it is probably best to stick with building up your skill in using he core words.


A few words from the sponsors...

Please get in touch if you discover flaws in this page. Please mention the page's URL. (wywtk.com/prgm/bas/bas4-clever-move-draw.htm).

If you found this of interest, please mention in forums, give it a Facebook "like", Google "Plus", or whatever. If you want more of this stuff, help!? There's not much point in me writing these things, if no one feels they are of any use.



index sitemap
What's New at the Site Advanced search
Search tool (free) provided by FreeFind... whom I've used since 2002. Happy with it, obviously!

Unlike the clever Google search engine, this one merely looks for the words you type, so....
*    Spell them properly.
*    Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....

Please also note that I have four other sites, and that this search will not include them. They have their own search buttons.

My SheepdogSoftware.co.uk site, where you'll find my main homepage. It has links for other areas, such as education, programming, investing.

My SheepdogGuides.com site.

My SkyWoof.com site.

My site at Arunet.




How to email or write this page's editor, Tom Boyd. Please cite page's URL if you write.


Valid HTML? Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org.

AND tested for... Valid CSS?



(eXTReMe tracker icon) Why is there a script or hidden graphic on this page? I have my web-traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try one, check out their site. Neither my webpages nor my programs incorporate spyware, but if the page has Google ads, they also involve scripts. Why do I mention the script? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....