贪吃蛇 C语言 简易程序设计

贪吃蛇 C语言 简易程序设计,第1张

#include<graphicsh>

#include<stdlibh>

#define N 200

#define up 0x4800

#define down 0x5000

#define left 0x4b00

#define right 0x4d00

#define esc 0x011b

#define Y 0x1579

#define n 0x316e

int gamespeed; / 游戏速度 /

int i,key,color;

int score=0; / 游戏分数 /

char cai48H[]={

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x04,0x00,0x18,0x00,0x00,0x00,0x0E,0x00,

0x1C,0x00,0x00,0x00,0x1C,0x00,0x1C,0x00,

0x00,0x00,0x20,0x00,0x38,0x00,0x00,0x00,

0x40,0x00,0x78,0x00,0x00,0x01,0x80,0x40,

0x70,0x00,0x00,0x03,0x80,0xC0,0xE0,0x00,

0x00,0x07,0x80,0x80,0xC0,0x00,0x00,0x0E,

0x11,0x81,0xC0,0x00,0x00,0x08,0x61,0x01,

0x80,0x00,0x00,0x00,0x23,0x03,0x04,0x00,

0x00,0x02,0x02,0x00,0x06,0x00,0x00,0x1E,

0x04,0x00,0x0F,0x00,0x00,0x1C,0x1F,0x80,

0x1E,0x00,0x00,0x08,0x3F,0x80,0x3C,0x00,

0x00,0x00,0xFF,0x80,0x38,0x00,0x00,0x03,

0xFF,0x80,0x78,0x00,0x00,0x0F,0xF8,0x00,

0xF0,0x00,0x00,0x7F,0xF0,0x00,0xE0,0x00,

0x03,0xFF,0xFC,0x01,0x80,0x00,0x03,0xC0,

0xFF,0x01,0x03,0x80,0x01,0x01,0xFF,0x00,

0x03,0x80,0x00,0x01,0x3F,0x00,0x07,0x80,

0x00,0x02,0x11,0x00,0x07,0x00,0x00,0x00,

0x10,0x00,0x07,0x00,0x00,0x00,0x10,0x00,

0x0E,0x00,0x00,0x08,0x10,0x00,0x1C,0x00,

0x00,0x30,0x10,0x00,0x18,0x00,0x00,0x70,

0x10,0x00,0x30,0x00,0x01,0xE0,0x10,0x00,

0x70,0x00,0x03,0x80,0x10,0x00,0x60,0x00,

0x00,0x00,0x30,0x00,0xE0,0x00,0x00,0x00,

0xF0,0x01,0xC0,0x00,0x00,0x00,0x70,0x03,

0xC0,0x00,0x00,0x00,0x10,0x07,0x80,0x00,

0x00,0x00,0x00,0x0F,0x00,0x00,0x00,0x00,

0x00,0x1E,0x00,0x00,0x00,0x00,0x00,0x3C,

0x00,0x00,0x00,0x00,0x00,0x70,0x00,0x00,

0x00,0x00,0x01,0xC0,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

};

char she48H[]={

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,

0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x00,

0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x00,

0x00,0x0E,0x00,0x00,0x00,0x03,0x00,0x07,

0x00,0x00,0x00,0x02,0x00,0x03,0x00,0x00,

0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x02,

0x00,0x00,0xF8,0x00,0x00,0x02,0x00,0x07,

0x86,0x00,0x00,0x02,0x00,0x18,0x03,0x00,

0x00,0x02,0x00,0x00,0x07,0x80,0x00,0x03,

0xF0,0x00,0x07,0x80,0x00,0x0F,0xFC,0x00,

0x0C,0x00,0x00,0x7E,0x3F,0x80,0x00,0x00,

0x01,0xFE,0x1F,0x80,0x00,0x00,0x01,0xE2,

0x39,0x8C,0x00,0x00,0x00,0xC2,0x30,0x08,

0x00,0x00,0x00,0xC2,0x60,0x08,0x00,0x00,

0x00,0xC3,0xE0,0x08,0x60,0x00,0x00,0x7F,

0xE0,0x01,0xE0,0x00,0x00,0x3F,0x80,0x1F,

0xE0,0x00,0x00,0x1E,0x00,0x1F,0x80,0x00,

0x00,0x1E,0x00,0x1F,0x00,0x00,0x00,0x02,

0x38,0x1E,0x00,0x00,0x00,0x07,0xFC,0x1C,

0x00,0x20,0x00,0x07,0xFC,0x18,0x00,0x20,

0x00,0x1F,0x0C,0x10,0x00,0x20,0x00,0x7C,

0x04,0x10,0x00,0x60,0x01,0xF0,0x00,0x10,

0x00,0x60,0x01,0xE0,0x00,0x08,0x00,0xF0,

0x00,0x80,0x00,0x08,0x03,0xF0,0x00,0x00,

0x00,0x07,0xFF,0xF0,0x00,0x00,0x00,0x07,

0xFF,0xF0,0x00,0x00,0x00,0x03,0xFF,0xE0,

0x00,0x00,0x00,0x01,0xFF,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

};

