#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();
}
无法将参数 1 从“const char [13]”转换为“const wchar_t ” c:\users\
类型不一样不可以转换哦, 一个是char ,一个是 wchar_t ,当然不能转换
建议你学习炫彩界面库, 比MFC简单, 好多年不用MFC了
达到你说的要求,可以使用kbhit()函数,上百度搜一下它的用法,这个函数可以检测到游戏中是否有按键被按下,如果没有就使用一个死循环使蛇身一直移动,如果检测到了有按键被按下,就判断是否是方向键或者程序中设置的其他功能键(如ESC退出键,调速键等),如果不是,不做响应,如果是,就就bioskey()函数接收这个键,并根据这个键值做出相应的响应!
写游戏要注意模块化,你这样全写在main里很乱的。
使用数组解决贪吃蛇的问题有点挠头,最好是自己构造一个合适的数据类型。
还有就是学习一下<graphicsh>这个头文件,只需要里面的几个函数就可以设计贪吃蛇的图形界面。
给你个现成的贪吃蛇游戏,代码不长,仔细看一下里面的数据结构和算法思想。
/ 贪吃蛇游戏 /
#define N 200
#include <graphicsh>
#include <stdlibh>
#include <dosh>
#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"); /
initgraph(&gd,&gm,"c:\\JMSOFT\\DRV");
}
/开始画面,左上角坐标为(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();/随机数发生器/
foodyes=1;/1表示需要出现新食物,0表示已经存在食物/
snakelife=0;/活着/
snakedirection=1;/方向往右/
snakex[0]=100;snakey[0]=100;/蛇头/
snakex[1]=110;snakey[1]=100;
snakenode=2;/节数/
PrScore();/输出得分/
while(1)/可以重复玩游戏,压ESC键结束/
{
while(!kbhit())/在没有按键的情况下,蛇自己移动身体/
{
if(foodyes==1)/需要出现新食物/
{
foodx=rand()%400+60;
foody=rand()%350+60;
while(foodx%10!=0)/食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到/
foodx++;
while(foody%10!=0)
foody++;
foodyes=0;/画面上有食物了/
setcolor(GREEN);
rectangle(foodx,foody,foodx+10,foody-10);
}
for(i=snakenode-1;i>0;i--)/蛇的每个环节往前移动,也就是贪吃蛇的关键算法/
{
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
/1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头/
switch(snakedirection)
{
case 1:snakex[0]+=10;break;
case 2: snakex[0]-=10;break;
case 3: snakey[0]-=10;break;
case 4: snakey[0]+=10;break;
}
for(i=3;i<snakenode;i++)/从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来/
{
if(snakex[i]==snakex[0]&&snakey[i]==snakey[0])
{
GameOver();/显示失败/
snakelife=1;
break;
}
}
if(snakex[0]<55||snakex[0]>595||snakey[0]<55||
snakey[0]>455)/蛇是否撞到墙壁/
{
GameOver();/本次游戏结束/
snakelife=1; /蛇死/
break;
}
if(snakex[0]==foodx&&snakey[0]==foody)/吃到食物以后/
{
setcolor(0);/把画面上的食物东西去掉/
rectangle(foodx,foody,foodx+10,foody-10);
snakex[snakenode]=-20;snakey[snakenode]=-20;
/新的一节先放在看不见的位置,下次循环就取前一节的位置/
snakenode++;/蛇的身体长一节/
foodyes=1;/画面上需要出现新的食物/
score+=10;
PrScore();/输出新得分/
}
setcolor(4);/画出蛇/
for(i=0;i<snakenode;i++)
rectangle(snakex[i],snakey[i],snakex[i]+10,
snakey[i]-10);
delay(gamespeed);
setcolor(0);/用黑色去除蛇的的最后一节/
rectangle(snakex[snakenode-1],snakey[snakenode-1],
snakex[snakenode-1]+10,snakey[snakenode-1]-10);
} /endwhile(!kbhit)/
if(snakelife==1)/如果蛇死就跳出循环/
break;
key=bioskey(0);/接收按键/
if(key==ESC)/按ESC键退出/
break;
else
if(key==UP&&snakedirection!=4)
/判断是否往相反的方向移动/
snakedirection=3;
else
if(key==RIGHT&&snakedirection!=2)
snakedirection=1;
else
if(key==LEFT&&snakedirection!=1)
snakedirection=2;
else
if(key==DOWN&&snakedirection!=3)
snakedirection=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();
}
这个程序有只有一个地方需要改,就是initgraph(&gd,&gm,"c:\\JMSOFT\\DRV");
这句代码中的第三个参数是 bgi文件的路径,你按下键盘上的alt + f 搜索一下 bgi在哪个目录下,把那个路径复制到这个地方就可以了直接运行了,不然可能提示你 初始化错误!。
可以Sleep函数设置延时来控制贪吃蛇的速度。
函数名: Sleep
功 能: 执行挂起一段时间
用 法: void Sleep(DWORD dwMilliseconds);
在VC中使用带上头文件
#include <windowsh>
( Sleep函数存放头文件:WinBaseh)
在gcc编译器中,使用的头文件因gcc版本的不同而不同
#include <unistdh>
Sleep()单位为毫秒,sleep()单位为秒(如果需要更精确可以用usleep单位为微秒)
返回值
若进程/线程挂起到参数所指定的时间则返回0,若有信号中断则返回剩余秒数。
例程:
/ --------------------snakeh --------------------/#ifndef SNAKE_H
#define SNAKE_H
#define LEFT 'a'
#define RIGHT 'd'
#define DOWN 's'
#define UP 'w'
#define ESC 27
#define N 200 /蛇的最大长度/
char key; /控制按键/
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;
#endif
/ --------------------snakec --------------------/
#include <graphicsh>
#include <stdlibh>
#include <conioh>
#include <timeh>
#include <stdioh>
#include "snakeh"
int score = 0;
int gamespeed = 100; //蛇运行速度
static void Init(void); /图形驱动/
static void Close(void); /图形结束/
static void Game_interface(void); /游戏界面/
static void GameOver(void); /结束游戏/
static void GamePlay(void); /游戏过程/
static void PrScore(void); /输出成绩/
/主函数/
int main(void)
{
Init();
Game_interface();
GamePlay();
Close();
return 0;
}
/图形驱动/
static void Init(void)
{
int gd=9,gm=2;
initgraph(&gd,&gm," ");
cleardevice();
}
/ 开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙 /
static void Game_interface(void)
{
int i;
setcolor(LIGHTCYAN); /setbkcolor(LIGHTGREEN);/
setlinestyle(PS_SOLID,0,1); /设置线型/
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); /右边框/
}
}
/ 游戏主函数 /
static void GamePlay(void)
{
int i;
srand(time(NULL)); /随机数发生器/
foodyes = 1; /1表示需要出现新食物,0表示已经存在食物/
snakelife = 0; /活着/
snakedirection = 1; /方向往右/
snakex[0] = 100;
snakey[0] = 100;
snakex[1] = 110;
snakey[1] = 100;
snakenode = 2; /节数/
PrScore(); /输出得分/
while(1) /可以重复玩游戏,压ESC键结束/
{
while( !kbhit() ) /在没有按键的情况下,蛇自己移动/
{
if(foodyes == 1) /需要出现新食物/
{
foodx = rand()%400 + 60;
foody = rand()%350 + 60;
while(foodx%10 != 0) /食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到/
foodx++;
while(foody%10 != 0)
foody++;
foodyes = 0; /画面上有食物了/
}
if(foodyes == 0) /画面上有食物了就要显示/
{
setcolor(GREEN);
rectangle(foodx,foody,foodx + 10,foody - 10);
}
for(i=snakenode-1;i>0;i--) /蛇的每个环节往前移动,也就是贪吃蛇的关键算法/
{
snakex[i] = snakex[i-1];
snakey[i] = snakey[i-1];
}
/1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头/
switch(snakedirection)
{
case 1:
snakex[0] += 10;
break;
case 2:
snakex[0] -= 10;
break;
case 3:
snakey[0] -= 10;
break;
case 4:
snakey[0] += 10;
break;
}
/ 从蛇的第四节开始判断是否撞到自己 /
for(i=3;i<snakenode;i++)
{
if((snakex[i] == snakex[0]) && (snakey[i] == snakey[0]))
{
GameOver(); /显示失败/
snakelife = 1;
break;
}
}
if((snakex[0] < 55) || (snakex[0] > 595) || (snakey[0] < 55) || (snakey[0] > 455)) /蛇是否撞到墙壁/
{
GameOver(); /本次游戏结束/
snakelife = 1; /蛇死/
}
if(snakelife == 1) /以上两种判断以后,如果蛇死就跳出内循环,重新开始/
break;
if((snakex[0] == foodx) && (snakey[0] == foody)) /吃到食物以后/
{
setcolor(BLACK); /把画面上的食物东西去掉/
rectangle(foodx,foody,foodx+10,foody-10);
snakex[snakenode] = -20;
snakey[snakenode] = -20;
/ 新的一节先放在看不见的位置,下次循环就取前一节的位置 /
snakenode++; /蛇的身体长一节/
foodyes = 1; /画面上需要出现新的食物/
score += 10;
PrScore(); /输出新得分/
}
setcolor(RED); /画出蛇/
for(i=0;i<snakenode;i++)
rectangle(snakex[i],snakey[i],snakex[i]+10,snakey[i]-10);
Sleep(gamespeed); /用延迟控制贪吃蛇速度/
setcolor(BLACK); /用黑色去除蛇的的最后一节/
rectangle(snakex[snakenode-1],snakey[snakenode-1],
snakex[snakenode-1]+10,snakey[snakenode-1]-10);
} /endwhile(!kbhit)/
if(snakelife == 1) /如果蛇死就跳出循环/
break;
key=getch(); /接收按键/
if (key == ESC) break; /按ESC键退出/
switch(key)
{
case UP:
if(snakedirection != 4) /判断是否往相反的方向移动/
snakedirection = 3;
break;
case RIGHT:
if(snakedirection != 2)
snakedirection = 1;
break;
case LEFT:
if(snakedirection != 1)
snakedirection = 2;
break;
case DOWN:
if(snakedirection != 3)
snakedirection = 4;
break;
}
}/endwhile(1)/
}
/游戏结束/
static void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
setfont(56,0,"黑体");
outtextxy(200,200,"GAME OVER");
getch();
}
/输出成绩/
static void PrScore(void)
{
char str[10];
setfillstyle(YELLOW);
bar(50,15,220,35);
setcolor(BROWN);
setfont(16,0,"宋体");
sprintf(str,"score:%d",score);
outtextxy(55,16,str);
}
static void Close(void)
{
closegraph();
}
以上就是关于贪吃蛇 C语言 简易程序设计全部的内容,包括:贪吃蛇 C语言 简易程序设计、MFC贪吃蛇编程小问题、c语言简易版贪吃蛇怎么写用一个方块形状代表蛇的一个关节(如:printf("%c",4)是一个方块图样)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)