Skip to content
Game 14 Unit 10 of 32 1 hr learning time

A Row of Drones

Eight drones tracked with arrays — individual state for each enemy.

31% of Blockstorm

One drone is a test. Eight drones in a row is a game. This unit introduces arrays to track multiple enemies at once. Each drone has its own position and its own alive flag. The bullet checks against every drone in the row. Shoot one and the others remain, waiting for the next shot.

The Program

5 REM === A ROW OF DRONES ===
10 BORDER 0: PAPER 0: INK 7: CLS
15 GO SUB 800
20 LET sc = 0: LET hi = 0: LET li = 3
22 LET wv = 1: LET ne = 8
25 DIM e(8): DIM r(8): DIM h(8)
30 FOR i = 1 TO ne
32 LET e(i) = (i - 1) * 2: LET r(i) = 0: LET h(i) = 1
34 NEXT i
36 LET fx = 6: LET fy = 4
40 LET px = 15: LET py = 20
42 LET ba = 0: LET bx = 0: LET by = 0
50 GO SUB 600
55 PRINT AT py, px; INK 7; CHR$ 152
57 FOR i = 1 TO ne
58 PRINT AT fy + r(i), fx + e(i); INK 4; CHR$ 144
59 NEXT i
60 REM === game loop ===
70 LET k$ = INKEY$
80 IF k$ = "o" OR k$ = "O" THEN IF px > 1 THEN PRINT AT py, px; " ": LET px = px - 1: PRINT AT py, px; INK 7; CHR$ 152
90 IF k$ = "p" OR k$ = "P" THEN IF px < 30 THEN PRINT AT py, px; " ": LET px = px + 1: PRINT AT py, px; INK 7; CHR$ 152
100 IF k$ = " " THEN IF ba = 0 THEN LET bx = px: LET by = py - 1: LET ba = 1: PRINT AT by, bx; INK 7; CHR$ 148
110 IF ba = 0 THEN GO TO 60
120 PRINT AT by, bx; " "
130 LET by = by - 1
140 IF by < 1 THEN LET ba = 0: GO TO 60
150 REM check hit against all drones
155 LET ht = 0
160 FOR i = 1 TO ne
162 IF h(i) <= 0 THEN GO TO 168
164 IF bx = fx + e(i) AND by = fy + r(i) THEN LET ht = i: LET i = ne
168 NEXT i
170 IF ht = 0 THEN PRINT AT by, bx; INK 7; CHR$ 148: GO TO 60
180 LET ba = 0: LET h(ht) = 0
190 PRINT AT fy + r(ht), fx + e(ht); INK 2; CHR$ 149
195 BEEP 0.02, -5: BEEP 0.02, -10
200 PAUSE 8
210 PRINT AT fy + r(ht), fx + e(ht); " "
220 LET sc = sc + 10
225 GO SUB 600
230 GO TO 60
600 REM === draw HUD ===
605 PRINT AT 0, 0; INK 7; BRIGHT 1; "SC:"; sc; "  HI:"; hi; "  LV:"; li; " W"; wv; "  "
610 RETURN
800 REM === define UDGs ===
805 FOR i = 0 TO 13
810 FOR j = 0 TO 7
815 READ a
820 POKE USR CHR$ (65 + i) + j, a
825 NEXT j
830 NEXT i
835 RETURN
840 REM UDG A: drone
842 DATA 36, 126, 219, 255, 255, 102, 66, 0
844 REM UDG B: scout
846 DATA 24, 60, 126, 255, 219, 24, 36, 0
848 REM UDG C: tank
850 DATA 126, 255, 255, 255, 255, 255, 126, 0
852 REM UDG D: bomber
854 DATA 66, 231, 255, 126, 60, 90, 36, 0
856 REM UDG E: bullet
858 DATA 16, 56, 16, 16, 0, 0, 0, 0
860 REM UDG F: explosion
862 DATA 36, 153, 66, 129, 66, 153, 36, 0
864 REM UDG G: double shot
866 DATA 84, 170, 84, 170, 84, 170, 84, 0
868 REM UDG H: shield
870 DATA 60, 126, 255, 255, 255, 126, 60, 0
872 REM UDG I: player ship
874 DATA 16, 56, 56, 124, 254, 254, 130, 0
876 REM UDG J: enemy bomb
878 DATA 16, 16, 56, 16, 0, 0, 0, 0
880 REM UDG K: explosion 2
882 DATA 129, 66, 36, 0, 36, 66, 129, 0
884 REM UDG L: shield icon
886 DATA 60, 126, 126, 126, 60, 24, 0, 0
888 REM UDG M: drone frame 2
890 DATA 66, 102, 255, 255, 219, 126, 36, 0
892 REM UDG N: scout frame 2
894 DATA 36, 24, 219, 255, 126, 60, 24, 0

How It Works

Lines 25-27 create the enemy arrays with DIM. Array e() holds x-positions (column offsets), r() holds y-positions (row offsets), t() holds enemy types, and h() holds hit points. Each array is dimensioned to hold up to 20 enemies — more than enough for any wave.

Lines 54-58 read enemy data from DATA statements. A FOR/NEXT loop reads four values for each enemy: x-offset, y-offset, type and hit points. The wave data at line 906 defines eight drones in a single row, each spaced two columns apart.

Lines 366-385 extend the collision check. Instead of comparing the bullet position to one drone, a FOR/NEXT loop checks every enemy in the array. If the bullet matches any living enemy (one with hit points greater than 0), that enemy is destroyed.

Arrays for Game Objects

DIM creates a numbered list of variables. DIM e(20) gives you e(1) through e(20) — twenty slots for twenty enemy positions. Without arrays, you would need twenty separate variables (e1, e2, e3…) and twenty separate IF statements to check each one. Arrays let a single FOR/NEXT loop handle them all:

FOR i = 1 TO ne
  IF h(i) > 0 THEN check collision
NEXT i

One loop, any number of enemies. Add more enemies and the loop handles them automatically. This is the pattern for managing groups of objects in any game.

The h() Array

The hit points array h() serves double duty. When h(i) is greater than 0, the enemy is alive. When h(i) reaches 0 (or below), the enemy is dead. For drones, hit points start at 1 — one shot kills. Later, tanks will start with 2 hit points, requiring two shots to destroy. The same array handles both cases.

Try This

Change the spacing. The DATA defines drones at columns 0, 2, 4, 6, 8, 10, 12, 14 — every other column. Change the offsets to 0, 1, 2, 3, 4, 5, 6, 7 for a tight block, or 0, 4, 8, 12 for wider spacing. Which arrangement is more fun to shoot?

Count survivors. After clearing a few drones, count how many remain by looping through h() and counting values greater than 0. Display the count on screen. This is the foundation for wave-clear detection.