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

Re: Elementary BASIC code snippets for Beginner

Post by firelord »

I was having a look at the books I posted. They are amazing. The fact that they do not allow copy/paste is a drawback but I might be ablr to solve it with an OCR program.


From what I see SpecBAS is very good but the lack of some commands make it a bit incompatible for what I want to do. Also,most listings I found need modification to run there.
Basin seems to win that race -even if copy/paste sometimes misbevhaves...
Is there something like Basin in javascript so it can be hosted online?
eg QAOP with the ability to copy/paste would do just fine!
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: Thu Dec 16, 2021 8:30 amFrom what I see SpecBAS is very good but the lack of some commands make it a bit incompatible for what I want to do. Also,most listings I found need modification to run there.
Although I don't recommend trying to use SpecBAS to run pure Sinclair BASIC programs - you will get issues with display size, number of colours, lack of colour attributes - I do strive to get as much compatibility with the language as possible. If you find some features that are lacking, let me know.
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: Thu Dec 16, 2021 4:09 pm
Although I don't recommend trying to use SpecBAS to run pure Sinclair BASIC programs - you will get issues with display size, number of colours, lack of colour attributes - I do strive to get as much compatibility with the language as possible. If you find some features that are lacking, let me know.
SpecBAS as I said is very good but I had Sinclair basic in my mind. Easy spectrum Basic programs (or type-ins) with visible results (eg color changes in INK,FLASH,BORDER etc) .
Basin is much closer to this idea -even if I think is not as good as SpecBAS.
+3code

Re: Elementary BASIC code snippets for Beginner

Post by +3code »

No idea about the british magazines, but in the spanish Microhobby was a regular section called "Tricks" with loads of nice small BASIC programs for graphics and sounds, perhaps can be a source of inspiration.

Image
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 »

+3code wrote: Thu Dec 16, 2021 7:31 pm No idea about the british magazines, but in the spanish Microhobby was a regular section called "Tricks" with loads of nice small BASIC programs for graphics and sounds, perhaps can be a source of inspiration.
Yes these seem very nice. Are these type ins in a format that can be copied?
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 »

You mentioned cut and paste issues with Basin, the alternative is to save the listing as a .BAS file and load it in to Basin
Definition of loop : see loop
+3code

Re: Elementary BASIC code snippets for Beginner

Post by +3code »

firelord wrote: Thu Dec 16, 2021 8:46 pm Yes these seem very nice. Are these type ins in a format that can be copied?
I think no, but they normally are very short.
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 »

This is some selected parts from one of the free books :

https://1fichier.com/?yojbxnhrbys81g3pfedp

It is quite simple and easy for kids.
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

1 REM Arrow (ripped from a BASIC code in WOS )
10 LET B=0: LET d=0
15 CLS 
20 PRINT AT 0,B;"O"
30 LET B=B+(INKEY$="6")-(INKEY$="7")
40 POKE 23692,255: REM stop scroll message
50 LET X=RND*30: PRINT AT 20,X; INK RND*6;"^"
60 PRINT AT 21,0: PRINT '
70 IF  SCREEN$ (0,B)="^" THEN LET d=d+1: PRINT INVERSE 1;"HIT ";d;" TIMES"; INVERSE 0
80 GO TO 20
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 »

firelord wrote: Thu Dec 16, 2021 8:30 am ...
Is there something like Basin in javascript so it can be hosted online?
eg QAOP with the ability to copy/paste would do just fine!
In addition to the above, anyone knows of a linux port of Basin?
It runs very well with wine but it takes much more RAM.
My target PCs might be old and low RAM PCs (some of them run 32bit linux to save some ram and speed) -that's why O look for either Browser based or generic version of Basin-like programs.
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 »

Another 1 liner Ski clone converted to show how to make your own 8x8 graphics. Kids could draw what they want in a 8x8 grid on paper.

Code: Select all

