Author Topic: BASIC programming tips  (Read 39685 times)

0 Members and 1 Guest are viewing this topic.

Offline Morri

  • CPC6128
  • ****
  • Posts: 284
  • Country: nz
  • Liked: 236
  • Likes Given: 215
Re: basic programming tips
« Reply #50 on: 12:38, 16 January 16 »
I'm only just getting my head around control codes after 25+ years and I LOVE THEM!
Only 1 year late but I will definitely use the control code/machine code trick in my next game.

I've attached my attempt to make multicolored sprites using control codes.

The sprite that prints out is my new character for a BASIC game I am working on.

In the one line there are commands for MODE, BORDER, INK, PEN, SYMBOL, transparent (chr$(22)) and the characters to print.
« Last Edit: 12:40, 16 January 16 by Morri »
Keeping it Kiwi since 1977

Offline Urusergi

  • CPC6128
  • ****
  • Posts: 172
  • Country: es
  • Liked: 253
  • Likes Given: 1202
Re: basic programming tips
« Reply #51 on: 18:29, 16 January 16 »
Palette in string?

10 c$="\0@@\1zz\2kk\3bb\4aa\5cc\6ff\7pp\8yy\9vv\:oo\;mm\<ss\=rr\>nn\?ii":PRINT c$

Type it but instead "\" put ctrl+"\" and effect is the same, without FOR-NEXT. This is exactly your palette. :)

Incredible but true!! this is a valid basic program:

?"\0@@\1zz\2kk\3bb\4aa\5cc\6ff\7pp\8yy\9vv\:oo\;mm\<ss\=rr\>nn\?ii"

 ;D

Offline Morri

  • CPC6128
  • ****
  • Posts: 284
  • Country: nz
  • Liked: 236
  • Likes Given: 215
Re: basic programming tips
« Reply #52 on: 20:50, 16 January 16 »
Here is a screenshot of my "BASIC" listing. Really quite amazing that this works.  :o

The dsk image is in my post above for anyone who would like to see the result.
 
« Last Edit: 20:52, 16 January 16 by Morri »
Keeping it Kiwi since 1977

Offline ZbyniuR

  • 464 Plus
  • *****
  • Posts: 316
  • Country: pl
  • 6128 A1230 PSX Win7
    • Jedyne polskie forum o CPC. :)
  • Liked: 332
  • Likes Given: 143
Re: basic programming tips
« Reply #53 on: 03:53, 17 January 16 »
Nice example of sprite in SYMBOL, but I prefer narrow text. So 4 colors in 1 character without CHR$(22), work little bit faster. Try GUI.BAS with sprite arrow pointer 100% Basic.
Or GUI2.BAS if you don't have joystick (move pointer by F1,F2,F3,F5).

And another example SYMBOL in string, to short program define national letters. All German signs in 1 line, the same other language exept Greek which need 2 lines. ;)   ALPHABET.BAS - type CONT or press "F." to change language. :)

TREK is better than WARS.

Offline Morri

  • CPC6128
  • ****
  • Posts: 284
  • Country: nz
  • Liked: 236
  • Likes Given: 215
Re: basic programming tips
« Reply #54 on: 07:05, 17 January 16 »
Wow! You blew my mind with the GUI code.
I didn't know the multi-coloured characters were even possible. How did you do that? I assume it has something to do with your .fnt file but to be fair, trying to read control codes is near impossible so I have no idea what happening past the first line setting MODE and INKS.  :laugh:
Keeping it Kiwi since 1977

Offline pelrun

  • Supporter
  • 6128 Plus
  • *
  • Posts: 580
  • Country: au
    • index.php?action=treasury
  • Liked: 296
  • Likes Given: 167
Re: basic programming tips
« Reply #55 on: 08:49, 17 January 16 »
Being able to set colours and move the cursor using control codes lets you do overprinting. Set transparent mode, set the first colour, print a character, move the cursor back, set the second colour, print the second character, repeat.

Offline Morri

  • CPC6128
  • ****
  • Posts: 284
  • Country: nz
  • Liked: 236
  • Likes Given: 215
Re: basic programming tips
« Reply #56 on: 09:02, 17 January 16 »
Being able to set colours and move the cursor using control codes lets you do overprinting. Set transparent mode, set the first colour, print a character, move the cursor back, set the second colour, print the second character, repeat.
I might be wrong, but this seemed different. I created my sprite using the technique you have just mentioned (my sprite is 16x16, 2 colour, 8 characters in all), but if you *break* ZbyniuR's code and type the character set, there are multi-coloured characters immediately (i.e. no over printing). This is what I am referring to.
Keeping it Kiwi since 1977

