This is a remake of a popular microcomputer game SNAKE. I do not know who
with the idea for the game (or, even, on what microcomputer it
appeared first). The implementation I saw was
30-something lines of code, which was amazingly short for such a nice and
seemingly complex game.
Control snake that crawls across the screen. Avoid walls, stones ('#')
and biting own body. Eat digits that randomly appear on the screen. Each eaten
causes snake to grow. The goal is to grow as much as you can.
There is a way to clear stones from the way: press space bar when snake's head
is right before stone and stone will disappear. Timing is crucial: too early or
too late and you will hit the stone, causing game over.
Code and comments
Download the SNAKE.NSG code.
Original 30-something line code was not terribly complicated or awfully long.
It did have, however, quite a few "IF"s -- more than would comfortably fit into
3 lines. Some of those ifs seemed inevitable. How would you read keyboard
input and assign new direction without at least 4 "IF"s?
Below is unfolded, spaceified and commented code.
FOR I=1 TO 10
This clears function key hints, so they are not displayed. Easier
way would be to use KEY OFF operator, but I needed them on, so I could youse hint for "F8" to display current score.
Initialize G (direction) with 1 ("to the right") and also randomize random seed
using timer at the same time. Since DEFINT A-Z is in action, G will remain 1.
FOR I=0 TO 39
Horizotal walls -- one on top, one on bottom.
FOR I=40 TO 880 STEP40
Vertical walls -- one on left, one on right. Walls are composed of "*"
character and cannot be wiped with a space bar.
FOR X=1 TO 15
FOR Y=1 TO 3
FOR Z=1 TO 20
' if h=" " and e="#" then e=" "
' if h=28 then g=1 else if h=29 then g=-1 else if h=30 then g=-40 else if g=31 then g=40 else if h=33 or h=32 then g=g else g=0
C=(C+1) MOD 800
IF E<48 AND E>32 THEN
Exit main loop
This game layed groundwork for the rest of the series. For the first time here I
used comparison and logical operators in arithmetic expressions, which allowed
significantly simplify program structure and reduce size. Without this tecnique
most of the following would not be possible.
IF D>0 THEN
B=(B+1) MOD 800
IF S>500 THEN
FOR I=0 TO 999
[ MSX BASIC
| << Previous
| Next >>