我没帮上什么忙,还在学习与探索中。
贪吃蛇游戏:(文字转码自己弄,程序基本上是对的,后续问题都可以百度解决的)
#define N 200
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;/*µÃ·Ö*/
int gamespeed=50000;/*ÓÎÏ·ËÙ¶È×Ô¼ºµ÷Õû*/
struct Food
{
int x;/*ʳÎïµÄºá×ø±ê*/
int y;/*ʳÎïµÄ×Ý×ø±ê*/
int yes;/*ÅжÏÊÇ·ñÒª³öÏÖʳÎïµÄ±äÁ¿*/
}food;/*ʳÎïµÄ½á¹¹Ìå*/
struct Snake
{
int x[N];
int y[N];
int node;/*ÉߵĽÚÊý*/
int direction;/*ÉßÒƶ¯·½Ïò*/
int life;/* ÉßµÄÉúÃü,0»î×Å,1ËÀÍö*/
}snake;
void Init(void);/*ͼÐÎÇý¶¯*/
void Close(void);/*ͼÐνáÊø*/
void DrawK(void);/*¿ªÊ¼»­Ãæ*/
void GameOver(void);/*½áÊøÓÎÏ·*/
void GamePlay(void);/*ÍæÓÎÏ·¾ßÌå¹ý³Ì*/
void PrScore(void);/*Êä³ö³É¼¨*/
/*Ö÷º¯Êý*/
void main(void)
{
Init();/*ͼÐÎÇý¶¯*/
DrawK();/*¿ªÊ¼»­Ãæ*/
GamePlay();/*ÍæÓÎÏ·¾ßÌå¹ý³Ì*/
Close();/*ͼÐνáÊø*/
}
/*ͼÐÎÇý¶¯*/
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
/*¿ªÊ¼»­Ã棬×óÉϽÇ×ø±êΪ£¨50£¬40£©£¬ÓÒϽÇ×ø±êΪ£¨610£¬460£©µÄΧǽ*/
void DrawK(void)
{
/*setbkcolor(LIGHTGREEN);*/
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*ÉèÖÃÏßÐÍ*/
for(i=50;i<=600;i+=10)/*»­Î§Ç½*/
{
rectangle(i,40,i+10,49); /*Éϱß*/
rectangle(i,451,i+10,460);/*챧*/
}
for(i=40;i<=450;i+=10)
{
rectangle(50,i,59,i+10); /*×ó±ß*/
rectangle(601,i,610,i+10);/*ÓÒ±ß*/
}
}
/*ÍæÓÎÏ·¾ßÌå¹ý³Ì*/
void GamePlay(void)
{
randomize();/*Ëæ»úÊý·¢ÉúÆ÷*/
food.yes=1;/*1±íʾÐèÒª³öÏÖÐÂʳÎï,0±íʾÒѾ­´æÔÚʳÎï*/
snake.life=0;/*»î×Å*/
snake.direction=1;/*·½ÏòÍùÓÒ*/
snake.x[0]=100;snake.y[0]=100;/*ÉßÍ·*/
snake.x[1]=110;snake.y[1]=100;
snake.node=2;/*½ÚÊý*/
PrScore();/*Êä³öµÃ·Ö*/
while(1)/*¿ÉÒÔÖظ´ÍæÓÎÏ·,ѹESC¼ü½áÊø*/
{
while(!kbhit())/*ÔÚûÓа´¼üµÄÇé¿öÏÂ,Éß×Ô¼ºÒƶ¯ÉíÌå*/
{
if(food.yes==1)/*ÐèÒª³öÏÖÐÂʳÎï*/
{
food.x=rand()%400+60;
food.y=rand()%350+60;
while(food.x%10!=0)/*ʳÎïËæ»ú³öÏÖºó±ØÐëÈÃʳÎïÄܹ»ÔÚÕû¸ñÄÚ,ÕâÑù²Å¿ÉÒÔÈÃÉß³Ôµ½*/
food.x++;
while(food.y%10!=0)
food.y++;
food.yes=0;/*»­ÃæÉÏÓÐʳÎïÁË*/
}
if(food.yes==0)/*»­ÃæÉÏÓÐʳÎïÁ˾ÍÒªÏÔʾ*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--)/*ÉßµÄÿ¸ö»·½ÚÍùÇ°Òƶ¯,Ò²¾ÍÊÇÌ°³ÔÉߵĹؼüËã·¨*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
/*1,2,3,4±íʾÓÒ,×ó,ÉÏ,ÏÂËĸö·½Ïò,ͨ¹ýÕâ¸öÅжÏÀ´Òƶ¯ÉßÍ·*/
switch(snake.direction)
{
case 1:snake.x[0]+=10;break;
case 2: snake.x[0]-=10;break;
case 3: snake.y[0]-=10;break;
case 4: snake.y[0]+=10;break;
}
for(i=3;i<snake.node;i++)/*´ÓÉߵĵÚËĽڿªÊ¼ÅжÏÊÇ·ñײµ½×Ô¼ºÁË£¬ÒòΪÉßͷΪÁ½½Ú£¬µÚÈý½Ú²»¿ÉÄܹչýÀ´*/
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();/*ÏÔʾʧ°Ü*/
snake.life=1;
break;
}
}
if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||
snake.y[0]>455)/*ÉßÊÇ·ñײµ½Ç½±Ú*/
{
GameOver();/*±¾´ÎÓÎÏ·½áÊø*/
snake.life=1; /*ÉßËÀ*/
}
if(snake.life==1)/*ÒÔÉÏÁ½ÖÖÅжÏÒÔºó,Èç¹ûÉßËÀ¾ÍÌø³öÄÚÑ­»·£¬ÖØпªÊ¼*/
break;
if(snake.x[0]==food.x&&snake.y[0]==food.y)/*³Ôµ½Ê³ÎïÒÔºó*/
{
setcolor(0);/*°Ñ»­ÃæÉϵÄʳÎﶫÎ÷È¥µô*/
rectangle(food.x,food.y,food.x+10,food.y-10);
snake.x[snake.node]=-20;snake.y[snake.node]=-20;
/*еÄÒ»½ÚÏÈ·ÅÔÚ¿´²»¼ûµÄλÖÃ,Ï´ÎÑ­»·¾ÍÈ¡Ç°Ò»½ÚµÄλÖÃ*/
snake.node++;/*ÉßµÄÉíÌ峤һ½Ú*/
food.yes=1;/*»­ÃæÉÏÐèÒª³öÏÖеÄʳÎï*/
score+=10;
PrScore();/*Êä³öе÷Ö*/
}
setcolor(4);/*»­³öÉß*/
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,
snake.y[i]-10);
delay(gamespeed);
setcolor(0);/*ÓúÚÉ«È¥³ýÉߵĵÄ×îºóÒ»½Ú*/
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
}
/*endwhile£¨£¡kbhit£©*/
if(snake.life==1)/*Èç¹ûÉßËÀ¾ÍÌø³öÑ­»·*/
break;
key=bioskey(0);/*½ÓÊÕ°´¼ü*/
if(key==ESC)/*°´ESC¼üÍ˳ö*/
break;
else
if(key==UP&&snake.direction!=4)
/*ÅжÏÊÇ·ñÍùÏà·´µÄ·½ÏòÒƶ¯*/
snake.direction=3;
else
if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}/*endwhile(1)*/
}
/*ÓÎÏ·½áÊø*/
void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}
/*Êä³ö³É¼¨*/
void PrScore(void)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"score:%d",score);
outtextxy(55,20,str);
}
/*ͼÐνáÊø*/
void Close(void)
{
getch();
closegraph();
}