-
Notifications
You must be signed in to change notification settings - Fork 1
/
snake.inc
302 lines (286 loc) · 7.58 KB
/
snake.inc
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
.data
MY_SNAKE_ACC byte ?
MY_SNAKE_DIR byte ? ; 0 - up, 1 - down, 2 - left, 3 - right
MY_SNAKE_LEN word ?
MY_SNAKE_POS word 300 dup(?)
;----------------------------------------------------------
SNAKE_X word ?
SNAKE_Y word ?
.code
InitSnake proc
; 初始蛇态,长度为3
mov esi,0
mov ebx,offset MY_SNAKE_POS
mov word ptr [ebx+esi],14
mov word ptr [ebx+esi+2],8
add esi,4
mov word ptr [ebx+esi],15
mov word ptr [ebx+esi+2],8
add esi,4
mov word ptr [ebx+esi],16
mov word ptr [ebx+esi+2],8
add esi,4
shr esi,2
mov MY_SNAKE_LEN,si
mov MY_SNAKE_DIR,3
mov MY_SCORE,0
mov MY_SNAKE_ACC,0
ret
InitSnake endp
DrawSnake proc ; 画出蛇身
invoke SetFontColor,6
movzx ecx,MY_SNAKE_LEN
mov edx,0
mySnakeDraw:
push ecx
push edx
mov esi,offset MY_SNAKE_POS
add esi,edx
mov ax,word ptr[esi]
mov SNAKE_X,ax
mov ax,word ptr[esi+2]
mov SNAKE_Y,ax
.if ecx==1
invoke SetFontColor,15
.endif
invoke PrintCircle,SNAKE_X,SNAKE_Y
pop edx
pop ecx
add edx,4
loop mySnakeDraw
ret
DrawSnake endp
OverEdge proc ; 判断蛇头是否碰到墙壁,0-未碰壁,1-碰壁
local X:word,Y:word
mov ebx,offset MY_SNAKE_POS
movzx esi,MY_SNAKE_LEN
sub esi,1
imul esi,4
mov ax,word ptr[ebx+esi]
mov X,ax
mov ax,word ptr[ebx+esi+2]
mov Y,ax
mov eax,1
.if X>10 && X<39 && Y>1 && Y<30
dec eax
.endif
ret
OverEdge endp
HitItself proc ; 判断蛇头是否和蛇身重合,0-不重合,1-重合
local X:word,Y:word
local flag:byte
; 获得蛇头位置
mov ebx,offset MY_SNAKE_POS
movzx esi,MY_SNAKE_LEN
dec esi
imul esi,4
mov ax,word ptr[ebx+esi]
mov X,ax
mov ax,word ptr[ebx+esi+2]
mov Y,ax
; 遍历蛇身
mov flag,0
movzx ecx,MY_SNAKE_LEN
dec ecx ; 除去蛇头
mov edx,0
myHitLoop:
push ecx
push edx
mov esi,offset MY_SNAKE_POS
add esi,edx
mov ax,word ptr[esi]
mov cx,ax
mov ax,word ptr[esi+2]
.if flag==0 && X==cx && Y==ax
mov flag,1
.endif
pop edx
pop ecx
add edx,4
loop myHitLoop
movzx eax,flag
ret
HitItself endp
isGetFood proc ; 判断是否吃到食物,0-没吃到,1-吃到
mov ebx,offset MY_SNAKE_POS
movzx esi,MY_SNAKE_LEN
dec esi
imul esi,4
mov cx,word ptr[ebx+esi]
mov dx,word ptr[ebx+esi+2]
mov eax,0
.if cx==MY_FOOD_X && dx==MY_FOOD_Y
inc eax
.endif
ret
isGetFood endp
isGetBonus proc ; 判断是否吃到bonus食物,0-没吃到,1-吃到
mov ebx,offset MY_SNAKE_POS
movzx esi,MY_SNAKE_LEN
dec esi
imul esi,4
mov cx,word ptr[ebx+esi]
mov dx,word ptr[ebx+esi+2]
mov eax,0
.if cx==MY_FOOD_SP_X && dx==MY_FOOD_SP_Y
inc eax
.endif
ret
isGetBonus endp
Growing proc ; 蛇身前进一格
mov ebx,offset MY_SNAKE_POS
movzx esi,MY_SNAKE_LEN
dec esi
imul esi,4
mov cx,word ptr[ebx+esi]
mov dx,word ptr[ebx+esi+2]
add esi,4
.if MY_SNAKE_DIR==0 ; up
dec dx
.elseif MY_SNAKE_DIR==1 ; down
inc dx
.elseif MY_SNAKE_DIR==2 ; left
dec cx
.elseif MY_SNAKE_DIR==3 ; right
inc cx
.endif
mov [ebx+esi],cx
mov [ebx+esi+2],dx
mov ax,MY_SNAKE_LEN
inc ax
mov MY_SNAKE_LEN,ax
invoke DrawSnake
ret
Growing endp
Moving proc
local X:word,Y:word
; 删除蛇尾
mov esi,offset MY_SNAKE_POS
mov ax,[esi]
mov X,ax
mov ax,[esi+2]
mov Y,ax
invoke PrintBlank,X,Y
; 原蛇身平移一格
movzx ecx,MY_SNAKE_LEN
dec ecx
mov edx,0
myMovLoop:
mov esi,offset MY_SNAKE_POS
add esi,edx
mov ax,[esi+4]
mov [esi],ax
mov ax,[esi+6]
mov [esi+2],ax
add edx,4
loop myMovLoop
mov ax,MY_SNAKE_LEN
dec ax
mov MY_SNAKE_LEN,ax
invoke Growing ; 蛇头前进
ret
Moving endp
RefreshDirection proc ; 读取键盘输入,0-ESC键,1-方向键,2-其它键,3-未按下任何按键
invoke crt__kbhit
.if eax <= 0
mov eax,3
ret
.endif
invoke crt__getch
.if eax==224
invoke crt__getch
.if eax==72 ; up
.if MY_SNAKE_DIR!=1
.if MY_SNAKE_DIR==0
mov MY_SNAKE_ACC,1
.else
mov MY_SNAKE_DIR,0
.endif
invoke SetFontColor,MY_MENU_COL
invoke _SetCursorPosition,9,10
invoke crt_printf,addr MENU_DIR_UP
.endif
.elseif eax==80 ; down
.if MY_SNAKE_DIR!=0
.if MY_SNAKE_DIR==1
mov MY_SNAKE_ACC,1
.else
mov MY_SNAKE_DIR,1
.endif
invoke SetFontColor,MY_MENU_COL
invoke _SetCursorPosition,9,10
invoke crt_printf,addr MENU_DIR_DOWN
.endif
.elseif eax==75 ; left
.if MY_SNAKE_DIR!=3
.if MY_SNAKE_DIR==2
mov MY_SNAKE_ACC,1
.else
mov MY_SNAKE_DIR,2
.endif
invoke SetFontColor,MY_MENU_COL
invoke _SetCursorPosition,9,10
invoke crt_printf,addr MENU_DIR_LEFT
.endif
.elseif eax==77 ; right
.if MY_SNAKE_DIR!=2
.if MY_SNAKE_DIR==3
mov MY_SNAKE_ACC,1
.else
mov MY_SNAKE_DIR,3
.endif
invoke SetFontColor,MY_MENU_COL
invoke _SetCursorPosition,9,10
invoke crt_printf,addr MENU_DIR_RIGHT
.endif
.endif
mov eax,1
.elseif eax==27 ; ESC
mov eax,0
.else
mov eax,2
.endif
ret
RefreshDirection endp
; RefreshDirection proc ; 读取键盘输入,0-ESC键,1-其它键,2-未按下任何按键
; call ReadKey
; jnz LookForKey
; mov eax,2
; ret
; LookForKey:
; .if al!=27
; .if ah==72 ; up
; .if MY_SNAKE_DIR!=1 && MY_SNAKE_DIR !=0
; mov MY_SNAKE_DIR,0
; invoke SetFontColor,MY_MENU_COL
; invoke _SetCursorPosition,9,10
; invoke crt_printf,addr MENU_DIR_UP
; .endif
; .elseif ah==80 ; down
; .if MY_SNAKE_DIR!=0 && MY_SNAKE_DIR !=1
; mov MY_SNAKE_DIR,1
; invoke SetFontColor,MY_MENU_COL
; invoke _SetCursorPosition,9,10
; invoke crt_printf,addr MENU_DIR_DOWN
; .endif
; .elseif ah==75 ; left
; .if MY_SNAKE_DIR!=3 && MY_SNAKE_DIR !=2
; mov MY_SNAKE_DIR,2
; invoke SetFontColor,MY_MENU_COL
; invoke _SetCursorPosition,9,10
; invoke crt_printf,addr MENU_DIR_LEFT
; .endif
; .elseif ah==77 ; right
; .if MY_SNAKE_DIR!=2 && MY_SNAKE_DIR !=3
; mov MY_SNAKE_DIR,3
; invoke SetFontColor,MY_MENU_COL
; invoke _SetCursorPosition,9,10
; invoke crt_printf,addr MENU_DIR_RIGHT
; .endif
; .endif
; mov eax,1
; .else ; ESC
; mov eax,0
; .endif
; ret
; RefreshDirection endp