Classic Computer Magazine Archive COMPUTE! ISSUE 86 / JULY 1987 / PAGE 8

Sprites On The 64

I have a question about the Commodore 64. How do you display two different sprites at the same time? Also, how do you use multicolor sprites?

Ty Neyedli

All the Commodore 64's sprite features are generated by the computer's VIC-II video chip. That chip is controlled by registers that appear at memory locations 53248–53294. You manipulate sprites by POKEing values into these registers. For example, memory location 53269 is used to turn any of the 64's eight sprites on or off. Here is the general formula for turning on a sprite:

POKE 53269, 2↑SN

In this case, SN represents the number of the sprite you wish to turn on. The sprites are usually numbered 0–7. Thus, POKE 53269,1 turns on sprite 0. POKE 53269,4 turns on sprite 2, and so forth. To turn on more than one sprite, you simply add the POKE values together. Thus, POKE 53269,5 turns on both sprite 0 and sprite 2. To turn on sprites 3, 5, and 7, you would use POKE 53269, 168 (2↑3 + 2↑5 + 2↑7 = 8 + 32 + 128 168).

Even after you turn on a sprite, it won't be visible until you position it somewhere on the active portion of the screen. Memory locations 53248–53264 control the sprite vertical and horizontal positions. The even-numbered addresses (53248, 53250, and so on) control horizontal positioning, while odd-numbered addresses (53249, 53251, and so on) are for vertical positions. Each sprite uses a pair of adjacent addresses. For instance, the horizontal position of sprite 0 is controlled by location 53248 and the vertical position by location 53249. If you don't care to memorize these locations, you can calculate the position location address for a given sprite with ihe formula 53248 + SN * 2, where SN is the sprite number.

These program lines display sprites 0 and 1 in the middle of the screen:

10 POKE 53269, 3: REM 3 = 2↑0 + 2↑1
20 POKE 53248,90: POKE 53249, 150
30 POKE 53250, 130: POKE 53251, 150

Making things more complicated is the horizontal "seam" that runs down the right side of the screen. The active screen area is 320 pixels wide—more possible horizontal positions than can be specified with a single memory location (one byte can only hold values 0–255). Thus, it's necessary to use a second location that indicates whether a sprite is on the right or left side of the seam. Location 53264 works like the one that turns sprites on and off. To position a sprite on the right side of the seam, use

POKE 53264, 2↑SN

where SN represents the sprite number. For example, you can display sprite 0 near the right border by substituting this line in the example program:

20 POKE 53248, 90: POKE 53249, 90:POKE 53264, 1

If you type in and run this program, you'll notice that the sprites have a random, disorganized form, because we haven't yet defined their shapes. Locations 2040–2047 contain pointers that tell the computer where each sprite's shape data is located. By POKEing different values in these registers, you can change a sprite's shape. While the sprites are on the screen, type in the following line and press RETURN:

FOR J = 0 TO 255 : POKE 2040, J : NEXT

This line makes sprite 0 flip rapidly from one shape to another. Again, because we're pointing to memory areas that don't contain sprite shapes, these shapes will look random. Each sprite pattern contains 63 bytes of data. The easiest way to design sprites is with a sprite editor program such as "Sprite Designer," found elsewhere in this issue.

Sprite colors are controlled by locations 53287–53294. The color numbers for sprites are the same numbers used for screen and text colors. Thus, POKE 53287,0 turns sprite 0 black. POKE 53288,7 changes sprite 1 to yellow, and so on.

Multicolor sprites have four different colors, but only half the resolution of normal sprites, making them more colorful, but chunkier in appearance. Location 53276 controls sprite multicolor mode. This register works like locations 53269 and 53264. To set a sprite to multicolor mode, use POKE 53276, 2↑|SN, where SN represents the number of the sprite you wish to change.

In multicolor mode, locations 53285 and 53286 determine the third and fourth colors for all the multicolor sprites. The first and second colors of the sprite are set by the screen background color register (53280) and the individual sprite color locations (53287–53294).

The following program displays and animates three multicolor sprites:

EB 10 REM POKE IN SOLID SPRITE PATTERN
HS 20 FOR 1 = 832 TO 1023 : POKE I, 170 : NEXT
QH 30 REM PUT IN STRIPE
HJ 40 FOR 1 = 0 TO 5
XA 50 POKE 832 + I + 12, 255 : POKE 8 96 + I + 36, 255 : POKE 960 + I + 5 7, 255
HH 60 NEXT I
JA 70 REM PUT IN SECOND MULTICOLOR STRIPE
CX 80 FOR I = 0 TO 8
PH 90 POKE 832 + I + 18, 85 : POKE 89 6 + I + 42, 85 : POKE 960 + I, 85
AP 100 NEXT I
KA 110 REM P() IS POINTER
SJ 120 P(1) = 13 : P(5) = 14 : P(6) = 14
CQ 130 REM X() IS HORIZONTAL POSITION
FF 140 X(1) = 90 : X(5) = 200 : X(6) = 280
RX 150 REM Y() IS VERTICAL POSITION
XC 160 Y(1) = 99 : Y(5) = 88 : Y(6) = 77
RE 170 REM TURN ON SPRITES 98 = 2↑1 + 2↑5 + 2↑6
HE 180 POKE 53269,98
SA 190 REM MAKE THEM ALL MULTICOLOR
DF 200 POKE 53276, 98
XP 210 REM SET MULTICOLORS TO {SPACE}YELLOW,BLACK
HM 220 POKE 53285, 7 : POKE 53286, 0
DH 230 REM SET SPRITE PRIMARY {SPACE}COLOR
EQ 240 POKE 53288, 1 : POKE 53292, 2 : POKE 53293, 3
DK 250 FOR SN = 0 TO 7
GH 260 IF P(SN) = 0 THEN360
JE 270 P(SN) = P(SN) + 1 : IF P(SN) = 16 THEN P(SN) = 13
CK 280 POKE 2040 + SN, P(SN)
CR 290 Y(SN) = Y(SN) + 2
SS 300 IF Y(SN)>255 THEN Y(SN) = 32
MR 310 IF X(SN)>256 THEN340
AK 320 POKE 53248 + SN * 2, X(SN) : POKE 53264, PEEK(53264) AND (255-2↑SN) : GOTO350
SP 330 REM FAR RIGHT
FQ 340 T = X(SN)-256 : POKE 53264, PEEK(53264) OR 2↑SN
SO 350 POKE 53249 + SN * 2,Y(SN)
MQ 360 NEXT SN : GOTO250