Files
akbasic/docs/18-tutorial-breakout-artwork.md
Logikoma 1a7226eef1 Keep BASIC fixtures within the input line limit
Reflow the approved reference cases, shorten local fixture comments, and split the executable docs line so the 80-byte input buffer can read every source line.

Co-authored-by: andrew <andrew@aklabs.net>
2026-08-04 20:05:41 -04:00

60 KiB
Raw Blame History

18. Tutorial: Breakout with artwork

This chapter builds Breakout a second time, out of loaded PNG artwork, with a drawn brick field, five powerups, three voices of sound and a HUD in colour. It is a bigger program than Chapter 17's and it uses a completely different set of verbs: everything here is drawn or loaded, and nothing is written into the text grid.

This is what you are building:

The finished game: a coloured HUD across the top, five rows of coloured bricks, a gem falling, the ball above the paddle

The finished listing is examples/breakout/sprites/breakout.bas.

Read Chapter 17 first if you have not. The rules it teaches — declare every name up front, loop with GOTO, parenthesise mixed + and -, build a random-number generator — all apply here too, and are not repeated.

One of them is worth repeating, because this chapter leans on it harder than Chapter 17 does. # is an integer, % is a float and $ is a string. That is the opposite way round from Commodore BASIC, and it is why ball positions and velocities here are BLX% and BLVX% while counters and flags are BLN# and BLON#. Get one wrong and the ball moves in whole pixels, or stops.

Chapter 3 is the language reference for the rest — MOD, INSTR, MID, LEN — and Chapter 4 is BEGIN/BEND, DO ... LOOP UNTIL and GOSUB.

$ ./build-akgl/basic examples/breakout/sprites/breakout.bas
Key Does
left / right move the paddle
space start a game, launch the ball, release a stuck ball
P pause
S sound on and off
Q or escape quit

A broken brick drops a gem about one time in seven. Catch it with the paddle; the colour tells you which it is.

Gem Name Does For
red EXPAND doubles the paddle's width 20 seconds
yellow MULTI throws two more balls off the one in play until they are lost
green SLOW drops the ball's speed to about two thirds 16 seconds
blue STICKY the ball sticks where it lands; space fires it 18 seconds
purple CATCH a second bar appears higher up the field 24 seconds

What you will do

  • Step 1 — load PNG artwork into sprites
  • Step 2 — decide what each of the eight sprites is, before writing anything else
  • Step 3 — take the text layer out of the way, so what you draw can be seen and stays seen
  • Step 4 — make one brick per colour and stamp the field out of them
  • Step 5 — draw at most one thing per frame, chosen by dirty flags
  • Step 6 — erase a broken brick in place, so the field is drawn once and not rebuilt
  • Step 7 — draw lettering with a stroke font, because text has no colour
  • Step 8 — move the ball, bounce it off the bars without a square root, and register the bricks as collision geometry so the interpreter finds the hits
  • Step 9 — drop gems and apply what catching one does
  • Step 10 — three voices of sound and a mute that costs nothing
  • Step 11 — the frame loop's states: title, serve, play, lost, cleared
  • Step 12 — put the pieces in one file, in the right order

Step 1: Put artwork on the screen

Goal: a picture from a file, on the screen, at its own size.

SPRSAV takes an image file path as well as an array of pattern bytes, and a sprite loaded from a file keeps the image's own size rather than being squeezed into 24 by 21. That is the whole of it:

I# = 0
SPRSAV "art/paddleBlu.png", 3
SPRSAV "art/paddleRed.png", 4
SPRSAV "art/ballBlue.png", 5
SPRSAV "art/element_red_polygon_glossy.png", 6
SPRSAV "art/element_green_polygon_glossy.png", 7
SPRSAV "art/element_purple_polygon_glossy.png", 8
FOR I# = 3 TO 8
  SPRITE I#, 1, 2