Offline pelrun

  • Supporter
  • 6128 Plus
  • *
  • Posts: 580
  • Country: au
    • index.php?action=treasury
  • Liked: 296
  • Likes Given: 167
Re: basic programming tips
« Reply #57 on: 11:42, 17 January 16 »
Hmm, you're right. Looks like the firmware print routine has been modified to interpret the character data as 4x8 4-colour instead of 8x8 2 colour.

Offline ZbyniuR

  • 464 Plus
  • *****
  • Posts: 316
  • Country: pl
  • 6128 A1230 PSX Win7
    • Jedyne polskie forum o CPC. :)
  • Liked: 332
  • Likes Given: 143
Re: basic programming tips
« Reply #58 on: 12:15, 17 January 16 »
Half of games on CPC is using narrow font 4x8 in 4 colors in MODE 0, quite often with horizontal stripes or antialiasing with pixels in different color on corners.
To do that just type   MODE 1:CALL &BD1C  of course you have to prepare right font because original it that case look terrible. :)
Similar way is possible to made 80 column with narrow font in MODE 1.
Just type   MODE 2:CALL &BD1C,1    Font for this mode have to be different than to previous mode. In character's matrix 8x8 left half defines color 1, right half defines color 2, like this: 11112222. If the same pixel is on, on both halves that mean color 3.

If both halves look exactly the same then  POKE 46895 change color of pen, but this is mask not color, so: 240=color1, 15=2, 255=3, 0=0. Others values makes vertical stripes. Next byte do it the same with PAPER. :)
I mean those POKEs work in 6128, cause in 464 this address is 45711.
If both halves look different proper value is 255 or just type PEN 3. Copy cursor work only in thats case.

In 40 column in MODE 0, font and colors are more complicated. In sign matrix  first half with first color and second are mixed like this: 11221122. PEN and PAPER from 0 to 3 you can change like in normal MODE 1. Others colors you can choose by POKE like above, but value for color 0 to 15 are: 0, 192, 12, 204, 48, 240, 60, 252, 3, 195, 15, 207, 51, 243, 63, 255. Others values makes vertical stripes. :)

To change between those 2 different narrow font 11112222 and 11221122 you have do with each byte of this font:
b=PEEK(a):POKE a,(b AND 195)+(b AND 48)/4+(b AND 12)*4

One more thing. If you planning use colorful font in graphics mode TAG as sprite, you have to move left-right by whole byte, or prepare least 2 version for mode 0 or least 4 version for mode 1. In other case will be look disaster. ;)

Try program NARROW.BAS  type CONT to look next trick. ;)  80IN3COL.BAS is funny too. Program 0.BAS can show fonts.
TREK is better than WARS.

Offline ZbyniuR

  • 464 Plus
  • *****
  • Posts: 316
  • Country: pl
  • 6128 A1230 PSX Win7
    • Jedyne polskie forum o CPC. :)
  • Liked: 332
  • Likes Given: 143
Re: BASIC programming tips
« Reply #59 on: 22:27, 17 January 16 »
I hope now is everything clear how to do narrow text in CPC.
Only thing we still need is editor for narrow color fonts.
TREK is better than WARS.

Offline Gryzor

  • Administrator
  • 6128 Plus
  • *****
  • Posts: 15.167
  • Country: gr
  • CPC-Wiki maintainer
    • CPCWiki
  • Liked: 3006
  • Likes Given: 5332
Re: BASIC programming tips
« Reply #60 on: 15:21, 19 January 16 »
Really lovely sets! And I love the syntax 'highlighting' :D

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.718
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 952
  • Likes Given: 1676
Re: basic programming tips
« Reply #61 on: 23:59, 24 January 16 »
Wow! You blew my mind with the GUI code.
I didn't know the multi-coloured characters were even possible. How did you do that? I assume it has something to do with your .fnt file but to be fair, trying to read control codes is near impossible so I have no idea what happening past the first line setting MODE and INKS.  :laugh:


I nearly forgot, I typed in this program from Home Computing Weekly from 1985. So the concept is very old, but their using CHR$ instead of control codes to get the job done.  :)
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Offline Morri

  • CPC6128
  • ****
  • Posts: 284
  • Country: nz
  • Liked: 236
  • Likes Given: 215
Re: BASIC programming tips
« Reply #62 on: 11:07, 30 January 16 »
I nearly forgot, I typed in this program from Home Computing Weekly from 1985. So the concept is very old, but their using CHR$ instead of control codes to get the job done.  :)
For my new game, I have decided on a mixture of using CHR$ and control codes.
It was far easier to use the SYMBOL command over the control code equivalent CHR$(25) or CTRL-Y, but putting a multicoloured sprite into a string with all needed control codes makes for much shorter code.
I looked at @ZbyniuR 's font file technique but decided against it as I just want the one basic file for this project (In case I enter it into this year's CPCretroDev2016 comp as last years rules did not allow any machine code call's or multi-load games. ::)
Keeping it Kiwi since 1977

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.718
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 952
  • Likes Given: 1676