char tun48H[]={

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x3E,

0x00,0x00,0x00,0x00,0x00,0x7F,0x00,0x00,

0x00,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,

0x03,0xC0,0x00,0x00,0x00,0x00,0x1F,0x00,

0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,

0x00,0x01,0xF8,0x00,0x00,0x00,0x00,0x03,

0xF8,0x00,0x40,0x00,0x00,0x00,0x06,0x07,

0xC0,0x00,0x00,0x00,0x07,0xFF,0xE0,0x00,

0x00,0x00,0x07,0xFF,0xE0,0x00,0x00,0x00,

0x0F,0xFF,0x80,0x00,0x00,0x00,0x7F,0xF8,

0x00,0x00,0x00,0x1F,0xFF,0xF8,0x00,0x00,

0x00,0x1F,0xFF,0xF8,0x00,0x00,0x00,0x1F,

0xFC,0x3C,0x00,0x00,0x00,0x0F,0xF8,0x0E,

0x00,0x00,0x00,0x04,0x70,0x07,0x00,0x00,

0x00,0x00,0x60,0x03,0x80,0x00,0x00,0x00,

0xC0,0x00,0xC0,0x00,0x00,0x01,0x80,0x00,

0x30,0x00,0x00,0x01,0x00,0x3C,0x18,0x00,

0x00,0x02,0x03,0xFF,0x0C,0x00,0x00,0x0C,

0x7F,0xFF,0x8E,0x00,0x00,0x18,0xFF,0xFF,

0xC7,0x80,0x00,0x78,0xFE,0x07,0x87,0xE0,

0x01,0xF0,0x70,0x07,0x03,0xF8,0x07,0xE0,

0x70,0x0E,0x03,0xFE,0x00,0x00,0x38,0x1E,

0x01,0xFE,0x00,0x00,0x3F,0xFE,0x00,0x0C,

0x00,0x00,0x1F,0xFE,0x00,0x00,0x00,0x00,

0x1F,0xFE,0x00,0x00,0x00,0x00,0x0F,0xFE,

0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

};

char dan48H[]={

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0xFC,0x00,0x00,0x00,0x00,0x07,0xFF,

0x00,0x00,0x00,0x00,0x7F,0xC0,0x80,0x00,

0x00,0x03,0xFF,0x80,0x40,0x00,0x00,0x01,

0xF1,0x80,0x40,0x00,0x00,0x01,0x81,0x80,

0xE0,0x00,0x00,0x00,0x01,0x93,0xF0,0x00,

0x00,0x00,0x01,0xFF,0xF0,0x00,0x00,0x00,

0x21,0xFF,0xF0,0x00,0x00,0x00,0x21,0xF8,

0x00,0x00,0x00,0x00,0x61,0xC0,0x00,0x00,

0x00,0x00,0x61,0x80,0x00,0x00,0x00,0x00,

0xF3,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,

0x00,0x00,0x00,0x01,0xFF,0xC0,0x00,0x00,

0x00,0x03,0xFF,0xF8,0x00,0x00,0x00,0x02,

0x00,0xFC,0x00,0x00,0x00,0x04,0x02,0x1F,

0x00,0x00,0x00,0x08,0x03,0x01,0xC0,0x00,

0x00,0x38,0x03,0x00,0x7C,0x00,0x00,0xF8,

0x07,0xF8,0x3F,0xC0,0x01,0xF0,0x3F,0xFE,

0x3F,0xF8,0x03,0xC1,0xFF,0x0F,0x1F,0xF8,

0x00,0x01,0xE3,0x0F,0x0F,0xF0,0x00,0x01,

0xC3,0x0E,0x00,0x00,0x00,0x01,0x83,0xFC,

0x00,0x00,0x00,0x00,0xC7,0xF8,0x00,0x00,

0x00,0x00,0xFF,0xF8,0x00,0x00,0x00,0x00,

0x7F,0xF0,0x00,0x00,0x00,0x00,0x3F,0x03,

0x80,0x00,0x00,0x00,0x03,0x04,0x00,0x00,

0x00,0x00,0x03,0xF8,0x00,0x00,0x00,0x00,

0x1F,0xF8,0x20,0x00,0x00,0x00,0xFF,0xFF,

0xE0,0x00,0x00,0x07,0xFF,0x81,0xE0,0x00,

0x00,0x07,0xE0,0x00,0xE0,0x00,0x00,0x03,

0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

};

char zuo16H[]={

0x18,0xC0,0x18,0xC0,0x19,0x80,0x31,0xFE,

0x33,0xFE,0x76,0xC0,0xF0,0xFC,0xB0,0xFC,

0x30,0xC0,0x30,0xC0,0x30,0xFE,0x30,0xFE,

0x30,0xC0,0x30,0xC0,0x30,0xC0,0x00,0x00,

};

char zhe16H[]={

0x03,0x00,0x03,0x0C,0x1F,0xCC,0x1F,0xD8,

0x03,0x30,0xFF,0xFE,0xFF,0xFE,0x03,0x00,

0x0F,0xF8,0x3F,0xF8,0xEC,0x18,0xCF,0xF8,

0x0C,0x18,0x0F,0xF8,0x0F,0xF8,0x00,0x00,

};

char tian16H[]={

0x00,0x00,0x3F,0xFC,0x3F,0xFC,0x31,0x8C,

0x31,0x8C,0x31,0x8C,0x3F,0xFC,0x3F,0xFC,

0x31,0x8C,0x31,0x8C,0x31,0x8C,0x3F,0xFC,

0x3F,0xFC,0x30,0x0C,0x00,0x00,0x00,0x00,

};

char xue16H[]={

0x33,0x18,0x19,0x98,0x08,0xB0,0x7F,0xFC,

0x7F,0xFC,0x60,0x0C,0x1F,0xF0,0x1F,0xF0,

0x00,0xC0,0x7F,0xFC,0x7F,0xFC,0x01,0x80,

0x01,0x80,0x07,0x80,0x03,0x00,0x00,0x00,

};