5 REM -----GENERATE GRAPHICS---- (keys:1,0)
10 FOR f=USR "a" TO USR "b"+7
20 READ a: POKE f,a:
30 NEXT f:
35 REM -----Main Game-------
40 LET score=0: BORDER 0: PAPER 0: INK 6: CLS : LET x=15:
50 FOR f=1 TO 1000:
60 PRINT AT 0,x;CHR$ 144: PRINT AT 21,INT (RND*32); INK 2;CHR$ 145: LET score=score+1: LET f=USR 3282:
65 rem SET keys and move player
70 LET x=x+(INKEY$="0")-(INKEY$="1"): LET x=x+(x<0)-(x>31): LET f=1000*(ATTR (0,x)<>6):
80 NEXT f:
90 PRINT AT 0,0;"SCORE=";score
3000 REM ----8x8 Graphics - Draw it on paper!----
3160 REM UDG player
3170 DATA BIN 00100100
3180 DATA BIN 11111111
3190 DATA BIN 11111111
3200 DATA BIN 01111110
3210 DATA BIN 01111110
3220 DATA BIN 00111100
3230 DATA BIN 00111100
3240 DATA BIN 00011000
3250 REM UDG oenemy
3260 DATA BIN 00111100
3270 DATA BIN 01011110
3280 DATA BIN 11111101
3290 DATA BIN 11110111
3300 DATA BIN 10111111
3310 DATA BIN 11111011
3320 DATA BIN 01110110
3330 DATA BIN 00111100
9998 REM BASIC 1liner 2007 -Space Ski v01- by Matt_B with UDG!! https://worldofspectrum.org/forums/discussion/comment/189539/#Comment_189539
User avatar
PeterJ
Site Admin
Posts: 6879
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: Elementary BASIC code snippets for Beginner

Post by PeterJ »

Just to say that I'm really enjoying this thread. Thank you!

How about in the new year a series' of BASUC challenges.
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 »

As you can see I use 1/10liners contest (because I want very small games) and try to "simplify" them so they are more easy to explain....

Another competition would be nice so I could have more input but maybe there could be an rule so that the code can be directly copy/paste on Basin.
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 »

firelord wrote: Mon Dec 20, 2021 12:54 pm Another 1 liner Ski clone converted to show how to make your own 8x8 graphics. Kids could draw what they want in a 8x8 grid on paper.
What part of this code knows what to do with the data used to create the UDG's? Is it the READ statement and if so how does it know that line 3170 > 3240 is the player character?
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: Elementary BASIC code snippets for Beginner

Post by FFoulkes »

There also was a "Tenliner Cave Adventure" by Einar Saukas, a text adventure game in just 10 lines of ZX81 BASIC code. To get it that small, the code was rather complicated though.
I spent some time to make it more readable and easier to understand (and less tiny), then I translated it to Python and C. If you translate that code back to Sinclair BASIC, maybe you'd have another entry for this thread.
Here's what I did (Python and C).
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 20, 2021 4:59 pm
firelord wrote: Mon Dec 20, 2021 12:54 pm Another 1 liner Ski clone converted to show how to make your own 8x8 graphics. Kids could draw what they want in a 8x8 grid on paper.
What part of this code knows what to do with the data used to create the UDG's? Is it the READ statement and if so how does it know that line 3170 > 3240 is the player character?
Read finds the first DATA of the program the next read gets the second DATA etc... Wherever you put DATA - even in the beginning.

[mention]FFoulkes[/mention]
I already have seen that code (in digital prawns site) but it's not simple to explain . For an adventure-like game some of my own older CSSCGC entries are easier (even if they are not actually an adventure -and use extemely simple syntax of INPUT,PRINT,GOSUB,IF,FOR NEXT and RND ).

The goal here is to learn simple BASIC stuff for introduction to programming.

For more complex ideas we have much better alternatives...
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: Elementary BASIC code snippets for Beginner

Post by FFoulkes »

FFoulkes wrote: Mon Dec 20, 2021 5:15 pm If you translate that code back to Sinclair BASIC, maybe you'd have another entry for this thread.
Ok, ok, so I did it myself. Not so tiny though. But you can't deny, it's cool. Thanks to Einar Saukas!

Code: Select all

