程序名 : 贪吃蛇
程序作者: Softgsy
编程时间: 2008-04-19
修改时间: 2008-04-2304-2504-26
*****************************************************************
___________________________________________________文档__________
函数功能:
Protect和Recover : 保护和恢复寄存器现场
Display : 显示蛇头的'*'和用' '覆盖旧蛇尾的'*'
Curser : 将光标置于方框外
Move : 移动蛇,并判断蛇长是否已经满15,并置al:ffh
nextstep : 蛇移动一步
judge : 判断蛇的前方是什么(al作接口):
00h:食物 0fh:正常 f0h:障碍
food : 置食物
_________________________________________________________________
include macro_snake.inc
*********************************************数据段定义**********
Data segment
snake dw 0101h,0102h,0103h,13 dup(0000h)
graph db 9 dup('#'),0dh,0ah,'#***#',0dh,0ah
db 11 dup('#',7 dup(' '),'#',0dh,0ah)
db 9 dup('#'),0dh,0ah,'$'
win db 'You Win!',0dh,0ah,'$'
lose db 'What a pity!',0dh,0ah,'$'
direction dw 0064h
speed1 equ 1000h
speed2 equ 0100h
Data ends
*********************************************堆栈段定义**********
Stack segment stack
dw 0ffh dup(?)
Stack ends
*********************************************附加段定义**********
Extra segment
snake_food dw 0b06h,0703h,0c01h,0204h,0606h,0a02h,0405h,0905h
dw 0101h,0c04h,0502h,0207h,0c07h,0604h,0c02h,0304h
Extra ends
*********************************************代码段定义**********
Code segment
assume cs:Code,ds:Data,ss:Stack,es:Extra
*********************************************主程序定义**********
start:
mov ax,Data
mov ds,ax
mov ax,Stack
mov ss,ax
mov ax,Extra
mov es,ax
mov dx,offset graph
mov ah,09h
int 21h
mov bx,0000h 蛇尾位置及bh页号
mov si,0002h 蛇头位置
mov di,offset snake_food
call food
xor ax,ax
outloop:
cmp al,0ffh
jz gamewin
cmp al,0f0h
jz gamelose
call nextstep
jmp outloop
gamewin:
mov dx,offset win
mov ah,09h
int 21h
jmp gameover
gamelose:
mov dx,offset lose
mov ah,09h
int 21h
gameover:
mov ax,4c00h
int 21h
*********************************************子程序定义**********
_____________________________________________向前走一步__________
nextstep proc near
input:
get_direction direction,speed1,speed2
mov ax,direction
cmp al,'a'
jz m_l
cmp al,'A'
jz m_l
cmp ah,4bh
jz m_l
cmp al,'d'
jz m_r
cmp al,'D'
jz m_r
cmp ah,4dh
jz m_r
cmp al,'w'
jz m_u
cmp al,'W'
jz m_u
cmp ah,48h
jz m_u
cmp al,'s'
jz m_d
cmp al,'S'
jz m_d
cmp ah,50h
jz m_d
jmp input
m_l:
Move sub,0001h
jmp ret_
m_r:
Move add,0001h
jmp ret_
m_u:
Move sub,0100h
jmp ret_
m_d:
Move add,0100h
jmp ret_
ret_: ret
nextstep endp
_____________________________________________判断下一步__________
judge proc near 00h:食物,0fh:正常,f0h:障碍
push cx
mov ah,02h
int 10h
mov ah,08h
int 10h
mov ch,al
mov al,00h
cmp ch,'o'
jz outjudge
mov al,0fh
cmp ch,' '
jz outjudge
mov al,0f0h
outjudge:
pop cx
ret
judge endp
_____________________________________________置食物函数__________
food proc near
Protect
inc si
and si,000fh
mov cx,bx
getfood:
add di,0002h
cmp di,20h
jb go_on
xor di,di
go_on:
mov bx,cx保证每次检查都从尾开始
lp:
shl bx,1
mov dx,es:[di]
cmp dx,snake[bx]
jz getfood
shr bx,1
inc bx
and bx,000fh
cmp bx,si
jnz lp
mov dx,es:[di]
mov ah,02h
int 10h
mov al,'o'
mov ah,0eh
int 10h
Curser
Recover
ret
food endp
*********************************************子程序结束**********
Code ends
end start
这个是代码#include"time.h"
#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
#define ESC 033
#define UP 0110
#define DOWN 0120
#define LEFT 0113
#define RIGHT 0115
#define X1 15
#define X2 40
#define Y1 2
#define Y2 20
#define N 15
/*************************************************************/
void food(int *b)
{
*b=random(X2-X1+1)+X1b++/*产生食物*/
*b=random(Y2-Y1+1)+Y1
}
/*************************************************************/
void init(int (*a)[2],int *b)
{
int i,j
food(b)
a[0][0]=random(X2-X1-5)+X1+3/*随机生成一3断长的snack */
a[0][1]=random(Y2-Y1-5)+Y1+3
switch(random(4))
{case 0:i=-1j=0break
case 1:i=1j=0break
case 2:i=0j=-1break
case 3:i=0j=1break}
a[1][0]=a[0][0]+i
a[1][1]=a[0][1]+j
a[2][0]=2*a[1][0]-a[0][0]
a[2][1]=2*a[1][1]-a[0][1]
window(1,1,80,25)
textbackground(BLACK)
clrscr()
window(X1,Y1,X2,Y2)
textbackground(BLUE)
clrscr()
window(1,1,80,25)
textbackground(YELLOW)
textcolor(YELLOW)
for(i=0i<3i++)
{gotoxy(a[i][0],a[i][1])
cprintf("%c",'%')}
gotoxy(b[0],b[1])
cprintf("%c",'*')
}
/*************************************************************/
void putout(int a[][2],int n,int b[],int step)
{
textbackground(YELLOW)
gotoxy(a[0][0],a[0][1])
cprintf("%c",'%')
gotoxy(b[0],b[1])
cprintf("%c",'#')
textbackground(BLUE)
gotoxy(a[n][0],a[n][1])
cprintf("%c",' ')/*清除蛇的尾部*/
gotoxy(X1,Y2+2)
cprintf("Length:%2d Delay:%3dms",n,step)
}
/*************************************************************/
void direction(int a[][2],int n)
{
char ch
int i,across,down
across=a[0][0]-a[1][0]
down=a[0][1]-a[1][1]
if(kbhit())
{ch=getch()
while(ch==0)
ch=getch()
if(ch==LEFT) {if(across!=1) {down=0across=-1}}
else if(ch==RIGHT) {if(across!=-1) {down=0across=1}}
else if(ch==UP) {if(down!=1) {down=-1across=0}}
else if(ch==DOWN) {if(down!=-1) {down=1across=0}}
else if(ch==ESC) exit(1)
}
for(i=ni>0i--)
{a[i][0]=a[i-1][0]
a[i][1]=a[i-1][1]
}
a[0][0]+=across
a[0][1]+=down
}
/*************************************************************/
int die(int a[][2],int n)
{int i
if(a[0][0]>X2||a[0][0]<X1||a[0][1]>Y2||a[0][1]<Y1) return 1
for(i=4i<=ni++)
{if(a[0][0]==a[i][0]&&a[0][1]==a[i][1])
return 1
}
return 0
}
/*************************************************************/
int meet(int a[][2],int b[])
{
if(a[0][0]==b[0]&&a[0][1]==b[1]) return 1
return 0
}
/*************************************************************/
void delay2(int step)
{float t
static clock_t start=0
clock_t end
do{end=clock()
t=(end-start)/CLK_TCK
}
while(t*1000<step)
start=clock()
}
/*************************************************************/
void ifdie()
{
char ch
gotoxy(X1,Y2+2)
printf("You die. Do you want to go on?(Y/N)")
ch=getch()
if(ch!='y') exit(1)
}
/*************************************************************/
void ifwin()
{
char ch
gotoxy(X1,Y2+2)
puts("You win. Do you want to continue?(Y/N)")
ch=getch()
if(ch=='y') return
exit(1)
}
/**************************************************************/
int main()
{
int a[N][2],b[2]
int n,step
REPLAY:
for(step=3001step*=.9)
{randomize()
init(a,b)
for(n=3n<Nn++)
{do{putout(a,n,b,step)
delay2(step)
direction(a,n)
if(die(a,n))
{putout(a,n,b,step)
ifdie()
goto REPLAY
}
}
while(!meet(a,b))
food(b)
}
ifwin()
}
return 0
}