# Snake in ZX Spectrum BASIC

Andy Balaam
artificialworlds.net/blog

# Snake

• Why Snake?
• It's Snake!
• Easy, but requires:
• Arrays
• Loops
• UI
• Input

# ZX Spectrum

• Lightweight, netbook style
• Requires external display
• Unique tactile keyboard
• British

# BASIC

• Procedural
• Designed for beginners
• High-level: subroutines, user-defined functions
• Many incompatible dialects

# Variables

``` 1179 REM length of snake
1180 LET e=5
...
7929 REM we ate an apple
7930 LET e=e+5
```

# Variables

```  999 REM arrays
1000 DIM x(400)
1010 DIM y(400)
...
4999 REM 1-based
5000 LET y(1)=y(1)-1
```

# Flow control

``` 1260 FOR n=1 TO e
1270 LET x(n)=10
1280 LET y(n)=9+n
1290 NEXT n
```

# Flow control

IF...THEN

``` 2520 IF x(1)=x(n) THEN PRINT "Died!": STOP
```

IF...THEN...GOTO

``` 5040 IF x(1)=a AND y(1)=b THEN GOTO 7900
```

# Flow control

```   10 REM "while" loop
20 LET i=0
30 IF i>10 THEN GOTO 100
40 PRINT i
50 LET i=i+1
60 GOTO 30
100 PRINT "finished!"
```

# Subroutines

``` 5040 IF x(1)=a AND y(1)=b THEN GO SUB 7900
...
7900 REM SUB eat apple
7910 LET e=e+5
7920 GO SUB 8000
7930 RETURN
```

# Sugar

Backward looping:

``` 4500 FOR n=e+1 TO 2 STEP -1
```

PRINT AT:

``` 2077 PRINT AT y(1),x(1);"*"
```

# Sugar

Logical operator composition:

``` 2400 IF x(1)<1 OR x(1)>18 OR y(1)<1 ...
```

":" statement separator:

```   ... THEN PRINT "Died!": STOP
```

# Sugar

Random numbers

```  900 RANDOMIZE
...
8010 LET a=INT (1+RND*17)
```

# Wrinkles

• Only 16KB memory available
• Serious performance issues
• Tricky to interface with hardware?

# Wrinkles

• BASIC syntax can be inflexible
• No functions or classes
• Line editor plus LIST for editing