IGNORED

# IntyBASIC bats!

## Recommended Posts

Hi.

Just I worked out this IntyBASIC demo of bats flying and attacking on screen.

It uses the new structured programming features (block IF and DO loop) so when you check the source code, there are no labels!

Also the SPRITE expr feature available now in IntyBASIC v1.2.3

Enjoy it!

```	REM
REM Bats
REM Demo for IntyBASIC
REM by Oscar Toledo G. http://nanochess.org/
REM Sep/01/2015
REM

' Note there is no use of labels, thanks to the use of the structured loops

include "constants.bas"

DIM x(,y(,f(,dx(

' Put in random coordinate each bat
FOR a = 0 TO 7
x(a) = RANDOM(152) + 8
y(a) = RANDOM(48) + 24
f(a) = \$2d		' Uses M and W letter (\$2d and \$37), V for attack (\$36)
dx(a) = 1
NEXT a

bats = 8

' Main loop
DO
' Show bats
FOR a = 0 TO 7
SPRITE a,x(a)+VISIBLE,y(a),f(a)*8+(a AND 1)*3+2
NEXT a
WAIT
' Move bats
FOR a = 0 TO 7
IF f(a) = \$36 THEN	' Attacking?
IF y(a) < 104 THEN y(a) = y(a) + 1:IF y(a) > 103 THEN bats = bats - 1
ELSE
' Bats goes left to right and back
x(a) = x(a) + dx(a)
IF x(a) = 8 OR x(a) = 160 THEN dx(a) = -dx(a)
' Vertical movement is random (-2 to +2)
y(a) = y(a) + RANDOM(5) - 2
IF y(a) < 8 THEN y(a) = 8
IF y(a) > 80 THEN y(a) = 80
' Move wings each 4 frames
IF (FRAME AND 3) = 0 THEN IF f(a) = \$2D THEN f(a) = \$37 ELSE f(a) = \$2d
END IF
NEXT a
IF (FRAME AND 255) = 0 THEN	' Each 256 frames, a bat attacks
FOR a = 0 TO 7
IF f(a) <> \$36 THEN f(a) = \$36:EXIT FOR
NEXT a
END IF
LOOP UNTIL bats = 0

WAIT

FOR a = 0 TO 7
SPRITE a,0
NEXT a

```

bats.bas

##### Share on other sites

Lovely !

PS

Thanks for the new implementation.

Now one can pass from this:

sprite: procedure
on n goto s0,s1,s2,s3,s4,s5,s6,s7
s0: SPRITE 0,x+VISIBLE,y+ZOOMY2,SPR00+SPR_BLUE: return
s1: SPRITE 1,x+VISIBLE,y+ZOOMY2,SPR00+SPR_BLUE: return
s2: SPRITE 2,x+VISIBLE,y+ZOOMY2,SPR00+SPR_BLUE: return
s3: SPRITE 3,x+VISIBLE,y+ZOOMY2,SPR00+SPR_BLUE: return
s4: SPRITE 4,x+VISIBLE,y+ZOOMY2,SPR00+SPR_BLUE: return
s5: SPRITE 5,x+VISIBLE,y+ZOOMY2,SPR00+SPR_BLUE: return
s6: SPRITE 6,x+VISIBLE,y+ZOOMY2,SPR00+SPR_BLUE: return
s7: SPRITE 7,x+VISIBLE,y+ZOOMY2,SPR00+SPR_BLUE: return
end
to this:
SPRITE n,x+VISIBLE,y+ZOOMY2,SPR00+SPR_BLUE
Edited by artrag
##### Share on other sites

Suddenly I had a thought of a port of the Atari 2600 version of Demon Attack...

##### Share on other sites

You know what this sample needs? More bats!!!