Re: BASIC programming tips
« Reply #63 on: 12:17, 30 January 16 »
For my new game, I have decided on a mixture of using CHR$ and control codes.
It was far easier to use the SYMBOL command over the control code equivalent CHR$(25) or CTRL-Y, but putting a multicoloured sprite into a string with all needed control codes makes for much shorter code.


In that case, you might want to check out @HAL 6128 Color Flash game, which also makes good use of Control Codes for the Graphics it's producing. The project wasn't completed and there were a few bugs with it, I tried to isolate where the bugs were in the program, though couldn't figure how to address the issue. Not that I'm trying to pass the program around for it to get fixed, there might be some useful code snippets for your program.
« Last Edit: 12:24, 30 January 16 by AMSDOS »
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Offline ZbyniuR

  • 464 Plus
  • *****
  • Posts: 316
  • Country: pl
  • 6128 A1230 PSX Win7
    • Jedyne polskie forum o CPC. :)
  • Liked: 332
  • Likes Given: 143
Re: BASIC programming tips
« Reply #64 on: 19:06, 25 March 16 »
Walking Lemming in one line. :)

Code: [Select]
10 MODE 1:SYMBOL 244,60,56,60,24,24,24,56,24:SYMBOL 245,20,56,60,24,58,26,52,48:SYMBOL 246,0,40,56,60,24,24,60,102:SYMBOL 247,24,60,60,24,24,28,120,76:WHILE-1:FOR a=244 TO 247:PRINT CHR$(a)CHR$(8 );:FOR b=1 TO 80:NEXT:NEXT:WEND
« Last Edit: 19:19, 25 March 16 by Gryzor »
TREK is better than WARS.

Offline Morri

  • CPC6128
  • ****
  • Posts: 284
  • Country: nz
  • Liked: 236
  • Likes Given: 215
Re: BASIC programming tips
« Reply #65 on: 20:27, 25 March 16 »
Basic Help Wanted: Is there a way in Basic only (no asm) to disable multiple button presses? The game I am currently making uses the left & right cursor keys and the space bar. If you press two of these keys at the same time, the character on screen does all sorts of weird things.
I've disabled it to a point by using - IF INKEY(button1)=0 AND INKEY(button2)=0 then loop but the odd weird movement still gets through every now and then.

Just wondering if theres a different / better way.
Keeping it Kiwi since 1977

Offline ZbyniuR

  • 464 Plus
  • *****
  • Posts: 316
  • Country: pl
  • 6128 A1230 PSX Win7
    • Jedyne polskie forum o CPC. :)
  • Liked: 332
  • Likes Given: 143
Re: BASIC programming tips
« Reply #66 on: 20:42, 25 March 16 »
Do you need combination space and arrow simultaneous?

If not, maybe better is use INKEY$ insted INKEY(). One more benefit is you can KEY DEF  left, right and fire in joystick the same codes as arrows and space and both way of steering will be work without extra INKEY$.

If yes, you can check both keys in one line with ELSE like this:
IF INKEY(b1)=0 THEN (how react on b1...) ELSE IF INKEY(b2)=0 THEN (how react on b2...)
Now is not possible to notice both keys together.
Or use both my ideas. :)


« Last Edit: 22:14, 25 March 16 by ZbyniuR »
TREK is better than WARS.

Offline ZbyniuR

  • 464 Plus
  • *****
  • Posts: 316
  • Country: pl
  • 6128 A1230 PSX Win7
    • Jedyne polskie forum o CPC. :)
  • Liked: 332
  • Likes Given: 143
Re: BASIC programming tips
« Reply #67 on: 23:55, 25 March 16 »
The first rule of inventiveness is: Everything can be done better.

Here you are. Bunch of colorful Lemmings moving on screen.
Still in 1 line of pure Basic. I'm proud about myself. ;)

If you swap digits 7 and 2 in RIGHT$ and LEFT$ they walk backward. :D

TREK is better than WARS.

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.718
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 952
  • Likes Given: 1676
Re: BASIC programming tips
« Reply #68 on: 00:26, 26 March 16 »
Very impressive.
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Offline Morri

  • CPC6128
  • ****
  • Posts: 284
  • Country: nz
  • Liked: 236
  • Likes Given: 215
Re: BASIC programming tips
« Reply #69 on: 07:18, 26 March 16 »
The first rule of inventiveness is: Everything can be done better.

Here you are. Bunch of colorful Lemmings moving on screen.
Still in 1 line of pure Basic. I'm proud about myself. ;)