NEXT I#
MOVSPR 3, 20, 20
MOVSPR 4, 20, 60
MOVSPR 5, 160, 30
MOVSPR 6, 30, 120
MOVSPR 7, 130, 120
MOVSPR 8, 230, 120

Two paddles, a ball and three coloured gems

That is the whole game's cast: two bars, a ball and five gems.

Write down the sizes now, because every collision test later in this chapter is built out of them:

Artwork Is Shows up later as
paddleBlu.png, paddleRed.png 104 by 24 PDW# = 104, and the bar's height in HITBAR
ballBlue.png, ballGrey.png 22 by 22 + 21 on every edge of the ball's box
the five gems 48 by 46 the + 48 and + 46 in the catch test

Three more things to note.

The path is tried against the working directory first, then against the directory the program was loaded from. A .bas stored beside its own art/ folder therefore runs from anywhere.

SPRITE n, 1, 2 turns sprite n on in colour 2. A sprite's colour multiplies the artwork rather than replacing it, so colour 2 — white — is the one that leaves loaded artwork looking like itself. Any other colour tints it.

Put the art where the licence lets you. The artwork here is Kenney's Puzzle Pack 1, released under CC0, and examples/breakout/sprites/art/PROVENANCE.md records which file is used for what. CC0 does not require crediting Kenney. Do it anyway.

Step 2: Budget the eight sprite slots

Goal: know what all eight sprites are before you write the program.

There are eight sprite slots and no more. That is not a limit you will design your way around later, so spend them on paper first. This game spends them like this:

Slot Is
1 free
2 free
3 the paddle
4 the catcher bar (the purple gem)
5, 6, 7 up to three balls
8 the falling gem

Six are spent and two are left over, which is more room than the program needs. Two consequences fall straight out of the six that are spent:

  • The bricks are drawn rather than made of artwork. Sixty bricks will not fit in six slots, and a sprite is the only way to get an image file onto the screen — GSHAPE cannot stamp a sprite and SPRSAV cannot read one back out.
  • Only one gem falls at a time. There is one slot for it, so a brick broken while a gem is already falling drops nothing.

Deciding this first is what stops a feature costing an afternoon before it is abandoned.

Step 3: Make room for what you draw

Goal: something you drew, still on the screen on the next frame.

Two things have to be true before a drawing is any use, and only one of them is automatic.

A drawing persists. DRAW, BOX, CIRCLE and PAINT render into a layer the frame composites underneath the text and the sprites, so a picture you draw once is there on every frame after. You do not redraw it and you do not have to keep it anywhere.

But the text layer covers it. It repaints every row it owns, opaque, every frame — and by default it owns the whole window. So the first executable line of this program is:

WINDOW 0, 35, 49, 36

Two rows at the bottom, which is enough for the final score, and the other thirty-five belong to the drawing verbs. WINDOW l, t, r, b takes character cells, and RWINDOW(0) and RWINDOW(1) report what you ended up with.

That is the whole of it. Draw your field once and it stays; draw your HUD once and it stays.

Two consequences shape the rest of this chapter, and both are things you no longer have to do.

There is no drawing deadline. The host runs a fixed number of source lines and then presents the frame, and a drawing longer than one batch used to be a problem — it does not matter here, because a drawing that spans two batches simply arrives over two frames and the layer keeps both halves.

And there is nothing to redraw. If you change one brick, erase that brick; the other fifty-nine are still on the screen. Which is why this chapter has no routine that walks a list of everything still standing.

Step 4: Make the brick stamps

Goal: six coloured bricks, and a field stamped out of them.

Draw one brick per row colour, capture the six of them, and then stamp them wherever a brick belongs with GSHAPE. Drawing six things and stamping sixty is far cheaper than drawing sixty.

DIM BRC#(6)
I# = 0
R# = 0
K# = 0
T1# = 0
T2# = 0
FOR I# = 0 TO 5
  READ BRC#(I#)