char ke16H[]={

0x00,0x00,0x0C,0x18,0xFD,0x98,0xF8,0xD8,

0x18,0x58,0xFE,0x18,0xFE,0x98,0x18,0xD8,

0x3C,0x58,0x7E,0x1E,0xDB,0xFE,0x9B,0xF8,

0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x00,

};

struct Food/定义结构体存储食物的属性/

{

int x; / 食物的坐标 /

int y;

int yes; / 值为0表示屏幕上没有食物,值为1表示屏幕上有食物 /

int color; / 食物颜色 /

}food;

struct Snake/定义结构体存储蛇的属性/

{

int x[N]; / 每一节蛇的坐标 /

int y[N];

int color[N];/存储每一节蛇的颜色/

int node; / 蛇的节数 /

int direction; / 蛇移动的方向 /

int life; / 蛇的生命,如果为1,蛇死,游戏结束 /

}snake;

void init(void)/图形驱动/

{

int driver=DETECT,mode=0;

registerbgidriver(EGAVGA_driver);

initgraph(&driver,&mode,"");

}

void drawmat(char mat,int matsize,int x,int y,int color)/汉字点阵/

{

int i,j,k,m;

m=(matsize-1)/8+1;

for(j=0;j<matsize;j++)

for(i=0;i<m;i++)

for(k=0;k<8;k++)

if(mat[jm+i]&(0x80>>k))

putpixel(x+i8+k,y+j,color);

}

void showword(void)

{/ 调用汉字点阵输出程序,显示标题和作者信息 /

drawmat(cai48H,48,249,-4,7);

drawmat(she48H,48,329,-4,7);

drawmat(tun48H,48,409,-4,7);

drawmat(dan48H,48,489,-4,7);

drawmat(cai48H,48,250,-5,4);

drawmat(she48H,48,330,-5,4);

drawmat(tun48H,48,410,-5,4);

drawmat(dan48H,48,490,-5,4);

/作者 田学科/

drawmat(zuo16H,16,515,465,7);

drawmat(zhe16H,16,530,465,7);

drawmat(tian16H,16,550,465,7);

drawmat(xue16H,16,565,465,7);

drawmat(ke16H,16,580,465,7);

}

void draw(void)/画出四周的墙/

{

if(color==15)

color=0;

setcolor(++color);

setlinestyle(SOLID_LINE,0,1);

for(i=30;i<=600;i+=10)

{

rectangle(i,40,i+10,49);

rectangle(i,451,i+10,460);

}

for(i=40;i<450;i+=10)

{

rectangle(30,i,39,i+10);

rectangle(601,i,610,i+10);

}

}

void prscore(void)

{/ 打印游戏分数 /

char str[10];

setfillstyle(SOLID_FILL,YELLOW);

bar(50,10,200,30);

setcolor(6);

settextstyle(0,0,2);

sprintf(str,"score:%d",score);

outtextxy(55,15,str);

}

void gameover(void)

{

cleardevice(); / 清屏函数 /

for(i=0;i<snakenode;i++)/ 画出蛇死时的位置 /

{

setcolor(snakecolor[i]);

rectangle(snakex[i],snakey[i],snakex[i]+10,snakey[i]+10);

}

prscore(); / 显示分数 /

draw();

showword();

settextstyle(0,0,6);

setcolor(7);

outtextxy(103,203,"GAME OVER");

setcolor(RED);

outtextxy(100,200,"GAME OVER");

}

void gameplay(void)/ 玩游戏的具体过程 /

{

int flag,flag1;

randomize();

prscore();

gamespeed=50000;

foodyes=0;/ foodyes=0表示屏幕上没有食物 /

snakelife=1;/ snakelife=1表示蛇是活着的 /

snakedirection=4;/ 表示蛇的初始方向为向右 /

snakenode=2; / 蛇的初始化为两节 /

snakecolor[0]=2; /两节蛇头初始化为绿色/

snakecolor[1]=2;

snakex[0]=100;snakey[0]=100;

snakex[1]=110;snakey[1]=100;

foodcolor=random(15)+1;

while(1)

{

while(1)

{

if(foodyes==0) / 如果蛇活着 /

{

while(1)

{

flag=1;

foodyes=1;

foodx=random(56)10+40;

foody=random(40)10+50;

for(i=0;i<snakenode;i++)

{

if(foodx==snakex[i]&&foody==snakey[i])

flag=0;

}

if(flag) break;

}

}

if(foodyes)

{

setcolor(foodcolor);

rectangle(foodx,foody,foodx+10,foody+10);

}

for(i=snakenode-1;i>0;i--)

{

snakex[i]=snakex[i-1];

snakey[i]=snakey[i-1];

}

switch(snakedirection)

{

case 1: snakey[0]-=10;break;

case 2: snakey[0]+=10;break;

case 3: snakex[0]-=10;break;

case 4: snakex[0]+=10;break;

}

for(i=3;i<snakenode;i++)

{

if(snakex[i]==snakex[0]&&snakey[i]==snakey[0])

{

gameover();

snakelife=0;

break;

}

}

if(snakex[0]<40||snakex[0]>590||snakey[0]<50||snakey[0]>440)

{

gameover();

snakelife=0;

}

if(snakelife==0)

break;

if(snakex[0]==foodx&&snakey[0]==foody)/蛇吃掉食物/

{

setcolor(0);

rectangle(foodx,foody,foodx+10,foody+10);

snakex[snakenode]=-20;

snakey[snakenode]=-20;

snakecolor[snakenode]=foodcolor;

snakenode++;

foodyes=0;

foodcolor=random(15)+1;

score+=10;

prscore();

if(score%100==0&&score!=0)

{

for(i=0;i<snakenode;i++)/ 画出蛇 /

{

setcolor(snakecolor[i]);

rectangle(snakex[i],snakey[i],snakex[i]+10,snakey[i]+10);

}

sound(200);

delay(50000);delay(50000);delay(50000);

delay(50000);delay(50000);delay(50000);

nosound();

gamespeed-=5000;

draw();

}

else

{

sound(500);

delay(500);

nosound();

}

}

for(i=0;i<snakenode;i++)/ 画出蛇 /

{

setcolor(snakecolor[i]);

rectangle(snakex[i],snakey[i],snakex[i]+10,snakey[i]+10);

}

delay(gamespeed);

delay(gamespeed);

flag1=1;

setcolor(0);

rectangle(snakex[snakenode-1],snakey[snakenode-1],

snakex[snakenode-1]+10,snakey[snakenode-1]+10);

if(kbhit()&&flag1==1)/如果没按有效键就重新开始循环/

{

flag1=0;

key=bioskey(0);

if(key==esc)

exit(0);

else if(key==up&&snakedirection!=2)

snakedirection=1;

else if(key==down&&snakedirection!=1)

snakedirection=2;

else if(key==left&&snakedirection!=4)

snakedirection=3;

else if(key==right&&snakedirection!=3)

snakedirection=4;

}

}

if(snakelife==0)/如果蛇死了就退出循环/

break;

}

}