```	REM Bats
REM Demo for IntyBASIC
REM by Oscar Toledo G. http://nanochess.org/
REM Sep/01/2015
REM

' Note there is no use of labels, thanks to the use of the structured loops

include "constants.bas"
FLICKER=0
Timer=0
DIM x(16),y(16),f(16),dx(16)

' Put in random coordinate each bat
FOR a = 0 TO 15
x(a) = RANDOM(152) + 8
y(a) = RANDOM(48) + 24
f(a) = \$2d		' Uses M and W letter (\$2d and \$37), V for attack (\$36)
dx(a) = 1
NEXT a

bats = 16

' Main loop
DO
' Show bats
Timer=Timer+1
IF flicker=0 then
FOR a = 0 TO 7
SPRITE a,x(a)+VISIBLE,y(a),f(a)*8+(a AND 1)*3+2
NEXT a
END IF
if flicker=0 then flicker=1:goto flick
IF flicker=1 then
FOR a = 0 TO 7
SPRITE a,x(a++VISIBLE,y(a+,f(a+*8+(a+8 AND 1)*3+2
NEXT a
flicker=0
END IF
flick:
WAIT
' Move bats
FOR a = 0 TO 15
IF f(a) = \$36 THEN	' Attacking?
IF y(a) < 104 THEN y(a) = y(a) + 1:IF y(a) > 103 THEN bats = bats - 1
ELSE
' Bats goes left to right and back
x(a) = x(a) + dx(a)
IF x(a) = 8 OR x(a) = 160 THEN dx(a) = -dx(a)
' Vertical movement is random (-2 to +2)
y(a) = y(a) + RANDOM(5) - 2
IF y(a) < 8 THEN y(a) = 8
IF y(a) > 80 THEN y(a) = 80
' Move wings each 4 frames
IF (Timer AND 3) = 0 THEN IF f(a) = \$2D THEN f(a) = \$37 ELSE f(a) = \$2d
END IF
NEXT a
IF (Timer AND 255) = 0 THEN	' Each 256 frames, a bat attacks
FOR a = 0 TO 15
IF f(a) <> \$36 THEN f(a) = \$36:EXIT FOR
NEXT a
END IF
LOOP UNTIL bats = 0

WAIT

FOR a = 0 TO 7
SPRITE a,0
NEXT a

```

I would have got fired from Mattel for multiplexing sprites and get fired from nanochess for using a label in this sample.

Anyway, I used label to do flickering routine for the Colecovision and decided to use it here since I didn't know a way to do flipflop. I changed FRAME to Timer since FRAME is a 16-bit variable and Timer is 8-bit. So the event of the bats to attack wasn't working properly with 16 bats. This is just a short exercise to test out multiplexing.

##### Share on other sites

Hehehe, pretty well done!

BTW, as you increase timer in >each< frame, you get same behavior as with FRAME AND 255, besides as Timer is an 8 bits variable the AND 255 is unneeded.

Furthermore, you could have created a constant called MAX_BATS = 16, and use it in the FOR loop as MAX_BATS - 1

Going further, you can delete the label using the IF block feature:

```IF flicker=0 then
FOR a = 0 TO 7
SPRITE a,x(a)+VISIBLE,y(a),f(a)*8+(a AND 1)*3+2
NEXT a
flicker = 1
ELSE
FOR a = 0 TO 7
SPRITE a,x(a++VISIBLE,y(a+,f(a+*8+(a+8 AND 1)*3+2
NEXT a
flicker=0
END IF```
##### Share on other sites

While optimizing, you could replace this:

```        FOR a = 0 TO 7
SPRITE a,x(a++VISIBLE,y(a+,f(a+*8+(a+8 AND 1)*3+2
NEXT a
```

by the following code, that saves 8*4 = 32 additions at the cost of 8 subtractions

```        FOR a = 8 TO 15
SPRITE a-8,x(a)+VISIBLE,y(a),f(a)*8+(a AND 1)*3+2
NEXT a
```
##### Share on other sites

While optimizing, you could replace this:

```        FOR a = 0 TO 7
SPRITE a,x(a++VISIBLE,y(a+,f(a+*8+(a+8 AND 1)*3+2
NEXT a
```

by the following code, that saves 8*4 = 32 additions at the cost of 8 subtractions

```        FOR a = 8 TO 15
SPRITE a-8,x(a)+VISIBLE,y(a),f(a)*8+(a AND 1)*3+2
NEXT a
```

In fact thanks to the new IntyBASIC optimizer, both code sequences run at same speed you can write it as you like most

##### Share on other sites

Impressive! Is it already released in the current intybasic or will it appear in the next versions ?

##### Share on other sites

Hehehe, pretty well done!

BTW, as you increase timer in >each< frame, you get same behavior as with FRAME AND 255, besides as Timer is an 8 bits variable the AND 255 is unneeded.

Furthermore, you could have created a constant called MAX_BATS = 16, and use it in the FOR loop as MAX_BATS - 1

Going further, you can delete the label using the IF block feature:

```IF flicker=0 then
FOR a = 0 TO 7
SPRITE a,x(a)+VISIBLE,y(a),f(a)*8+(a AND 1)*3+2
NEXT a
flicker = 1
ELSE
FOR a = 0 TO 7
SPRITE a,x(a++VISIBLE,y(a+,f(a+*8+(a+8 AND 1)*3+2
NEXT a
flicker=0
END IF```

