## Bouncing ball

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Shaun_B
Posts: 471
Joined: Wed Apr 22, 2009 10:22 am

### Bouncing ball

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.
Posts: 133
Joined: Mon Mar 24, 2014 3:11 pm
Location: Aberdeen, Scotland

### Re: Bouncing ball

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.
Posts: 133
Joined: Mon Mar 24, 2014 3:11 pm
Location: Aberdeen, Scotland

### Re: Bouncing ball

Oh, and SGN PI gives you 1 in 2 bytes also.
gozzo
Posts: 448
Joined: Fri Jul 08, 2011 8:52 pm

### Re: Bouncing ball

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!
Shaun_B
Posts: 471
Joined: Wed Apr 22, 2009 10:22 am

### Re: Bouncing ball

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.
Buka
Posts: 33
Joined: Sun May 12, 2013 2:51 pm
Location: Russia

### Re: Bouncing ball

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"+".
"The Earth is just too small and fragile a basket for the human race to keep all its eggs in." ― Robert Anson Heinlein
Elektronika MK61, Psion series 5mx have now. Many years ago had Spectrum 48, 128k/TRDOS and Amiga A600 ...
olofsen
Posts: 177
Joined: Wed Jan 08, 2014 12:29 pm

### Re: Bouncing ball

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.
Attachments
bb.zip
Shaun_B
Posts: 471
Joined: Wed Apr 22, 2009 10:22 am

### Re: Bouncing ball

Good work there - nice and minimised, the way I like it

Regards,

Shaun.
Shaun_B
Posts: 471
Joined: Wed Apr 22, 2009 10:22 am

### Re: Bouncing ball

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-plot.png (3.83 KiB) Viewed 3901 times
Regards,

Shaun.
dr beep
Posts: 1296
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

### Re: Bouncing ball

IF NOT LEN INKEY\$ THEN NEXT I

Or just a GOTO without a FOR NEXT LOOP