void main(void)

{

while(1)

{

color=0;

init();

cleardevice();

showword();

draw();

gameplay();

setcolor(15);

settextstyle(0,0,2);

outtextxy(200,400,"CONTINUE(Y/N)");

while(1)

{

key=bioskey(0);

if(key==Y||key==n||key==esc)

break;

}

if(key==n||key==esc)

break;

}

closegraph();

}

#include <windowsh>

#include <ctime>

#include <iostream>

#include <vector>

#include <queue>

using namespace std;

#ifndef SNAKE_H

#define SNAKE_H

class Cmp

{

friend class Csnake;

int rSign; //横坐标

int lSign; //竖坐标

public:

// friend bool isDead(const Cmp& cmp);

Cmp(int r,int l){setPoint(r,l);}

Cmp(){}

void setPoint(int r,int l){rSign=r;lSign=l;}

Cmp operator-(const Cmp &m)const

{

return Cmp(rSign-mrSign,lSign-mlSign);

}

Cmp operator+(const Cmp &m)const

{

return Cmp(rSign+mrSign,lSign+mlSign);

}

};

const int maxSize = 5; //初始蛇身长度

class Csnake

{

Cmp firstSign; //蛇头坐标

Cmp secondSign;//蛇颈坐标

Cmp lastSign; //蛇尾坐标

Cmp nextSign; //预备蛇头

int row; //列数

int line; //行数

int count; //蛇身长度

vector<vector<char> > snakeMap;//整个游戏界面

queue<Cmp> snakeBody; //蛇身

public:

int GetDirections()const;

char getSymbol(const Cmp& c)const

//获取指定坐标点上的字符

{

return snakeMap[clSign][crSign];

}

Csnake(int n)

//初始化游戏界面大小

{

if(n<20)line=20+2;

else if(n>30)line=30+2;

else line=n+2;

row=line3+2;

}

bool isDead(const Cmp& cmp)

{

return ( getSymbol(cmp)=='@' || cmprSign == row-1

|| cmprSign== 0 || cmplSign == line-1 ||

cmplSign == 0 );

}

void InitInstance(); //初始化游戏界面

bool UpdataGame(); //更新游戏界面

void ShowGame(); //显示游戏界面

};

#endif // SNAKE_H

using namespace std;

//测试成功

void Csnake::InitInstance()

{

snakeMapresize(line); // snakeMap[竖坐标][横坐标]

for(int i=0;i<line;i++)

{

snakeMap[i]resize(row);

for(int j=0;j<row;j++)

{

snakeMap[i][j]=' ';

}

}

for(int m=1;m<maxSize+1;m++)

{

//初始蛇身

snakeMap[line/2][m]='@';

//将蛇身坐标压入队列

snakeBodypush(Cmp(m,(line/2)));

//snakeBody[横坐标][竖坐标]

}

//链表头尾

firstSign=snakeBodyback();

secondSignsetPoint(maxSize-1,line/2);

}

//测试成功

int Csnake::GetDirections()const

{

if(GetKeyState(VK_UP)<0) return 1; //1表示按下上键

if(GetKeyState(VK_DOWN)<0) return 2; //2表示按下下键

if(GetKeyState(VK_LEFT)<0) return 3; //3表示按下左键

if(GetKeyState(VK_RIGHT)<0)return 4; //4表示按下右键

return 0;

}

bool Csnake::UpdataGame()

