C语言简单可用的贪吃蛇游戏文档格式.docx
《C语言简单可用的贪吃蛇游戏文档格式.docx》由会员分享,可在线阅读,更多相关《C语言简单可用的贪吃蛇游戏文档格式.docx(23页珍藏版)》请在冰点文库上搜索。
image);
DWORD*pMem=GetImageBuffer(&
for(inti=0;
i<
10*10;
i++)
pMem[i]=BGR(RGB(0,0,i*156/(10*10)));
SetWorkingImage(NULL);
putimage(m_x,m_y,&
shortReturnFull()//返回格子状态
returnm_full;
Cell()
{
m_x=0,m_y=0,m_full=0;
m_color=BLACK;
};
//游戏区类
classGameArea
intm_random1,m_random2;
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<
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<
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].display(BLACK);
CreatFood();
EndBatchDraw();
//蛇类,定义蛇的结构和行为
classSnake
structnode//蛇身链表
intm_x,m_y;
structnode*next;
//下一结点
structnode*prior;
//上一结点
};
structnode*head;
structnode*last;
structnode*now;
inttoward;
//蛇头朝向
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;
m_x=300;
m_y=300;
now=(structnode*)malloc(sizeof(structnode));
next=now;
now->
prior=head;
next=NULL;
m_y=290;
last=now;
toward=DOWN;
voidSnakeAdd()
now=head;
prior=(structnode*)malloc(sizeof(structnode));
now=now->
prior;
prior=NULL;
next=head;
switch(toward)
caseUP:
now->
m_x=head->
m_x;
m_y=head->
m_y-10;
break;
caseDOWN:
m_x=head->
m_y=head->
m_y+10;
caseLEFT:
m_x-10;
m_y;
caseRIGHT:
m_x+10;
head=now;
//设置当前结点为蛇头
voidSnakeDel()//释放蛇尾结点(删除蛇尾)
last=last->
free(last->
next);
last->
next=NULL;
voidSnakeMove()
SnakeAdd();
//增加蛇头
SnakeDel();
//删除蛇尾
//游戏类,用来初始化游戏的参数,及实现其他游戏操作
classGame
intd;
Game()
d=1;
closegraph();
initgraph(800,600);
setbkmode(TRANSPARENT);
//使用透明的输出背景
LOGFONTf;
getfont(&
f);
f.lfHeight=50;
_tcscpy(f.lfFaceName,_T("
黑体"
));
f.lfQuality=ANTIALIASED_QUALITY;
setfont(&
voidFailGame()//游戏失败显示的画面
setcolor(RED);
setfont(70,0,_T("
微软雅黑"
outtextxy(150,265,_T("
游戏失败"
setcolor(WHITE);
setfont(40,0,_T("
outtextxy(160,365,_T("
退出按'
O'
重玩按'
R'
"
voidWinGame()//游戏胜利时显示的画面
setfont(70,0,_T("
outtextxy(150,265,_T("
胜利!
FlushBatchDraw();
Sleep(10000);
exit(0);
voidExitGame()//游戏中途退出
BeginBatchDraw();
if(d==1)
setfont(25,0,_T("
bar(15,200,500,450);
setfont(45,0,_T("
outtextxy(130,220,_T("
EXITTHEGAME?
setfont(40,0,_T("
outtextxy(150,300,_T("
YES(Y)orNO(N)"
elseif(d==2)
setcolor(BLACK);
EXITTHEGAME?
////////////////////////////////////////////
//管理区类,定义管理区域的相关操作
classManageArea
doublem_time;
shortm_score;
shortm_leave;
boolm_pause;
TCHARm_str_leave[2],m_str_time[33],m_str_score[5];
COLORREFm_bgcolor;
voidDisplayTime()//显示当前耗时
_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)是画二维条形图的函数
outtextxy(680,430,m_str_time);
voidDisplayScore()//显示当前分数
_stprintf(m_str_score,_T("
%d"
),m_score);
bar(680,460,800,485);
outtextxy(680,460,m_str_score);
voidTimeAdd(doubleadd)//增加时间(可在类外直接调用实现动态刷新时间)
m_time+=add;
DisplayTime();
voidScoreAdd(shortadd)//增加分数(可在类外直接调用实现动态刷新分数)
m_score+=add;
DisplayScore();
voidDisplayPause()//显示暂停或者开始
//setfont(25,0,_T("
bar(625,200,800,300);
setcolor(RGB(12,45,19));
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;
//格式化转换类型,将m_score输出到m_str_score
m_bgcolor=BLACK;
bar(601,0,800,600);
setfont(40,0,_T("
setcolor(YELLOW);
outtextxy(625,30,_T("
贪吃蛇"
setfont(20,0,_T("
setfillstyle(BLUE);
fillcircle(640,140,25);
outtextxy(670,140,_T("
制作:
软五莫智豪"
setcolor(WHITE);
outtextxy(625,340,_T("
退出(O键)"
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键)"
outtextxy(720,400,m_str_leave);
outtextxy(680,460,m_str_score);
outtextxy(680,430,m_str_time);
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'
:
f=1;
break;
w'
W'
if(s.toward==UP)Keepdown=true;
elseif(s.toward!
=DOWN&
&
s.toward!
=UP)s.toward=UP;
break;
s'
S'
if(s.toward==DOWN)Keepdown=true;
=UP&
=DOWN)s.toward=DOWN;
a'
A'
if(s.toward==LEFT)Keepdown=true;
=RIGHT&
=LEFT)s.toward=LEFT;
d'
D'
if(s.toward==RIGHT)Keepdown=true;
=LEFT&
=RIGHT)s.toward=RIGHT;
p'
P'
manager.m_pause=!
manager.m_pause;
//设置暂停或开始
manager.DisplayPause();
//显示暂停或开始
r'
ReInit();
//----------------------------------------------------------------
if(f==1)
game.d=1;
f=0;
game.ExitGame();
while((t!
='
y'
)&
(t!
Y'
n'
('
N'
))
t=getch();
if((t=='
)||(t=='
t='
elseif((t=='
game.d=2;
game.ExitGame();
t='
//----------------------------------------------