-
Notifications
You must be signed in to change notification settings - Fork 14
/
snake_expanded.a
109 lines (97 loc) · 2.52 KB
/
snake_expanded.a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
10 HOME
20 LET L = 1 : REM SNAKE STARTS WITH LENGTH 1
25 LET LE = 1 : REM SNAKE ENDS AT INDEX 1
27 LET LS = 1 : REM SNAKE STARTS AT INDEX 1
28 LET LAST = 99
30 LET W = 40 : LET H = 24 : REM WIDTH AND HEIGHT
35 LET KEY = 0
40 GOSUB 2000 : REM INITIALIZE FOOD
50 DIM X(LAST + 1) : DIM Y(LAST + 1) : REM INTIALIZE ARRAY
60 X(L) = 20 : Y(L) = 12
65 REM MAIN LOOP
70 IF L > 98 THEN GOSUB 9000
80 GOSUB 3000
90 FOR N = 1 TO 1000 - L * 2 : NEXT N
100 GOTO 70
999 REM READ KEYBOARD INPUT
1000 LET K = PEEK(49152)
1010 IF K = 196 OR K = 193 OR K = 215 OR K = 211 THEN KEY = K
1020 IF KEY = 196 THEN X(LE) = X(LE) + 1
1030 IF KEY = 193 THEN X(LE) = X(LE) - 1
1040 IF KEY = 215 THEN Y(LE) = Y(LE) - 1
1050 IF KEY = 211 THEN Y(LE) = Y(LE) + 1
1060 IF X(LE) < 1 OR X(LE) > W THEN GOTO 6000
1070 IF Y(LE) < 1 OR Y(LE) > H THEN GOTO 6000
1080 IF L > 1 THEN GOSUB 7000
1090 RETURN
1990 REM CREATE FOOD
2000 LET FX = INT(RND(1) * (W - 1)) + 1
2010 LET FY = INT(RND(1) * (H - 1)) + 1
2020 HTAB FX : VTAB FY
2030 PRINT "F"
2040 RETURN
2990 REM DISPLAY SNAKE
3000 HTAB X(LS) : VTAB Y(LS)
3010 PRINT " "
3030 IF X(LS) = FX AND Y(LS) = FY THEN GOSUB 8000
3040 IF L > 1 THEN GOSUB 5000
3050 GOSUB 1000
3060 IF X(LE) = FX AND Y(LE) = FY THEN GOSUB 4000
3070 HTAB X(LE) : VTAB Y(LE)
3080 PRINT "S"
3090 RETURN
3990 REM GROW SNAKE
4000 L = L + 1
4010 LE = LE + 1
4020 IF LE < LAST THEN GOTO 4070
4030 LE = 1
4040 X(LE) = X(LAST)
4050 Y(LE) = Y(LAST)
4060 GOTO 4090
4070 X(LE) = X(LE - 1)
4080 Y(LE) = Y(LE - 1)
4090 GOSUB 2000
4100 RETURN
4990 REM SHIFT SNAKE ARRAY
5000 LS = LS + 1
5010 IF LS < LAST THEN GOTO 5030
5020 LS = 1
5030 LE = LE + 1
5040 X(LE) = X(LE - 1)
5050 Y(LE) = Y(LE - 1)
5060 IF LE < LAST THEN GOTO 5100
5070 LE = 1
5080 X(LE) = X(LAST)
5090 Y(LE) = Y(LAST)
5100 RETURN
6000 REM GAME OVER
6010 HOME
6020 HTAB 15 : VTAB 10
6030 PRINT "GAME OVER!"
6040 HTAB 13 : VTAB 11 : PRINT "YOUR SCORE WAS "
6050 HTAB 20 : VTAB 12 : PRINT L
6060 END
7000 REM CHECK IF SNAKE HIT ITSELF
7010 IF LS > LE THEN GOTO 7060
7020 FOR I = LS TO LE-1
7030 IF X(LE) = X(I) AND Y(LE) = Y(I) GOTO 6000
7040 NEXT I
7050 GOTO 7130
7060 FOR I = LS TO LAST
7070 IF X(LE) = X(I) AND Y(LE) = Y(I) GOTO 6000
7080 NEXT I
7090 IF LE = 1 THEN GOTO 7130
7100 FOR I = 1 TO LE-1
7110 IF X(LE) = X(I) AND Y(LE) = Y(I) GOTO 6000
7120 NEXT I
7130 RETURN
8000 REM REDRAW FOOD
8010 HTAB FX : VTAB FY
8020 PRINT "F"
8030 RETURN
9000 REM VICTORY
9010 HOME
9020 HTAB 14 : VTAB 10
9030 PRINT "YOU HAVE WON!"
9040 HTAB 13 : VTAB 11 : PRINT "SNAKE IS FULL!"
9050 END