{

//-----------------------------------------------

//初始化得分0

static int score=0;

//获取用户按键信息

int choice;

choice=GetDirections();

cout<<"Total score: "<<score<<endl;

//随机产生食物所在坐标

int r,l;

//开始初始已经吃食,产生一个食物

static bool eatFood=true;

//如果吃了一个,才再出现第2个食物

if(eatFood)

{

do

{

//坐标范围限制在(1,1)到(line-2,row-2)对点矩型之间

srand(time(0));

r=(rand()%(row-2))+1; //横坐标

l=(rand()%(line-2))+1;//竖坐标

//如果随机产生的坐标不是蛇身,则可行

//否则重新产生坐标

if(snakeMap[l][r]!='@')

{snakeMap[l][r]='';}

}while (snakeMap[l][r]=='@');

}

switch (choice)

{

case 1://向上

//如果蛇头和社颈的横坐标不相同,执行下面 *** 作

if(firstSignrSign!=secondSignrSign)nextSignsetPoint(firstSignrSign,firstSignlSign-1);

//否则,如下在原本方向上继续移动

else nextSign=firstSign+(firstSign-secondSign);

break;

case 2://向下

if(firstSignrSign!=secondSignrSign)nextSignsetPoint(firstSignrSign,firstSignlSign+1);

else nextSign=firstSign+(firstSign-secondSign);

break;

case 3://向左

if(firstSignlSign!=secondSignlSign)nextSignsetPoint(firstSignrSign-1,firstSignlSign);

else nextSign=firstSign+(firstSign-secondSign);

break;

case 4://向右

if(firstSignlSign!=secondSignlSign)nextSignsetPoint(firstSignrSign+1,firstSignlSign);

else nextSign=firstSign+(firstSign-secondSign);

break;

default:

nextSign=firstSign+(firstSign-secondSign);

}

//----------------------------------------------------------

if(getSymbol(nextSign)!='' && !isDead(nextSign))

//如果没有碰到食物(且没有死亡的情况下),删除蛇尾,压入新的蛇头

{

//删除蛇尾

lastSign=snakeBodyfront();

snakeMap[lastSignlSign][lastSignrSign]=' ';

snakeBodypop();

//更新蛇头

secondSign=firstSign;

//压入蛇头

snakeBodypush(nextSign);

firstSign=snakeBodyback();

snakeMap[firstSignlSign][firstSignrSign]='@';

//没有吃食

eatFood=false;

return true;

}

//-----吃食-----

else if(getSymbol(nextSign)=='' && !isDead(nextSign))

{

secondSign=firstSign;

snakeMap[nextSignlSign][nextSignrSign]='@';

//只压入蛇头

snakeBodypush(nextSign);

firstSign=snakeBodyback();

eatFood=true;

//加分

score+=20;

return true;

}

//-----死亡-----

else {cout<<"Dead"<<endl;cout<<"Your last total score is "<<score<<endl; return false;}

}

void Csnake::ShowGame()

{

for(int i=0;i<line;i++)

{

for(int j=0;j<row;j++)

cout<<snakeMap[i][j];

cout<<endl;

}

Sleep(1);

system("cls");

}

int main()

{

Csnake s(20);

sInitInstance();

//sShowGame();

int noDead;

do

{

sShowGame();

noDead=sUpdataGame();

}while (noDead);

system("pause");

return 0;

}

这个代码可以运行的,记得给分啦

话说,我用c++11和opengl在mac上写过一个贪吃蛇,可以参考一下:>

主要是贪吃蛇本身的核心逻辑才70行左右(偷懒用了STL的list,不用自己实现链表)

当时装逼用英语写的注释,请不要打我!

核心逻辑大概是这样子 每次移动的时候:

1 如果蛇头出界或者碰到自己,game over

2 如果蛇头吃到食物,蛇变长。

3 否则(蛇头什么都没碰到)移动蛇。

用链表存蛇身的各个位置的话,链表尾部当蛇头,那蛇变长的 *** 作很简单:把新的蛇头位置插入链表尾部。

移动蛇的 *** 作也简单:把新的蛇头位置插入链表尾部,删除链表头。

然后,剩下的事情还有两个:

1 拿到蛇身的所有位置,在对应位置画上蛇身

2 获取用户输入,改变蛇的移动方向

那。。。你需要了解的只有:

opengl怎么在指定位置画方块(圆圈)

opengl怎么处理用户输入

没了。

/

C/C++贪吃蛇游戏,zjlj,2015316

/

#define DEBUG 0 //当程序在调试阶段时 DEBUG为 1

#include<iostream>

#include<windowsh>

#include<timeh>

#include<conioh>

using namespace std;

void readini(FILE fphead, int score, char argv[]) //创建或打开一个和运行文件对应的ini文件,读取最高纪录

{

char filename[200],pfilename;

int flag=-1,i;

strcpy(filename,argv[0]);

for(i=0;filename[i]!='\0';i++)

{

if (''==filename[i])flag=1;

}

if(1==flag)

{

filename[i-1]='i';

filename[i-2]='n';

filename[i-3]='i';

}

else

{

filename[i]='';

filename[i+1]='i';

filename[i+2]='n';

filename[i+3]='i';

filename[i+4]='\0';

}

for(;filename[i]!='\\'&&i>=0;i--)pfilename=&filename[i];

if ( (fphead=fopen(pfilename, "rb+"))==NULL)

{

if ( (fphead=fopen(pfilename, "wb+"))==NULL)

{

printf("无法创建或打开\"%s\"文件\n",pfilename);

system("pause");

exit(0);

}

}

else

{

fread(score,sizeof(int),1,fphead);

}

}

void writeini(FILE fphead, int score, char argv[]) //打开一个和运行文件对应的ini文件,写入最高纪录

{

char filename[200],pfilename;

int flag=-1,i;

strcpy(filename,argv[0]);

for(i=0;filename[i]!='\0';i++)

{

if (''==filename[i])flag=1;

}

if(1==flag)

{

filename[i-1]='i';

filename[i-2]='n';

filename[i-3]='i';

}

else

{

filename[i]='';

filename[i+1]='i';

filename[i+2]='n';

filename[i+3]='i';

filename[i+4]='\0';

}

for(;filename[i]!='\\'&&i>=0;i--)pfilename=&filename[i];

if ( (fphead=fopen(pfilename, "wb+"))==NULL)

{

printf("无法写入\"%s\"文件,磁盘写保护!\n",pfilename);

system("pause");

exit(0);

}

else

{

rewind(fphead);

fwrite(score,sizeof(int),1,fphead);

fclose(fphead);

}

}