10 LET locationnr = 1
20 LET chestopen = 0
30 LET i$ = ""
40 DIM t$(13, 10)
50 FOR i = 1 TO 13: READ t$(i): NEXT i
60 DIM l$(4, 4)
70 FOR i = 1 TO 4: READ l$(i): NEXT i
100 REM Main loop
110 LET movement = 0
120 LET textnr = 0
130 LET a$ = l$(locationnr): GO SUB 5000: REM rstrip a$
135 IF a$ = "lake" THEN PRINT "You are by a lake.": GO TO 150
140 PRINT "You are in a " + a$ + "."
150 INPUT "What now? "; u$
160 IF u$ = "q" THEN PRINT: PRINT "Bye.": STOP
170 IF locationnr < 3 AND u$ = "north" THEN LET movement = 2
180 IF locationnr > 2 AND u$ = "south" THEN LET movement = movement - 2
190 IF locationnr = 2 AND u$ = "west" THEN LET movement = movement + 1
200 IF locationnr = 3 AND u$ = "east" THEN LET movement = movement - 1
210 IF locationnr = 2 AND u$ = "look chest" THEN GO SUB 2000
220 IF locationnr = 3 AND u$ = "kill dragon" THEN GO SUB 3000
230 IF movement <> 0 THEN LET textnr = textnr + 1
240 IF u$ = "look" THEN LET textnr = textnr + 5 + locationnr
250 IF u$ = "inventory" THEN GO SUB 1000
260 IF locationnr = 4 AND u$ = "look corpse" THEN LET textnr = textnr + 6: IF i$ = "" THEN LET textnr = textnr - 1
270 IF locationnr = 4 AND chestopen = 0 AND i$ = "" AND u$ = "take key" THEN LET textnr = textnr + 10
280 IF locationnr = 2 AND chestopen = 0 AND i$ = "key" AND u$ = "open chest" THEN LET textnr = textnr + 2
290 IF locationnr = 2 AND chestopen = 1 AND i$ = "key" AND u$ = "take sword" THEN LET textnr = textnr + 10
300 LET locationnr = locationnr + movement
310 LET b$ = t$(textnr + 1): GO SUB 5050: REM rstrip b$
320 IF b$ = "Opened" THEN LET chestopen = 1
330 IF b$ = "Taken" THEN GO SUB 4000
340 PRINT b$ + "."
350 PRINT
360 IF textnr < 11 THEN GO TO 100
370 GO TO 9999: REM End of program.
1000 REM Inventory
1010 LET textnr = textnr + 6
1020 IF i$ = "key" THEN LET textnr = textnr - 1: RETURN
1030 IF i$ = "sword" THEN LET textnr = textnr - 2
1040 RETURN
2000 REM look into chest
2010 LET textnr = 3
2020 IF chestopen = 1 THEN LET textnr = textnr + 1
2030 IF i$ = "sword" THEN LET textnr = textnr + 2
2040 RETURN
3000 REM Kill dragon
3010 LET textnr = textnr + 11
3020 IF i$ = "sword" THEN LET textnr = textnr + 1
3030 RETURN
4000 REM Take something
4010 IF i$ = "" THEN LET i$ = "key": RETURN
4020 IF i$ = "key" THEN LET i$ = "sword"
4030 RETURN
5000 REM rstrip a$
5010 IF a$ = "pit " THEN LET a$ = "pit"
5020 RETURN
5050 REM rstrip b$
5060 FOR i = 10 TO 2 STEP -1
5070 IF b$(i) <> " " THEN RETURN
5080 LET b$ = b$(1 TO i - 1)
5090 NEXT i
5100 RETURN
9000 DATA "Cannot do", "You walk", "Opened", "Closed"
9010 DATA "A sword", "A key", "Nothing", "a chest"
9020 DATA "A dragon", "A corpse"
9030 DATA "Taken", "You died", "You won"
9050 DATA "cave", "pit", "hall", "lake"
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 »

Cave adventure seems simple enough

Also, I converted (at least tried to) Digital Prawn City Bomber.This is also big but it's a fun game :
I'm not 100% sure that negative values in DATA are the same as positive so I just left them. Also, there is a bug in my multiline conversion (coparing with the original 1liner)

Code: Select all

