C语言简单可用的贪吃蛇游戏.docx
《C语言简单可用的贪吃蛇游戏.docx》由会员分享,可在线阅读,更多相关《C语言简单可用的贪吃蛇游戏.docx(23页珍藏版)》请在冰点文库上搜索。
C语言简单可用的贪吃蛇游戏
//作者:
莫智豪
//名称:
贪吃蛇游戏
//绝对绚丽、可用的贪吃蛇游戏,欢迎参考和指点
#include
#include
#include
#include
#defineUP1
#defineDOWN2
#defineLEFT3
#defineRIGHT4
//格子类
classCell
{
private:
shortm_x;
shortm_y;
shortm_full;//0为空,1为阻挡,2为食物
COLORREFm_color;//格子的颜色
public:
friendclassGameArea;
voidsetxy(shortx,shorty)
{
m_x=x;m_y=y;
}
voidsetfull(shortfull)
{
m_full=full;
}
voiddisplay(COLORREFcolor)
{
m_color=color;
setfillstyle(m_color);
bar(m_x,m_y,m_x+7,m_y+7);
}
voidlaserDisplay()//显示镭射状态的格子
{
IMAGEimage(10,10);
SetWorkingImage(&image);
DWORD*pMem=GetImageBuffer(&image);
for(inti=0;i<10*10;i++)
pMem[i]=BGR(RGB(0,0,i*156/(10*10)));
SetWorkingImage(NULL);
putimage(m_x,m_y,&image);
}
shortReturnFull()//返回格子状态
{
returnm_full;
}
Cell()
{
m_x=0,m_y=0,m_full=0;
m_color=BLACK;
}
};
//游戏区类
classGameArea
{
private:
intm_random1,m_random2;
public:
Cellm_game[60][60];
friendvoidMessageDispose();
boolCreatFood()//随机产生食物
{
srand(time(NULL));
m_random1=rand()%58+1;
m_random2=rand()%58+1;
if(m_game[m_random1][m_random2].m_full==0)//检查生成的食物坐标是否在障碍上
{
m_game[m_random1][m_random2].display(GREEN);
m_game[m_random1][m_random2].m_full=2;
returntrue;
}
//如果随机的食物正好出现在蛇身上,则进入下面的循环寻找可以生成食物的地方
for(m_random1=1;m_random1<59;m_random1++)
{
for(m_random2=1;m_random2<59;m_random2++)
{
if(m_game[m_random2][m_random2].m_full==0)
{
m_game[m_random1][m_random2].m_full=2;
}
}
}
//如果没有找到可以生成食物的地方,则通关
returnfalse;
}
voidDelFood()//删除食物
{
m_game[m_random1][m_random2].m_full=0;
}
voidChangeColor(intflag1,intflag2,COLORREFcolor)//设置指定格子的颜色
{
m_game[flag1][flag2].display(color);
}
voidInit()
{
intflag1,flag2;
BeginBatchDraw();
setfillstyle(BLACK);
bar(0,0,600,600);
for(flag1=0;flag1<60;flag1++)
{
for(flag2=0;flag2<60;flag2++)
{
if(flag1==0||flag1==59||flag2==0||flag2==59)
{
m_game[flag1][flag2].setfull
(1);
m_game[flag1][flag2].setxy(flag1*10,flag2*10);
m_game[flag1][flag2].display(WHITE);
}
else
{
m_game[flag1][flag2].setfull(0);
m_game[flag1][flag2].setxy(flag1*10,flag2*10);
m_game[flag1][flag2].display(BLACK);
}
}
}
CreatFood();
EndBatchDraw();
}
};
//蛇类,定义蛇的结构和行为
classSnake
{
private:
structnode//蛇身链表
{
intm_x,m_y;
structnode*next;//下一结点
structnode*prior;//上一结点
};
structnode*head;
structnode*last;
structnode*now;
public:
inttoward;//蛇头朝向
friendvoidMessageDispose();
friendintHitDetect();
friendvoidReInit();
Snake()
{
head=NULL;
last=NULL;
now=NULL;
}
voidInit()//初始化蛇的结构
{
if(head!
=NULL)//重玩游戏时,释放原先的链表所有结点
{
for(now=head->next;now->next!
=NULL;now=now->next)
{
free(now->prior);
}
}
head=(structnode*)malloc(sizeof(structnode));//为蛇头分配内存
head->prior=NULL;
head->m_x=300;
head->m_y=300;
now=(structnode*)malloc(sizeof(structnode));
head->next=now;
now->prior=head;
now->next=NULL;
now->m_x=300;
now->m_y=290;
last=now;
toward=DOWN;
}
voidSnakeAdd()
{
now=head;
now->prior=(structnode*)malloc(sizeof(structnode));
now=now->prior;
now->prior=NULL;
now->next=head;
switch(toward)
{
caseUP:
now->m_x=head->m_x;
now->m_y=head->m_y-10;
break;
caseDOWN:
now->m_x=head->m_x;
now->m_y=head->m_y+10;
break;
caseLEFT:
now->m_x=head->m_x-10;
now->m_y=head->m_y;
break;
caseRIGHT:
now->m_x=head->m_x+10;
now->m_y=head->m_y;
break;
}
head=now;//设置当前结点为蛇头
}
voidSnakeDel()//释放蛇尾结点(删除蛇尾)
{
last=last->prior;
free(last->next);
last->next=NULL;
}
voidSnakeMove()
{
SnakeAdd();//增加蛇头
SnakeDel();//删除蛇尾
}
};
//游戏类,用来初始化游戏的参数,及实现其他游戏操作
classGame
{
public:
intd;
Game()
{
d=1;
}
friendvoidMessageDispose();
voidInit()
{
closegraph();
initgraph(800,600);
setbkmode(TRANSPARENT);//使用透明的输出背景
LOGFONTf;
getfont(&f);
f.lfHeight=50;
_tcscpy(f.lfFaceName,_T("黑体"));
f.lfQuality=ANTIALIASED_QUALITY;
setfont(&f);
}
voidFailGame()//游戏失败显示的画面
{
setcolor(RED);
setfont(70,0,_T("微软雅黑"));
outtextxy(150,265,_T("游戏失败"));
setcolor(WHITE);
setfont(40,0,_T("微软雅黑"));
outtextxy(160,365,_T("退出按'O',重玩按'R'"));
}
voidWinGame()//游戏胜利时显示的画面
{
setcolor(RED);
setfont(70,0,_T("微软雅黑"));
outtextxy(150,265,_T("胜利!
"));
FlushBatchDraw();
Sleep(10000);
exit(0);
}
voidExitGame()//游戏中途退出
{
BeginBatchDraw();
if(d==1)
{
setfont(25,0,_T("微软雅黑"));
setfillstyle(BLACK);
bar(15,200,500,450);
setcolor(RED);
setfont(45,0,_T("微软雅黑"));
outtextxy(130,220,_T("EXITTHEGAME?
"));
setfont(40,0,_T("微软雅黑"));
outtextxy(150,300,_T("YES(Y)orNO(N)"));
}
elseif(d==2)
{
setfont(25,0,_T("微软雅黑"));
setfillstyle(BLACK);
bar(15,200,500,450);
setcolor(BLACK);
setfont(45,0,_T("微软雅黑"));
outtextxy(130,220,_T("EXITTHEGAME?
"));
setfont(40,0,_T("微软雅黑"));
outtextxy(150,300,_T("YES(Y)orNO(N)"));
}
EndBatchDraw();
}
};
////////////////////////////////////////////
//管理区类,定义管理区域的相关操作
////////////////////////////////////////////
classManageArea
{
private:
doublem_time;
shortm_score;
shortm_leave;
boolm_pause;
TCHARm_str_leave[2],m_str_time[33],m_str_score[5];
COLORREFm_bgcolor;
voidDisplayTime()//显示当前耗时
{
BeginBatchDraw();
_stprintf(m_str_time,_T("%.1lf"),m_time);
setfont(25,0,_T("微软雅黑"));//设置字体(nHeight指定高度nWidth字符的平均宽度(0为自适应)lpszFace字体名称)
setfillstyle(m_bgcolor);
bar(680,430,800,455);//bar(intleft,inttop,intright,intbottom)是画二维条形图的函数
setcolor(RED);
outtextxy(680,430,m_str_time);
EndBatchDraw();
}
voidDisplayScore()//显示当前分数
{
BeginBatchDraw();
_stprintf(m_str_score,_T("%d"),m_score);
setfont(25,0,_T("微软雅黑"));
setfillstyle(m_bgcolor);
bar(680,460,800,485);
setcolor(RED);
outtextxy(680,460,m_str_score);
EndBatchDraw();
}
public:
friendvoidMessageDispose();
voidTimeAdd(doubleadd)//增加时间(可在类外直接调用实现动态刷新时间)
{
m_time+=add;
DisplayTime();
}
voidScoreAdd(shortadd)//增加分数(可在类外直接调用实现动态刷新分数)
{
m_score+=add;
DisplayScore();
}
voidDisplayPause()//显示暂停或者开始
{
BeginBatchDraw();
_stprintf(m_str_score,_T("%d"),m_score);
//setfont(25,0,_T("微软雅黑"));
setfillstyle(m_bgcolor);
bar(625,200,800,300);
setcolor(RGB(12,45,19));
setfont(40,0,_T("微软雅黑"));
if(!
m_pause)
outtextxy(630,250,_T("进行中..."));
else
{
setcolor(RED);
outtextxy(630,250,_T("暂停"));
}
EndBatchDraw();
}
voidInit()//初始化管理界面
{
m_time=0.0;
m_score=0;
m_leave=1;
m_pause=false;
_stprintf(m_str_score,_T("%d"),m_score);//格式化转换类型,将m_score输出到m_str_score
_stprintf(m_str_time,_T("%.1lf"),m_time);
m_bgcolor=BLACK;
BeginBatchDraw();
setfillstyle(BLACK);
bar(601,0,800,600);
setfont(40,0,_T("微软雅黑"));
setcolor(YELLOW);
outtextxy(625,30,_T("贪吃蛇"));
setfont(20,0,_T("微软雅黑"));
setcolor(YELLOW);
setfillstyle(BLUE);
fillcircle(640,140,25);
outtextxy(670,140,_T("制作:
软五莫智豪"));
setfont(25,0,_T("微软雅黑"));
setcolor(WHITE);
outtextxy(625,340,_T("退出(O键)"));
setfont(25,0,_T("微软雅黑"));
setcolor(WHITE);
line(620,330,780,330);
line(620,490,780,490);
outtextxy(625,430,_T("时间:
"));
outtextxy(625,460,_T("分数:
"));
outtextxy(625,400,_T("暂停/开始(P键)"));
outtextxy(625,370,_T("重新游戏(R键)"));
setcolor(RED);
outtextxy(720,400,m_str_leave);
outtextxy(680,460,m_str_score);
outtextxy(680,430,m_str_time);
EndBatchDraw();
DisplayPause();
}
};
////////////////////////////////////////////
//声明游戏需要的类
////////////////////////////////////////////
Gamegame;//用于初始化游戏、设置游戏相关参数
GameAreaa;//用于初始化游戏区域,设置,改变游戏区域相关参数
ManageAreamanager;//用于初始化管理区域,设置管理区相关参数
Snakes;//用于初始化蛇,数据化蛇,操作蛇
////////////////////////////////////////////
//游戏的消息控制,流程控制函数
////////////////////////////////////////////
voidMessageDispose()//消息处理函数
{
charc;
//---------------------------
chart='';
intd=0;//我添加的
intf=0;
//---------------------------
s.now=NULL;
boolKeepdown=false;
while(true)
{
Keepdown=false;//是否持续按WASD中的一个按键
if(kbhit())
{
switch(c=getch())//处理按键消息(WASD)
{
case'o':
case'O':
{
f=1;
break;
}
case'w':
case'W':
if(s.toward==UP)Keepdown=true;
elseif(s.toward!
=DOWN&&s.toward!
=UP)s.toward=UP;
break;
case's':
case'S':
if(s.toward==DOWN)Keepdown=true;
elseif(s.toward!
=UP&&s.toward!
=DOWN)s.toward=DOWN;
break;
case'a':
case'A':
if(s.toward==LEFT)Keepdown=true;
elseif(s.toward!
=RIGHT&&s.toward!
=LEFT)s.toward=LEFT;
break;
case'd':
case'D':
if(s.toward==RIGHT)Keepdown=true;
elseif(s.toward!
=LEFT&&s.toward!
=RIGHT)s.toward=RIGHT;
break;
case'p':
case'P':
manager.m_pause=!
manager.m_pause;//设置暂停或开始
manager.DisplayPause();//显示暂停或开始
break;
case'r':
case'R':
ReInit();
break;
}
}
//----------------------------------------------------------------
if(f==1)
{
game.d=1;
f=0;
game.ExitGame();
while((t!
='y')&&(t!
='Y')&&(t!
='n')&&('N'))
{
t=getch();
}
if((t=='y')||(t=='Y'))
{
t='';
break;
}
elseif((t=='n')||(t=='N'))
{
game.d=2;
game.ExitGame();
t='';
}
}
//----------------------------------------------