实验_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
上传于||暂无简介
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩1页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢TC程序_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
上传于||文档简介
&&小车随机位置显示
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩1页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢利用C++实现的贪吃蛇游戏 - 【游戏程序】 - 游艺网|GAME798|-游戏美术、游戏培训、游戏艺术工厂、游戏开发交流论坛!
后使用快捷导航没有帐号?
只需一步,快速开始
查看: 976|回复: 1
利用C++实现的贪吃蛇游戏
利用C++实现的贪吃蛇游戏
系统分类: &
作品版权:本作品版权归原作者所有,本帖为游艺网用户发布,如侵犯你的权益,请联系客服。 976  0  1
& && && && & 在tc 3.0下调试通过,因为tc 3.0不支持bool 类型,所以程序中自定义了个枚举类型变量bool假如在vc或者bc中运行,可以把这个bool类型注释掉.贪吃蛇的核心算法时如何实现移动和吃掉食物.没有碰到食物的时候,把当前运动方向上的下个节点入队,并以蛇节点的颜色重绘这个节点.然后把头指针所指的节点出队,并以游戏框架内部背景色重绘出队的节点,这样就可以达到移动的效果.而在吃到食物的时候,则只需把食物入队即可.
// greedsnake.cpp #include &bios.h& #include
&conio.h& #include &dos.h& #include &graphics.h& #include &stdlib.h& #include &time.h& #include &conf.h& typedef strUCt node { int x,y; struct node * }N typedef struct { Node *head,* }S typedef struct& && && && && && &&& { int left,top,right, }F typedef enum& && && && && && &&&//四个方向 { up,down,left,right }D typedef enum { false,true } void InitGraphMode();& && && && && && && &//初始化图形驱动 void CloseGraphMode();& && && && && & void Foot(int,int);& && && && && && && && && & void Head(int,int); void CreateFrame();& && && && && && &&&//完成整个游戏框架的绘制& & void CreateSnake();& && && && && && &&&//创建一条两个节点的蛇,蛇的每一节是队列中的一个节点 bool PlayGame();& && && && && && && && &//游戏的主体函数, int Hit(int,int);& && && && && && && && && && & //判定是否越界,或者撞到自身,两个参数分别是新的头接点的x,y坐标 bool GameOver();& && && && && && && && &//绘制游戏结束时弹出的对话框& && && && && && && &&& void Enqueue(Node);& && && && && && &&&//入队函数
Node Dequeue();& && && && && && && && &//出队函数 void ClearKeyBuf();& && && && && && && &&&//清除键盘缓冲,此函数可以消除不停的按无效键的影响 S F void main() { InitGraphMode(); do { CreateFrame(); }while(PlayGame()); CloseGraphMode(); } void InitGraphMode() { int gdriver=DETECT, initgraph(&gdriver,&gmode,&../bgi/&); cleardevice(); } void CloseGraphMode() { cleardevice(); closegraph(); } void CreateFrame() { setbkcolor(CYAN);& && && && && && &&& //下面的四行代码用于计算主框架的左上角和右下角的坐标 frame.left=(getmaxx()+1-BlockWidth*RowOfFrame)/2; frame.top=(getmaxy()+1-BlockHeight*ColumnOfFrame)/2; frame.right=frame.left+BlockWidth*RowOfF frame.bottom=frame.top+BlockHeight*ColumnOfF 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(int row=1;row&RowOfFrow++) line(frame.left+row*BlockWidth,frame.top,frame.left+row*BlockWidth,frame.bottom); for(int column=1;column&ColumnOfFcolumn++) line(frame.left,frame.top+column*BlockHeight,frame.right,frame.top+column*BlockHeight); Foot(frame.left,frame.bottom+20); }
void CreateSnake() { Node *node1=new N Node *node2=new N node1-&x=frame.left+BlockW node1-&y=frame. node1-&next=NULL; snake.tail=node1; node2-&x=frame. node2-&y=frame. node2-&next=snake. 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); } bool PlayGame() { int speed=300, Direction CurrentDirection= Node randomN Node newNode,outN bool neednode= bool overlap= int randx, CreateSnake(); while(true) { if(neednode==true) { randomize();
&P p align=left& do { randx=frame.left+rand()%RowOfFrame*BlockW randy=frame.top+rand()%ColumnOfFrame*BlockH&& for(Node *p=snake.p!=NULL;p=p-&next)//hit itself if(randx==p-&x&&randy==p-&y) {overlap=} } while(overlap==true); randomNode.x= randomNode.y= randomNode.next=NULL; setfillstyle(SOLID_FILL,RED); bar(randomNode.x+1,randomNode.y+1,randomNode.x+BlockWidth-1,randomNode.y+BlockHeight-1); neednode= } if((key=bioskey(1))!=0) { switch(key) { case ESC:&& case UP: if(CurrentDirection!=down) CurrentDirection= ClearKeyBuf(); case DOWN: if(CurrentDirection!=up) CurrentDirection= ClearKeyBuf(); case LEFT: if(CurrentDirection!=right) CurrentDirection= ClearKeyBuf(); case RIGHT: if(CurrentDirection!=left) CurrentDirection= ClearKeyBuf(); case PAGEUP:speed=speed-100; if(speed&100) speed=100; ClearKeyBuf(); case PAGEDOWN:speed=speed+100; if(speed&500) speed=500; ClearKeyBuf(); default : } } int headx=snake.tail-&x; int heady=snake.tail-&y; switch(CurrentDirection) { case up: heady-=BlockH case down: heady+=BlockH case left: headx-=BlockW case right: headx+=BlockW } if(Hit(headx,heady))& & //whether the snake hit the wall or itself return GameOver(); 更多内容请看C/C++技术专题&&网络游戏攻略&&专题,或
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= } else& &&&//no eat { newNode.x= newNode.y= 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); } } void ClearKeyBuf() { do bioskey(0); while(bioskey(1)); } void Foot(int x,int y) { setcolor(BLUE); outtextxy(x,y,&writer:[T]RealXL E-MAIL:&); } void Head(int x,int y) { setcolor(RED);
outtextxy(x,y,&GREEDY SNAKE&); } void Enqueue(Node inNode) { Node *p=new N p-&x=inNode.x; p-&y=inNode.y; p-&next=inNode. snake.tail-&next=p; snake.tail=p; snake.length++; }
更多内容请看C/C++技术专题&&网络游戏攻略&&游戏开发专题,或
Node Dequeue() { Node *p=snake. Node outNode=*p; snake.head=p-& snake.length--; delete p; return outN } int Hit(int x,int y) { if(x&frame.leftx&=frame.righty&frame.topy&=frame.bottom)//hit the wall return 1; Node *p=snake.head-& for(int i=snake.length-1;i&3;i--,p=p-&next)//hit itself if(x==p-&x&&y==p-&y) return 1; return 0; } bool GameOver() { int x=getmaxx()/2-50; int y=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,&GAGE OVER!&); char c; while(true)& && && && && && && && && && && && && & //按q或Q表示退出程序,按r或R表示重新开始游戏 { c=getch(); if(c=='q'c=='Q') return else if(c=='r'c=='R') return } } //conf.h& && && && && && && && && && && && && && && && && && && &&& #ifndef _conf_h #define _conf_h #define RowOfFrame& & 20& && && &//主框架的行数 #define ColumnOfFrame 20& && && &//主框架的列数 #define BlockWidth& & 15& && &&&//每个蛇节点的宽度 #define BlockHeight& &15& && &&&//每个蛇节点的高度 #define UP& &18432 #define DOWN&&20480 #define LEFT&&19200 #define RIGHT&&19712 #define ESC& &283 #define ENTER&&7181 #define PAGEUP&&18688 #define PAGEDOWN 20736 #endif
帖子推广:
把推广链接复制到你的qq群,qq空间或qq好友。每次推广奖励一金币,每天最多奖励100金币。
楼主辛苦了,谢谢!
798热心会员
798热心会员
微信二维码(超级移动图书馆)
(怦然心动998)
(忧郁的蓝色)
(云淡风轻)
第三方登录: