## Control codes

Posts: 13

Joined: Mon Apr 18, 2016 9:51 pm

### Control codes

Hi
I'm new here, so excuse me if anyone asked this before. I tryed to search for it but didnt find anything.

So i'm playing with ZX basic for last week or so and i was doing some of my usual tests.
In one of them i was trying to print out a block of characters with random ink and color. And because making a loop that will print it character by character is rather slow, i tryed to be clever. It didn't work ^_^

So my problem:

i wanted to make a string variable with control codes that i would print out. And sure enough if i do

p\$="\{i5}X"
print p\$

it work just fine.

Problem is when i want to put some randomness to it and do something like

i\$=str(int(rnd*7))
p\$="\{i"+i\$+"}X"
print p\$

it outputs

\{i2}X

instead of colored X. In my mind, it should work...
Am i doing something wrong?

Also, implementing AT as \{ax,y} like it works in BASin would be nice ^_^

Posts: 1483

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

### Re: Control codes

BASIN has its own codes. ZX BASIC is a compiler, so it tries to guess some (a few of them!) at compile time. NOT at runtime.
It's much faster if you use the ink code which is CHR(16) (Read here).
Code:
`p\$="{\i" + x\$ + "}X"`

use
Code:
`p\$=CHR\$(16) + i\$ + "X";`

Actually, CHR\$ is optimized for several chars at once. So CHR\$(x) + CHR\$(y) + CHR\$(z) + ... can be written as CHR\$(x, y, z, ...). Example:
Code:
`p\$ = CHR\$(16, INT(RND * 7), CODE "X")`

Give it a try. Also, in this case, you can ignore the INT( )... because CHR\$ will take only INT args and do it for you.
Tell me is this work for you

Posts: 13

Joined: Mon Apr 18, 2016 9:51 pm

### Re: Control codes

Thanks, that was really helpful. I guess i wasnt coding in speccy basic for quite some time, otherwise i woul'd realise.
Will give it a go.

Posts: 13

Joined: Mon Apr 18, 2016 9:51 pm

### Re: Control codes

Ok it works.
Well what works i when i use
Code:
`x\$ = chr(17) + str(p) + "O"print x\$`

I also tryed to use chr(22) which is supposed to be at and it kinda works
there i actually use
Code:
`x\$ = chr(22,5,5) + "O"print x\$`

and yes, it prints on 6th row, but in the 1st column...

Thanks for your help anyway, gotta figure out what to use this for now ^_^

Posts: 1483

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

### Re: Control codes

That might be a bug. I'll check it, but
CHR\$(22, 5, 5) + "0" should be equivalent to PRINT AT 5, 5; "0"

Posts: 13

Joined: Mon Apr 18, 2016 9:51 pm

### Re: Control codes

That would be awsome.
I often use thease to generate a block of "graphics" i can place on the screen fairly quickly ^_^

Posts: 1483

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

### Re: Control codes

rikokun wrote:That would be awsome.