⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1.c

📁 贪吃蛇
💻 C
字号:
#include"SNAKE.H"
#include "stdlib.h"

void ClearEnd(void);

void move(void);

void ReadCursor();

void ShowScore(int);

int score=0;

void main()
{

	int x=rand()%79,y=rand()%24;
//randomize();


ClearWindow(0,0,80,25,' ',0x70);
        ClearWindow(12,5,71,22,' ',0x00);
        ClearWindow(11,4,70,21,' ',0x10);
        WriteAt(5,16,"---------Welcome to the super snake game---------",0x1f);
		WriteAt(9,19,"This game is make by assembly language and BC ",0x2f);
		WriteAt(12,30,"MY name is Zhaoyang",0x1e);
        WriteAt(18,28,"Press any key to start",0x1e);
ClearEnd();
        ClearWindow(0,0,80,25,' ',0x10);
        GotoXY(x, y);
        PutChar(' ',0xe0);

        Delay(0xffff);
        move();
			while(!kbhit())
	{
        ClearEnd();
		ShowScore(score);

	}


}

void ShowScore(int score)
 {
  textcolor(10);
  gotoxy(60,8);
  cprintf("SCORE:");
  gotoxy(62,9);
  cprintf("%-5d",score);
 }



void ClearWindow(BYTE Left, BYTE Top, BYTE Right, BYTE Bottom,BYTE Char, BYTE Colour)
{
    asm{
                mov ax,0b800h
                mov es,ax

                mov al,Top
                mov bl,80
                mul bl

                mov dh,0
                mov dl,Left
                add ax,dx

                mov cl,Right
                sub bl,cl
                mov bh,0
                add dx,bx

                shl ax,1
                shl dx,1
                mov di,ax

                mov bl,Bottom
                sub bl,Top
                }
cls:
    asm{
                mov al,Right
                mov ah,Left
                mov cl,al
                sub cl,ah
                mov ch,0

                mov al,Char
                mov ah,Colour

                cld
                rep stosw
                add di,dx

                dec bl
                jnz cls

                }
}

void ClearEnd(void)
{
            asm{
                      mov ah,1
                      int 21h
              }
}

void WriteAt(BYTE X, BYTE Y,char *Str, BYTE Colour)
{
    BYTE Length=StrLen(Str);
      asm{
          mov ax,0B800h
          mov es,ax

          mov al,X
          mov dl,80
          mul dl

          mov dh,0
          mov dl,Y
          add ax,dx

          shl ax,1
          shl dx,1
          mov di,ax

          mov ah,Colour
          mov si,Str
          mov cl,Length
          mov ch,0
        }
write:
    asm{
          mov al,[si]
          mov es:[di],ax
          add di,2
          inc si
          loop write
        }
}

short StrLen(const char *Str)
{
    asm{
            mov bx,word ptr[bp+4]
            xor si,si
            dec si
        }
length:
    asm{
            inc si
            cmp byte ptr[bx+si],0
            jne length
            mov cx,si
        }
    return _CX;

}


void PutChar(char Char, char Colour)

{
          asm {

                      mov al,Char
                      mov bh,0
                      mov bl,Colour
                      mov cx,1
                      mov ah,9
                      int 10h

                      mov bh,0
                      mov ah,8
                      int 10h

              }
}

void GotoXY(BYTE x,BYTE y)
{

      asm{
                mov ah,2
                mov bh,0
                mov dl,x
                mov dh,y
                int 10h
          }

    PutChar(' ',0xe0);

}


void Delay(WORD NumTicks)
{ 
      asm{ 
                mov bx,0100h 
          } 
delay1: 
      asm{ 

                mov cx,NumTicks 
          } 
delay2: 
      asm{ 
                loop delay2 
                dec bx 
                jnz delay1 
        } 
} 

WORD GetKey() 
{ 
      asm{ 
                mov ah,0 
                int 16h 
        } 
      return _AH; 

} 


bool KbHit(void) 
{ 
      asm{      
                mov ah,1 
                int 16h 

                jz In 
                jmp NotIn 
        } 
In: 
                return true; 

NotIn: 
                return false; 
} 

WORD GetScreenChar() 
{ 
      asm{      
                mov ah,0fh 
                int 10h 

                mov ah,8 
                mov bh,0 
                int 10h 
        } 

      return _AL; 
} 

