c++贪吃蛇源码.docx
- 文档编号:8724615
- 上传时间:2023-05-14
- 格式:DOCX
- 页数:7
- 大小:16.56KB
c++贪吃蛇源码.docx
《c++贪吃蛇源码.docx》由会员分享,可在线阅读,更多相关《c++贪吃蛇源码.docx(7页珍藏版)》请在冰点文库上搜索。
c++贪吃蛇源码
贪吃蛇代码具体
2008-04-1021:
44
//greedsnake.cpp
#include
#include
#include
#include
#include
#include
#include"conf.h"
typedefstructnode
{
intx,y;
structnode*next;
}Node;
typedefstruct
{
Node*head,*tail;
intlength;
}Snake;
typedefstruct
{
intleft,top,right,bottom;
}Frame;
typedefenum //四个方向
{
up,down,left,right
}Direction;
typedefenum
{
false,true
}bool;//*/
voidInitGraphMode(); //初始化图形驱动
voidCloseGraphMode();
voidFoot(int,int);
voidHead(int,int);
voidCreateFrame(); //完成整个游戏框架的绘制
voidCreateSnake(); //创建一条两个节点的蛇,蛇的每一节是队列中的一个节点
boolPlayGame(); //游戏的主体函数,
intHit(int,int); //判断是否越界,或者撞到自身,两个参数分别是新的头接点的x,y坐标
boolGameOver(); //绘制游戏结束时弹出的对话框
voidEnqueue(Node); //入队函数
NodeDequeue(); //出队函数
voidClearKeyBuf(); //清除键盘缓冲,此函数可以消除不停的按无效键的影响
Snakesnake;
Frameframe;
voidmain()
{
InitGraphMode();
do
{
CreateFrame();
}while(PlayGame());
CloseGraphMode();
}
voidInitGraphMode()
{
intgdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"../bgi/");
cleardevice();
}
voidCloseGraphMode()
{
cleardevice();
closegraph();
}
voidCreateFrame()
{
setbkcolor(CYAN);
//下面的四行代码用于计算主框架的左上角和右下角的坐标
frame.left=(getmaxx()+1-BlockWidth*RowOfFrame)/2;
frame.top=(getmaxy()+1-BlockHeight*ColumnOfFrame)/2;
frame.right=frame.left+BlockWidth*RowOfFrame;
frame.bottom=frame.top+BlockHeight*ColumnOfFrame;
Head(frame.left+100,frame.top-20);
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(frame.left,frame.top,frame.right,frame.bottom);
setlinestyle(SOLID_LINE,1,1);
setcolor(DARKGRAY);
line(frame.left,frame.top,frame.right,frame.top);
line(frame.left,frame.top,frame.left,frame.bottom);
setlinestyle(SOLID_LINE,1,1);
setcolor(WHITE);
line(frame.left,frame.bottom,frame.right,frame.bottom);
line(frame.right,frame.top,frame.right,frame.bottom);
setlinestyle(DOTTED_LINE,1,1);
setcolor(BLUE);
for(introw=1;row line(frame.left+row*BlockWidth,frame.top,frame.left+row*BlockWidth,frame.bottom); for(intcolumn=1;column line(frame.left,frame.top+column*BlockHeight,frame.right,frame.top+column*BlockHeight); Foot(frame.left,frame.bottom+20); } voidCreateSnake() { Node*node1=newNode; Node*node2=newNode; node1->x=frame.left+BlockWidth; node1->y=frame.top; node1->next=NULL; snake.tail=node1; node2->x=frame.left; node2->y=frame.top; node2->next=snake.tail; snake.head=node2; snake.length=2; setfillstyle(SOLID_FILL,BLUE); bar(snake.head->x+1,snake.head->y+1,snake.head->x+BlockWidth-1,snake.head->y+BlockHeight-1); bar(snake.tail->x+1,snake.tail->y+1,snake.tail->x+BlockWidth-1,snake.tail->y+BlockHeight-1); } boolPlayGame() { intspeed=300,key; DirectionCurrentDirection=right; NoderandomNode; NodenewNode,outNode; boolneednode=true; booloverlap=false; intrandx,randy; CreateSnake(); while(true) { if(neednode==true) { randomize(); do { randx=frame.left+rand()%RowOfFrame*BlockWidth; randy=frame.top+rand()%ColumnOfFrame*BlockHeight; for(Node*p=snake.head;p! =NULL;p=p->next)//hititself if(randx==p->x&&randy==p->y) {overlap=true;break;} } while(overlap==true); randomNode.x=randx; randomNode.y=randy; randomNode.next=NULL; setfillstyle(SOLID_FILL,RED); bar(randomNode.x+1,randomNode.y+1,randomNode.x+BlockWidth-1,randomNode.y+BlockHeight-1); neednode=false; } if((key=bioskey (1))! =0) { switch(key) { caseESC: returnfalse; caseUP: if(CurrentDirection! =down) CurrentDirection=up; ClearKeyBuf(); break; caseDOWN: if(CurrentDirection! =up) CurrentDirection=down; ClearKeyBuf(); break; caseLEFT: if(CurrentDirection! =right) CurrentDirection=left; ClearKeyBuf(); break; caseRIGHT: if(CurrentDirection! =left) CurrentDirection=right; ClearKeyBuf(); break; casePAGEUP: speed=speed-100; if(speed<100) speed=100; ClearKeyBuf(); break; casePAGEDOWN: speed=speed+100; if(speed>500) speed=500; ClearKeyBuf(); break; default: break; } } intheadx=snake.tail->x; intheady=snake.tail->y; switch(CurrentDirection) { caseup: heady-=BlockHeight;break; casedown: heady+=BlockHeight;break; caseleft: headx-=BlockWidth;break; caseright: headx+=BlockWidth;break; } if(Hit(headx,heady)) //whetherthesnakehitthewalloritself returnGameOver(); else { //eat if(headx==randomNode.x&&heady==randomNode.y) { Enqueue(randomNode); setfillstyle(SOLID_FILL,BLUE); bar(randomNode.x+1,randomNode.y+1,randomNode.x-1+BlockWidth,randomNode.y-1+BlockHeight); neednode=true; } else //noeat { newNode.x=headx; newNode.y=heady; newNode.next=NULL; Enqueue(newNode); outNode=Dequeue(); setfillstyle(SOLID_FILL,LIGHTGRAY); bar(outNode.x+1,outNode.y+1,outNode.x+BlockWidth-1,outNode.y+BlockHeight-1); setfillstyle(SOLID_FILL,BLUE); bar(newNode.x+1,newNode.y+1,newNode.x-1+BlockWidth,newNode.y-1+BlockHeight); } } delay(speed); } } voidClearKeyBuf() { do bioskey(0); while(bioskey (1)); } voidFoot(intx,inty) { setcolor(BLUE); outtextxy(x,y,"writer: [T]RealXLE-MAIL: realgeneral@"); } voidHead(intx,inty) { setcolor(RED); outtextxy(x,y,"GREEDYSNAKE"); } voidEnqueue(NodeinNode) { Node*p=newNode; p->x=inNode.x; p->y=inNode.y; p->next=inNode.next; snake.tail->next=p; snake.tail=p; snake.length++; } NodeDequeue() { Node*p=snake.head; NodeoutNode=*p; snake.head=p->next; snake.length--; deletep; returnoutNode; } intHit(intx,inty) { if(x return1; Node*p=snake.head->next; for(inti=snake.length-1;i>3;i--,p=p->next)//hititself if(x==p->x&&y==p->y) return1; return0; } boolGameOver() { intx=getmaxx()/2-50; inty=getmaxy()/2-20; setfillstyle(SOLID_FILL,DARKGRAY); bar(x+3,y+3,x+103,y+43); setfillstyle(SOLID_FILL,MAGENTA); bar(x,y,x+100,y+40); setlinestyle(0,3,1); setcolor(RED); rectangle(x,y,x+100,y+40); outtextxy(x+20,y+10,"GAGEOVER! "); charc; while(true) //按q或Q表示退出程序,按r或R表示重新开始游戏 { c=getch(); if(c=='q'||c=='Q') returnfalse; elseif(c=='r'||c=='R') returntrue; } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 贪吃 源码