您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > C语言小游戏源代码《贪吃蛇》
C语言小游戏源代码《贪吃蛇》#defineN200/*定义全局常量*/#definem25#includegraphics.h#includemath.h#includestdlib.h#includedos.h#defineLEFT0x4b00#defineRIGHT0x4d00#defineDOWN0x5000#defineUP0x4800#defineEsc0x011binti,j,key,k;structFood/*构造食物结构体*/{intx;inty;intyes;}food;structGoods/*构造宝贝结构体*/{intx;inty;intyes;}goods;structBlock/*构造障碍物结构体*/{intx[m];inty[m];intyes;}block;structSnake{/*构造蛇结构体*/intx[N];inty[N];intnode;intdirection;intlife;}snake;structGame/*构建游戏级别参数体*/{intscore;intlevel;intspeed;}game;/*定义函数*/voidinit(void);/*定义图形驱动*/voidclose(void);/*定义关闭函数*/voiddrawk(void);/*定义界面函数*/voidgameover(void);/*定义游戏结束函数*/voidgameplay(void);/*定义游戏主函数*/voidprscore(void);/*定义得分函数*/voidmain(void){/*主函数体,调用以下四个函数*/init();setbkcolor(7);drawk();gameplay();close();}voidinit(void){/*构建图形驱动函数*/intgd=DETECT,gm;initgraph(&gd,&gm,);cleardevice();}voiddrawk(void){/*构建游戏界面函数*//*setbkcolor(LIGHTGREEN);*/charstr3[50];setfillstyle(SOLID_FILL,BLUE);/*条型边框,显示版本信息*/bar3d(48,9,610,38,1,45);setcolor(YELLOW);/*版本信息*/sprintf(str3,Version:5.01,PowerwingStudio);outtextxy(330,20,str3);setfillstyle(LTSLASH_FILL,YELLOW);/*设定墙边的填充形式*/bar3d(48,48,58,462,0,0);/*设定墙边*/bar3d(48,39,611,48,0,0);bar3d(48,452,611,462,0,0);bar3d(602,39,611,462,0,0);}voidgameplay(void){/*构建游戏主函数*//*初始化游戏角色*/randomize();/*随机数发生器*/goods.yes=1;block.yes=1;food.yes=1;/*场景中需建立新的食物*/snake.life=1;/*初始化蛇生命值*/snake.direction=1;/*蛇起始的移动方向定义为向右*/snake.x[0]=100;snake.y[0]=100;/*蛇头的位置坐标初始化*/snake.x[1]=110;snake.y[1]=100;snake.node=2;/*蛇初始化节数,共两节只有蛇头*//*初始化障碍物的数组*/block.x[0]=170;block.y[0]=270;/*level1*/block.x[1]=410;block.y[1]=310;block.x[2]=300;block.y[2]=200;block.x[3]=320;block.y[3]=420;block.x[4]=250;block.y[4]=350;block.x[5]=220;block.y[5]=320;/*level2*/block.x[6]=310;block.y[6]=410;block.x[7]=400;block.y[7]=500;block.x[8]=230;block.y[8]=230;block.x[9]=280;block.y[9]=280;block.x[10]=170;block.y[10]=280;/*level3*/block.x[11]=420;block.y[11]=310;block.x[12]=310;block.y[12]=200;block.x[13]=320;block.y[13]=400;block.x[14]=250;block.y[14]=260;/*level4*/block.x[15]=220;block.y[15]=330;block.x[16]=130;block.y[16]=410;block.x[17]=310;block.y[17]=510;block.x[18]=230;block.y[18]=340;block.x[19]=280;block.y[19]=380;block.x[20]=270;block.y[20]=170;/*level5*/block.x[21]=410;block.y[21]=450;block.x[22]=190;block.y[22]=200;block.x[23]=150;block.y[23]=320;block.x[24]=270;block.y[24]=350;block.x[25]=340;block.y[25]=320;game.score=0;game.speed=50000;game.level=1;prscore();/*得分初始化*/while(1){/*判断为真可以按Esc退出循环结束游戏*/while(!kbhit()){/*无按键按下时,蛇自己移动身体*/if(game.level==1){/*画出障碍物*/for(j=0;j5;j++){setcolor(5);/**/rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);block.yes=0;}}if(game.level==2){/*画出障碍物*/for(j=0;j9;j++){setcolor(5);/**/rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);block.yes=0;}}if(game.level==3){/*画出障碍物*/for(j=0;j14;j++){setcolor(5);/**/rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);block.yes=0;}}if(game.level==4){/*画出障碍物*/for(j=0;j19;j++){setcolor(5);/**/rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);block.yes=0;}}if(game.level==5){/*画出障碍物*/for(j=0;j25;j++){setcolor(5);/**/rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);block.yes=0;}}if(food.yes==1){/*需要画出新的食物*/food.x=rand()%400+60;/*获得间隔60的随机数食物坐标值*/food.y=rand()%350+60;while(food.x%10!=0)/*判断坐标值是否满足被10整除,否,自动增加*/food.x++;while(food.y%10!=0)food.y++;food.yes=0;/*新的食物已经产生*/}if(goods.yes==1){/*需要画出新的宝物*/goods.x=rand()%380+60;/*获得间隔60的随机数宝贝坐标值*/goods.y=rand()%320+80;while(goods.x%10!=0)/*判断坐标值是否满足被10整除,否,自动增加*/goods.x++;while(goods.y%10!=0)goods.y++;goods.yes=0;/*新的宝贝已经产生*/}if(goods.yes==0){/*新宝贝产生,应显示出来*/setcolor(0);/*擦除*/rectangle(goods.x,goods.y,goods.x+10,goods.y-10);delay(50);/*延时*/setcolor(YELLOW);goods.x=goods.x+random(10)-random(20);/*随机数增量*/goods.y=goods.y+random(10)-random(20);while(goods.x%10!=0)/*判断变化后的坐标值是否满足被10整除,否,自动增加*/goods.x++;while(goods.y%10!=0)goods.y++;rectangle(goods.x,goods.y,goods.x+10,goods.y-10);/*重画出宝贝*/if(goods.x65||goods.x585||goods.y65|goods.y445){/*判定宝贝是否越界*/setcolor(0);/*擦除越界的宝贝*/rectangle(goods.x,goods.y,goods.x+10,goods.y-10);goods.yes=1;/*越界后重新生成宝贝*/}}if(food.yes==0){/*新食物产生,应显示出来*/setcolor(GREEN);setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设定当前线型*/rectangle(food.x,food.y,food.x+10,food.y-10);}for(i=snake.node-1;i0;i--){/*取得需重画的蛇的节数*/snake.x[i]=snake.x[i-1];/*最后一节的坐标值等于倒数第二节的坐标值*/snake.y[i]=snake.y[i-1];}switch(snake.direction){/*判断蛇头的移动方向*/case1:snake.x[0]+=10;break;/*向右*/case2:snake.x[0]-=10;break;/*向左*/case3:snake.y[0]-=10;break;/*向上*/case4:snake.y[0]+=10;break;/*向下*/}for(i=3;isnake.node;i++){/*超过4节后,判断蛇自身碰撞*/if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0]){/*即自身的任一节坐标值与蛇头坐标相等*/for(i=1;isnake.node-1;i++){/*擦除自己碰撞后位置蛇的身子*/setcolor(0);rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);}snake.life-=1;/*生命值减少一*/snake.node-=5;prscore();/*输出结果*/if(snake.life==0){/*判断生命值是否为0*/gameover();/*游戏结束*/break;/*退出内循环*/}}}if(snake.x[0]55||snake.x[0]
本文标题:C语言小游戏源代码《贪吃蛇》
链接地址:https://www.777doc.com/doc-6239304 .html