-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceCode.asm
More file actions
609 lines (571 loc) · 15.9 KB
/
SourceCode.asm
File metadata and controls
609 lines (571 loc) · 15.9 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
INCLUDE Irvine32.inc
BoxWidth = 20
BoxHeight = 7
mouseWidth = 3
mouseHeight = 4
.data
highestScoreString BYTE "歷史最高分數: ",0
highestScore DWORD 0
fileName BYTE "score.txt",0
fileHandle HANDLE ?
redPopProbability DWORD 5
menuTitle BYTE "***打地鼠***",0
menuTurtorialTitle BYTE "按1~9打對應格子的地鼠",0dh,0ah,0;
menuTutorialGraph BYTE "-------------------",0dh,0ah,
"| 7 | 8 | 9 |",0dh,0ah,
"| 4 | 5 | 6 |",0dh,0ah,
"| 1 | 2 | 3 |",0dh,0ah,
"-------------------",0dh,0ah,0
menuStartMessage BYTE "按W開始",0
endTitle BYTE "你的分數: ", 0
endRestart BYTE "按W重新開始,按其他按鍵離開", 0
totalTime DWORD ?
divisor DWORD 1000
gameStartTime DWORD ?
timeString BYTE " time: ",0
score DWORD 0
scoreString BYTE "score: ",0
lastUpdatePopTime DWORD ?
popBaseTime DWORD ?
lastUpdateDrawTime DWORD ?
popUp byte 0,0,0,0,0,0,0,0,0
redPopUp byte 0,0,0,0,0,0,0,0,0
counter byte 0 ;給loop用的counter
mousePicture BYTE "***" ;地鼠的1/3
keyBuffer BYTE ?
baseTime DWORD ? ;遊戲一開始就記住系統時間
currentTime DWORD 0
boxTop BYTE BoxWidth DUP('*')
boxBody BYTE '*',(BoxWidth - 2)DUP(' '),'*'
boxBottom BYTE BoxWidth DUP('*')
fps BYTE 33
outputHandle DWORD 0
bytesWritten DWORD 0
count DWORD 0
mousePosition COORD <BoxWidth+8,BoxHeight+2>, <BoxWidth+28,BoxHeight+2>, <BoxWidth+48,BoxHeight+2>, <BoxWidth+8,BoxHeight+9>, <BoxWidth+28,BoxHeight+9>, <BoxWidth+48,BoxHeight+9>, <BoxWidth+8,BoxHeight+16>, <BoxWidth+28,BoxHeight+16>, <BoxWidth+48,BoxHeight+16>
position COORD <BoxWidth,BoxHeight>,<BoxWidth*2,BoxHeight>,<BoxWidth*3,BoxHeight>,<BoxWidth*1,BoxHeight*2>,<BoxWidth*2,BoxHeight*2>,<BoxWidth*3,BoxHeight*2>,<BoxWidth*1,BoxHeight*3>,<BoxWidth*2,BoxHeight*3>,<BoxWidth*3,BoxHeight*3>
cellsWritten DWORD ?
attributesNotHit0 WORD BoxWidth DUP(0Ah)
attributesNotHit1 WORD 0Ah, (BoxWidth-2) DUP(0), 0Ah
attributesNotHit2 WORD BoxWidth DUP(0Ah)
attributesHit0 WORD BoxWidth DUP(0Ch)
attributesHit1 WORD 0Ch, (BoxWidth-2) DUP(0), 0Ch
attributesHit2 WORD BoxWidth DUP(0Ch)
.code
setRedPop PROC USES eax edx ebx
call Randomize
mov eax, redPopProbability
INVOKE RandomRange
.IF eax == 0 && popUp[0] == 0
mov redPopUp[0], 1 ;if eax is 0, the red rat at redPopUp[i] shows up.
.ELSE
mov redPopUp[0],0 ;if eax is 1, the rat rat at redDopUp[i] hides.
.ENDIF
mov eax, redPopProbability
INVOKE RandomRange
.IF eax == 0 && popUp[1] == 0
mov redPopUp[1], 1 ;if eax is 0, the red rat at redPopUp[i] shows up.
.ELSE
mov redPopUp[1],0 ;if eax is 1, the rat rat at redDopUp[i] hides.
.ENDIF
mov eax, redPopProbability
INVOKE RandomRange
.IF eax == 0 && popUp[2] == 0
mov redPopUp[2], 1 ;if eax is 0, the red rat at redPopUp[i] shows up.
.ELSE
mov redPopUp[2],0 ;if eax is 1, the rat rat at redDopUp[i] hides.
.ENDIF
mov eax, redPopProbability
INVOKE RandomRange
.IF eax == 0 && popUp[3] == 0
mov redPopUp[3], 1 ;if eax is 0, the red rat at redPopUp[i] shows up.
.ELSE
mov redPopUp[3],0 ;if eax is 1, the rat rat at redDopUp[i] hides.
.ENDIF
mov eax, redPopProbability
INVOKE RandomRange
.IF eax == 0 && popUp[4] == 0
mov redPopUp[4], 1 ;if eax is 0, the red rat at redPopUp[i] shows up.
.ELSE
mov redPopUp[4],0 ;if eax is 1, the rat rat at redDopUp[i] hides.
.ENDIF
mov eax, redPopProbability
INVOKE RandomRange
.IF eax == 0 && popUp[5] == 0
mov redPopUp[5], 1 ;if eax is 0, the red rat at redPopUp[i] shows up.
.ELSE
mov redPopUp[5],0 ;if eax is 1, the rat rat at redDopUp[i] hides.
.ENDIF
mov eax, redPopProbability
INVOKE RandomRange
.IF eax == 0 && popUp[6] == 0
mov redPopUp[6], 1 ;if eax is 0, the red rat at redPopUp[i] shows up.
.ELSE
mov redPopUp[6],0 ;if eax is 1, the rat rat at redDopUp[i] hides.
.ENDIF
mov eax, redPopProbability
INVOKE RandomRange
.IF eax == 0 && popUp[7] == 0
mov redPopUp[7], 1 ;if eax is 0, the red rat at redPopUp[i] shows up.
.ELSE
mov redPopUp[7],0 ;if eax is 1, the rat rat at redDopUp[i] hides.
.ENDIF
mov eax, redPopProbability
INVOKE RandomRange
.IF eax == 0 && popUp[8] == 0
mov redPopUp[8], 1 ;if eax is 0, the red rat at redPopUp[i] shows up.
.ELSE
mov redPopUp[8],0 ;if eax is 1, the rat rat at redDopUp[i] hides.
.ENDIF
ret
setRedPop ENDP
setPop PROC USES eax edx ebx ;讓地鼠陣列隨機出現0 or 1
call Randomize
mov eax, 2 ;set the range of random number [0,2)
INVOKE RandomRange ;store a randon number in eax
.IF eax == 0
mov popUp[0], 1 ;if eax is 0, the rat at popUp[i] shows up.
.ELSE
mov popUp[0],0 ;if eax is 1, the rat at popUp[i] hides.
.ENDIF
mov eax, 2
INVOKE RandomRange
.IF eax == 0
mov popUp[1], 1
.ELSE
mov popUp[1],0
.ENDIF
mov eax, 2
INVOKE RandomRange
.IF eax == 0
mov popUp[2], 1
.ELSE
mov popUp[2],0
.ENDIF
mov eax, 2
INVOKE RandomRange
.IF eax == 0
mov popUp[3], 1
.ELSE
mov popUp[3],0
.ENDIF
mov eax, 2
INVOKE RandomRange
.IF eax == 0
mov popUp[4], 1
.ELSE
mov popUp[4],0
.ENDIF
mov eax, 2
INVOKE RandomRange
.IF eax == 0
mov popUp[5], 1
.ELSE
mov popUp[5],0
.ENDIF
mov eax, 2
INVOKE RandomRange
.IF eax == 0
mov popUp[6], 1
.ELSE
mov popUp[6],0
.ENDIF
mov eax, 2
INVOKE RandomRange
.IF eax == 0
mov popUp[7], 1
.ELSE
mov popUp[7],0
.ENDIF
mov eax, 2 ;set the range of random number [0,2)
INVOKE RandomRange ;store a randon number in eax
.IF eax == 0
mov popUp[8], 1 ;if eax is 0, the rat at popUp[i] shows up.
.ELSE
mov popUp[8],0 ;if eax is 1, the rat at popUp[i] hides.
.ENDIF
;mov popUp[0],0
ret
setPop ENDP
drawMouse PROC USES eax ecx edx,
xyPosition:COORD,
attributes0:PTR WORD,
attributes1:PTR WORD,
attributes2:PTR WORD
mov counter, 3
push xyPosition
L1: INVOKE WriteConsoleOutputAttribute,
outputHandle,
attributes0,
mouseWidth,
xyPosition,
addr bytesWritten
INVOKE WriteConsoleOutputCharacter,
outputHandle, ; console output handle
addr mousePicture, ; pointer to the mouse
mouseWidth, ; size of box line
xyPosition, ; coordinates of first char
addr count ; output count
inc xyPosition.Y
dec counter
cmp counter, 0
jne L1
pop xyPosition
ret
drawMouse ENDP
drawBackGround PROC USES eax ecx,
xyPosition:COORD,
attributes0:PTR WORD,
attributes1:PTR WORD,
attributes2:PTR WORD
invoke getStdHandle, STD_OUTPUT_HANDLE
mov outputHandle, eax ; save console handle
;call Clrscr
; draw top of the box
INVOKE WriteConsoleOutputAttribute,
outputHandle,
attributes0,
BoxWidth,
xyPosition,
addr bytesWritten
INVOKE WriteConsoleOutputCharacter,
outputHandle, ; console output handle
addr boxTop, ; pointer to the top box line
BoxWidth, ; size of box line
xyPosition, ; coordinates of first char
addr count ; output count
inc xyPosition.y ; next line
; draw body of the box
mov ecx, BoxHeight-2 ; number of lines in body
L1: push ecx ; save counter
INVOKE WriteConsoleOutputAttribute,
outputHandle,
attributes1,
BoxWidth,
xyPosition,
addr bytesWritten
INVOKE WriteConsoleOutputCharacter,
outputHandle, ; console output handle
addr boxBody, ; pointer to the box body
BoxWidth, ; size of box line
xyPosition, ; coordinates of first char
addr count ; output count
inc xyPosition.y ; next line
pop ecx ; restore counter
loop L1
; draw bottom of the box
INVOKE WriteConsoleOutputAttribute,
outputHandle,
attributes2,
BoxWidth,
xyPosition,
addr bytesWritten
INVOKE WriteConsoleOutputCharacter,
outputHandle, ; console output handle
addr boxBottom, ; pointer to the bottom of the box
BoxWidth, ; size of box line
xyPosition, ; coordinates of first char
addr count ; output count
;call WaitMsg
;call Clrscr
ret
drawBackGround ENDP
menuPage MACRO
call Clrscr
mov edx, OFFSET menuTitle
INVOKE WriteString
mov edx, OFFSET menuTurtorialTitle
INVOKE Crlf
INVOKE WriteString
mov edx, OFFSET menuTutorialGraph
INVOKE WriteString
INVOKE Crlf
mov edx, OFFSET menuStartMessage
INVOKE WriteString
INVOKE Crlf
INVOKE ReadChar
.IF al == 'w' || al == 'W'
jmp startPrepare
.ELSE
jmp EX
.ENDIF
ENDM
initialize MACRO
INVOKE GetTickCount
mov baseTime, eax
mov popBaseTime, eax
mov gameStartTime, eax ;紀錄遊戲開始時間
mov totalTime, 20
mov score, 0
mov popUp[0],0
mov popUp[1],0
mov popUp[2],0
mov popUp[3],0
mov popUp[4],0
mov popUp[5],0
mov popUp[6],0
mov popUp[7],0
mov popUp[8],0
mov redPopUp[0],0
mov redPopUp[1],0
mov redPopUp[2],0
mov redPopUp[3],0
mov redPopUp[4],0
mov redPopUp[5],0
mov redPopUp[6],0
mov redPopUp[7],0
mov redPopUp[8],0
ENDM
updateRatArray MACRO
INVOKE GetTickCount
sub eax, popBaseTime ;將距離上一次更新畫面的時間存入eax
mov lastUpdatePopTime, eax ;將距離上一次更新畫面的時間存入edx
.IF lastUpdatePopTime>2000
INVOKE setPop
INVOKE setRedPop
INVOKE GetTickCount
mov popBaseTime, eax
.ENDIF
ENDM
updateDrawBackground MACRO
INVOKE drawBackGround, position[sizeof coord*0], addr attributesNotHit0, addr attributesNotHit1, addr attributesNotHit2
INVOKE drawBackGround, position[sizeof coord*1], addr attributesNotHit0, addr attributesNotHit1, addr attributesNotHit2
INVOKE drawBackGround, position[sizeof coord*2], addr attributesNotHit0, addr attributesNotHit1, addr attributesNotHit2
INVOKE drawBackGround, position[sizeof coord*3], addr attributesNotHit0, addr attributesNotHit1, addr attributesNotHit2
INVOKE drawBackGround, position[sizeof coord*4], addr attributesNotHit0, addr attributesNotHit1, addr attributesNotHit2
INVOKE drawBackGround, position[sizeof coord*5], addr attributesNotHit0, addr attributesNotHit1, addr attributesNotHit2
INVOKE drawBackGround, position[sizeof coord*6], addr attributesNotHit0, addr attributesNotHit1, addr attributesNotHit2
INVOKE drawBackGround, position[sizeof coord*7], addr attributesNotHit0, addr attributesNotHit1, addr attributesNotHit2
INVOKE drawBackGround, position[sizeof coord*8], addr attributesNotHit0, addr attributesNotHit1, addr attributesNotHit2
ENDM
updateDrawRat MACRO
.IF popUp[0] == 1
INVOKE drawMouse, mousePosition[sizeof coord*0], addr attributesNotHit0, addr attributesNotHit1, addr attributesNotHit2
.ENDIF
.IF popUp[1] == 1
INVOKE drawMouse, mousePosition[sizeof coord*1], addr attributesNotHit0, addr attributesNotHit1, addr attributesNotHit2
.ENDIF
.IF popUp[2] == 1
INVOKE drawMouse, mousePosition[sizeof coord*2], addr attributesNotHit0, addr attributesNotHit1, addr attributesNotHit2
.ENDIF
.IF popUp[3] == 1
INVOKE drawMouse, mousePosition[sizeof coord*3], addr attributesNotHit0, addr attributesNotHit1, addr attributesNotHit2
.ENDIF
.IF popUp[4] == 1
INVOKE drawMouse, mousePosition[sizeof coord*4], addr attributesNotHit0, addr attributesNotHit1, addr attributesNotHit2
.ENDIF
.IF popUp[5] == 1
INVOKE drawMouse, mousePosition[sizeof coord*5], addr attributesNotHit0, addr attributesNotHit1, addr attributesNotHit2
.ENDIF
.IF popUp[6] == 1
INVOKE drawMouse, mousePosition[sizeof coord*6], addr attributesNotHit0, addr attributesNotHit1, addr attributesNotHit2
.ENDIF
.IF popUp[7] == 1
INVOKE drawMouse, mousePosition[sizeof coord*7], addr attributesNotHit0, addr attributesNotHit1, addr attributesNotHit2
.ENDIF
.IF popUp[8] == 1
INVOKE drawMouse, mousePosition[sizeof coord*8], addr attributesNotHit0, addr attributesNotHit1, addr attributesNotHit2
.ENDIF
ENDM
updateDrawRedRat MACRO
.IF redPopUp[0] == 1
INVOKE drawMouse, mousePosition[sizeof coord*0], addr attributesHit0, addr attributesHit1, addr attributesHit2
.ENDIF
.IF redPopUp[1] == 1
INVOKE drawMouse, mousePosition[sizeof coord*1], addr attributesHit0, addr attributesHit1, addr attributesHit2
.ENDIF
.IF redPopUp[2] == 1
INVOKE drawMouse, mousePosition[sizeof coord*2], addr attributesHit0, addr attributesHit1, addr attributesHit2
.ENDIF
.IF redPopUp[3] == 1
INVOKE drawMouse, mousePosition[sizeof coord*3], addr attributesHit0, addr attributesHit1, addr attributesHit2
.ENDIF
.IF redPopUp[4] == 1
INVOKE drawMouse, mousePosition[sizeof coord*4], addr attributesHit0, addr attributesHit1, addr attributesHit2
.ENDIF
.IF redPopUp[5] == 1
INVOKE drawMouse, mousePosition[sizeof coord*5], addr attributesHit0, addr attributesHit1, addr attributesHit2
.ENDIF
.IF redPopUp[6] == 1
INVOKE drawMouse, mousePosition[sizeof coord*6], addr attributesHit0, addr attributesHit1, addr attributesHit2
.ENDIF
.IF redPopUp[7] == 1
INVOKE drawMouse, mousePosition[sizeof coord*7], addr attributesHit0, addr attributesHit1, addr attributesHit2
.ENDIF
.IF redPopUp[8] == 1
INVOKE drawMouse, mousePosition[sizeof coord*8], addr attributesHit0, addr attributesNotHit1, addr attributesNotHit2
.ENDIF
ENDM
handleGameInformation MACRO
mov edx, OFFSET scoreString
INVOKE WriteString ;在畫面賞顯示"score: "
mov eax, score
INVOKE WriteDec ;在畫面上顯示目前分數
mov edx, OFFSET timeString
INVOKE WriteString ;在畫面上顯示" time: "
INVOKE GetTickCount
sub eax, gameStartTime
mov edx,0
div divisor
sub totalTime, eax
.IF totalTime <= 0
jmp endGame
.ENDIF
mov eax, totalTime
mov totalTime, 20
INVOKE WriteDec
INVOKE GetTickCount
mov baseTime, eax
jmp notEnd
ENDM
endGameMenu MACRO
call Clrscr
mov edx, OFFSET endTitle
INVOKE WriteString
mov eax, score
INVOKE WriteDec
INVOKE Crlf
mov edx, OFFSET endRestart
INVOKE WriteString
INVOKE Crlf
;mov edx, OFFSET highestScoreString
;INVOKE WriteString
;mov eax, highestScore
;INVOKE WriteDec
;INVOKE Crlf
INVOKE ReadChar
.IF al == 'w' || al == 'W'
INVOKE GetTickCount
jmp startPrepare
.ELSE
jmp EX
.ENDIF
.ENDIF
ENDM
readKeyboard MACRO
INVOKE ReadKey
.IF al == '1'
.IF popUp[6] == 1
mov popUp[6], 0
inc score
.ELSEIF redPopUp[6] == 1
mov redPopUp[6], 0
.IF score > 0
dec score
.ENDIF
.ENDIF
.ENDIF
.IF al == '2'
.IF popUp[7] == 1
mov popUp[7], 0
inc score
.ELSEIF redPopUp[7] == 1
mov redPopUp[7], 0
.IF score > 0
dec score
.ENDIF
.ENDIF
.ENDIF
.IF al == '3'
.IF popUp[8] == 1
mov popUp[8], 0
inc score
.ELSEIF redPopUp[8] == 1
mov redPopUp[8], 0
.IF score > 0
dec score
.ENDIF
.ENDIF
.ENDIF
.IF al == '4'
.IF popUp[3] == 1
mov popUp[3], 0
inc score
.ELSEIF redPopUp[3] == 1
mov redPopUp[3], 0
.IF score > 0
dec score
.ENDIF
.ENDIF
.ENDIF
.IF al == '5'
.IF popUp[4] == 1
mov popUp[4], 0
inc score
.ELSEIF redPopUp[4] == 1
mov redPopUp[4], 0
.IF score > 0
dec score
.ENDIF
.ENDIF
.ENDIF
.IF al == '6'
.IF popUp[5] == 1
mov popUp[5], 0
inc score
.ELSEIF redPopUp[5] == 1
mov redPopUp[5], 0
.IF score > 0
dec score
.ENDIF
.ENDIF
.ENDIF
.IF al == '7'
.IF popUp[0] == 1
mov popUp[0], 0
inc score
.ELSEIF redPopUp[0] == 1
mov redPopUp[0], 0
.IF score > 0
dec score
.ENDIF
.ENDIF
.ENDIF
.IF al == '8'
.IF popUp[1] == 1
mov popUp[1], 0
inc score
.ELSEIF redPopUp[1] == 1
mov redPopUp[1], 0
.IF score > 0
dec score
.ENDIF
.ENDIF
.ENDIF
.IF al == '9'
.IF popUp[2] == 1
mov popUp[2], 0
inc score
.ELSEIF redPopUp[2] == 1
mov redPopUp[2], 0
.IF score > 0
dec score
.ENDIF
.ENDIF
.ENDIF
ENDM
main PROC
menu:
menuPage
startPrepare:
initialize
startGame:
updateRatArray
INVOKE GetTickCount
sub eax, baseTime ;將距離上一次更新畫面的時間存入eax
mov lastUpdateDrawTime, eax ;將距離上一次更新畫面的時間存入edx
.IF lastUpdateDrawTime>33
call Clrscr
updateDrawBackground
updateDrawRat
updateDrawRedRat
handleGameInformation
endGame:
endGameMenu
notEnd:
readKeyboard
jmp startGame
EX:
exit
main ENDP
END main