Page 1 of 2

Bouncing ball

Posted: Sun May 11, 2014 11:28 pm
by Shaun_B
Quick example:

Code: Select all

 1 LET I=VAL "0"
 2 LET X=I
 3 LET Y=I
 4 LET X1=PI/PI
 5 LET Y1=X1
 6 FOR I=X TO X1
 7 PRINT AT Y,X;" "
 8 LET X=X+X1
 9 LET Y=Y+Y1
10 IF X>=VAL "31" THEN LET X1=VAL "-1"
11 IF X<=PI-PI THEN LET X1=PI/PI
12 IF Y>=VAL "21" THEN LET Y1=VAL "-1"
13 IF Y<=PI-PI THEN LET Y1=PI/PI
14 PRINT AT Y,X;"O"
15 LET I=VAL "0"
16 NEXT I
Obviously does not use GOTO, which I don't think would be any quicker, just another way to solve the same problem.

There might be a way to minimise the if statements somehow but my brain isn't working at the moment - any suggestions and fixes are welcome :-)

Regards,

Shaun.

Re: Bouncing ball

Posted: Mon May 12, 2014 12:26 am
by poglad
Well your avoidance of GOTO has simply made it use more bytes really, as well as subverting the usual meaning of FOR. But anyway - one thing I remember from my 1K days is that NOT PI gives you zero in two bytes, whereas PI-PI takes 3 bytes. :D

Re: Bouncing ball

Posted: Mon May 12, 2014 12:27 am
by poglad
Oh, and SGN PI gives you 1 in 2 bytes also.

Re: Bouncing ball

Posted: Mon May 12, 2014 1:01 am
by gozzo
Another way of an 'infinite' FOR-NEXT loop:

FOR X=NOT PI TO NOT PI STEP NOT PI
...
...
NEXT X

and no need for the LET I=VAL "0" to keep it running...

but surely a normal GOTO VAL "7", and omitting line 6, would use less bytes?!

as for minimizing the IF's... try ...

LET X1=-SGN PI AND X>=VAL "31" + SGN PI AND X<=NOT PI
LET Y1=-SGN PI AND Y>=VAL "21" + SGN PI AND Y<=NOT PI

or

LET X1=-X1 AND X>=VAL "31" OR X<=NOT PI
LET Y1=-Y1 AND Y>=VAL "21" OR Y<=NOT PI

haven't tried these as of now but have used similar 'strange looking' logic comparisons before!

Re: Bouncing ball

Posted: Mon May 12, 2014 7:40 am
by Shaun_B
gozzo wrote: LET X1=-SGN PI AND X>=VAL "31" + SGN PI AND X<=NOT PI
LET Y1=-SGN PI AND Y>=VAL "21" + SGN PI AND Y<=NOT PI

or

LET X1=-X1 AND X>=VAL "31" OR X<=NOT PI
LET Y1=-Y1 AND Y>=VAL "21" OR Y<=NOT PI

haven't tried these as of now but have used similar 'strange looking' logic comparisons before!
I was thinking about something like this but really, my brain was fried yesterday.

Thanks for the tips (y)

Regards,

Shaun.

Re: Bouncing ball

Posted: Mon May 12, 2014 11:43 am
by Buka
command LET x = CODE "symbol" much faster = VAL "NN".
Unfortunately not always suitable.

But the VAL"31" and VAL "21" should be replaced by CODE "3" and CODE"+".

Re: Bouncing ball

Posted: Wed May 21, 2014 7:47 pm
by olofsen
In the emulator available here http://rullf2.xs4all.nl/sg/z81-2.2.tar.gz I've added a startup flag (-f) so that the six bytes after a number are not stored in the BASIC program when lines are entered. Programs still run, but of course more slowly. Just for fun, to see what changes are needed to the ROM (23 bytes, see function zx81hacks), and to evaluate the decrease in performance. When a p-file is loaded, the six bytes are shown as cursor characters; this could be improved, but gives a visual indication of the space needed to store the floating point numbers.

Re: Bouncing ball

Posted: Wed May 28, 2014 8:59 pm
by Shaun_B
Good work there - nice and minimised, the way I like it :-)

Regards,

Shaun.

Re: Bouncing ball

Posted: Thu Feb 23, 2017 4:13 pm
by Shaun_B
As I'm back on 8-bit stuff again, after at least a few years, I thought I'd post this bouncing ball thing but using PLOT/UNPLOT stuff (attached as an image).
Bounce with PLOT
Bounce with PLOT
bounce-plot.png (3.83 KiB) Viewed 3903 times
Regards,

Shaun.

Re: Bouncing ball

Posted: Sat Feb 25, 2017 10:17 pm
by dr beep
IF NOT LEN INKEY$ THEN NEXT I

Or just a GOTO without a FOR NEXT LOOP