Elementary BASIC code snippets for Beginner

The place for codemasters or beginners to talk about programming any language for the Spectrum.
firelord
Manic Miner
Posts: 557
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Elementary BASIC code snippets for Beginner

Post by firelord »

I start this thread to gather some ideas for BASIC programs suitable for teaching 9-12 years old kids to program.
I'm not sure if this will work but since I have used MANY child-oriented coding tools (eg code.org,lightbot,scratch ,logo etc...) I'm beggining to think that Sir Clive Sinclair Basic with his Spectrum is still a viable option.

The idea is to give very small programs to the kids with a noticeable result and then explain them and let them modify the code.
Some examples of what I'm trying to do:

Code: Select all

10 PAPER RND*7: CLS: GOTO 10

Code: Select all

10 PRINT "Hello"
20 INPUT "Give me X=";x
30 INPUT "Give me y=";y
40 PRINT "Result x*y=";x*y
50 REM PRINT "Thank you - Lets play again ":PAUSE 0: GO TO 10

Code: Select all

10 PRINT "NUMBER","BIGGEST SO FAR"
20 INPUT A
30 LET BIGGEST=A
40 PRINT A,BIGGEST
50 INPUT A
60 IF BIGGEST<A THEN LET BIGGEST=A
70 GOTO 40

Code: Select all

10 print "sheep counter"
20 FOR M=1 TO 10
30 PRINT M;" Sheep(s)"
40 NEXT M
50 PRINT "Did you fall asleep?"

And some that I consider to be a difficult one for that age group :

Code: Select all

10 PRINT "NESTED LOOP"
15 FOR M=0 TO 6
20 FOR N=0 TO M
30 PRINT M;":";N;" ";
40 NEXT N
50 PRINT
60 NEXT M

Code: Select all

5 print "SORT"
10 INPUT "Text 1 :";A$
20 iNPUT "Text 2 :";B$
30 IF A$<=B$ THEN GO TO 70
40 LET C$=A$
50 LET A$=B$
60 LET B$=C$
70 PRINT A$;" ";("<" AND A$<B$)+("=" AND A$=B$);" ";B$
80 GO TO 10

And the almost winning entry of CSSCGC 2008 rip-off :) :

Code: Select all

10 CLS 
15 RANDOMIZE 
50 PAPER 4 :INVERSE 1: PRINT "'Mess Screen Simulator' ": INVERSE 0 
85 PRINT "(Press any key).": INK 0: PAPER 7 : PAUSE 0
90 CLS 
100 LET c$="*"
110 PAPER 3 : PRINT AT 0,0;"Lets start with:* ": PAPER 7: PAUSE 30
120 LET x=12 : LET y=12
1200 PRINT AT x,y;c$
2250 LET x=1+INT (RND*20) : LET y=INT (RND*31)
2300 PAPER 4: PRINT AT 0,0;"Now using : ";c$;"     ": PAPER 7: PAUSE 50
2500 IF INKEY$<>"" THEN LET c$=INKEY$
4500 GO TO 1200

I don't think more complex program would be suitable.
Any simple program ideas are welcome.
I'm not sure that something will actually come out of this but I could spend 2-3 lessons to try it.
Thanks
User avatar
Mpk
Dynamite Dan
Posts: 1008
Joined: Tue Feb 09, 2021 8:10 am

Re: Elementary BASIC code snippets for Beginner

Post by Mpk »