If you swap digits 7 and 2 in RIGHT$ and LEFT$ they walk backward. :D

[youtube]https://www.youtube.com/watch?v=gXN1yxax448[/youtube]

Amazing!
Keeping it Kiwi since 1977

Offline Morri

  • CPC6128
  • ****
  • Posts: 284
  • Country: nz
  • Liked: 236
  • Likes Given: 215
Re: BASIC programming tips
« Reply #70 on: 07:20, 26 March 16 »
Do you need combination space and arrow simultaneous?

If not, maybe better is use INKEY$ insted INKEY(). One more benefit is you can KEY DEF  left, right and fire in joystick the same codes as arrows and space and both way of steering will be work without extra INKEY$.

No simultaneous key presses. Your KEY DEF suggestion intrigues me, do you have an example?
Keeping it Kiwi since 1977

Offline ZbyniuR

  • 464 Plus
  • *****
  • Posts: 316
  • Country: pl
  • 6128 A1230 PSX Win7
    • Jedyne polskie forum o CPC. :)
  • Liked: 332
  • Likes Given: 143
Re: BASIC programming tips
« Reply #71 on: 12:30, 26 March 16 »
To read keys left and right and space by INKEY()
50 IF INKEY(8 )=0 THEN x=MAX(x-1,1) ELSE IF INKEY(1)=0 THEN x=MIN(x+1,40) ELSE IF INKEY(47)=0 THEN 100
60 GOTO 50
MAX and MIN makes x never is smaller than 1 or bigger than 40. Of course you can put other value. :)
If you want steering joystick <-, -> & fire in the same way
55 IF INKEY(74)=0 THEN x=MAX(x-1,1) ELSE IF INKEY(75)=0 THEN x=MIN(x+1,40) ELSE IF INKEY(76)=0 THEN 100
but loop checking keys become longer, so work little bit slower.

So we can use INKEY$ for the same keys:
50 i$=INKEY$:IF i$=CHR$(242) THEN x=MAX(x-1,1) ELSE IF i$=CHR$(243) THEN x=MIN(x+1,40) ELSE IF i$=" " THEN 100
To add serring by joystick KEY DEF before keys loop:
30 KEY DEF 74,1,242:KEY DEF 75,1,243:KEY DEF 76,0,32
In that case keys loop is still in 1 line, so still work fast. :)
Insted CHR$(242) can be "<"   to made it, type without line number:
?CHR$(242)     and EDIT 50   use copy cursor to put there arrow character.

If someone need checking up and down too. May just add more keys in the same line. INKEY() or INKEY$ whatever. But if need possibility to steering diagonal move too, it have to be done with INKEY() and vertical keys must be check in other line than horizontal. I made it in my GUI in post #53. Thats all. :)


PS.: My Lemmings can be little bit shorter, just instead LOCATE use PRINT"0LJ"l$l$.... of course 3 characters in quotation marks must be type with Control. I made in line 20 in the same way walking Smurfs. :) And I try in 30 made Minions, but they look terrible. I can't make them pretty in 8x8 in 4 colors in 4 frame, because they have so big bellies and so small legs. :(
Maybe I will try in the future, it could be fun to do simple game with Minions, including crazy violet Minions. :D
TREK is better than WARS.

Offline arnoldemu

  • Supporter
  • 6128 Plus
  • *
  • Posts: 5.332
  • Country: gb
    • Unofficial Amstrad WWW Resource
  • Liked: 2247
  • Likes Given: 3477
Re: BASIC programming tips
« Reply #72 on: 08:28, 07 August 16 »
The first rule of inventiveness is: Everything can be done better.
I missed this post until now. This is awesome.
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

Offline AMSDOS

  • Supporter
  • 6128 Plus
  • *
  • Posts: 3.718
  • Country: au
    • index.php?action=treasury
    • Programs for Turbo Pascal 3
  • Liked: 952
  • Likes Given: 1676
Re: BASIC programming tips
« Reply #73 on: 09:22, 07 August 16 »
I missed this post until now. This is awesome.


It's blows my BASIC code away!  :o
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Offline Morri

  • CPC6128
  • ****
  • Posts: 284
  • Country: nz
  • Liked: 236
  • Likes Given: 215
Re: BASIC programming tips
« Reply #74 on: 06:43, 12 March 17 »
Does anyone have some simple BASIC code to have the following sequence of numbers...
1 2 3 2 1 2 3 2 1 2 3 etc...
I can do it in the following code but just wondering if someone can do it faster.

Code: [Select]
10 a=1
20 IF a=1 THEN b=1
30 IF a=3 THEN b=-1
40 PRINT a
50 a=a+b
60 GOTO 20
Keeping it Kiwi since 1977