void ReadCursor() 
{ 
      asm{ 
                mov ah,0fh 
                int 10h 

                mov ah,3
                int 10h

        }
}

void move(void)
{
      char x=0,y=0;

      char i=0;

      char j=0;

begin0:
      if(KbHit()==true)
      asm{
                jmp begin1
        }

      else
      asm{
                jmp begin0
        }

begin1:

      i=GetKey();

      asm{
                xor dx,dx
                mov bh,i

                cmp bh,48h
                jne temp1
jmp Top
  }
temp1:

      asm{
                cmp bh,4bh
    jne temp2
jmp Left
        }
temp2:
      asm{
                cmp bh,4dh
                jne temp3
                jmp Right
        }

temp3:
      asm{
                cmp bh,50h
                jne begin1
                jmp Bottom
        }
Top:
      ReadCursor();

      asm{
                dec dh
                cmp dh,-1
                jne Top1
                jmp GameOver
        }
Top1:
      asm{
                mov x,dl
                mov y,dh
        }
      GotoXY(x,y);
      j=GetScreenChar();

      asm{
                mov bl,j
                cmp bl,0x0e

                jne  Top2
                jmp GameOver
        }

Top2:
      PutChar(0xdb,0x0e);

      Delay(0xffff);

      if(KbHit()==true)
      asm{
                jmp begin1
        }

      else
      asm{
                jmp GameOver
        }

Left:
      ReadCursor();

      asm{
                dec dl
                cmp dl,-1
                jne Left1
                jmp GameOver
        }
Left1:
      asm{
                mov x,dl
                mov y,dh
        }

      GotoXY(x,y);
      j=GetScreenChar();
      asm{
                mov bl,j
                cmp bl,0x0e

                jne  Left2
                jmp GameOver
        }

Left2:
      PutChar(0xdb,0x0e);

      Delay(0xffff);

      if(KbHit()==true)
      asm{
                jmp begin1
        }

      else
      asm{
                jmp GameOver
        }

Right:
    ReadCursor();

    asm{
                inc dl
                cmp dl,80
                jne Right1
                jmp GameOver
        }

Right1:
    asm{
                mov x,dl
                mov y,dh
        }

    GotoXY(x,y);
    j=GetScreenChar();


    asm{
                mov bl,j
                cmp bl,0x0e

                jne  Right2
                jmp GameOver
        }

Right2:
    PutChar(0xdb,0x0e);

    Delay(0xffff);

    if(KbHit()==true)
      asm{
                jmp begin1
        }

      else
      asm{
                jmp GameOver
        }

Bottom:
    ReadCursor();

    asm{

                inc dh
                cmp dh,25
                jne Bottom1
                jmp GameOver
        }

Bottom1:
    asm{
                mov x,dl
                mov y,dh
        }

    GotoXY(x,y); 
    j=GetScreenChar(); 
    asm{ 
                mov bl,j 
                mov bl,0x0e 

                jne  Bottom2 
                jmp  GameOver 
        } 

Bottom2: 
    PutChar(0xdb,0x0e); 

    Delay(0xffff); 

    if(KbHit()==true) 
      asm{ 
                jmp begin1 
        } 

      else 
      asm{ 
                jmp GameOver 
        } 
      
GameOver: 
      
      if(GameOver()==false) 
        
        asm{ 
                mov ah,4ch 
                int 21h 
          } 
        else 
                ClearWindow(0,0,80,25,' ',0x10); 
                GotoXY(rand()%80, rand()%25); 
                PutChar(' ',0xe0); 
                move(); 
        
    
} 

bool  GameOver() 
{  
      ClearWindow(18,10,52,15,' ',0x00); 
      ClearWindow(17,9,51,14,' ',0x70); 
      WriteAt(10,18,"Your score:0",0x7f); 
      WriteAt(12,18,"Press Q to quit or C to continue",0x7e); 

GameOver: 
        asm{ 
                mov ah,7 
                int 21h 
              
                cmp al,'q' 
                jne Not_Q 
                jmp FALSE 
          } 
Not_Q: 
        asm{ 
                cmp al,'Q' 
                jne Not_c 
                jmp FALSE 
          } 

Not_c: 
        asm{ 
                cmp al,'c' 
                jne Not_C 
                jmp TRUE 
          } 

Not_C: 
        asm{ 
                cmp al,'C' 
                jne GameOver 
                jmp TRUE 
          } 

TRUE: 

    return true; 

FALSE: 

    return false; 

} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -