汇编贪吃蛇

   1  DATAS SEGMENT 
   2          ;此处输入数据段代码 
   3          dw 0,0 
   4          snk db 1           ;蛇身的字符
   5          snake_tail2 db 2   ;蛇倒数第二节的字符
   6          food db 3          ;食物的字符
   7          snake_tail db 4    ;蛇尾巴的字符
   8          door_shape db 8    ;门的字符
   9          squ db 9           ;障碍的字符
  10          big_food db 15     ;大食物的字符
  11          blank_space db 32  ;空字符
  12          adrs db 5          ;读取的键盘数码 
  13          snake_length db ?  ;蛇长
  14          level db ?         ;关卡
  15          level_up_score db 4;过关条件 
  16          character db ?     ;读取的字符 
  17          addrs dw ?         ;蛇运动的方向
  18          food_row db ?      ;食物所在的行
  19          food_column db ?   ;食物所在列 
  20          hwrt db ? 
  21          gmov1 db'  #######       ###       ##   ##     ######## $'
  22          gmov2 db' #       #     #   #     #  # #  #    #        $'
  23          gmov3 db'#         #   #     #   #    #    #   #        $'
  24          gmov4 db'###########   #     #   #    #    #   #######  $'
  25          gmov5 db'#             #######   #    #    #   #        $'
  26          gmov6 db' #    ####    #     #   #    #    #   #        $'
  27          gmov7 db'  #      #    #     #   #    #    #   #        $'
  28          gmov8 db'   ######     #     #   #    #    #   ######## $'
  29          gmov9 db'                                               $'
  30      gmov10    db'  #####      #     #    #########    ######     $'
  31      gmov11    db' #     #     #     #    #            #     #    $'
  32     gmov12    db'#       #    #     #    #            #      #   $'
  33      gmov13    db'#       #    #     #    ########     #     #    $'
  34      gmov14    db'#       #    #     #    #            ######     $'
  35      gmov15    db'#       #     #   #     #            # ##       $'
  36      gmov16    db' #     #       # #      #            #   ##     $'
  37      gmov17    db'  #####         #       #########    #     ##   $'
  38                    
  39          xing db '# $'        
  40          gmov db 'press r to restart press q to quit $'  ;初始化信息 
  41          suc_msg db' You are successful going over all zhe levels $' 
  42          suc_msg1 db' Please press q to quit $' 
  43          score_msg db 'score :$' 
  44          score1 db 48              ;分数
  45          score2 db 48 
  46          writer db 'Developer: $'
  47          writer1 db 'Liu Gaoliang   $' 
  48          ID db 'ID: 13349068    $'
  49          level_msg db 'level: $' 
  50          msg1 db 'The way to play the game:$' 
  51          way db ' press w to up ,press s to down,press a to left,press d to right$' 
  52          msg db 'Press any key(except a,s,d,w) to start$' 
  53 DATAS ENDS 
  54 
  55 STACKS SEGMENT ;此处输入堆栈段代码 
  56     db 200 dup(0) 
  57 STACKS ENDS 
  58 
  59 CODES SEGMENT 
  60     ASSUME CS:CODES,DS:DATAS,SS:STACKS 
  61     
  62 START: 
  63     MOV AX,DATAS 
  64     MOV DS,AX ;此处输入代码段代码 
  65     mov ax,0 
  66     mov es,ax 
  67 ;******************************************** 
  68 ;清屏 
  69 clean_p macro 
  70     mov ah,6 
  71     mov al,0 
  72     mov ch,0 
  73     mov cl,0 
  74     mov dh,24 
  75     mov dl,79 
  76     mov bh,10 
  77     int 10h 
  78 endm 
  79 ;******************************************* 
  80 ;游戏左侧界面 
  81 firstrow macro 
  82     mov dh,2  ;score_msg的行
  83     mov dl,2  ;score_msg的列
  84     mov ah,2  ;int10h的功能号
  85     mov bh,0  ;页号  
  86     int 10h 
  87     mov ah,9
  88     lea dx,score_msg 
  89     int 21h 
  90     
  91     mov dh,3
  92     mov dl,2
  93     mov ah,2
  94     mov bh,0
  95     int 10h 
  96     mov ah,9 
  97     lea dx,level_msg 
  98     int 21h
  99     
 100     mov dh,5  ;writer的行
 101     mov dl,2  ;writer的列
 102     mov ah,2  ;int10h的功能号
 103     mov bh,0  ;页号
 104     int 10h 
 105     mov ah,9 
 106     lea dx,writer 
 107     int 21h 
 108     
 109     mov dh,6  ;writer的行
 110     mov dl,5  ;writer的列
 111     mov ah,2  ;int10h的功能号
 112     mov bh,0  ;页号
 113     int 10h 
 114     mov ah,9 
 115     lea dx,writer1 
 116     int 21h 
 117     
 118     mov dh,8
 119     mov dl,2
 120     mov ah,2
 121     mov bh,0
 122     int 10h
 123     mov ah,9 
 124     lea dx,ID 
 125     int 21h
 126     
 127     ;-----------------------------------------
 128     ;-------------------------------------------
 129     mov dh,0
 130     mov dl,19 
 131     mov ah,2 
 132     mov bh,0 
 133     int 10h 
 134     mov ah,9 
 135     lea dx,xing 
 136     int 21h
 137     
 138     mov dh,1
 139     mov dl,19 
 140     mov ah,2 
 141     mov bh,0 
 142     int 10h 
 143     mov ah,9 
 144     lea dx,xing 
 145     int 21h 
 146     
 147     mov dh,2
 148     mov dl,19 
 149     mov ah,2 
 150     mov bh,0 
 151     int 10h 
 152     mov ah,9 
 153     lea dx,xing 
 154     int 21h
 155     
 156     mov dh,3
 157     mov dl,19 
 158     mov ah,2 
 159     mov bh,0 
 160     int 10h 
 161     mov ah,9 
 162     lea dx,xing 
 163     int 21h
 164     
 165     mov dh,4
 166     mov dl,19 
 167     mov ah,2 
 168     mov bh,0 
 169     int 10h 
 170     mov ah,9 
 171     lea dx,xing 
 172     int 21h
 173     
 174     mov dh,5
 175     mov dl,19 
 176     mov ah,2 
 177     mov bh,0 
 178     int 10h 
 179     mov ah,9 
 180     lea dx,xing 
 181     int 21h
 182     
 183     mov dh,6
 184     mov dl,19 
 185     mov ah,2 
 186     mov bh,0 
 187     int 10h 
 188     mov ah,9 
 189     lea dx,xing 
 190     int 21h
 191     
 192     mov dh,7
 193     mov dl,19 
 194     mov ah,2 
 195     mov bh,0 
 196     int 10h 
 197     mov ah,9 
 198     lea dx,xing 
 199     int 21h
 200     
 201     mov dh,8
 202     mov dl,19 
 203     mov ah,2 
 204     mov bh,0 
 205     int 10h 
 206     mov ah,9 
 207     lea dx,xing 
 208     int 21h 
 209     
 210     mov dh,9
 211     mov dl,19 
 212     mov ah,2 
 213     mov bh,0 
 214     int 10h 
 215     mov ah,9 
 216     lea dx,xing 
 217     int 21h
 218     
 219     mov dh,10
 220     mov dl,19 
 221     mov ah,2 
 222     mov bh,0 
 223     int 10h 
 224     mov ah,9 
 225     lea dx,xing 
 226     int 21h
 227     
 228     mov dh,11
 229     mov dl,19 
 230     mov ah,2 
 231     mov bh,0 
 232     int 10h 
 233     mov ah,9 
 234     lea dx,xing 
 235     int 21h
 236     
 237     mov dh,12
 238     mov dl,19 
 239     mov ah,2 
 240     mov bh,0 
 241     int 10h 
 242     mov ah,9 
 243     lea dx,xing 
 244     int 21h
 245     
 246     mov dh,13
 247     mov dl,19 
 248     mov ah,2 
 249     mov bh,0 
 250     int 10h 
 251     mov ah,9 
 252     lea dx,xing 
 253     int 21h
 254     
 255     mov dh,14
 256     mov dl,19 
 257     mov ah,2 
 258     mov bh,0 
 259     int 10h 
 260     mov ah,9 
 261     lea dx,xing
 262     int 21h
 263     
 264     mov dh,15
 265     mov dl,19 
 266     mov ah,2 
 267     mov bh,0 
 268     int 10h 
 269     mov ah,9 
 270     lea dx,xing 
 271     int 21h
 272     
 273     mov dh,16
 274     mov dl,19 
 275     mov ah,2 
 276     mov bh,0 
 277     int 10h 
 278     mov ah,9 
 279     lea dx,xing 
 280     int 21h
 281     
 282     mov dh,17
 283     mov dl,19 
 284     mov ah,2 
 285     mov bh,0 
 286     int 10h 
 287     mov ah,9 
 288     lea dx,xing 
 289     int 21h
 290     
 291     mov dh,18
 292     mov dl,19 
 293     mov ah,2 
 294     mov bh,0 
 295     int 10h 
 296     mov ah,9 
 297     lea dx,xing 
 298     int 21h
 299     
 300     mov dh,19
 301     mov dl,19 
 302     mov ah,2 
 303     mov bh,0 
 304     int 10h 
 305     mov ah,9 
 306     lea dx,xing 
 307     int 21h
 308     
 309     mov dh,20
 310     mov dl,19 
 311     mov ah,2 
 312     mov bh,0 
 313     int 10h 
 314     mov ah,9 
 315     lea dx,xing
 316     int 21h
 317     
 318     mov dh,21
 319     mov dl,19 
 320     mov ah,2 
 321     mov bh,0 
 322     int 10h 
 323     mov ah,9 
 324     lea dx,xing 
 325     int 21h
 326     
 327     mov dh,22
 328     mov dl,19 
 329     mov ah,2 
 330     mov bh,0 
 331     int 10h 
 332     mov ah,9 
 333     lea dx,xing 
 334     int 21h
 335     
 336     mov dh,23
 337     mov dl,19 
 338     mov ah,2 
 339     mov bh,0 
 340     int 10h 
 341     mov ah,9 
 342     lea dx,xing 
 343     int 21h
 344     
 345     
 346     mov dh,24
 347     mov dl,19 
 348     mov ah,2 
 349     mov bh,0 
 350     int 10h 
 351     mov ah,9 
 352     lea dx,xing 
 353     int 21h
 354     
 355     mov dh,25
 356     mov dl,19 
 357     mov ah,2 
 358     mov bh,0 
 359     int 10h 
 360     mov ah,9 
 361     lea dx,xing 
 362     int 21h
 363     
 364     ;------------------------------------------
 365     ;-----------------------------------------
 366     
 367 endm 
 368 
 369 ;*****************************************  
 370 ;宏定义用于向当前光标处输出字符 
 371 write macro row,col,cnt,color
 372     push bx 
 373     push cx 
 374     push dx 
 375     mov dh,row 
 376     mov dl,col 
 377     mov ah,2 
 378     mov bh,0 
 379     int 10h 
 380     mov ah,9 
 381     mov bl,color 
 382     mov cx,1 
 383     lea di,cnt 
 384     mov al,[di] 
 385     int 10h 
 386     pop dx 
 387     pop cx 
 388     pop bx 
 389 endm 
 390 
 391 ;*************************************** 
 392 ;用于读出当前光标处字符的宏定义 
 393 read macro row,col 
 394     push dx 
 395     push ax 
 396     push bx 
 397     mov dh,row 
 398     mov dl,col 
 399     mov ah,2 
 400     mov bh,0 
 401     int 10h 
 402     mov ah,08h 
 403     int 10h 
 404     mov character,al 
 405     pop bx 
 406     pop ax 
 407     pop dx 
 408 endm 
 409 ;************************************************************ 
 410 ;宏定义只用在宏中当readcg的所有判断都不成立调用此宏 
 411 wnear macro 
 412 local wnext1 
 413 local wnext2 
 414 local wnext3 
 415 local wnext4 
 416     push dx 
 417     dec dh 
 418     read dh,dl 
 419     cmp character,1 
 420     jne wnext1 
 421     write dh,dl,snake_tail2,9
 422     jmp wnext4 
 423 wnext1: 
 424     inc dh 
 425     dec dl 
 426     read dh,dl 
 427     cmp character,1 
 428     jne wnext2 
 429     write dh,dl,snake_tail2,9
 430     jmp wnext4 
 431 wnext2: 
 432     inc dh 
 433     inc dl 
 434     read dh,dl 
 435     cmp character,1 
 436     jne wnext3 
 437     write dh,dl,snake_tail2,9
 438     jmp wnext4 
 439 wnext3: 
 440     dec dh 
 441     inc dl 
 442     read dh,dl 
 443     cmp character,1 
 444     jne wnext4 
 445     write dh,dl,snake_tail2,9 
 446 wnext4: 
 447     pop dx 
 448 endm 
 449 
 450 ;***************************************************** 
 451 ;宏定义用于改变判断出来的字符 
 452 ;实际就是实现蛇的前进过程 
 453 readcg macro row,col 
 454 local turn_up,turn_up1,turn_up2,turn_left,turn_left1,turn_left2,turn_down,turn_down1 ,turn_down2,turn_right,turn_right1,turn_right2,goout 
 455     push bx 
 456     push ax 
 457     push dx 
 458     ;row,col是倒数第二节蛇的行和列
 459     write dh,dl,snake_tail,9 
 460     dec row 
 461     read dh,dl 
 462     cmp character,4 
 463     jne turn_up1 
 464     jmp turn_up2 
 465 turn_up1: 
 466     jmp turn_up 
 467 turn_up2: 
 468     write dh,dl,blank_space,0 
 469     inc dh 
 470     inc dh 
 471     read dh,dl 
 472     cmp character,1 
 473     jne turn_up 
 474     write dh,dl,snake_tail2,9 
 475     jmp near ptr goout 
 476 turn_up: 
 477     pop dx 
 478     push dx 
 479     dec col 
 480     read dh,dl 
 481     cmp character,4 
 482     jne turn_left1 
 483     jmp turn_left2 
 484 turn_left1: 
 485     jmp near ptr turn_left 
 486 turn_left2: 
 487     write dh,dl,blank_space,0 
 488     inc dl 
 489     inc dl 
 490     read dh,dl 
 491     cmp character,1 
 492     jne turn_left 
 493     write dh,dl,snake_tail2,9 
 494     jmp near ptr goout 
 495 turn_left: 
 496     pop dx 
 497     push dx 
 498     inc row 
 499     read dh,dl 
 500     cmp character,4 
 501     jne turn_down1 
 502     jmp turn_down2 
 503 turn_down1: 
 504     jmp near ptr turn_down 
 505 turn_down2: 
 506     write dh,dl,blank_space,0 
 507     dec dh 
 508     dec dh 
 509     read dh,dl 
 510     cmp character,1 
 511     jne turn_down 
 512     write dh,dl,snake_tail2,9 
 513     jmp near ptr goout 
 514 turn_down: 
 515     pop dx 
 516     push dx 
 517     inc col 
 518     read dh,dl 
 519     cmp character,4 
 520     jne turn_right1 
 521     jmp turn_right2 
 522 turn_right1: 
 523     jmp near ptr turn_right 
 524 turn_right2: 
 525     write dh,dl,blank_space,0 
 526     dec dl 
 527     dec dl 
 528     read dh,dl 
 529     cmp character,1 
 530     jne turn_right 
 531     write dh,dl,snake_tail2,9 
 532     jmp goout 
 533 turn_right: 
 534     pop dx 
 535     push dx 
 536 wnear 
 537 goout:
 538     pop dx 
 539     pop ax 
 540     pop bx 
 541 endm 
 542 ;********************************************* 
 543 ;画一条初始蛇 
 544 creat_snake macro 
 545 local wrt 
 546     mov addrs,offset turnright 
 547     mov dh,10 
 548     mov dl,21 
 549     mov cx,3 
 550     write dh,dl,snake_tail,9
 551     inc dl 
 552     write dh,dl,snake_tail2,9 
 553 wrt: 
 554     inc dl 
 555     write dh,dl,snk,9
 556     loop wrt 
 557 endm 
 558 ;****************************************************** 
 559 ;画障碍方块的宏定义 
 560 creat_square macro square_row,square_col,row,col 
 561 local square1,square2,cycle1
 562     mov dh, row 
 563     mov dl, col 
 564     mov cx ,square_row 
 565 square1: 
 566     push cx 
 567     mov cx,square_col 
 568 square2: 
 569     write dh,dl,squ,5 
 570     inc dl 
 571 cycle1: 
 572     loop square2 
 573     sub dl,square_col 
 574     inc dh 
 575     pop cx 
 576     loop square1 
 577 endm 
 578 
 579 ;*********************************************************8 
 580 ;程序开始运行的初始界面 
 581     mov dh,10    ;设置光标位置
 582     mov dl,26 
 583     mov ah,2 
 584     mov bh,0 
 585     int 10h 
 586     mov ah,9     ;显示字符串
 587     lea dx,msg1 
 588     int 21h 
 589     
 590     mov dh,11    ;设置光标位置 16 
 591     mov dl,7 
 592     mov ah,2 
 593     mov bh,0 
 594     int 10h 
 595     mov ah,9       ;显示字符串
 596     lea dx,way 
 597     int 21h 
 598     
 599     mov dh,12      ;设置光标位置 
 600     mov dl,20 
 601     mov ah,2 
 602     mov bh,0 
 603     int 10h 
 604     mov ah,9       ;显示字符串
 605     lea dx,msg 
 606     int 21h 
 607 
 608     mov ah,0       ;读字符 任意!
 609     int 16h 
 610     push es:[9*4]    ;将原int9入口地址保存 
 611     pop ds:[0] 
 612     push es:[9*4+2] 
 613     pop ds:[2] 
 614     mov word ptr es:[9*4],offset int9 ;更改中断向量表 
 615     mov es:[9*4+2],cs  
 616     jmp begin_snake 
 617 ;********************************* 
 618 ;当蛇碰壁或自身转到此游戏结束 
 619 game_fail: 
 620     clean_p ;清屏
 621  
 622     mov dh,1
 623     mov dl,17 
 624     mov ah,2 
 625     mov bh,0 
 626     int 10h 
 627     mov ah,9 
 628     lea dx,gmov1 
 629     int 21h 
 630     
 631     mov dh,2
 632     mov dl,17 
 633     mov ah,2 
 634     mov bh,0 
 635     int 10h 
 636     mov ah,9 
 637     lea dx,gmov2 
 638     int 21h
 639     
 640     mov dh,3
 641     mov dl,17 
 642     mov ah,2 
 643     mov bh,0 
 644     int 10h 
 645     mov ah,9 
 646     lea dx,gmov3 
 647     int 21h
 648     
 649     mov dh,4
 650     mov dl,17 
 651     mov ah,2 
 652     mov bh,0 
 653     int 10h 
 654     mov ah,9 
 655     lea dx,gmov4 
 656     int 21h
 657     
 658     mov dh,5
 659     mov dl,17 
 660     mov ah,2 
 661     mov bh,0 
 662     int 10h 
 663     mov ah,9 
 664     lea dx,gmov5 
 665     int 21h
 666     
 667     mov dh,6
 668     mov dl,17 
 669     mov ah,2 
 670     mov bh,0 
 671     int 10h 
 672     mov ah,9 
 673     lea dx,gmov6 
 674     int 21h
 675     
 676     mov dh,7
 677     mov dl,17 
 678     mov ah,2 
 679     mov bh,0 
 680     int 10h 
 681     mov ah,9 
 682     lea dx,gmov7 
 683     int 21h
 684     
 685     mov dh,8
 686     mov dl,17 
 687     mov ah,2 
 688     mov bh,0 
 689     int 10h 
 690     mov ah,9 
 691     lea dx,gmov8 
 692     int 21h 
 693     
 694     mov dh,9
 695     mov dl,17 
 696     mov ah,2 
 697     mov bh,0 
 698     int 10h 
 699     mov ah,9 
 700     lea dx,gmov9 
 701     int 21h
 702     
 703     mov dh,10
 704     mov dl,17 
 705     mov ah,2 
 706     mov bh,0 
 707     int 10h 
 708     mov ah,9 
 709     lea dx,gmov10 
 710     int 21h
 711     
 712     mov dh,11
 713     mov dl,17 
 714     mov ah,2 
 715     mov bh,0 
 716     int 10h 
 717     mov ah,9 
 718     lea dx,gmov11 
 719     int 21h
 720     
 721     mov dh,12
 722     mov dl,17 
 723     mov ah,2 
 724     mov bh,0 
 725     int 10h 
 726     mov ah,9 
 727     lea dx,gmov12 
 728     int 21h
 729     
 730     mov dh,13
 731     mov dl,17 
 732     mov ah,2 
 733     mov bh,0 
 734     int 10h 
 735     mov ah,9 
 736     lea dx,gmov13 
 737     int 21h
 738     
 739     mov dh,14
 740     mov dl,17 
 741     mov ah,2 
 742     mov bh,0 
 743     int 10h 
 744     mov ah,9 
 745     lea dx,gmov14 
 746     int 21h
 747     
 748     mov dh,15
 749     mov dl,17 
 750     mov ah,2 
 751     mov bh,0 
 752     int 10h 
 753     mov ah,9 
 754     lea dx,gmov15 
 755     int 21h
 756     
 757     mov dh,16
 758     mov dl,17 
 759     mov ah,2 
 760     mov bh,0 
 761     int 10h 
 762     mov ah,9 
 763     lea dx,gmov16 
 764     int 21h
 765     
 766     mov dh,17
 767     mov dl,17 
 768     mov ah,2 
 769     mov bh,0 
 770     int 10h 
 771     mov ah,9 
 772     lea dx,gmov17 
 773     int 21h
 774     ;------------------------------------------
 775     ;-----------------------------------------
 776     
 777     mov dh,19
 778     mov dl,17 
 779     mov ah,2 
 780     mov bh,0 
 781     int 10h 
 782     mov ah,9 
 783     lea dx,gmov 
 784     int 21h 
 785     
 786     mov ax,0        ;恢复int9中断
 787     mov es,ax 
 788     push ds:[0] 
 789     pop es:[9*4] 
 790     push ds:[2] 
 791     pop es:[9*4+2] 
 792 stop: 
 793     mov ah,0 
 794     int 16h 
 795     cmp al,'r' 
 796     je restar 
 797     jmp quit 
 798 restar: 
 799     jmp near ptr start 
 800 quit: 
 801     cmp al,'q' 
 802     jne stop 
 803     jmp near ptr exit 
 804 ;*********************************************** 
 805 ;关卡升级 
 806 level_up: 
 807     inc level 
 808     cmp level,50 
 809     je  new1
 810     cmp level,51 
 811     je  new2
 812     cmp level,52 
 813     je  new3
 814     cmp level,53 
 815     je  new4
 816 
 817 new1:
 818     jmp level_1
 819 new2:
 820     jmp level_2 
 821 new3:
 822     jmp level_3 
 823 new4:
 824     jmp succssul_exit
 825 ;************************************************ 
 826 ;从这开始产生最原始的蛇 
 827 begin_snake: 
 828     clean_p 
 829     firstrow 
 830     creat_snake 
 831     mov snake_length,0 
 832     mov ax,0 
 833     mov score1,48 
 834     mov score2,48 
 835     mov level,49 
 836     call creat_food 
 837     write 2,9,level,5 
 838     jmp snake_run 
 839 level_1:
 840     CLEAN_P 
 841     FIRSTROW 
 842     write 2,9,level,5 
 843     creat_square 13,4,7,40 
 844     creat_snake 
 845     mov snake_length,0 
 846     jmp level_back 
 847 
 848 level_2: 
 849     CLEAN_P 
 850     FIRSTROW 
 851     write 2,9,level,5 
 852     creat_square 7,20,0,20 
 853     creat_square 7,20,18,20 
 854     creat_square 7,20,0,60 
 855     creat_square 7,20,18,60 
 856     creat_snake 
 857     mov snake_length,0 
 858     jmp level_back 
 859 
 860 level_3: 
 861     CLEAN_P 
 862     FIRSTROW 21 
 863     write 2,9,level,5 
 864     creat_square 13,4,7,20 
 865     creat_square 13,4,7,60 
 866     creat_square 2,22,6,30 
 867     creat_square 2,22,20,30 
 868     creat_snake 
 869     mov snake_length,0 
 870     jmp level_back 
 871 ;************************************************** 
 872 ;************************************************* 
 873 ;此处蛇行走过程的无限循环,进入后也是在此一直循环 
 874 snake_run: 
 875     call dly 
 876 ;*********************************************** 
 877 ;判断进关卡门 
 878     push dx 
 879     lea ax,turnright 
 880     cmp addrs,ax    ; 先判断,蛇前进的方向,
 881     jne cgn_door2  ;再判断蛇头前面的是否是门
 882     inc dl 
 883     read dh,dl 
 884     cmp character,8 
 885     je cgn_door1 
 886     jmp cgn_door2 
 887 cgn_door1: 
 888     jmp level_up 
 889 cgn_door2: 
 890     pop dx 
 891     push dx 
 892     lea ax,turnup 
 893     cmp addrs,ax 
 894     jne cgn_door4 
 895     dec dh 
 896     read dh,dl 
 897     cmp character,8 
 898     je cgn_door3 
 899     jmp cgn_door4 
 900 cgn_door3: 
 901     jmp level_up 
 902 cgn_door4: 
 903     pop dx 
 904     push dx 
 905     lea ax,turndown 
 906     cmp addrs,ax 
 907     jne cgn_door6 
 908     inc dh 
 909     read dh,dl 
 910     cmp character,8 
 911     je cgn_door5 
 912     jmp cgn_door6 
 913 cgn_door5: 
 914     jmp level_up 
 915 cgn_door6: 
 916     pop dx 
 917     push dx 
 918     lea ax,turnleft 
 919     cmp addrs,ax 
 920     jne cgn_door8 
 921     dec dl 
 922     read dh,dl 
 923     cmp character,8 
 924     je cgn_door7 
 925     jmp cgn_door8 
 926 cgn_door7: 
 927     jmp level_up 
 928 cgn_door8: 
 929     pop dx 
 930 ;********************************** 
 931 ;判断是否碰墙 
 932     push dx 
 933     inc dh 
 934     cmp dh,25 
 935     je tonxt1
 936     inc dl 
 937     cmp dl,80 
 938     je tonxt1 
 939     pop dx 
 940     push dx 
 941     dec dh 
 942     cmp dh,0 
 943     je tonxt1 
 944     dec dl 
 945     cmp dl,18 
 946     je tonxt1 
 947     pop dx 
 948 ;判断是否碰自己,障碍 
 949 ;有判断前面那个是什么, 
 950 ;分上下左右4个情况 
 951     push dx 
 952     lea ax,turnright 
 953     cmp addrs,ax 
 954     jne tonxt2 
 955     inc dl 
 956     read dh,dl 
 957     cmp character,1 
 958     je tonxt1
 959     cmp character,2 
 960     je tonxt1 
 961     cmp character,4 
 962     je tonxt1 
 963     cmp character,9 
 964     je tonxt1 
 965     jmp tonxt2 
 966 tonxt1: 
 967     jmp game_fail 
 968 tonxt2: 
 969     pop dx
 970     push dx 
 971     lea ax,turnup 
 972     cmp addrs,ax 
 973     jne tonxt4 
 974     dec dh 
 975     read dh,dl 
 976     cmp character,1 
 977     je tonxt3 
 978     cmp character,2 
 979     je tonxt3 
 980     cmp character,4 
 981     je tonxt3 
 982     cmp character,9 
 983     je tonxt3 
 984     jmp tonxt4 
 985 tonxt3: 
 986     jmp game_fail 
 987 tonxt4: 
 988     pop dx 
 989     push dx 
 990     lea ax,turndown 
 991     cmp addrs,ax 
 992     jne tonxt6 
 993     inc dh 
 994     read dh,dl 
 995     cmp character,1 
 996     je tonxt5 
 997     cmp character,2 
 998     je tonxt5 
 999     cmp character,4 
1000     je tonxt5 
1001     cmp character,9 
1002     je tonxt5 
1003     jmp tonxt6 
1004 tonxt5: 
1005     jmp game_fail 
1006 tonxt6: 
1007     pop dx 
1008     push dx 
1009     lea ax,turnleft 
1010     cmp addrs,ax 
1011     jne tonxt8 
1012     dec dl 
1013     read dh,dl 
1014     cmp character,1 
1015     je tonxt7 
1016     cmp character,2 
1017     je tonxt7 
1018     cmp character,4 
1019     je tonxt7 
1020     cmp character,9 
1021     je tonxt7 
1022     jmp tonxt8 
1023 tonxt7: 
1024     jmp game_fail 
1025 tonxt8: 
1026     pop dx 
1027     jmp nexta ;再去判断前面的是否是食物 
1028 crtf1: 
1029     call addone 
1030 crtf: 
1031     push dx 
1032     push cx 
1033     push ax 
1034     mov al,level_up_score 
1035     cmp snake_length,al ;达到条件
1036     jb level_back       ;创建一个门
1037     call creat_door 
1038     pop ax 
1039     pop cx 
1040     pop dx 
1041 level_back: 
1042     call creat_food 
1043 nexta: 
1044     mov ah,food_row 
1045     mov al,food_column 
1046     cmp ax,dx 
1047     je crtf12 
1048     jmp crtf13 
1049 crtf12: 
1050     jmp crtf1 
1051 crtf13:  
1052     push dx 
1053     cmp score2,58 
1054     jb normal 
1055     sub score2,10 
1056      inc score1 
1057      normal: 
1058      mov dh,1 
1059      mov dl,9 
1060      write dh,dl,score1,4
1061      add dl,1 
1062      write dh,dl,score2,4
1063      pop dx 
1064      cmp adrs,17 
1065      je jmp1 
1066      cmp adrs,145 
1067      je jmp1 
1068      cmp adrs,31 
1069      je jmp2 
1070      cmp adrs,159 
1071      je jmp2 
1072      cmp adrs,32 
1073      je jmp3 
1074      cmp adrs,160 
1075      je jmp3 
1076      cmp adrs,30 
1077      je jmp4 
1078      cmp adrs,158 
1079      je jmp4 
1080      jmp addrs 
1081 jmp1: 
1082      lea ax, turndown 
1083      cmp ax,addrs 
1084      je jmp2 
1085      mov addrs,offset turnup 
1086      jmp near ptr turnup 
1087 jmp2: 
1088      lea ax,turnup 
1089      cmp ax,addrs 
1090      je jmp1 
1091      mov addrs,offset turndown 
1092      jmp near ptr turndown 
1093 jmp3: 
1094      lea ax,turnleft 
1095      cmp ax,addrs 
1096      je jmp4 
1097      mov addrs,offset turnright 
1098      jmp near ptr turnright 
1099 jmp4: 
1100      lea ax,turnright 
1101      cmp ax,addrs 
1102      je jmp3 
1103      mov addrs,offset turnleft 
1104      jmp near ptr turnleft 
1105 ;*********************************** 
1106 ;此处实现蛇向左走 
1107 
1108 turnright: 
1109     push dx 
1110     mov dh,1
1111     mov dl,0 
1112     mov cx,23 
1113 cmpr1: 
1114     push cx 
1115     mov cx,79 
1116 cmpr2: 
1117     read dh,dl 
1118     cmp character,2 
1119     je nextr4 
1120     jmp near ptr nextr 
1121 nextr4: 
1122     readcg dh,dl 
1123     pop cx 
1124     jmp near ptr endr 
1125 nextr: 
1126     inc dl 
1127     jmp nextr2 
1128 chgr2: 
1129     jmp near ptr cmpr2 
1130 nextr2: 
1131     loop chgr2 
1132     sub dl,79 
1133     inc dh 
1134     pop cx 
1135     jmp nextr1 
1136 chgr1: 
1137     jmp near ptr cmpr1 
1138 nextr1: 
1139     loop chgr1 
1140 endr: 
1141     pop dx 
1142     inc dl 
1143     write dh,dl,snk,9 ;食物变蛇,实现前进 
1144     jmp near ptr snake_run 
1145     
1146 ;********************************* 
1147 ;此处实现蛇向上走 
1148 turnup: 
1149     push dx 
1150     mov dh,1 
1151     mov dl,0 
1152     mov cx,23 
1153 cmpu1: 
1154     push cx 
1155     mov cx,79 
1156     
1157 cmpu2: 
1158     read dh,dl 
1159     cmp character,2 
1160     jne nextu3 
1161     jmp nextu4 
1162 nextu3: 
1163     jmp near ptr nextu 
1164 nextu4: 
1165     readcg dh,dl 
1166     pop cx 
1167     jmp near ptr endu
1168 nextu: 
1169     inc dl 
1170     jmp nextu2 
1171 chgu2: 
1172     jmp near ptr cmpu2 
1173 nextu2: 
1174     loop chgu2 
1175     sub dl,79 
1176     inc dh 
1177     pop cx 
1178     jmp nextu1 
1179 chgu1: 
1180     jmp near ptr cmpu1 
1181 nextu1: 
1182     loop chgu1 
1183 endu: 
1184     pop dx 
1185     dec dh 
1186     write dh,dl,snk,9 
1187     jmp near ptr snake_run 
1188 ;************************************* 
1189 ;此处实现蛇向下走 
1190 turndown: 
1191     push dx 
1192     mov dh,1 
1193     mov dl,0 
1194     mov cx,23 
1195 cmpd1: 
1196     push cx 
1197     mov cx,79 
1198 cmpd2: 
1199     read dh,dl 
1200     cmp character,2 
1201     jne nextd3
1202     jmp nextd4 
1203 nextd3: 
1204     jmp near ptr nextd 
1205 nextd4: 
1206     readcg dh,dl 
1207     pop cx 
1208     jmp near ptr endd 
1209 nextd: 
1210     inc dl 
1211     jmp nextd2 
1212 chgd2: 
1213     jmp near ptr cmpd2 
1214 nextd2: 
1215     loop chgd2 
1216     sub dl,79 
1217     inc dh 
1218     pop cx 
1219     jmp nextd1 
1220 chgd1: 
1221     jmp near ptr cmpd1 
1222 nextd1: 
1223     loop chgd1 
1224 endd: 
1225     pop dx 
1226     inc dh 
1227     write dh,dl,snk,9 
1228     jmp near ptr snake_run 
1229 ;********************************************** 
1230 ;此处实现蛇向右走 
1231 turnleft: 
1232     push dx 
1233     mov dh,1 
1234     mov dl,0 
1235     mov cx,23 
1236 cmpb1: 
1237     push cx 
1238     mov cx,79 
1239 cmpb2: 
1240     read dh,dl 
1241     cmp character,2 
1242     jne nextb3 
1243     jmp nextb4 
1244 nextb3: 
1245     jmp near ptr nextb 
1246 nextb4: 
1247     readcg dh,dl 
1248     pop cx 
1249     jmp near ptr endb 
1250 nextb: 
1251     inc dl 
1252     jmp nextb2 
1253 chgb2: 
1254     jmp near ptr cmpb2 
1255 nextb2: 
1256     loop chgb2 
1257     sub dl,79 
1258     inc dh 
1259     pop cx 
1260     jmp nextb1 
1261 chgb1: 
1262     jmp near ptr cmpb1
1263 nextb1: 
1264      loop chgb1 
1265 endb: 
1266     pop dx 
1267     dec dl 
1268     write dh,dl,snk,9 
1269     jmp near ptr snake_run 
1270 ;******************************** 
1271 ;过关成功,从此退出 
1272 succssul_exit: 
1273     mov ah,6 
1274     mov al,0 
1275     mov ch,0 
1276     mov cl,0 
1277     mov dh,24 
1278     mov dl,79 
1279     mov bh,7 
1280     int 10h 
1281     mov dh,17 
1282     mov dl,17 
1283     mov ah,2 
1284     mov bh,0 
1285     int 10h 
1286     mov ah,9 
1287     lea dx,suc_msg 
1288     int 21h 
1289     
1290     mov dh,19 
1291     mov dl,17 
1292     mov ah,2 
1293     mov bh,0 
1294     int 10h 
1295     mov ah,9 
1296     lea dx,suc_msg1 
1297     int 21h 
1298     
1299     ;恢复int9中断 
1300     mov ax,0 
1301     mov es,ax 
1302     push ds:[0] 
1303     pop es:[9*4] 
1304     push ds:[2] 
1305     pop es:[9*4+2] 
1306 stop1: 
1307     mov ah,0 
1308     int 16h 
1309     cmp al,'q' 
1310     jne stop1 
1311     jmp near ptr exit
1312 ;************************* 
1313 ;退出时,恢复int9中断 
1314 exit: 
1315     mov ax,0 
1316     mov es,ax 
1317     push ds:[0] 
1318     pop es:[9*4] 
1319     push ds:[2] 
1320     pop es:[9*4+2] 
1321     mov ah,4ch 
1322     int 21h 
1323 ;**************************** 
1324 ;更改后的中断服务程序 
1325 int9: 
1326     push ax 
1327     in al,60h 
1328     mov adrs,al 
1329     mov al,20h 
1330     out 20h,al 
1331     pop ax 
1332     iret 
1333 ;********************************************* 
1334 ;延时子程序 
1335 DLY PROC near 
1336     PUSH CX 
1337     PUSH DX 
1338     MOV DX,50
1339 DL1:  
1340     MOV CX,9801 
1341 DL2:   
1342     LOOP DL2 
1343     DEC DX 
1344     JNZ DL1 
1345     POP DX 
1346     POP CX 
1347     RET 
1348 DLY ENDP 
1349 ;*********************************************** 
1350 ;创建过关的门 
1351 creat_door proc 
1352     mov dh,10 
1353     mov dl, 78 
1354     mov cx ,4 
1355 square1: 
1356     push cx 
1357     mov cx,2 
1358 square2: 
1359     write dh,dl,door_shape ,13 
1360     inc dl 
1361     
1362 cycle1: 
1363     loop square2 
1364     sub dl,2 
1365     inc dh 
1366     pop cx 
1367     loop square1 
1368     ret 
1369 creat_door endp 
1370 ;************************************************ 
1371 ;将蛇身增加一 
1372 addone proc 
1373     call music 
1374     push dx 
1375     inc score2 
1376     cmp snake_length,6 
1377     jb normal_score 
1378     inc score2 
1379 normal_score: 
1380     inc snake_length 
1381     mov dh,1 
1382     mov dl,0 
1383     mov cx,23 
1384 cmpad1: 
1385     push cx 
1386     mov cx,79 
1387 cmpad2: 
1388     read dh,dl 
1389     cmp character,2 
1390     jne nextad3 
1391     jmp nextad4 
1392 nextad3: 
1393     jmp near ptr nextad 
1394 nextad4: 
1395     write dh,dl,snk,9 
1396     dec dh 
1397     read dh,dl 
1398     ;移动1位 
1399     cmp character,4 
1400     jne natup 
1401     write dh,dl,snake_tail2,9 
1402     dec dh 
1403     write dh,dl,snake_tail,9 
1404     jmp outo 
1405 natup:                  ;上下左右四种情况 
1406     inc dh 
1407     dec dl 
1408     read dh,dl 
1409     cmp character,4 
1410     jne natlf  
1411     write dh,dl,snake_tail2,9 
1412     dec dl 
1413     write dh,dl,snake_tail,9 
1414     jmp outo 
1415 natlf: 
1416     inc dh 
1417     inc dl 
1418     read dh,dl 
1419     cmp character,4 
1420     jne natdn 
1421     write dh,dl,snake_tail2,9 
1422     inc dh 
1423     write dh,dl,snake_tail,9 
1424     jmp outo 
1425 natdn: 
1426     dec dh 
1427     inc dl 
1428     read dh,dl 
1429     cmp character,4 
1430     jne natrt 
1431     write dh,dl,snake_tail2,9 
1432     inc dl 
1433     write dh,dl,snake_tail,9 
1434 natrt: 
1435 outo: 
1436     pop cx 
1437     jmp near ptr endad 
1438 nextad: 
1439     inc dl 
1440     jmp nextad2 
1441 chgad2: 
1442     jmp near ptr cmpad2 
1443 nextad2: 
1444     loop chgad2 
1445     sub dl,79 
1446     inc dh 
1447     pop cx 
1448     jmp nextad1 
1449 chgad1: 
1450     jmp near ptr cmpad1 
1451 nextad1: 
1452     loop chgad1 
1453 endad: 
1454     pop dx  
1455     ret 
1456 addone endp 
1457 ;****************************************************** 
1458 ;创建食物的子程序 
1459 creat_food proc 
1460     call rand1 
1461     call rand2 
1462     mov ah,food_row 
1463     mov al,food_column 
1464 re_creat_food: 
1465     mov food_row,ah 
1466     mov food_column,al 
1467     push dx 
1468     mov dh,1 
1469     mov dl,0 
1470     push cx 
1471     mov cx,23 
1472 check1: 
1473     push cx 
1474     mov cx,79 
1475 check2: 
1476     read dh,dl 
1477     cmp character,1 
1478     je nextn 
1479     cmp character,2 
1480     je nextn 
1481     cmp character,4 
1482     je nextn 
1483     cmp character,5 
1484     je nextn 
1485     cmp character,9 
1486     je nextn 
1487     jmp nextnn 
1488 nextn: 
1489     cmp ax,dx 
1490     jne nextnn 
1491     inc ah 
1492     inc al 
1493     cmp ah,22 
1494     jb go1 
1495     sub ah,21 
1496 go1: 
1497     cmp al,78 ;食物位置的限制
1498     jb go2 
1499     sub al,77 
1500 go2: 
1501     pop cx 
1502     pop cx 
1503     pop dx 
1504     jmp re_creat_food 
1505 
1506 nextnn: 
1507     inc dl 
1508     loop check2 
1509     inc dh 
1510     sub dl,79 
1511     pop cx 
1512     loop check1 
1513     pop cx 
1514     pop dx 
1515     cmp snake_length,5;length大于5 产生大食物 
1516     ja bigfood 
1517     write AH,AL,food,4 
1518     jmp normalfood 
1519 bigfood:
1520     write AH,AL,big_food,4 
1521     jmp normalfood 
1522 normalfood: 
1523     ret 
1524 creat_food endp 
1525 ;******************************************************* 
1526 ;随机数产生食物的行,列 
1527 RAND1 PROC 
1528     PUSH CX 
1529     PUSH DX 
1530     PUSH AX 
1531     STI 
1532     MOV AH,0 
1533     INT 1AH 
1534     MOV AX,DX 
1535     AND AH,3 
1536     MOV DL,21        ;除21  ,产生0~20余数
1537     DIV DL 
1538     add ah,2 
1539     MOV food_row,AH  ;余数存food_row,作随机行数 
1540     POP AX 
1541     POP DX  
1542     POP CX 
1543     RET 
1544 RAND1 ENDP 
1545 
1546 RAND2 PROC 
1547     PUSH CX 
1548     PUSH DX 
1549     PUSH AX 
1550     STI 
1551     MOV AH,0 
1552     INT 1AH 
1553     MOV AX,DX 
1554     AND AH,3 
1555     MOV DL,57              ;除57,产生0~57余数
1556     DIV DL 
1557     inc ah 
1558     add ah,20
1559     MOV food_column,AH     ;余数存food_column,作随机列数
1560     POP AX 
1561     POP DX 
1562     POP CX 
1563     RET 
1564 RAND2 ENDP     
1565 ;********************************************************************* 
1566 ;声音程序 
1567 music proc 
1568     push cx 
1569     push di 
1570     mov cx,10 
1571 freq: 
1572     mov di ,600 
1573     call gensound 
1574     loop freq 
1575 end_mus: 
1576     pop di 
1577     pop cx 
1578     ret 
1579 music endp 
1580 
1581 gensound proc 
1582     push ax 
1583     push bx 
1584     push cx 
1585     push dx 
1586     push di 
1587     mov al,0b6h 
1588     out 43h,al 
1589     mov dx,12h 
1590     mov ax,348ch 
1591     div di 
1592     out 42h,al 
1593     mov al,ah 
1594     out 42h,al 
1595     in al,61h 
1596     mov ah,al 
1597     or al,3 
1598     out 61h,al 
1599 wait1: 
1600     mov cx,400 
1601 delay: 
1602     loop delay 
1603     dec dx 
1604     jnz wait1 
1605     mov al,ah 
1606     out 61h,al 
1607     pop di 
1608     pop dx 
1609     pop cx 
1610     pop bx 
1611     pop ax 
1612     ret 
1613 gensound endp 
1614 
1615 CODES ENDS 
1616     END START 
汇编贪吃蛇

masm版本

原文地址:https://www.cnblogs.com/liugl7/p/4584760.html