-
Notifications
You must be signed in to change notification settings - Fork 0
/
food.asm
287 lines (205 loc) · 6.11 KB
/
food.asm
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
checkFoodCollisionWithSnakeAndBoundary:
;takes the position of food on video memory as input in stack, returns 1 if it matches with some position of snake or boundary
;or some other food or some hurdle. returns -1 otherwise.
push bp
mov bp, sp
pusha
mov ax, [bp + 4]
mov bx, snake
mov cx, [size]
mov dx, -1
whileCheckingFoodCollision:
cmp ax, [bx]
jne noCollisionWithFood
mov dx, 1
noCollisionWithFood:
add bx, 2
loop whileCheckingFoodCollision
push word 0
push word 0
push ax
call calRowAndColumn ;gets the rows and column corresponding to the position of food on screen for easy comparison with boundary.
pop ax ;col
pop bx ;row
cmp ax, 0
je foodCollisionWithBoundary
cmp ax, 79
je foodCollisionWithBoundary
cmp bx, 0
je foodCollisionWithBoundary
cmp bx, 24
jne noFoodCollisionWithBoundary
foodCollisionWithBoundary:
mov dx, 1
noFoodCollisionWithBoundary:
cmp word[level], 1
jne foodLevel1ChecksNotNecessary
push word 0
push word [bp + 4] ;the value received.
call generalCollisionWithLevel1Hurdles
pop si
cmp si, 0
je foodLevel1ChecksNotNecessary
mov dx, 1
foodLevel1ChecksNotNecessary:
cmp word[level], 2
jne foodLevel2ChecksNotNecessary
push word 0
push word [bp + 4] ;the value received.
call generalCollisionWithLevel2Hurdles
pop si
cmp si, 0
je foodLevel2PortalChecks
mov dx, 1
foodLevel2PortalChecks:
push word 0
push word[bp + 4]
call generalCollisionWithPortals
pop si
cmp si, 0
je foodLevel2ChecksNotNecessary
mov dx, 1
foodLevel2ChecksNotNecessary:
mov ax, [bp + 4] ;address of food.
cmp ax, [foodGreen]
jne foodNotCollidedWithGreenFood
mov dx, 1
foodNotCollidedWithGreenFood:
cmp ax, [bonusFood]
jne foodNotCollidedWithBonusFood
mov dx, 1
foodNotCollidedWithBonusFood:
cmp ax, [bombFood]
jne foodNotCollidedWithBombFood
mov dx, 1
foodNotCollidedWithBombFood:
mov [bp + 6], dx
popa
mov sp, bp
pop bp
ret 2
generateNewFood:
;takes the offset of the food to generate.
push bp
mov bp, sp
pusha
regenerate:
mov bx, [bp + 4] ;offset of food.
push word 0
call rand
pop ax
mov dx, ax
shr dx, 1
jnc foodGenAtEven
dec ax
foodGenAtEven:
push word 0
push word ax
call checkFoodCollisionWithSnakeAndBoundary
pop si
cmp si, 1
je regenerate
mov [bx], ax
popa
mov sp, bp
pop bp
ret 2
bombFoodCell: dw 0x780f
greenFoodCell: dw 0x7e06
bonusFoodCell: dw 0x7c15
drawFood:
pusha
push es
mov ax, 0xb800
mov es, ax
mov bx, [foodGreen]
mov dx, [greenFoodCell]
mov word[es:bx], dx
mov bx, [bonusFood]
mov dx, [bonusFoodCell]
mov word[es:bx], dx
mov bx, [bombFood]
mov dx, [bombFoodCell]
mov word[es:bx], dx
pop es
popa
ret
eatFood:
;takes no paramters. checks if the snake's head is at the same position as the food. if so, then it removes the food, elongates snake
;and makes the food appear on a different position.
pusha
mov ax, [snake] ;head
cmp ax, [foodGreen] ;cmp head's position with fruit's position.
jne greenFoodNotEaten
call elongateSnake
push word foodGreen
call generateNewFood
push word [eatSound]
push word [soundDuration]
call generateGeneralSound
greenFoodNotEaten:
cmp ax, [bonusFood]
jne bonusFoodNotEaten
push word [eatSound]
push word [soundDuration]
call generateGeneralSound
call elongateSnake
call elongateSnake
call elongateSnake
call elongateSnake
call elongateSnake ;to add 20 characters to the length.
mov word[bonusFood], 8000 ;to move it out of screen.
;the strategy of normal food cannot be applied here because the bonus food generates after some time.
bonusFoodNotEaten:
cmp ax, [bombFood]
jne bombFoodNotEaten
mov word[bombFood], 8000
call updateLives
bombFoodNotEaten:
popa
ret
foodManager:
;takes no parameters. It initialies the greenFood at the start of game. It also controls the appearence and disappearence
;of bonus food.
pusha
cmp word[foodGreen], 0
jne greenFoodAlreadyGenerated
push word foodGreen
call generateNewFood
greenFoodAlreadyGenerated:
mov ax, [seconds]
mov bl, 10
div bl
cmp ah, 0 ;for generating food every 10 seconds.
jne notTimeForBonusFood
cmp word[bonusFoodCountdown], 0
jg notTimeForBonusFood ;this means that the bonus food was already generated in the current second.
;this prevents the bonus food from ebing regenerated multiple time in the 10th second.
push word bonusFood
call generateNewFood
mov word[bonusFoodCountdown], 5
notTimeForBonusFood:
cmp word[bonusFoodCountdown], 0
jg bonusFoodAvailable
mov word[bonusFood], 8000 ;a value outside of visible screen.
;so that whenever time is up, the location of bonus food is outside of screen.
bonusFoodAvailable:
cmp word[seconds], 10
jne notTimeForBombFood
cmp word[minutes], 0
je timeForBombFood
cmp word[minutes], 3
jne notTimeForBombFood ;if(seconds == 30 && (minutes == 0 || minutes == 3))
timeForBombFood:
cmp word[bombFoodCountdown], 0
jg notTimeForBombFood ;see the logic for bonus food.
push word bombFood
call generateNewFood
mov word[bombFoodCountdown], 10
notTimeForBombFood:
cmp word[bombFoodCountdown], 0
jg bombFoodAvailable
mov word[bombFood], 8000 ;outside the screen.
bombFoodAvailable:
popa
ret