-
Notifications
You must be signed in to change notification settings - Fork 39
/
AsmFlappyBird.Asm
652 lines (562 loc) · 14.2 KB
/
AsmFlappyBird.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
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
.386
.model flat,stdcall
option casemap:none
include AsmFlappyBird.inc
.code
start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke GetCommandLine
mov CommandLine,eax
invoke InitCommonControls
invoke WinMain,hInstance,NULL,CommandLine,SW_SHOWDEFAULT
invoke ExitProcess,eax
WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
mov wc.cbSize,sizeof WNDCLASSEX
mov wc.style,CS_HREDRAW or CS_VREDRAW
mov wc.lpfnWndProc,offset WndProc
mov wc.cbClsExtra,NULL
mov wc.cbWndExtra,DLGWINDOWEXTRA
push hInst
pop wc.hInstance
mov wc.hbrBackground,COLOR_BTNFACE+1
mov wc.lpszMenuName,IDM_MENU
mov wc.lpszClassName,offset ClassName
invoke LoadIcon,NULL,IDI_APPLICATION
mov wc.hIcon,eax
mov wc.hIconSm,eax
invoke LoadCursor,NULL,IDC_ARROW
mov wc.hCursor,eax
invoke RegisterClassEx,addr wc
invoke CreateDialogParam,hInstance,IDD_DIALOG,NULL,addr WndProc,NULL
invoke ShowWindow,hWnd,SW_SHOWNORMAL
invoke UpdateWindow,hWnd
.while TRUE
invoke GetMessage,addr msg,NULL,0,0
.BREAK .if !eax
invoke TranslateMessage,addr msg
invoke DispatchMessage,addr msg
.endw
mov eax,msg.wParam
ret
WinMain endp
PaintBoard proc uses ebx esi,hWin:HWND,hDC:HDC
LOCAL mDC:HDC
LOCAL hBmp:DWORD
LOCAL hOld:DWORD
LOCAL rect:RECT
LOCAL wRect:RECT
LOCAL xmax:DWORD
LOCAL buffer[32]:BYTE
invoke CreateCompatibleDC,hDC
mov mDC,eax
invoke CreateCompatibleBitmap,hDC,288,500
invoke SelectObject,mDC,eax
push eax
mov rect.left,0
mov rect.top,0
mov rect.right,288
mov rect.bottom,500
; Draw background
invoke ImageList_Draw, bgIml, 0, mDC, 0, 0, ILD_TRANSPARENT
; Draw words
.if gStatus == 0
mov wRect.left,0
mov wRect.top,210
mov wRect.right,288
mov wRect.bottom,500
invoke SetBkMode,mDC,TRANSPARENT
invoke SetTextColor,mDC,0000000h
invoke wsprintfA,addr buffer,offset lastFmtStr,lPoint
invoke DrawText,mDC,addr buffer,-1,addr wRect,DT_CENTER
mov wRect.top,235
invoke wsprintfA,addr buffer,offset maxFmtStr,maxPoint
invoke DrawText,mDC,addr buffer,-1,addr wRect,DT_CENTER
mov wRect.top,260
invoke wsprintfA,addr buffer,offset startStr
invoke DrawText,mDC,addr buffer,-1,addr wRect,DT_CENTER
.else
; Draw tube here
push ebx
mov ebx, offset upTube
.if [ebx].OBJECT.x < 288 || [ebx].OBJECT.x <= -1
invoke ImageList_Draw, tubeIml, 0, mDC, [ebx].OBJECT.x, [ebx].OBJECT.y, ILD_TRANSPARENT
.endif
add ebx, sizeof OBJECT
.if [ebx].OBJECT.x < 288 || [ebx].OBJECT.x <= -1
invoke ImageList_Draw, tubeIml, 0, mDC, [ebx].OBJECT.x, [ebx].OBJECT.y, ILD_TRANSPARENT
.endif
mov ebx, offset downTube
.if [ebx].OBJECT.x < 288 || [ebx].OBJECT.x <= -1
invoke ImageList_Draw, tubeIml, 1, mDC, [ebx].OBJECT.x, [ebx].OBJECT.y, ILD_TRANSPARENT
.endif
add ebx, sizeof OBJECT
.if [ebx].OBJECT.x < 288 || [ebx].OBJECT.x <= -1
invoke ImageList_Draw, tubeIml, 1, mDC, [ebx].OBJECT.x, [ebx].OBJECT.y, ILD_TRANSPARENT
.endif
pop ebx
; end draw tube
mov wRect.left,0
mov wRect.top,50
mov wRect.right,288
mov wRect.bottom,500
invoke SetBkMode,mDC,TRANSPARENT
invoke wsprintfA,addr buffer,offset fmtStr,cPoint
;invoke wsprintfA,addr buffer,offset fmtStr,actionToPerform
invoke SetTextColor,mDC,0ffffffh
invoke DrawText,mDC,addr buffer,-1,addr wRect,DT_CENTER
invoke SetTextColor,mDC,0000000h
mov wRect.top,49
mov wRect.right,286
invoke DrawText,mDC,addr buffer,-1,addr wRect,DT_CENTER
.if isAuto
mov wRect.top,30
mov wRect.right,288
invoke wsprintfA,addr buffer,offset maxFmtStr,maxPoint
invoke DrawText,mDC,addr buffer,-1,addr wRect,DT_CENTER
mov wRect.top,10
invoke wsprintfA,addr buffer,offset lastFmtStr,lPoint
invoke DrawText,mDC,addr buffer,-1,addr wRect,DT_CENTER
.endif
.endif
; Draw bird
.if cflap < 1 && cflapDir == 3
mov cflapDir, 0
.elseif cflap > 1 && cflapDir == 3
mov cflapDir, 1
.elseif cflap < 1 || cflap > 1
mov cflapDir, 3
.endif
.if cflapDir==0
inc cflap
.elseif cflapDir==1
dec cflap
.endif
invoke ImageList_Draw, birdIml, cflap, mDC, birdX, birdY, ILD_TRANSPARENT
; Draw bottom
mov eax, speed
sub bottomX, eax
.if bottomX < -47
mov bottomX, 0
.endif
invoke ImageList_Draw, barIml, 0, mDC, bottomX, bottomY, ILD_TRANSPARENT
invoke GetClientRect,hWin,addr rect
invoke StretchBlt,hDC,0,0,rect.right,rect.bottom,mDC,0,0,288,500,SRCCOPY
pop eax
invoke SelectObject,mDC,eax
invoke DeleteObject,eax
invoke DeleteDC,mDC
ret
PaintBoard endp
BoardProc proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
LOCAL ps:PAINTSTRUCT
.if uMsg==WM_PAINT
invoke BeginPaint,hWin,addr ps
invoke PaintBoard,hWin,ps.hdc
invoke EndPaint,hWin,addr ps
xor eax,eax
ret
.endif
invoke CallWindowProc,OldBoardProc,hWin,uMsg,wParam,lParam
ret
BoardProc endp
Random proc uses ecx edx,range:DWORD
inc rseed
mov eax, rseed
mov ecx, 23
mul ecx
add eax, 7
and eax, 0FFFFFFFFh
ror eax, 1
xor eax, rseed
mov rseed, eax
mov ecx, range
xor edx, edx
div ecx
mov eax, edx
ret
Random endp
SetupFly proc uses eax
inc rseed
; set bird
mov birdX, 63
mov birdY, 200
mov eax, jumpAc
mov birdAc, eax
; set tube
push ebx
push edx
mov ebx, offset upTube
mov edx, offset downTube
mov [ebx].OBJECT.x, 576
mov [edx].OBJECT.x, 576
invoke Random, 160
add eax, 195
mov [edx].OBJECT.y, eax
sub eax, 450
mov [ebx].OBJECT.y, eax
add ebx, sizeof OBJECT
add edx, sizeof OBJECT
mov [ebx].OBJECT.x, 756
mov [edx].OBJECT.x, 756
invoke Random, 160
add eax, 195
mov [edx].OBJECT.y, eax
sub eax, 450
mov [ebx].OBJECT.y, eax
pop edx
pop ebx
mov addedFlag, 0
ret
SetupFly endp
SetupDie proc uses eax,hWin:DWORD
mov eax, cPoint
mov cPoint, 0
mov lPoint, eax
.if eax > maxPoint
mov maxPoint, eax
.endif
mov gStatus, 0
mov birdX, 127
mov birdY, 168
.if isAuto
mov gStatus, 1
invoke SetupFly
.endif
invoke InvalidateRect,hWin,NULL,TRUE
ret
SetupDie endp
SetDist proc uses eax ebx
mov ebx, offset downTube
mov eax, [ebx].OBJECT.x
add eax, 52
.if eax < (birdX) || eax > 10000 ; behind the bird, use the second tube
add ebx, sizeof OBJECT
.endif
mov eax, [ebx].OBJECT.x
sub eax, birdX
;sub eax, 34
.if eax > 179
mov eax, 179
.endif
mov stateDashH, eax
mov eax, [ebx].OBJECT.y
mov ebx, birdY
add ebx, 24
sub eax, ebx
add eax, 200
mov stateDashV, eax
ret
SetDist endp
UpdateQ proc uses eax ebx edx
LOCAL V_s_dash_a_dash:REAL8
LOCAL Q_s_a:REAL8
LOCAL tempX:DWORD
LOCAL tempY:DWORD
finit
mov edx, 0
mov eax, stateDashV
mov ebx, 10
div ebx
mov tempX, eax
mov edx, 0
mov eax, stateDashH
mov ebx, 10
div ebx
mov tempY, eax
mov eax, tempX
mov ebx, 50
mul ebx
mov ebx, eax
add ebx, tempY
add ebx, tempY
fld QMap[ebx * type QMap] ; load Q[tempX][tempY][0]
inc ebx
fcom QMap[ebx * type QMap] ; compare with Q[tempX][tempY][1]
fnstsw ax
sahf
ja L1
fstp V_s_dash_a_dash
fld QMap[ebx * type QMap]
L1:
fstp V_s_dash_a_dash ; save the bigger number to V_s_dash_a_dash
finit
mov edx, 0
mov eax, stateV
mov ebx, 10
div ebx
mov tempX, eax
mov edx, 0
mov eax, stateH
mov ebx, 10
div ebx
mov tempY, eax
mov eax, tempX
mov ebx, 50
mul ebx
add eax, tempY
add eax, tempY
add eax, actionToPerform
mov ebx, eax
fld QMap[ebx * type QMap]
fstp Q_s_a ; get Q_s_a
fld V_s_dash_a_dash
fsub Q_s_a
fild reward
fadd
fld alpha_QL
fmul
fld Q_s_a
fadd
fstp QMap[ebx * type QMap]
mov eax, stateDashV
mov stateV, eax
mov eax, stateDashH
mov stateH, eax
ret
UpdateQ endp
PerformAction proc uses eax ebx
LOCAL tempX:DWORD
LOCAL tempY:DWORD
finit
mov edx, 0
mov eax, stateV
mov ebx, 10
div ebx
mov tempX, eax
mov edx, 0
mov eax, stateH
mov ebx, 10
div ebx
mov tempY, eax
mov eax, tempX
mov ebx, 50
mul ebx
add eax, tempY
add eax, tempY
mov ebx, eax
fld QMap[ebx * type QMap] ; load Q[tempX][tempY][0]
inc ebx
fcom QMap[ebx * type QMap] ; compare with Q[tempX][tempY][1]
fnstsw ax
sahf
jb L3
mov actionToPerform, 0 ; do nothing
jmp L4
L3:
mov actionToPerform, 1
mov eax, jumpAc
mov birdAc, eax
L4:
ret
PerformAction endp
TimerProc proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
.if gStatus == 1
.if isAuto
invoke PerformAction
.endif
mov eax, birdAc
add eax, gravityAc
mov reward, 1
.if eax > birdMaxAc && eax < 10000
mov eax, birdMaxAc
.endif
mov birdAc, eax
add eax, birdY
.if eax > 10000
mov eax, 0
.endif
.if eax > birdBottom ; touch the ground, failed
mov gStatus, 0
mov reward, -1000
invoke SetupDie, hBoard
.else
mov birdY, eax
.endif
push ebx
push edx
mov ebx, offset downTube
mov eax, [ebx].OBJECT.x
.if eax < birdX && addedFlag == 0
mov reward, 2
inc cPoint
mov addedFlag, 1
.endif
.if eax > 16 && eax < 97
mov eax, [ebx].OBJECT.y
sub eax, birdY
.if eax < 20 || eax > 135
mov gStatus, 0
mov reward, -1000
invoke SetupDie, hBoard
.endif
.endif
mov ebx, offset upTube
mov edx, offset downTube
.if [ebx].OBJECT.x < -54 && [ebx].OBJECT.x > 10000
mov eax, ebx
add eax, sizeof OBJECT
mov ecx, [eax].OBJECT.x
mov [ebx].OBJECT.x, ecx
mov ecx, [eax].OBJECT.y
mov [ebx].OBJECT.y, ecx
mov eax, edx
add eax, sizeof OBJECT
mov ecx, [eax].OBJECT.x
mov [edx].OBJECT.x, ecx
mov ecx, [eax].OBJECT.y
mov [edx].OBJECT.y, ecx
mov eax, [edx].OBJECT.x
add eax, 180
add ebx, sizeof OBJECT
add edx, sizeof OBJECT
mov [ebx].OBJECT.x, eax
mov [edx].OBJECT.y, eax
invoke Random, 160
add eax, 195
mov [edx].OBJECT.y, eax
sub eax, 450
mov [ebx].OBJECT.y, eax
mov addedFlag, 0
.endif
mov ecx, 2
mov ebx, offset upTube
mov edx, offset downTube
.while ecx
mov eax, [ebx].OBJECT.x
sub eax, speed
mov [ebx].OBJECT.x, eax
mov [edx].OBJECT.x, eax
add ebx, sizeof OBJECT
add edx, sizeof OBJECT
dec ecx
.endw
pop edx
pop ebx
.if isAuto
invoke SetDist
invoke UpdateQ
.endif
.endif
invoke InvalidateRect,hBoard,NULL,TRUE
ret
TimerProc endp
InitParam proc
mov speed, 4
mov bottomY, 387
mov birdMaxAc, 11
mov birdBottom, 369
mov gravityAc, 2
mov jumpAc, -14
mov cflap, 1
mov stateV, 179
mov stateH, 0
mov isAuto, 0
ret
InitParam endp
SaveFile proc uses eax ebx
LOCAL writtenByte:DWORD
invoke CreateFile,offset szFile,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
mov ebx, eax
invoke WriteFile, ebx, offset QMap, MapSize, addr writtenByte, 0
invoke CloseHandle, ebx
ret
SaveFile endp
ReadModelFile proc
LOCAL readByte
invoke CreateFile,offset szFile,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
.if eax
mov ebx, eax
invoke ReadFile, ebx, offset QMap, MapSize, addr readByte, 0
.endif
invoke CloseHandle, ebx
ret
ReadModelFile endp
WndProc proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
LOCAL hBmp:DWORD
mov eax,uMsg
.if eax==WM_INITDIALOG
push hWin
pop hWnd
; Load background image
invoke ImageList_Create,288,500,ILC_COLOR16,1,0
mov bgIml,eax
invoke LoadBitmap,hInstance,IDB_BG
mov hBmp,eax
invoke ImageList_AddMasked,bgIml,hBmp,0
invoke DeleteObject,hBmp
; Load tube image
invoke ImageList_Create,52,320,ILC_COLOR16 or ILC_MASK,2,2
mov tubeIml,eax
invoke LoadBitmap,hInstance,IDB_TUBE
mov hBmp,eax
invoke ImageList_AddMasked,tubeIml,hBmp,0
invoke DeleteObject,hBmp
; Load bird image
invoke ImageList_Create,34,24,ILC_COLOR16 or ILC_MASK,3,3
mov birdIml,eax
invoke LoadBitmap,hInstance,IDB_BIRD
mov hBmp,eax
invoke ImageList_AddMasked,birdIml,hBmp,0
invoke DeleteObject,hBmp
; Load bar image
invoke ImageList_Create,336,112,ILC_COLOR16,1,0
mov barIml,eax
invoke LoadBitmap,hInstance,IDB_BAR
mov hBmp,eax
invoke ImageList_AddMasked,barIml,hBmp,0
invoke DeleteObject,hBmp
invoke GetDlgItem,hWin,IDC_BOARD
mov hBoard,eax
invoke SetWindowLong,hBoard,GWL_WNDPROC,offset BoardProc
mov OldBoardProc,eax
invoke InitParam
invoke SetupDie, hBoard
invoke SetTimer,hWin,200,MTIMER,offset TimerProc
.elseif eax==WM_KEYDOWN
mov eax,wParam
.if eax==VK_SPACE
.if gStatus == 0
mov gStatus, 1
invoke SetupFly
.else
mov eax, jumpAc
mov birdAc, eax
.endif
.elseif eax==VK_A
.if isAuto
mov isAuto, 0
.else
mov isAuto, 1
.endif
.endif
.elseif eax==WM_COMMAND
mov eax,wParam
and eax,0FFFFh
.if eax==IDM_FILE_EXIT
invoke SendMessage,hWin,WM_CLOSE,0,0
.elseif eax==IDM_MODE
.if isAuto
mov isAuto, 0
.else
mov isAuto, 1
.endif
.elseif eax==IDM_SAVE
invoke SaveFile
.elseif eax==IDM_OPEN
invoke ReadModelFile
.endif
.elseif eax==WM_CLOSE
invoke DestroyWindow,hWin
.elseif uMsg==WM_DESTROY
invoke PostQuitMessage,NULL
.else
invoke DefWindowProc,hWin,uMsg,wParam,lParam
ret
.endif
xor eax,eax
ret
WndProc endp
end start