void gotoxy(int x,int y)//光标定位,光标定位函数SetConsoleCursorPosition是左上角位置是0,0然后向左向下延伸

{

COORD pos;

posX=2y;

posY=x;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);

}

void color(int a)//颜色函数

{

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);

}

void Refresh(int q[][22], int grade, int gamespeed, int length,int score) // 输出贪吃蛇棋盘

{

int i,j;

for(i=0;i<22;i++)

{

for(j=0;j<22;j++)

{

if(q[i][j]==0)//输出棋盘空白

{

gotoxy(i,j);

color(11);

cout<<"■";

}

if(q[i][j]==1||q[i][j]==2)//输出棋盘墙壁

{

gotoxy(i,j);

color(11);

cout<<"□";

}

if(q[i][j]==3)//输出蛇头

{

gotoxy(i,j);

color(14);

cout<<"★";

}

if(q[i][j]==4)//输出蛇身

{

gotoxy(i,j);

color(12);

cout<<"◆";

}

if(q[i][j]==5)//输出果子

{

gotoxy(i,j);

color(12);

cout<<"●";

}

}

if(i==0) cout << "\t";

if(i==1) cout << "\t等级为:" << grade;//显示等级

if(i==3) cout << "\t自动前进时间";

if(i==4) cout << "\t间隔为:" << gamespeed << "ms";//显示时间

if(i==6) cout << "\t历史最高分为:" << score << "分";

if(i==7) cout << "\t你现在得分为:" << (length+(grade-1)8)10 << "分";

if(i==8) cout << "\t";

if(i==9) cout << "\t游戏说明:";

if(i==10) cout << "\t(1)用小键盘方向键控制";

if(i==11) cout << "\t蛇头运动方向;";

if(i==12) cout << "\t(2)蛇每吃一个果子蛇身";

if(i==13) cout << "\t增加一节;";

if(i==14) cout << "\t(3)蛇咬到自己或碰到墙";

if(i==15) cout << "\t壁游戏结束。";

if(i==18) cout << "\t";

if(i==19) cout << "\tC/C++语言作业:";

if(i==20) cout << "\tzjlj,20150316 ";

}

}

