# here's a simple snake implementation

September 16 2010 at 6:04 PM
Moderator

Response to Super Low Res Game Challenge

as if the world doesn't have enough of them. i threw it together over the last hour or so, so the quality isn't too good. i haven't tested it much. there is no victory condition, the game keeps going until you hit something or press escape. the snake grows until it's length reaches the maxsl constant. i have a classic delay loop, but it is commented out. the game currently uses a timer loop. you can, if you want, comment out the timer loop, and use the classic delay loop instead.
regards.

'public domain, 2010 michael calkins
' "qbasic" + "michael" + chr\$(&h40) + "gmail.com"

'CONST delayv& = 800000
CONST maxsl = 16 'maximum snake length
DECLARE SUB drawcirc (x%, y%)
DECLARE SUB drawmatrix ()
DEFINT A-Z
DECLARE SUB blankmatrix ()
DIM SHARED fld(0 TO 39, 0 TO 19)
TYPE snaket
x AS INTEGER
y AS INTEGER
END TYPE
DIM snake(0 TO maxsl) AS snaket
RANDOMIZE TIMER

FOR x = 0 TO 39
FOR y = 0 TO 19
fld(x, y) = 0
IF (x = 0) OR (x = 39) THEN fld(x, y) = 4
IF (y = 0) OR (y = 19) THEN fld(x, y) = 4
NEXT y, x
drawmatrix
SLEEP 2
snakel = 1
snake(0).x = 2
snake(0).y = 2
snakexdir = 1
snakeydir = 0
tx = -1
DO
fld(snake(0).x, snake(0).y) = 3
drawcirc snake(0).x, snake(0).y
IF tx = -1 THEN
DO
tx = INT(RND * 40)
ty = INT(RND * 20)
LOOP WHILE fld(tx, ty)
fld(tx, ty) = 5
END IF
drawcirc tx, ty
s! = TIMER
DO
c! = TIMER
IF c! < s! THEN s! = s! - 86400
LOOP UNTIL (c! - s!) >= .15
'FOR delay& = 0 TO delayv&: NEXT delay&
k\$ = INKEY\$
SELECT CASE k\$
CASE CHR\$(&H1B): SCREEN 0: WIDTH 80, 25: SYSTEM
CASE MKI\$(&H4800): snakexdir = 0: snakeydir = 1
CASE MKI\$(&H4B00): snakexdir = -1: snakeydir = 0
CASE MKI\$(&H4D00): snakexdir = 1: snakeydir = 0
CASE MKI\$(&H5000): snakexdir = 0: snakeydir = -1
END SELECT
SELECT CASE fld(snake(0).x + snakexdir, snake(0).y + snakeydir)
CASE 5
tx = -1
IF snakel < maxsl THEN snakel = snakel + 1: skip = -1
CASE 0
CASE ELSE: SCREEN 0: WIDTH 80, 25: SYSTEM
END SELECT
IF NOT skip THEN
fld(snake(snakel - 1).x, snake(snakel - 1).y) = 0
drawcirc snake(snakel - 1).x, snake(snakel - 1).y
END IF
skip = 0
FOR i = snakel - 1 TO 1 STEP -1
snake(i).x = snake(i - 1).x
snake(i).y = snake(i - 1).y
NEXT i
snake(0).x = snake(0).x + snakexdir
snake(0).y = snake(0).y + snakeydir
LOOP

SUB drawcirc (x, y)
cx = (x * 15) + 15
cy = ((20 - y) * 15) + 80
CIRCLE (cx, cy), 7, 8
PAINT (cx, cy), fld(x, y), 8
END SUB

SUB drawmatrix

CLS
SCREEN 12

cx = 25: cy = 80

FOR x = 0 TO 39
FOR y = 0 TO 19
drawcirc x, y
NEXT y, x

END SUB

 Respond to this message
Responses