There's an interview with a teacher in the latest Crash who's doing exactly this. ( Edit : Not quite, they're actually using AGD )

The school website is at https://bit.ly/3noxJ6Q

There are in fact 12 games there that the kids wrote - I guess they should get added to the archive on here as well? ( Edit 2 : They're already there. :oops: )
And another 20+ here https://dougie9mcg.itch.io/bearsden-pri ... ding-games
Last edited by Mpk on Wed Nov 24, 2021 11:07 am, edited 2 times in total.
User avatar
ZXDunny
Manic Miner
Posts: 498
Joined: Tue Nov 14, 2017 3:45 pm

Re: Elementary BASIC code snippets for Beginner

Post by ZXDunny »

BASin was briefly in about 1,800 schools up and down the UK teaching BASIC. I even made a specially modified version for the task. It all went tits up (as these things do) but for a brief period in 2015/16, it was glorious.
firelord
Manic Miner
Posts: 557
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Re: Elementary BASIC code snippets for Beginner

Post by firelord »

Thanks for the information.

I was thinking of something more simple.
PRINT, INPUT, FOR loops , IF . Up to 3 class hours for start.
I'll check these out but for now the tools I think are qaopJS and SpecBAS. Basin is nice but will need a second step (loading to the emulator).


[mention]ZXDunny[/mention] Actually I downloaded the freepascal compiler and was planning to see if I could add a translation layer to your SpecBAS. Would this be easy? (1. Allow typing in foregn language and 2. Some of the basic commands would have a translated alias).
I've use this technique many tine in the past and it helps a lot.
User avatar
ZXDunny
Manic Miner
Posts: 498
Joined: Tue Nov 14, 2017 3:45 pm

Re: Elementary BASIC code snippets for Beginner

Post by ZXDunny »

firelord wrote: Wed Nov 24, 2021 3:21 pm Thanks for the information.

I was thinking of something more simple.
PRINT, INPUT, FOR loops , IF . Up to 3 class hours for start.
I'll check these out but for now the tools I think are qaopJS and SpecBAS. Basin is nice but will need a second step (loading to the emulator).
Just for information, but BASin is an emulator. It's built-in. You don't need to load to the internal emulation, the code editor works directly on the emulated memory space.
@ZXDunny Actually I downloaded the freepascal compiler and was planning to see if I could add a translation layer to your SpecBAS. Would this be easy? (1. Allow typing in foregn language and 2. Some of the basic commands would have a translated alias).
I've use this technique many tine in the past and it helps a lot.
I mean, feel free but you might find it hard going. It's written in Delphi, and to get it running in FPC you'll probably have to make modifications to the MainForm.pas file. Have a go, if you think you can handle it :)
firelord
Manic Miner
Posts: 557
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Re: Elementary BASIC code snippets for Beginner

Post by firelord »

ZXDunny wrote: Wed Nov 24, 2021 3:57 pm Just for information, but BASin is an emulator. It's built-in. You don't need to load to the internal emulation, the code editor works directly on the emulated memory space.
Thanks I had forgotten about that detail.I had written some of my CSSCGC games in Basin but for some reason I didn't use it's emulator. I can't remember why ...
ZXDunny wrote: Wed Nov 24, 2021 3:57 pm I mean, feel free but you might find it hard going. It's written in Delphi, and to get it running in FPC you'll probably have to make modifications to the MainForm.pas file. Have a go, if you think you can handle it :)
I thought the github version was ready for compilation (windows 32bit since it runs great with wine in linux)...
Then I guess I won't even try to check the code. Thanks for the warning :)
User avatar
ZXDunny
Manic Miner
Posts: 498
Joined: Tue Nov 14, 2017 3:45 pm

Re: Elementary BASIC code snippets for Beginner

Post by ZXDunny »

firelord wrote: Wed Nov 24, 2021 4:30 pm I thought the github version was ready for compilation (windows 32bit since it runs great with wine in linux)...
Then I guess I won't even try to check the code. Thanks for the warning :)
Yeah, you can load the sources up into a copy of Delphi and build SpecBAS just like that. But you'll need to be running Windows (as MainForm.pas uses Win32 API calls) and it's possible that FPC might not like the form handling as I tend to hijack the message loop a touch.

If you rewrite MainForm.pas for a linux (with say, SDL or something as the frontend) then the rest of SpecBAS will port without any changes at all.
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: Elementary BASIC code snippets for Beginner

Post by FFoulkes »

This one's from the book "Sams Teach Yourself Perl in 21 Days" by Laura Lemay (in Perl there, of course).
I use it in various programming languages, because when you're able to write it, you have already learnt something about the language:

Code: Select all

10 PRINT "Cookie Monster"
20 LET a$ = ""
30 INPUT "I want COOKIES: "; a$
40 IF a$ <> "COOKIES" THEN GO TO 30
50 CLS
60 PRINT "Mmmm. COOKIES."
Of course, in Perl or C, you would use "while()" instead of "GO TO".
I'm also not so sure, if it's such a good idea to teach today's children the bad practices of yesterday such as "GO TO". :)
Indentations would also be better than deprecated line numbers, so today I'd probably go for Python, when introducing beginners to programming.
Unless they're having 30 Spectrums in their class-room, all connected by their Interfaces 1. Then I'd probably use Spectrum BASIC. :)
firelord
Manic Miner
Posts: 557
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Re: Elementary BASIC code snippets for Beginner

Post by firelord »