int main(int argc, char argv[]){

int tcsQipan[22][22]; // 贪吃蛇棋盘是一个二维数组(如2222,包括墙壁)

int i,j,score,directiontemp;

FILE fpini;//fpini 信息文件

readini(&fpini, &score, argv);//读取ini文件的最高纪录

if (score<0)//最高成绩小于零设置为零,初建文件会是负数

score=0;

while(1)

{

for(i=1;i<=20;i++)

for(j=1;j<=20;j++)

tcsQipan[i][j]=0; //贪吃蛇棋盘相应坐标标上中间空白部分的标志0

for(i=0;i<=21;i++)

tcsQipan[0][i] = tcsQipan[21][i] = 1; //贪吃蛇棋盘相应坐标标上上下墙壁的标志1

for(i=1;i<=20;i++)

tcsQipan[i][0] = tcsQipan[i][21] = 2; //贪吃蛇棋盘相应坐标标上左右墙壁的标志2

int tcsZuobiao[2][500]; //蛇的坐标数组

for(i=0; i<4; i++)

{

tcsZuobiao[0][i] = 1;//蛇身和蛇头的x坐标

tcsZuobiao[1][i] = i + 1;//蛇身和蛇头的y坐标

}

int head = 3,tail = 0;//标示蛇头和蛇尾的数组偏移量

for(i=1;i<=3;i++)

tcsQipan[1][i]=4; //蛇身

tcsQipan[1][4]=3; //蛇头

int x1, y1; // 随机出果子

srand(time(0));//设置随机种子

do

{

x1=rand()%20+1;

y1=rand()%20+1;

}

while(tcsQipan[x1][y1]!=0);//如果不是在空白处重新出果子

tcsQipan[x1][y1]=5;//贪吃蛇棋盘相应坐标标上果子的标志5

color(12);

cout<<"\n\n\t\t\t\t贪吃蛇游戏即将开始 !"<<endl;//准备开始

long start,starttemp;

int grade = 1, length = 4; //设置初始等级和蛇的初始长度

int gamespeed = 500; //设置初始前进时间间隔

for(i=3;i>=0;i--)

{

start=clock();

while(clock()-start<=1000);

system("cls");

if(i>0)

cout << "\n\n\t\t\t\t进入倒计时:" << i << endl; //倒计时显示

else

Refresh(tcsQipan,grade,gamespeed,length,score); //初始棋盘显示

}

int timeover=1,otherkey=1;//初始化超时时间和按键判断参数

char direction = 77; // 设置初始情况下,向右运动

int x=tcsZuobiao[0][head],y=tcsZuobiao[1][head];//保存蛇头坐标到x,y变量

while(1)//运行一局游戏

{

start = clock();

while((timeover=((starttemp=clock())-start<=gamespeed))&&!kbhit());//如果有键按下或时间超过自动前进时间间隔则终止循环

if(direction==72||direction==80||direction==75 ||direction==77)

directiontemp=direction;//保留上一次方向按键

//starttemp=gamespeed+start-starttemp;//保留停留时间

if(timeover)

{

#if (DEBUG==1)

direction = getch();//调试代码

#else

if((direction =getch())==-32)

direction = getch();

#endif

}

#if (DEBUG==1)//调试代码

start=clock();

while(clock()-start<=2000);

gotoxy(24,4);

cout << "\t按键ASCII代码"<<(int)direction<<" "<<endl;

#endif

if(!(direction==72||direction==80||direction==75 ||direction==77))

{

otherkey=0;// 按键非方向键,otherkey设置为0

}

else

{

otherkey=1;// 按键为方向键,otherkey设置为1

}

if(direction==72 && directiontemp==80)//忽略反方向按键

{

direction=32;

otherkey=0;

//start = clock();

//while(clock()-start<=starttemp);

}

else if(direction==80 && directiontemp==72)

{

direction=32;//设置按键为非方向键

otherkey=0;// 按键为非方向键,otherkey设置为0

// start = clock();

//while(clock()-start<=starttemp);//补偿等待时间

}

else if(direction==75 && directiontemp==77)

{

direction=32;

otherkey=0;

//start = clock();

//while(clock()-start<=starttemp);

}

else if(direction==77 && directiontemp==75)

{

direction=32;

otherkey=0;

//start = clock();

//while(clock()-start<=starttemp);

}

switch(direction)//判断方向键

{

case 72: x= tcsZuobiao[0][head]-1; y= tcsZuobiao[1][head];break; // 向上

case 80: x= tcsZuobiao[0][head]+1; y= tcsZuobiao[1][head];break; // 向下

case 75: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]-1;break; // 向左

case 77: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]+1;break; // 向右

default: break;

}

if(x==0 || x==21 ||y==0 || y==21) // 蛇头碰到墙壁,结束本局游戏

{

gotoxy(22,12);

cout << "\t游戏已结束!" << endl;

if(score>=(length+(grade-1)8)10)//判断是否破记录

{

gotoxy(10,7);

color(12);

cout << "闯关失败 加油耶!" << endl;

fclose(fpini);//关闭ini文件

}

else

{

gotoxy(10,7);

color(12);

cout << "恭喜您打破记录" << endl;

score=(length+(grade-1)8)10;

writeini(&fpini, &score, argv);//写入ini文件的最高纪录

}

gotoxy(23,12);

cout << "按回车键重新开始,按ESC退出游戏" << endl;//显示的提示

break;//退出该局游戏

}

if(tcsQipan[x][y]!=0&&!(x==x1&&y==y1)&&tcsQipan[x][y]!=3) // 蛇头碰到蛇身,结束本局游戏

{

gotoxy(22,12);

cout << "\t游戏已结束!" << endl;

if(score>=(length+(grade-1)8)10)//判断是否破记录

{

gotoxy(10,7);

color(12);

cout << "闯关失败 加油耶!" << endl;

fclose(fpini);//关闭ini文件

}

else

{

gotoxy(10,7);

color(12);

cout << "恭喜您打破记录" << endl;

score=(length+(grade-1)8)10;

writeini(&fpini, &score, argv);//写入ini文件的最高纪录

}

gotoxy(23,12);

cout << "按回车键重新开始,按ESC退出游戏" << endl;//显示的提示

break;//退出该局游戏

}

/

游戏运行时的核心算法开始

/

if(x==x1 && y==y1) // 吃果子,长度加1

{

length ++;

if(length>=8)//长度大于等于8重新计算长度,等级加1

{

length -= 8;//重新计算长度

grade ++;//等级加1

if(gamespeed>50)//控制最快速度为50

gamespeed = 550 - grade 50; // 改变自动前进时间间隔

}

tcsQipan[x][y]= 3;//贪吃蛇棋盘相应坐标现在蛇头标志改为蛇头标志3

tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = 4;//贪吃蛇棋盘相应坐标原来蛇头标志改为蛇身标志4

head = (head+1)%400;//防止数组越界

tcsZuobiao[0][head] = x;//蛇头的x坐标

tcsZuobiao[1][head] = y;//蛇头的y坐标

do//随机出果子

{

x1=rand()%20+1;

y1=rand()%20+1;

}

while(tcsQipan[x1][y1]!=0);//如果不是在空白处重新出果子

tcsQipan[x1][y1]=5;//贪吃蛇棋盘相应坐标标上果子的标志5

gotoxy(22,12);

cout << "\t游戏进行中!" << endl;

Refresh(tcsQipan,grade,gamespeed,length,score);

}

else // 不吃果子

{

if(otherkey)

{

tcsQipan [tcsZuobiao[0][tail]][tcsZuobiao[1][tail]]=0;

tail=(tail+1)%400;//防止数组越界

tcsQipan [tcsZuobiao[0][head]][tcsZuobiao[1][head]]=4;

head=(head+1)%400;//防止数组越界

tcsZuobiao[0][head]=x;//蛇头的x坐标

tcsZuobiao[1][head]=y;//蛇头的y坐标

tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]=3;

gotoxy(22,12);

cout << "\t游戏进行中!" << endl;

Refresh(tcsQipan,grade,gamespeed,length,score);

}

else

{

gotoxy(22,12);

cout << "\t游戏暂停中!" << endl;

}

}

/

游戏运行时的核心算法结束

/

}

while(1)

{

while(!kbhit());

if((direction =getch())==13)//按回车键开始下一局

break;

if(direction ==27)//按ESC退出游戏

exit(0);

}

system("cls");//清除屏幕重新开始

}

return 0;

}

//友情提示:如想速度快点,请改小_sleep(500)函数中参数

#include

#include

#include

#include

#include

const int H = 8; //地图的高

const int L = 16; //地图的长

char GameMap[H][L]; //游戏地图

int key; //按键保存

int sum = 1, over = 0; //蛇的长度, 游戏结束(自吃或碰墙)

int dx[4] = {0, 0, -1, 1}; //左、右、上、下的方向

int dy[4] = {-1, 1, 0, 0};

struct Snake //蛇的每个节点的数据类型