10 REM City Bomber JONmulti-line ver03 (BUG when crash -single line is better) Digital prawn
11 REM http://reptonix.awardspace.co.uk/sinclair/oneliners/
20 READ i,j: REM LET i=0:LET j=0
30 FOR f=0 TO 39:
40 READ d:
50 POKE USR "a"+f,d:
60 NEXT f:
70 BORDER 5:
80 POKE 23693,41:
90 CLS :
100 FOR m=1 TO 99:
110 FOR a=10 TO 27:
120 FOR b=-9 TO -RND*8:
130 PRINT INK 2*(b>-9);AT 12-b,a;CHR$ (144+b/17);AT 11-b,a;CHR$ 145:
140 NEXT b:
150 NEXT a:
160 FOR y=1 TO 20:
170 FOR x=1 TO 25+4*(y<20):
180 LET j=(j+1)*(j>0)*(j<21)+(INKEY$>"")*(j=0)*y:
190 LET i=i+(x+1-i)*(j=0):
200 LET c=ATTR (y,x+3)>41:
210 PRINT AT j-(j>0),i;" ";AT j,i;CHR$ 148 AND (j>0)*(j<21);AT y,x;" "; FLASH c;CHR$ 146;CHR$ 147:
220 BEEP .01+c,d-j-f*c:
230 IF c=0 THEN NEXT x:
240 PRINT AT y,28,: NEXT y:
250 PRINT INK 0;m;" landed":
260 BEEP .5,14: BEEP .5,16: BEEP .5,18:
270 NEXT m:
279 REM ------------------------------
280 DATA 0,0
299 REM ---house body------------
300 DATA BIN 00000001:REM DATA -1
310 DATA BIN 10011111
320 DATA BIN 10011111
330 DATA BIN 10011111
340 DATA BIN 00000001:REM DATA -1
350 DATA BIN 11110011
360 DATA BIN 11110011
370 DATA BIN 00000001:REM DATA -1
399 REM ---house top-------------
400 DATA BIN 00000000
410 DATA BIN 00011000
420 DATA BIN 00111100
430 DATA BIN 01111110
440 DATA BIN 00000001:REM DATA -1
450 DATA BIN 11110011
460 DATA BIN 11110011
470 DATA BIN 00000001:REM DATA -1
499 REM ---plane tail-----------
500 DATA BIN 00000000
510 DATA BIN 00110000
520 DATA BIN 01111000
530 DATA BIN 01111100
540 DATA BIN 01111111
550 DATA BIN 01111111
560 DATA BIN 00111111
570 DATA BIN 00000000
599 REM ---plane nose---------
600 DATA BIN 00000000
610 DATA BIN 00000000
620 DATA BIN 00000000
630 DATA BIN 00000000
640 DATA BIN 00100100:REM DATA -4
650 DATA BIN 01000010:REM DATA -2
660 DATA BIN 00100100:REM DATA -4
670 DATA BIN 00000000
699 REM ---bomb------------
700 DATA BIN 00000000
710 DATA BIN 00111100
720 DATA BIN 00011000
730 DATA BIN 00011000
740 DATA BIN 00111100
750 DATA BIN 00111100
760 DATA BIN 00111100
770 DATA BIN 00011000
9000 REM ORIG DATA 0,0,-1,159,d,d,-1,243,d,-1,0,24,60,126,-1,243,d,-1,0,48,120,124,127,d,63,0,0,0,0,0,-4,-2,-4,0,0,60,24,d,60,d,d,24
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 »

firelord wrote: Mon Dec 20, 2021 5:31 pm
stupidget wrote: Mon Dec 20, 2021 4:59 pm

What part of this code knows what to do with the data used to create the UDG's? Is it the READ statement and if so how does it know that line 3170 > 3240 is the player character?
Read finds the first DATA of the program the next read gets the second DATA etc... Wherever you put DATA - even in the beginning.
I forgot to mention RESTORE. With Restore you can change that behaviour and READ the DATA in whichever order you want.
eg See bottom of this page : https://worldofspectrum.org/ZXBasicManu ... chap6.html


Also,I forgot to mention that in City Bomber my next modification will be to make more clear the line that defines the "drop bomb" keys.
In addition I might make an extra ASCII version of City Bomber without the BEEPs (So we have a smaller type-in).
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: Elementary BASIC code snippets for Beginner

Post by FFoulkes »

firelord wrote: Mon Dec 20, 2021 9:07 pmCave adventure seems simple enough
What's Cave adventure?
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: Tue Dec 21, 2021 11:22 am
firelord wrote: Mon Dec 20, 2021 9:07 pmCave adventure seems simple enough
What's Cave adventure?
Sorry I thought it was converted from this http://reptonix.awardspace.co.uk/sincla ... ve-adv.htm
User avatar
PeterJ
Site Admin
Posts: 6879
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: Elementary BASIC code snippets for Beginner

Post by PeterJ »

My two favourite BASIC books. Lots of creative ideas (especially in Delving Deeper): Both available cheaply, or grab the PDF.

Image
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: Elementary BASIC code snippets for Beginner

Post by FFoulkes »

firelord wrote: Tue Dec 21, 2021 12:10 pmSorry I thought it was converted from this http://reptonix.awardspace.co.uk/sincla ... ve-adv.htm
Yeah, that's the one. But it's not simple at all, it's just extremely small and compressed.
That's why I made it a little bigger again, so that it's a bit easier to understand the code.
The original version is nearly impossible to understand for programming beginners.
Even my simplified version is still not so easy to understand (because I basically kept the original algorithm).
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: Tue Dec 21, 2021 5:33 pm
The original version is nearly impossible to understand for programming beginners.
I agree!
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 »

A site with various links to source code is this (link is taken from WOS cool basic tips):
http://zxgury.epizy.com/list.php?src=5& ... Games&th=0
Size of code varies as its complexity.
Some of the listings show are not complete (for copy/paste eg to Basin) but there are links to find the original sources.
Also from the above site I was directed to this :
http://retrospec.sgn.net/users/tomcat/y ... p#section3
Post Reply