FRAME variable I believe is increased at every interrupt. So if the game is at half speed, then it won't hit 255 or 3, so I think FRAME is increasing by 2. So the events weren't activating. Currently, I'm trying to figure out how to make it stop multiplexing when the bats are less than 9. Eventually I'll crack the code. And also I'm thinking of making a game based of this sample. Edited by Kiwi
##### Share on other sites

I would avoid sprite multiplexing if you plan to run the main loop at 30Hz...

Flickering would be too high.

Btw

There is also on frame gosub...

It could be used to multiplex sprites independently of the main loop timings.

Edited by artrag
##### Share on other sites

Impressive! Is it already released in the current intybasic or will it appear in the next versions ?

FRAME variable I believe is increased at every interrupt. So if the game is at half speed, then it won't hit 255 or 3, so I think FRAME is increasing by 2. So the events weren't activating. Currently, I'm trying to figure out how to make it stop multiplexing when the bats are less than 9. Eventually I'll crack the code. And also I'm thinking of making a game based of this sample.

Yes, but you were updating the variable in each frame or maybe simply the program went slow and you were losing frames.

You can use the 'bats' variable as reference counter as the bats are deleted in sequence.

##### Share on other sites

Currently, I'm trying to figure out how to make it stop multiplexing when the bats are less than 9. Eventually I'll crack the code. And also I'm thinking of making a game based of this sample.

The trick I use is to keep track of the "first not displayed" sprite when scanning the list of active sprites, and display the first 8 active sprites after that. Then I update the "first not displayed" variable, so the next frame picks up where I left off. This results in gradual multiplexing as the scene gets busier.

I whipped up a really crude demo just now to demonstrate. Buttons 0..7 on the two controllers will toggle 16 different sprites on/off. Once 9 go active, it starts multiplexing, but only one sprite gets left off of each screen, and the unlucky sprite rotates through the whole list.

.

```
' Multiplexing demo with up to 16 sprites
' An inactive sprite sets x(i) = 0
'
' Demo by J. Zbiciak
' Hereby placed in public domain

DIM x(16), y(16), #a(16)

first_not_displayed = 0

CLS

' Initially all sprites are off
FOR i = 0 TO 15
x(i) = 0
NEXT i

WHILE 1
WAIT

' Keys 0 .. 7 on each controller toggle which sprites are active

IF bounce_counter = 0 THEN
IF CONT1.KEY < 8 THEN snum =     CONT1.KEY : GOSUB ToggleSprite
IF CONT2.KEY < 8 THEN snum = 8 + CONT2.KEY : GOSUB ToggleSprite
ELSE
IF CONT.KEY = 12 THEN bounce_counter = bounce_counter - 1
END IF

' Multiplex loop:  Scan starting at 'first not displayed' until up to
' 8 active sprites are found

active = 0
FOR i = 0 to 15
ii = (i + first_not_displayed) AND 15

IF x(ii) <> 0 THEN
SPRITE active, x(ii) + \$200, y(ii) + \$100, #a(ii)
active = active + 1
IF active = 8 THEN
first_not_displayed = (ii + 1) AND 15
EXIT FOR
END IF
END IF
NEXT i

WEND

ToggleSprite:   PROCEDURE
bounce_counter = 5

IF x(snum) > 0 THEN
x(snum) = 0
RETURN
END IF

x (snum) = 9 * snum + 9
y (snum) = 6 * snum + 6
#a(snum) = (\$41 + snum) * 8 + 7

RETURN
END

```
Edited by intvnut
##### Share on other sites

That's neat, intvnut. Do you mind if we add it to the SDK "Examples" folder?

##### Share on other sites

That's neat, intvnut. Do you mind if we add it to the SDK "Examples" folder?

Certainly! Be my guest!

I'll leave it as an "exercise for the reader" to fold this approach into the Bats program that started the thread.

##### Share on other sites

Certainly! Be my guest!

I'll leave it as an "exercise for the reader" to fold this approach into the Bats program that started the thread.

Cool, thanks.

##### Share on other sites

• 5 months later...

How about a mode where we can shoot the bats?

##### Share on other sites

How about a mode where we can shoot the bats?

Possible. It would look a lot like Galaxian

##### Share on other sites

Indeed! The demo reminds me a bit of the second level in Arcadia by Imagine Software, for the ZX Spectrum, VIC-20, C64 and Dragon 32. The game is said to borrow from both Galaxian and Gorf.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.