主题:贪吃蛇
DECLARE SUB food ()
DECLARE SUB judge ()
DECLARE SUB changearry ()
DECLARE SUB movesnake (x$)
DECLARE SUB showsnake ()
DECLARE SUB initiate ()
SCREEN 12
TYPE snaketype
x AS INTEGER
y AS INTEGER
isbody AS INTEGER
END TYPE
DIM SHARED snake(maxlenth) AS snaketype, snakelenth, foodx, foody, havefood
LINE (0, 0)-(639, 460), 3, B
CALL initiate
DO
CALL showsnake
DO
LOCATE 1, 1
PRINT "."
xx$ = INKEY$
i = i + 1
LOOP UNTIL xx$ <> "" OR i > snakespeed
i = 0
IF xx$ <> "" THEN x$ = xx$
IF x$ = "t" THEN
END
END IF
CALL movesnake(x$)
CALL judge
CALL food
IF snakelenth = maxlenth THEN
CLS
LOCATE 10, 30
PRINT "GOOD JOB!"
LOCATE 12, 30
PRINT "CONGRATULATIONS!!!"
END
END IF
LOOP
SUB changearry
FOR i = maxlenth TO 2 STEP -1
snake(i).x = snake(i - 1).x
snake(i).y = snake(i - 1).y
NEXT i
END SUB
SUB food
RANDOMIZE TIMER
IF havefood = 0 THEN
foodx = INT(RND * 63) + 1: foody = INT(RND * 45) + 1
havefood = 1
END IF
IF snake(1).x = foodx AND snake(1).y = foody THEN
havefood = 0
snakelenth = snakelenth + 1
snake(snakelenth).isbody = 1
END IF
CIRCLE (foodx * 10, foody * 10), 4, 5
END SUB
SUB initiate
snakelenth = 3
snake(1).x = 10: snake(1).y = 10: snake(1).isbody = 1
snake(2).x = 9: snake(2).y = 10: snake(2).isbody = 1
snake(3).x = 8: snake(3).y = 10: snake(3).isbody = 1
END SUB
SUB judge
IF snake(1).x = 0 OR snake(1).y = 0 OR snake(1).x = 64 OR snake(1).y = 46 THEN
END
END IF
FOR i = 2 TO snakelenth
IF snake(1).x = snake(i).x AND snake(1).y = snake(i).y AND snake(i).isbody = 1 THEN
END
END IF
NEXT i
END SUB
SUB movesnake(x$)
IF x$ = "w" THEN
CALL changearry
snake(1).y = snake(1).y - 1
ELSEIF x$ = "s" THEN
CALL changearry
snake(1).y = snake(1).y + 1
ELSEIF x$ = "a" THEN
CALL changearry
snake(1).x = snake(1).x - 1
ELSEIF x$ = "d" THEN
CALL changearry
snake(1).x = snake(1).x + 1
END IF
END SUB
SUB showsnake
FOR i = 1 TO maxlenth
IF snake(i).isbody = 1 THEN
CIRCLE (snake(i).x * 10, snake(i).y * 10), 4, 3
END IF
NEXT i
CIRCLE (snake(snakelenth + 1).x * 10, snake(snakelenth + 1).y * 10), 4, 0
END SUB
编过一个,本来不想再编了,大家对这个问题如此热情,连MOZ版主都讲话了,就写了一个,权当是抛砖引玉了。希望大家多来QB版,支持QB版的发展。
WSAD控制方向,T退出,根据电脑配置不同修改程序第一行SNAKESPEED的值控制蛇的速度,当蛇长到MAXLENTH个长度时通关退出。
程序还有不足的地方,有些变量设置得多余,有些地方效率不高