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
| assume cs:code, ds:data, ss:stack
; gpu prop byte ; 7 6 5 4 3 2 1 0 ; BL R G B I R G B ; | \_____/ | \_____/ ; 闪烁 背景 高亮 前景
; game map: 25 x 25 ; snake bodys max: 23 x 23
data segment score dw 0 ; game score scpre db 'score:' ; [6] score prefix dead db 'game over!' ; [10] game over food dw 0 ; snake food: x|y sdct db 0 ; snake direction 0001-up 0010-down 0100-left 1000-right slen db 0 ; snake length body dw 529 dup(0) ; snake bodys: x|y, ... data ends
stack segment dw 20 dup(0) stack ends
code segment start: mov ax, data mov ds, ax ; init data seg mov ax, 0b800h mov es, ax ; save gpu base addr mov ax, stack mov ss, ax mov sp, 20 ; init stack call clear mov ah, 24 mov al, 24 push ax call draw_map call create_snake call draw_snake call create_food call draw_food
call start_game call show_score
end_game: call game_over
mov ax, 4c00h int 21h ; # start game ; void -> void start_game: mov cx, 50 sg0: call listen_key call clear_last call move_snake call is_dead call draw_snake call is_eat call show_score call sleep loop sg0 ret
; # show game over alert ; void -> void game_over: push ax push cx push si
mov si, 28 mov cx, 10 ; string length go0: mov ax, si mov ah, al mov al, 12 push ax mov ah, 00000111b mov al, dead[si - 28] push ax call draw_char inc si loop go0 pop si pop cx pop ax ret
; # is the snake dead ; void -> void is_dead: push ax push cx push si mov ax, body[0] ; get head cmp ah, 0 ; touch the edge je end_game
cmp ah, 24 je end_game
cmp al, 0 je end_game cmp al, 24 je end_game mov ch, 0 mov cl, slen dec cx mov si, 2 ; start with second
id0: ; touch self body mov ax, body[si] cmp body[0], ax je end_game add si, 2 loop id0 pop si pop cx pop ax ret
; # add snake body ; void -> void add_body: push ax push si mov ah, 0 mov al, slen inc al mov slen, al ; add body mov si, ax add si, si
mov ax, body[si - 4] mov body[si - 2], ax inc score call create_food call draw_food pop si pop ax ret
; # is snake eat food ; void -> void is_eat: push ax
mov ax, food cmp ax, body[0] jne nt0 call add_body nt0: pop ax ret
; show game score ; void -> void show_score: push ax push bx push si mov si, 28 mov cx, 6 ; string length po0: ; draw score prefix mov ax, si mov ah, al mov al, 2 push ax mov ah, 00000111b mov al, scpre[si - 28] push ax call draw_char inc si loop po0 mov si, 0 mov ax, score sl0: mov bh, 0 mov bl, 10 div bl mov bx, ax mov bl, bh mov bh, 0 push bx ; save digit inc si ; count digits cmp al, 0 ; no more digit je se0 mov ah, 0 ; next digit jmp short sl0 se0: mov cx, si mov si, 34 ; show start x sn0: pop bx ; get each digit add bx, 30H ; get digit ascii mov ax, si mov ah, al mov al, 2 push ax mov ah, 00000111b mov al, bl push ax call draw_char ; draw each digit inc si loop sn0 pop si pop bx pop ax ret
; # listen to keys ; void -> void listen_key: push ax mov ah, 1 mov al, 0 int 16h ; keboard interrupt cmp ah, 1 je lk0 mov ax, 0 int 16h cmp ax, 4800h ; press up je kup cmp ax, 5000h ; press down je kdown cmp ax, 4b00h ; press left je kleft cmp ax, 4d00h ; press right je kright jmp short lk0 kup: cmp sdct, 0010b ; direction conflict je lk0 mov sdct, 0001b ; set direction jmp short lk0
kdown: cmp sdct, 0001b je lk0 mov sdct, 0010b jmp short lk0
kleft: cmp sdct, 1000b je lk0 mov sdct, 0100b jmp short lk0
kright: cmp sdct, 0100b je lk0 mov sdct, 1000b lk0: pop ax ret
; # clear last snake body ; void -> void clear_last: push ax push si mov ah, 0 mov al, slen mov si, ax add si, si
mov ax, body[si - 2] ; last body push ax mov ah, 00000000b mov al, ' ' push ax call draw_char ; clear
pop si pop ax ret
; # update snake bodys ; void -> void move_snake: push ax push cx push si
mov ch, 0 mov cl, slen dec cx mov ah, 0 mov al, slen mov si, ax add si, si ms0: mov ax, body[si - 4] mov body[si - 2], ax sub si, 2 loop ms0 mov ax, body[0] ; upd head cmp sdct, 0001b ; up je up cmp sdct, 0010b ; down je down cmp sdct, 0100b ; left je left cmp sdct, 1000b ; right je right up: dec al jmp short uok
down: inc al jmp short uok
left: dec ah jmp short uok right: inc ah jmp short uok
uok: mov body[0], ax
pop si pop cx pop ax ret
; # draw snake to map ; void -> void draw_snake: push cx push si push ax mov ch, 0 mov cl, slen mov si, 0 ds0: mov ax, body[si] push ax mov ah, 01110000b mov al, ' ' push ax call draw_char ; draw body add si, 2 loop ds0
pop ax pop si pop cx ret
; # create snake ; fixed: snake size is 3 ; void -> void create_snake: push cx push si push ax mov sdct, 1000b ; set direct is right mov ah, 1 mov al, 1 mov slen, 2 ; set snake length mov ch, 0 mov cl, slen mov si, 0 mov ah, 6 ; set snake head x mov al, 4 ; set snake head y c0: mov body[si], ax dec ah add si, 2 loop c0 pop ax pop si pop cx ret
; draw food to map ; void -> void draw_food: push ax mov ax, food push ax mov ah, 00101000b mov al, ' ' push ax call draw_char ; draw food pop ax ret
; # create a food ; fixed: map width and height ; void -> void create_food: push ax push bx push cx push dx push si
re0: mov bx, 23 push bx call rand ; get x inc dx ; range: 1-[23] mov ah, dl push bx call rand ; get y inc dx mov al, dl mov ch, 0 mov cl, slen mov si, 0 sb0: ; for snake body cmp ax, body[si] je re0 ; conflict with body, create again add si, 2 loop sb0 mov food, ax ; save food xy pop si pop dx pop cx pop bx pop ax ret
; # rand number ; 0: range -> dx: 0~(range) rand: push bp mov bp, sp push ax
mov ax, 0h out 43h, al in al, 40h in al, 40h in al, 40h mov bx, [bp + 4] div bl mov dh, 0 mov dl, ah pop ax pop bp ret 2
; # sleep tiem ; void -> void sleep: push cx mov cx, 50000 s0: push cx mov cx, 20 s1: loop s1 pop cx loop s0 pop cx ret ; # draw game map ; 0: width|height -> void draw_map: push bp mov bp, sp ; draw top push cx push ax push bx push dx mov dx, [bp + 4] ; get width mov ax, 0 mov ch, 0 mov cl, dh ; set width inc cl d0: mov bh, al ; draw top mov bl, 0 push bx mov bh, 00010000b mov bl, ' ' push bx call draw_char mov bh, al ; draw bottom mov bl, dl push bx mov bh, 00010000b mov bl, ' ' push bx call draw_char inc al loop d0 mov cl, dl ; set height dec cl ; inside content height mov al, 1 d1: mov bh, 0 ; draw left mov bl, al push bx mov bh, 00010000b mov bl, ' ' push bx call draw_char
mov bh, dh ; draw bottom mov bl, al push bx mov bh, 00010000b mov bl, ' ' push bx call draw_char
inc al loop d1 pop dx pop bx pop ax pop cx pop bp ret 2 ; # draw char to screen ; 0: x|y, 1: prop|char -> void draw_char: push bp mov bp, sp push ax push bx push si mov bx, [bp + 6] mov ah, 0 mov al, 0a0h mul bl ; set y mov bl, bh mov bh, 0 add bl, bl add bl, bl add ax, bx ; set x mov si, ax mov ax, [bp + 4] mov es:[si], ax ; set prop and char cmp al, ' ' jne jm0 mov es:[si + 2], ax ; for edge, double draw. jm0: ; for normal char pop si pop bx pop ax pop bp ret 4 ; # clear screen ; void -> void clear: mov ax, 3h int 10h ret code ends end start
|