FFoulkes wrote: Wed Nov 24, 2021 5:39 pm I'm also not so sure, if it's such a good idea to teach today's children the bad practices of yesterday such as "GO TO". :)
For a beginner GO TO for simple programs might be easier than procedures/functions/labels (I'm talking for elementary school ages -and also from my experience from lightbot,code.org and scratch).
FFoulkes wrote: Wed Nov 24, 2021 5:39 pm Indentations would also be better than deprecated line numbers, so today I'd probably go for Python, when introducing beginners to programming.
I believe Logo-like and Scratch are much more suited for these ages in comparison to python (I'm talking for a session of 3 hours). Also I believe that the line numbering seems easier to understand than other Indentation. This might be because it looks more like their textbook.

If a 9 year old kid sees the "Hello world" program in BASIC in Java and in Javascript which language will be his choice?

Also, I'm talking about small programs -maybe up to 20 lines.
I'm not absolute that this is the best way. I'm just trying a different approach...

Also as a "fun fact". The first time I used GO SUB in spectrum was in the last years (after I learned ideas like functions etc).
firelord
Manic Miner
Posts: 557
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Re: Elementary BASIC code snippets for Beginner

Post by firelord »

Code: Select all

10 REM Guess the number
20 INPUT "ENTER SECRET NUMBER = ";a: CLS
30 INPUT "Guess the number = ", b
40 IF b=a THEN PRINT "That is correct": STOP
50 IF b<a THEN PRINT "That is too small, try again"
60 IF b>a THEN PRINT "That is too big, try again"
70 GO TO 30

Code: Select all

10 LET total=0
20 FOR c=1 TO 5
30 INPUT "Enter number to add: ";a
40 REM c=number of times that a has been input so far
50 LET total=total+a
60 NEXT c
80 PRINT "Total sum= ";total
User avatar
Nitrowing
Manic Miner
Posts: 607
Joined: Mon Sep 21, 2020 8:38 pm
Location: Cleethorpes

Re: Elementary BASIC code snippets for Beginner

Post by Nitrowing »

10 Print "Gareth is cool"
Run

I was amazed

10 Print "Gareth is cool"
20 goto 10
Run

I was hooked

Start as simple as possible
firelord
Manic Miner
Posts: 557
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Re: Elementary BASIC code snippets for Beginner

Post by firelord »

Nitrowing wrote: Wed Nov 24, 2021 9:53 pm 10 Print "Gareth is cool"
....
Start as simple as possible
This is the "hello world program"
This is the first one that I will show...
firelord
Manic Miner
Posts: 557
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Re: Elementary BASIC code snippets for Beginner

Post by firelord »

ZXDunny wrote: Wed Nov 24, 2021 10:40 am BASin was briefly in about 1,800 schools up and down the UK teaching BASIC. I even made a specially modified version for the task. It all went tits up (as these things do) but for a brief period in 2015/16, it was glorious.
Do you know if any official lesson plans (or any other) exist?

I also remembered some things about about Basin. I had problems with copy/paste so I typed code in another text editor and then I loaded the code to Basin to convert it to TAP.
User avatar
WhatHoSnorkers
Manic Miner
Posts: 254
Joined: Tue Dec 10, 2019 3:22 pm

Re: Elementary BASIC code snippets for Beginner

Post by WhatHoSnorkers »

There's a lovely book by Ian Sinclair from back in 1983 that is "ZX Spectrum: How to Use and Program". It starts out with wiring a plug (how things change!) but then goes through all kinds of fun little fragments, animations, sounds, string checking and ends up with a little game.
I have a little YouTube channel of nonsense
https://www.youtube.com/c/JamesOGradyWhatHoSnorkers
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: Elementary BASIC code snippets for Beginner

Post by FFoulkes »

WhatHoSnorkers wrote: Thu Nov 25, 2021 11:32 amThere's a lovely book by Ian Sinclair from back in 1983 that is "ZX Spectrum: How to Use and Program".
One of the few that isn't preserved yet, it seems ...

Also, maybe a border colour change would be nice in my example above:

Code: Select all

10 BORDER 2
20 PRINT "Cookie Monster:"
30 LET a$ = ""
40 INPUT "I want COOKIES: "; a$
50 IF a$ <> "COOKIES" THEN GO TO 40
60 CLS
70 BORDER 4
80 PRINT "Mmmm. COOKIES."
Last edited by FFoulkes on Thu Nov 25, 2021 3:01 pm, edited 2 times in total.
User avatar
Morkin
Bugaboo
Posts: 3277
Joined: Mon Nov 13, 2017 8:50 am
Location: Bristol, UK

Re: Elementary BASIC code snippets for Beginner

Post by Morkin »

Nitrowing wrote: Wed Nov 24, 2021 9:53 pm 10 Print "Gareth is cool"
20 goto 10
Run
Don't forget this is school kids we're talking about. So it should probably be:

Code: Select all

10 PRINT "FART"
20 GO TO 10
My Speccy site: thirdharmoniser.com
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: Elementary BASIC code snippets for Beginner

Post by FFoulkes »

Plus corresponding "BEEP"-commands. :)
User avatar
WhatHoSnorkers
Manic Miner
Posts: 254
Joined: Tue Dec 10, 2019 3:22 pm

Re: Elementary BASIC code snippets for Beginner

Post by WhatHoSnorkers »

FFoulkes wrote: Thu Nov 25, 2021 2:55 pm
WhatHoSnorkers wrote: Thu Nov 25, 2021 11:32 amThere's a lovely book by Ian Sinclair from back in 1983 that is "ZX Spectrum: How to Use and Program".
One of the few that isn't preserved yet, it seems ...
If I'm free over the weekend I'll see about archiving it. I've done a video on it now!
I have a little YouTube channel of nonsense
https://www.youtube.com/c/JamesOGradyWhatHoSnorkers
firelord
Manic Miner
Posts: 557
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Re: Elementary BASIC code snippets for Beginner

Post by firelord »

Some books that are good & free are here: (foudn the link on some older forum post)

https://usborne.com/row/books/computer- ... ing-books
User avatar
stupidget
Dynamite Dan
Posts: 1644
Joined: Wed Jan 24, 2018 2:09 pm
Location: Sunny Wolverhampton

Re: Elementary BASIC code snippets for Beginner

Post by stupidget »

Is there any way to cut 'n paste these snippets into an emulator?
firelord
Manic Miner
Posts: 557
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Re: Elementary BASIC code snippets for Beginner

Post by firelord »

stupidget wrote: Mon Dec 13, 2021 9:55 am Is there any way to cut 'n paste these snippets into an emulator?
I think the books are made from images so I guess not.
Maybe with a PDF OCR program you could convert them to "normal" PDFs.
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: Elementary BASIC code snippets for Beginner

Post by FFoulkes »

stupidget wrote: Mon Dec 13, 2021 9:55 amIs there any way to cut 'n paste these snippets into an emulator?
If you got them in a text-file, you can use "bas2tap".

(If it's paper, you need OCR as mentioned. If it's pdf, some pdfs support copying as text, some don't. Last step (when it's become text) will be "bas2tap" again.)
User avatar
stupidget
Dynamite Dan
Posts: 1644
Joined: Wed Jan 24, 2018 2:09 pm
Location: Sunny Wolverhampton

