楼主做得不错,,只是在画面显示上,,建议使用直接写内存的技术,,这样画面会比较清晰,不会闪烁! 发一个我自己写的贪吃蛇游戏,turbo c 2.0编译通过! #include<bios.h> #include <dos.h> #include<stdio.h> #include<conio.h> #include<stdlib.h>
#define UP 18432 #define DOWN 20480 #define LEFT 19200 #define RIGHT 19712 #define ESC 283
#define TRUE 1 #define FALSE 0
/*设置显示模式的函数*/ #define VGA256 0x13 #define TXT16 0x03 void Set_video_mode(int mode) { union REGS inregs,outregs; inregs.h.ah=0; inregs.h.al=(unsigned char)mode; int86(0x10,&inregs,&outregs); }
/*画竖线的函数VLine*/ void VLine(int y1,int y2,int x,unsigned int color){ unsigned int LineOffset,index; unsigned char far *video_buffer=(char far *)0xA0000000L; LineOffset=((y1<<8)+(y1<<6))+x; for(index=0;index<=y2-y1;index++){ video_buffer[LineOffset]=color; LineOffset+=320; }}
/*画方块的函数*/ void draw(int x,int y,int color) { unsigned char far *video_buffer=(char far *)0xA0000000L; int i,j=0; for((i=((((y<<8)+(y<<6))+x)-2));j<=4;i++) {video_buffer[i]=color; j++; } j=0; for((i=(((((y-1)<<8)+((y-1)<<6))+x)-2));j<=4;i++) {video_buffer[i]=color; j++; } j=0; for((i=(((((y+1)<<8)+((y+1)<<6))+x)-2));j<=4;i++) {video_buffer[i]=color; j++; } j=0; for((i=(((((y-2)<<8)+((y-2)<<6))+x)-2));j<=4;i++) {video_buffer[i]=color; j++; } j=0; for((i=(((((y+2)<<8)+((y+2)<<6))+x)-2));j<=4;i++) {video_buffer[i]=color; j++; } }
/*清空键盘缓冲区的函数*/ void Clear_Key_Buffer(void) {int offset; offset=peek(0x40,0x1a); pokeb(0x40,0x1c,offset); }
main(void) { unsigned int LineOffset; unsigned char far *video_buffer=(char far *)0xA0000000L; int flag=90000; int i=0; int j; int count=2; int exit=FALSE; int key; int keydown=0 ; int rax; int ray;
struct kuai {int x; int y; };
struct first {int x; int y; int left; int right; int up; int down; };
struct first head[103]={232,167,0,0,0,0, 232,172,0,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,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0}; struct first temp;
Set_video_mode(VGA256);/*设置为320*200 256色的图形模式*/
/*画游戏界面外框*/ for(i=59;i<=260;i++) {VLine(19,19,i,111); VLine(180,180,i,111); } VLine(19,180,59,111); VLine(19,180,260,111);
draw(232,167,234); draw(232,172,234); draw(232,132,222);/*an egg*/
/*游戏大循环开始!!!*/ while (!exit) {
keydown=0; if (kbhit()) { key = bioskey(0); switch(key) { case UP: if( (head[0].down!=1)&& !keydown) {head[0].up = 1; head[0].down=0; head[0].left=0; head[0].right=0; Clear_Key_Buffer(); keydown = TRUE; break;} case DOWN: if( (head[0].up!= 1)&& !keydown) {head[0].up = 0; head[0].down=1; head[0].left=0; head[0].right=0; Clear_Key_Buffer(); keydown = TRUE; break;} case LEFT: if( (head[0].right!=1)&& !keydown) {head[0].up = 0; head[0].down=0; head[0].left=1; head[0].right=0;; Clear_Key_Buffer(); keydown = TRUE; break;} case RIGHT: if( (head[0].left!= 1)&& !keydown) {head[0].up = 0; head[0].down=0; head[0].left=0; head[0].right=1; Clear_Key_Buffer(); keydown = TRUE; break;} case ESC: exit = TRUE;break;
} }
if((head[0].up ==0)&&(head[0].down==0)&&(head[0].left==0)&&(head[0].right==0)) {;} else{
temp.x=head[count-1].x;/*擦除尾巴的代码*/ temp.y=head[count-1].y;
i=1; while((count-i-1)!=(-1))/*各个方块依次获取上一个方块的地址*/ {head[count-i].x=head[count-i-1].x; head[count-i].y=head[count-i-1].y; i++; }
if(head[0].left==1) head[0].x=head[0].x-5;
if(head[0].right==1) head[0].x=head[0].x+5;
if(head[0].up==1) head[0].y=head[0].y-5;
if(head[0].down==1) head[0].y=head[0].y+5;
if(((head[0].left==1)||(head[0].right==1))&&((head[0].x<62)||(head[0].x>257))) exit=1; if(((head[0].up==1)||(head[0].down==1))&&((head[0].y<22)||(head[0].y>177))) exit=1; LineOffset=((head[0].y<<8)+(head[0].y<<6))+head[0].x; if(video_buffer[LineOffset]==234) exit=1;
if(exit==1) goto END;
/*吃蛋的代码,确保让蛇离开蛋的时候,带着蛋一起走,并改变蛋的颜色*/ else{ LineOffset=((temp.y<<8)+(temp.y<<6))+temp.x; if(video_buffer[LineOffset]==222) {count++; head[count-1].x=temp.x; head[count-1].y=temp.y;
draw(head[0].x,head[0].y,234); draw(head[count-1].x,head[count-1].y,234);
loop: rax=(random(19)+6)*10+2; ray=(random(15)+2)*10+7; for(i=0;i<count;i++) {if((rax==head[i].x)&&(ray==head[i].y)) goto loop; } draw(rax,ray,222); }
/*在遇到蛋的时候继续向前走的代码,并不改变蛋的颜色直到离开蛋!*/ else { for(i=0;i<count;i++) {LineOffset=((head[i].y<<8)+(head[i].y<<6))+head[i].x; if(video_buffer[LineOffset]==222) {draw(head[i].x,head[i].y,222); draw(temp.x,temp.y,0); }
/*没有蛋的时候*/ else {draw(head[i].x,head[i].y,234); draw(temp.x,temp.y,0); } } }
/*延时部分,各种玩家可根据自己电脑的配置来选择时间值,我的是p3 128m 的*/ if((count-2)<10) delay(90000); if(((count-2)>=10)&&((count-2)<20)) delay(85000); if(((count-2)>=20)&&((count-2)<30)) delay(80000); if(((count-2)>=30)&&((count-2)<40)) delay(75000); if(((count-2)>=40)&&((count-2)<50)) delay(70000); if(((count-2)>=50)&&((count-2)<60)) delay(65000); if(((count-2)>=60)&&((count-2)<70)) delay(60000); if(((count-2)>=70)&&((count-2)<80)) delay(55000); if(((count-2)>=80)&&((count-2)<90)) delay(50000); if(((count-2)>=90)&&((count-2)<100)) delay(45000);
if(count==102) {exit=1; flag=1; }
} } }
END: Set_video_mode(TXT16); if(flag==1) {window(26,10,80,15); textcolor(1102); textbackground(000); cprintf(" Congratulation!!!"); window(13,12,80,17); textcolor(0104); cprintf("The snake has eaten 100 eggs, You are the winner!!!"); window(2,14,80,19); textbackground(000); cprintf("----------------------------------------------------------------------"); window(2,15,80,20); textcolor(0102); cprintf("If you have any advice for this game,please tell me by the next email:"); window(10,16,60,21); textcolor(0102); cprintf(" ************** kikix86@163.com ************** "); textcolor(0100);
} else { window(30,10,80,15); textcolor(1111); textbackground(001); textmode(800*600); cprintf("GAME OVER"); window(23,12,80,17); cprintf("The snake has eaten %d eggs!!!",count-2); window(2,14,80,19); textbackground(000); cprintf("----------------------------------------------------------------------"); window(2,15,80,20); textcolor(0102); cprintf("If you have any advice for this game,please tell me by the next email:"); window(10,16,60,21); textcolor(0102); cprintf(" ************** kikix86@163.com ************** "); textcolor(0100); }
getche();
}