#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=32000;
struct Food /食物的结构体/
{
int x; /食物的横坐标/
int y; /食物的纵坐标/
int yes; /食物是否出现的变量/
}food;
struct Snack /蛇的结构体/
{
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 PrScore(void); /分数输出函数/
DELAY(char ch)/调节游戏速度/
{
if(ch=='3')
{
delay(gamespeed); /delay是延迟函数/
delay(gamespeed);
}
else if(ch=='2')
{
delay(gamespeed);
}
}
Menu()/游戏开始菜单/
{
char ch;
printf("Please choose the gamespeed:\n");
printf("1-Fast 2-Normal 3-Slow\n");
printf("\nPlease Press The numbers\n");
do
{ch=getch();}
while(ch!='1'&&ch!='2'&&ch!='3');
clrscr();
return(ch);
}
/主函数/
void main(void)
{
int ch;
ch=Menu();
Init();
DrawK();
GamePlay(ch);
Close();
}
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
void DrawK(void)
{
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(char ch)
{
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) /可以重复游戏/
{
while(!kbhit()) /在没有按键的情况下蛇自己移动/
{
if(foodyes==1) /需要食物/
{
foodx=rand()%400+60;
foody=rand()%350+60; /使用rand函数随机产生食物坐标/
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]; /贪吃蛇的身体移动算法/
}
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(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);
DELAY(ch);
setcolor(0);
rectangle(snakex[snakenode-1],snakey[snakenode-1],snakex[snakenode-1]+10,snakey[snakenode-1]-10);
}
if(snakelife==1)
break;
key=bioskey(0); /接受按键/
if(key==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;
}
}
void GameOver(void)
{
cleardevice();
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,"scord:%d",score);
outtextxy(55,20,str);
}
void Close(void)
{
getch();
closegraph();
}
回答:Mremily
大师
6月3日 16:45 #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;
}snake;
void Init();
void Close();
void DrawK();
void GamePlay();
void GameOver();
void PrScore();
void main()
{ Init();
DrawK();
GamePlay();
Close();
}
void Init()
{int gd=DETECT,gm;
initgraph(&gd,&gm,"F:\\tuoboc2");/此处为turboc的路径,读者可以根据自己的电脑而改/
cleardevice();
}
void DrawK()
{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()
{randomize();
foodyes=1;
snakelife=0;
snakedirection=1;
snakex[0]=100;snakey[0]=100;
snakex[1]=110;snakey[1]=100 ;
snakenode=2;
PrScore();
while(1)
{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];
}
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(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);
}
if(snakelife==1)
break;
key=bioskey(0);
if(key==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;
}
}
void GameOver()
{
cleardevice();
PrScore();
setcolor(RED);
settextstyle(3,0,4);
outtextxy(100,100,"Mengmeng,i love you!");
getch();
}
void PrScore()
{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()
{ getch();
closegraph();
}
Mremily
以上就是关于求贪吃蛇的C++程序代码全部的内容,包括:求贪吃蛇的C++程序代码、用C语言编写贪吃蛇游戏的程序、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)