Re: Elementary BASIC code snippets for Beginner

Post by stupidget »

FFoulkes wrote: Mon Dec 13, 2021 1:22 pm
stupidget wrote: Mon Dec 13, 2021 9:55 amIs there any way to cut 'n paste these snippets into an emulator?
If you got them in a text-file, you can use "bas2tap".

(If it's paper, you need OCR as mentioned. If it's pdf, some pdfs support copying as text, some don't. Last step (when it's become text) will be "bas2tap" again.)
Text file into bas2tap it what I'm after. I don't think there is a MAC version of bas2tap though :(
User avatar
Turtle_Quality
Manic Miner
Posts: 506
Joined: Fri Dec 07, 2018 10:19 pm

Re: Elementary BASIC code snippets for Beginner

Post by Turtle_Quality »

Or BasinC - you can cut and paste in text and it's a really nice environment for editing and debugging Spectrum Basic. Yes it's for Windows but I read on a forum that it works with CrossOver, and if I've read something on a forum it must be true
Definition of loop : see loop
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: Elementary BASIC code snippets for Beginner

Post by FFoulkes »

stupidget wrote: Mon Dec 13, 2021 1:40 pmText file into bas2tap it what I'm after. I don't think there is a MAC version of bas2tap though :(
Well, the source code would be here:

https://github.com/speccyorg/bas2tap

If you're familiar with compiling on a Mac, you should be able to create it on your own.
Looks like a simple console program, that only depends on generic C-libraries.
You can just try executing the command "make" in the program's directory.
Post Reply