{

int x, y; //左边位置

int now; //保存当前节点的方向, 0,1,2,3分别为左右上下

}Snake[HL];

const char Shead = '@'; //蛇头

const char Sbody = '#'; //蛇身

const char Sfood = ''; //食物

const char Snode = ''; //''在地图上标示为空

void Initial(); //地图的初始化

void Create_Food(); //在地图上随机产生食物

void Show(); //刷新显示地图

void Button(); //取出按键,并判断方向

void Move(); //蛇的移动

void Check_Border(); //检查蛇头是否越界

void Check_Head(int x, int y); //检查蛇头移动后的位置情况

int main()

{

Initial();

Show();

return 0;

}

void Initial() //地图的初始化

{

int i, j;

int hx, hy;

system("title 贪吃蛇"); //控制台的标题

memset(GameMap, '', sizeof(GameMap)); //初始化地图全部为空''

system("cls");

srand(time(0)); //随机种子

hx = rand()%H; //产生蛇头

hy = rand()%L;

GameMap[hx][hy] = Shead;

Snake[0]x = hx; Snake[0]y = hy;

Snake[0]now = -1;

Create_Food(); //随机产生食物

for(i = 0; i < H; i++) //地图显示

{

for(j = 0; j < L; j++)

printf("%c", GameMap[i][j]);

printf("\n");

}

printf("\n小小C语言贪吃蛇\n");

printf("按任意方向键开始游戏\n");

getch(); //先接受一个按键,使蛇开始往该方向走

Button(); //取出按键,并判断方向

}

void Create_Food() //在地图上随机产生食物

{

int fx, fy;

while(1)

{

fx = rand()%H;

fy = rand()%L;

if(GameMap[fx][fy] == '') //不能出现在蛇所占有的位置

{

GameMap[fx][fy] = Sfood;

break;

}

}

}

void Show() //刷新显示地图

{

int i, j;

while(1)

{

_sleep(500); //延迟半秒(1000为1s),即每半秒刷新一次地图

Button(); //先判断按键在移动

Move();

if(over) //自吃或碰墙即游戏结束

{

printf("\n游戏结束\n");

printf(" >_<\n");

getchar();

break;

}

system("cls"); //清空地图再显示刷新吼的地图

for(i = 0; i < H; i++)

{

for(j = 0; j < L; j++)

printf("%c", GameMap[i][j]);

printf("\n");

}

printf("\n小小C语言贪吃蛇\n");

printf("按任意方向键开始游戏\n");

}

}

void Button() //取出按键,并判断方向

{

if(kbhit() != 0) //检查当前是否有键盘输入,若有则返回一个非0值,否则返回0

{

while(kbhit() != 0) //可能存在多个按键,要全部取完,以最后一个为主

key = getch(); //将按键从控制台中取出并保存到key中

switch(key)

{ //左

case 75: Snake[0]now = 0;

break;

//右

case 77: Snake[0]now = 1;

break;

//上

case 72: Snake[0]now = 2;

break;

//下

case 80: Snake[0]now = 3;

break;

}

}

}

void Move() //蛇的移动

{

int i, x, y;

int t = sum; //保存当前蛇的长度

//记录当前蛇头的位置,并设置为空,蛇头先移动

x = Snake[0]x; y = Snake[0]y; GameMap[x][y] = '';

Snake[0]x = Snake[0]x + dx[ Snake[0]now ];

Snake[0]y = Snake[0]y + dy[ Snake[0]now ];

Check_Border(); //蛇头是否越界

Check_Head(x, y); //蛇头移动后的位置情况,参数为: 蛇头的开始位置

if(sum == t) //未吃到食物即蛇身移动哦

for(i = 1; i < sum; i++) //要从蛇尾节点向前移动哦,前一个节点作为参照

{

if(i == 1) //尾节点设置为空再移动

GameMap[ Snake[i]x ][ Snake[i]y ] = '';

if(i == sum-1) //为蛇头后面的蛇身节点,特殊处理

{

Snake[i]x = x;

Snake[i]y = y;

Snake[i]now = Snake[0]now;

}

else //其他蛇身即走到前一个蛇身位置

{

Snake[i]x = Snake[i+1]x;

Snake[i]y = Snake[i+1]y;

Snake[i]now = Snake[i+1]now;

}

GameMap[ Snake[i]x ][ Snake[i]y ] = '#'; //移动后要置为'#'蛇身

}

}

void Check_Border() //检查蛇头是否越界

{

if(Snake[0]x 0 || Snake[0]x >= H

|| Snake[0]y 0 || Snake[0]y >= L)

over = 1;

}

void Check_Head(int x, int y) //检查蛇头移动后的位置情况

{

if(GameMap[ Snake[0]x ][ Snake[0]y ] == '') //为空

GameMap[ Snake[0]x ][ Snake[0]y ] = '@';

else

if(GameMap[ Snake[0]x ][ Snake[0]y ] == '') //为食物

{

GameMap[ Snake[0]x ][ Snake[0]y ] = '@';

Snake[sum]x = x; //新增加的蛇身为蛇头后面的那个

Snake[sum]y = y;

Snake[sum]now = Snake[0]now;

GameMap[ Snake[sum]x ][ Snake[sum]y ] = '#';

sum++;

Create_Food(); //食物吃完了马上再产生一个食物

}

else

over = 1;

}

以上就是关于贪吃蛇 C语言 简易程序设计全部的内容,包括:贪吃蛇 C语言 简易程序设计、贪吃蛇游戏的C语言编程、怎样在mac上用C语言写出一个贪吃蛇游戏等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9968362.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存