NEXT I#
GRAPHIC 1, 1
WIDTH 1
FOR R# = 0 TO 5
  COLOR 1, BRC#(R#)
  T1# = R# * 20
  T2# = T1# + 8
  FOR K# = 0 TO 7
    DRAW 1, 0, T1# + K# TO 67, T1# + K# : DRAW 1, 0, T2# + K# TO 67, T2# + K#
  NEXT K#
NEXT R#

DATA 3, 9, 8, 6, 4, 5

Six bricks, one per row colour, stacked vertically

Reading that from the top: GRAPHIC 1, 1 selects the graphics mode and clears it. COLOR 1, BRC#(R#) sets colour source 1 to the palette entry that row wants — a drawing verb names a source, not a colour, and there are seven sources. DRAW 1, x1, y1 TO x2, y2 draws a line using source 1.

Each brick is 68 by 16 and is filled with horizontal lines. The inner loop draws two scan lines per pass, T1# + K# and T2# + K#, so eight passes fill sixteen rows. That costs about a hundred and thirty lines for the whole set instead of the two hundred and seventy a line-at-a-time loop would take.

Capturing the six is six SSHAPEs:

SSHAPE Z$, 0, 0, 68, 16 : S0$ = Z$
SSHAPE Z$, 0, 20, 68, 36 : S1$ = Z$
SSHAPE Z$, 0, 40, 68, 56 : S2$ = Z$
SSHAPE Z$, 0, 60, 68, 76 : S3$ = Z$
SSHAPE Z$, 0, 80, 68, 96 : S4$ = Z$
SSHAPE Z$, 0, 100, 68, 116 : S5$ = Z$

SSHAPE has sixteen slots and nothing gives one back, and this program spends eight of them: six brick colours and the two erasers in Step 6. Spent once, at startup, and never again — nothing here captures per frame, so there is no pool to run dry and no rebuild:

LABEL DRAWPROTOS
GRAPHIC 1, 1
WIDTH 1

with the six DRAW loops, Step 6's two erasers, and eight SSHAPEs after it. Called once, from the setup block.

PAINT would be one statement instead of sixteen, and is not usable here. It costs nearly four milliseconds a call; sixty of those is seven frames' worth of time.

Now the whole screen — walls, the brick field stamped out of the six, and the artwork on top:

DIM BRC#(6)
I# = 0
R# = 0
C# = 0
K# = 0
T1# = 0
T2# = 0
Z$ = ""
FOR I# = 0 TO 5
  READ BRC#(I#)
NEXT I#
GRAPHIC 1, 1
WIDTH 1
FOR R# = 0 TO 5
  COLOR 1, BRC#(R#)
  T1# = R# * 20
  T2# = T1# + 8
  FOR K# = 0 TO 7
    DRAW 1, 0, T1# + K# TO 67, T1# + K# : DRAW 1, 0, T2# + K# TO 67, T2# + K#
  NEXT K#
NEXT R#
SSHAPE Z$, 0, 0, 68, 16 : S0$ = Z$
SSHAPE Z$, 0, 20, 68, 36 : S1$ = Z$
SSHAPE Z$, 0, 40, 68, 56 : S2$ = Z$
SSHAPE Z$, 0, 60, 68, 76 : S3$ = Z$
SSHAPE Z$, 0, 80, 68, 96 : S4$ = Z$
SSHAPE Z$, 0, 100, 68, 116 : S5$ = Z$
GRAPHIC 1, 1
WIDTH 2
COLOR 5, 16 : COLOR 1, 4
BOX 5, 2, 62, 797, 597
BOX 1, 6, 66, 793, 593
FOR C# = 0 TO 9
  Z$ = S0$ : GSHAPE Z$, 42 + C# * 72, 108
  Z$ = S1$ : GSHAPE Z$, 42 + C# * 72, 132
  Z$ = S2$ : GSHAPE Z$, 42 + C# * 72, 156
  Z$ = S3$ : GSHAPE Z$, 42 + C# * 72, 180
  Z$ = S4$ : GSHAPE Z$, 42 + C# * 72, 204
  Z$ = S5$ : GSHAPE Z$, 42 + C# * 72, 228
