Complete Projects

(Cells hold 1 or 0, so neighbor counts are simple sums; the border row/column of zeros avoids bounds checks.

39.1 Snake

OPTION _EXPLICIT
SCREEN _NEWIMAGE(640, 480, 32)
_TITLE "Snake - arrows to steer, Esc to quit"
RANDOMIZE TIMER

CONST CELL = 16, COLS = 40, ROWS = 30, MAXLEN = 1200
DIM SHARED sx(1 TO MAXLEN) AS INTEGER, sy(1 TO MAXLEN) AS INTEGER
DIM SHARED snakeLen AS INTEGER, dx AS INTEGER, dy AS INTEGER
DIM SHARED foodX AS INTEGER, foodY AS INTEGER, score AS LONG, dead AS INTEGER
DIM k AS LONG, i AS INTEGER, nx AS INTEGER, ny AS INTEGER
DIM wantDx AS INTEGER, wantDy AS INTEGER, lastMove AS DOUBLE

NewGame
wantDx = dx: wantDy = dy
DO
    k = _KEYHIT
    SELECT CASE k
        CASE 18432: IF dy = 0 THEN wantDx = 0: wantDy = -1
        CASE 20480: IF dy = 0 THEN wantDx = 0: wantDy = 1
        CASE 19200: IF dx = 0 THEN wantDx = -1: wantDy = 0
        CASE 19712: IF dx = 0 THEN wantDx = 1: wantDy = 0
        CASE 13: IF dead THEN NewGame: wantDx = dx: wantDy = dy
    END SELECT

    IF NOT dead AND (TIMER(.001) - lastMove > 0.09 OR TIMER(.001) < lastMove) THEN
        lastMove = TIMER(.001)
        dx = wantDx: dy = wantDy
        nx = sx(1) + dx: ny = sy(1) + dy
        IF nx < 0 OR nx >= COLS OR ny < 0 OR ny >= ROWS THEN dead = -1
        FOR i = 1 TO snakeLen - 1
            IF sx(i) = nx AND sy(i
…

39.2 Conway's Game of Life

(Cells hold 1 or 0, so neighbor counts are simple sums; the border row/column of zeros avoids bounds checks. GOSUB to a local label is still legal and handy in small programs.)

SCREEN _NEWIMAGE(800, 600, 32)
_TITLE "Game of Life - R: randomize, Esc: quit"
CONST W = 200, H = 150, SZ = 4
DIM grid(0 TO W + 1, 0 TO H + 1) AS INTEGER, nxt(0 TO W + 1, 0 TO H + 1) AS INTEGER
RANDOMIZE TIMER
GOSUB RandomFill
DO
    IF _KEYDOWN(114) THEN GOSUB RandomFill      ' r
    CLS
    FOR x = 1 TO W
        FOR y = 1 TO H
            n = grid(x - 1, y - 1) + grid(x, y - 1) + grid(x + 1, y - 1) + grid(x - 1, y) _
              + grid(x + 1, y) + grid(x - 1, y + 1) + grid(x, y + 1) + grid(x + 1, y + 1)
            IF grid(x, y) THEN
                nxt(x, y) = -(n = 2 OR n = 3)   ' survival
                LINE ((x - 1) * SZ, (y - 1) * SZ)-STEP(SZ - 2, SZ - 2), _RGB32(0, 255, 120), BF
            ELSE
                nxt(x, y) = -(n = 3)            ' birth
            END IF
        NEXT
    NEXT
    FOR x = 1 TO W: FOR y = 1 TO H: grid(x, y) = nxt(x, y): NEXT: NEXT
    _DISPLAY
    _LIMIT 15
LOOP UNTIL _KEYDOWN(27)
SYSTEM

RandomFill:
FOR x = 1 TO W: FOR y = 1 TO H: grid(x, y) = -(RND < 0.25): NEXT: NEXT
RETURN

39.3 More project outlines

To-do manager: TYPE-based list, add/complete/delete/sort by priority and due date, saved to a CSV file; colored text UI with LOCATE/COLOR.

Text adventure: rooms in DATA (name, description, exits), a parser for GO NORTH, TAKE LAMP, INVENTORY, game state saved to a binary file.

Breakout: paddle with _KEYDOWN or mouse, ball physics with angle depending on hit position, brick grid in a 2-D array, levels from DATA, sounds with _SNDRAW.

Bank account simulator: RANDOM-access file of accounts, deposits/withdrawals with validation, transaction log in an APPEND text file, monthly interest.