Rnd help please - Printable Version

+- Forum (
+-- Forum: Compilers and Computer Languages (
+--- Forum: ZX Basic Compiler (
+---- Forum: Help & Support (
+---- Thread: Rnd help please (/showthread.php?tid=305)

Rnd help please - slenkar - 02-18-2011

heres my code

POKE Uinteger 23675, @MyUdgBlock
  PRINT AT 3,13 ; PAPER 1 ; INK 7 ; "BattleField"
  PRINT AT 5,9 ; PAPER 7 ; INK 0 ; "Q - Up"
  PRINT AT 6,9 ; PAPER 7 ; INK 0 ; "A - Down"
  PRINT AT 7,9 ; PAPER 7 ; INK 0 ; "O - Left"
  PRINT AT 8,9 ; PAPER 7 ; INK 0 ; "P - Right"
  PRINT AT 9,9 ; PAPER 7 ; INK 0 ; "M - Select"

  PRINT AT 10,3 ; PAPER 7 ; INK 0 ; "You have to select units"
  PRINT AT 11,3 ; PAPER 7 ; INK 0 ; "to fight the enemy units"
  PRINT AT 15,3 ; PAPER 7 ; INK 0 ; "Press any key to start"

60  LET j$ = INKEY$
  IF j$ = "" THEN GOTO 60: END IF

  Function byterndrange (first As uByte, last As uByte) As uByte
    return Rnd * (last - first) + first
End Function
paper 4
for x=0 to 200
plot  byterndrange(0,255),byterndrange(0,192)

Dim BlueFactoryX (6) as uByte
Dim BlueFactoryY (6) as uByte
For x =0 to 5
BlueFactoryX(x)  = byterndrange(1,9)
BlueFactoryY(x)  = byterndrange(1,24)

dim RedFactoryX (6) as uByte
dim RedFactoryY (6) as uByte

For x =0 to 5
RedFactoryX(x)  = byterndrange(28,32)
RedFactoryY(x)  = byterndrange(1,24)


sub drawFactories()
for x =0 to 5
ink 1
print at BlueFactoryX(x), BlueFactoryY(x); CHR$(144)
ink 2
print at RedFactoryX(x), RedFactoryY(x); CHR$(144)

End sub

STOP : REM Avoid execution to enter this zone
MyUdgBlock: REM a simple Label; @Label returns it's memory address
incbin "factory.BIN"

When I draw random dots the rnd function works fine,
but when I draw factories at random positions it doesnt turn out right

I am trying to draw the red ones on the right side of the screen but they end up on the left
the blue ones are all over the screen

Re: Rnd help please - boriel - 02-18-2011

You're overflowing your screen: SINCLAIR Basic PRINT AT uses Y, X coordinates (row, column) Idea
I've reversed them in your listing and it seems to be working ok. Since this is a compiled program, by default it won't check your screen overflows (e.g. "OUT of Screen error"), but with the flag (NOT YET IMPLEMENTED) --debug-screen it will stop when this happens.

There are more checkers already done:
--debug-array Will raise a "Subscript out of range" if you access an non-existing array-cell
--debug-memory Will raise an "Out of memory error" in case the program run out of mem.

The idea is: If you suspect your program has an error, enable the checkers and tests it. Once your program runs ok, compile again without your check flags (faster and less memory required)

Idea Tip: PRINT ... ; is faster than PRINT ... , because the former doesn't need to print a newline. So use PRINT ... ; whenever possible. Also for legibility, you can use PRINT "\A" instead of PRINT CHRS$(144) (the compiled code is the same, though);

Re: Rnd help please - slenkar - 02-18-2011

thanks for the fix

Quote: PRINT ... ; is faster than PRINT ... ,

im not sure what this means,

like this?
Print y,x;
print y,x,

Re: Rnd help please - boriel - 02-18-2011

slenkar Wrote:thanks for the fix

Quote: PRINT ... ; is faster than PRINT ... ,

im not sure what this means,

like this?
Print y,x;
print y,x,
No, this way:
PRINT a$; : REM Faster than PRINT a$
When you use PRINT, by default it prints a NEW LINE (carriage return) if no semicolon is used. So use semicolon (Wink as it's slightly faster (specially during in-game prints)

Re: Rnd help please - slenkar - 02-19-2011

ok thanks for that

Re: Rnd help please - boriel - 02-20-2011

I've just uploaded a new revision 1.2.7-r2096. Please download and test! It has some bugfixes so the --debug-array and the --debug-memory works ok again. If you run your program with --debug-array it seems to work ok, but sometimes an "OUT of Screen" is returned. So check screen boundaries.