NEXT C#
SSHAPE Z$, 0, 60, 800, 600
SPRSAV Z$, 2
SPRITE 2, 1, 2
MOVSPR 2, 0, 60
SPRSAV "art/paddleBlu.png", 3
SPRSAV "art/ballBlue.png", 5
SPRITE 3, 1, 2
SPRITE 5, 1, 2
MOVSPR 3, 348, 540
MOVSPR 5, 389, 517

DATA 3, 9, 8, 6, 4, 5

The full field: a double border, six rows of coloured bricks, the paddle and the ball

Everything above the SSHAPE Z$, 0, 60, 800, 600 is a drawing nobody would ever see. Those four lines are what make it the screen.

A brick is 68 by 16 on a 72 by 24 pitch, ten columns by six rows, with the field's top-left corner at (42, 108). Those numbers are worth writing down once; they come back in Steps 7 and 9.

The finished game keeps its six stamps in six separate scalars — S0$ to S5$ — rather than in an array. An array works too.

Step 5: Draw at most one thing per frame

Goal: a frame loop that paces, draws one thing, and then plays the game.

LABEL FRAME
GOSUB DRAWJOB
GOSUB READKEYS
IF STATE# = 0 THEN GOSUB TITLETICK
IF STATE# = 1 THEN GOSUB SERVETICK
IF STATE# = 2 THEN GOSUB PLAYTICK
IF STATE# = 3 THEN GOSUB LOSTTICK
IF STATE# = 4 THEN GOSUB CLEARTICK
IF RUNNING# = 0 THEN GOTO SHUTDOWN
GOTO FRAME

At most one drawing job a frame, then the game. Nothing here has a deadline any more — a drawing that spans two batches arrives over two frames — but a queue of one still keeps the work even, and it keeps the decision about what needs redrawing in one place instead of scattered through the game.

DRAWJOB is a queue of one, chosen by dirty flags — stamps first, because everything else draws with them, then the field, then the HUD:

LABEL DRAWJOB
IF DPLAY# = 0 THEN GOTO DRAWJOB3
GOSUB DRAWPLAY
DPLAY# = 0
RETURN
LABEL DRAWJOB3
IF DHUD# = 0 THEN RETURN
GOSUB DRAWHUD
DHUD# = 0
RETURN

The rest of the game never draws. It sets DPLAY# = 1 or DHUD# = 1 when something has changed and gets on with its frame. One consequence is visible and deliberate: the score lags the bricks by one frame, because the field is drawn first. At thirty frames a second nobody can see it.

Those are LABELs and GOTOs rather than BEGIN blocks. Either works; the labels keep each arm to one RETURN and read the same.

Step 6: Erase one brick, not the whole field

Goal: take a broken brick off the screen without redrawing the other fifty-nine.

Because a drawing stays, removing something means covering it up. There is no filled-rectangle verb — BOX outlines and PAINT costs about four milliseconds a call — so the cheapest way to blank a region is a stamp of something blank:

COLOR 1, 1
FOR K# = 0 TO 15
  DRAW 1, 0, 130 + K# TO 67, 130 + K#
NEXT K#
SSHAPE Z$, 0, 130, 68, 146 : BL$ = Z$

The same sixteen lines the brick stamps are made of, in the background colour. Erasing is then one statement:

LABEL ERASEBRICK
Z$ = BL$
GSHAPE Z$, BRKX# + C# * 72, BRKY# + R# * 24
RETURN

Make a second one the width of the HUD strip while you are here, for the same reason: the strip is rewritten whenever a number in it changes, and the old digits have to go somewhere before the new ones are drawn. It is the same code with a bigger rectangle:

FOR K# = 0 TO 59
  DRAW 1, 0, 160 + K# TO 799, 160 + K#
NEXT K#