SPRITE MANAGEMENT: MULTIPLEXING MULTIPLE SPRITES

This example shows how to handle various sprites on a system that supports sprite multiplexing. It also uses some constructs to automatically calculate the position of the sprites.

source compile sandbox issues? back to examples

SOURCE CODE ()

	OPTION EXPLICIT
	
	DEFINE IMAGE FREE HEIGHT
	DEFINE MSPRITE ASYNC
	
	BITMAP ENABLE
	
	COLOR BORDER BLACK
	
	CONST maxAlienships = 5
	
	DIM index AS BYTE

	DIM spaceImage AS IMAGE

	DIM spaceshipImage AS IMAGE
	DIM spaceshipSprite AS MSPRITE
	DIM spaceshipLeftMove AS PATH
	DIM spaceshipRightMove AS PATH
	DIM spaceshipMove AS PATH
	DIM spaceshipX AS POSITION, spaceshipY AS POSITION

	DIM alienshipImage AS IMAGE
	DIM alienshipSprite(maxAlienships) AS MSPRITE
	DIM alienshipLeftMove(maxAlienships) AS PATH
	DIM alienshipRightMove(maxAlienships) AS PATH
	DIM alienshipMove(maxAlienships) AS PATH
	DIM alienshipLX(maxAlienships) AS POSITION, alienshipRX(maxAlienships) AS POSITION
	DIM alienshipX AS POSITION, alienshipY AS POSITION

	spaceImage := LOAD IMAGE("space.png")
		
	spaceshipImage := LOAD IMAGE("spaceship.png")
	spaceshipSprite = MSPRITE(spaceshipImage)
	spaceshipLeftMove = CREATE PATH( 200, 200 TO 50, 200 )
	spaceshipRightMove = CREATE PATH( 50, 200 TO 200, 200 )
	spaceshipMove = spaceshipRightMove

	alienshipImage := LOAD IMAGE("alienship.png")
	
	index = 0
	DO
		alienshipSprite(index) = MSPRITE(alienshipImage)
		alienshipLX(index) = index*32
		alienshipRX(index) = 200+index*10
		alienshipLeftMove(index) = CREATE PATH( alienshipRX(index), 60+index*16 TO alienshipLX(index), 60+index*32 )
		alienshipRightMove(index) = CREATE PATH( alienshipLX(index), 60+index*32 TO alienshipRX(index), 60+index*16 )
		alienshipMove(index) = alienshipRightMove(index)
		INC index
		EXIT IF index = maxAlienships
	LOOP

	CLS

	PUT IMAGE spaceImage AT 0, 0

	DO
		TRAVEL spaceshipMove TO spaceshipX, spaceshipY 
		SPRITE spaceshipSprite AT spaceshipX, spaceshipY
		IF spaceshipX = 200 THEN
			spaceshipMove = spaceshipLeftMove
		ELSE IF spaceshipX = 50 THEN
			spaceshipMove = spaceshipRightMove
		ENDIF
		
		index = 0
		DO
			TRAVEL alienshipMove(index) TO alienshipX, alienshipY 
			SPRITE alienshipSprite(index) AT alienshipX, alienshipY
			IF alienshipX = alienshipRX(index) THEN
				alienshipMove(index) = alienshipLeftMove(index)
			ELSE IF alienshipX = alienshipLX(index) THEN
				alienshipMove(index) = alienshipRightMove(index)
			ENDIF
			INC index
			EXIT IF index = maxAlienships		
		LOOP

		MSPRITE UPDATE
	LOOP

How to compile and run the example

The instructions here refer to compiling the example from the command line. For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile each single example with just one click.



Are instructions for your specific home computer / console missing? First of all, check if your computer is supported by clicking here. If so, since ugBASIC is a language which does not provide abstractions, it is possible that this example will not work on your target. If you think this is an issue, please click here.

Commodore 128 (CPU 8502)

In order to compile the example, type this command on the command line:

Linux

ugbc.c128 -O prg -o sprites_example_10.prg sprites_example_10.bas

Windows

ugbc.c128.exe -O prg -o sprites_example_10.prg sprites_example_10.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Commodore 64, Commodore Executive 64

In order to compile the example, type this command on the command line:

Linux

ugbc.c64 -O prg -o sprites_example_10.prg sprites_example_10.bas

Windows

ugbc.c64.exe -O prg -o sprites_example_10.prg sprites_example_10.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Commodore 64 + REU

In order to compile the example, type this command on the command line:

Linux

ugbc.c64reu -O d64 -o sprites_example_10.d64 sprites_example_10.bas

Windows

ugbc.c64reu.exe -O d64 -o sprites_example_10.d64 sprites_example_10.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Any problem?

If you have found a problem trying to run this example, if you think there is a bug or, more simply, you would like it to be improved, open an issue for this example on GitHub.
Thank you!

open an issue BACK TO EXAMPLES