当前位置:文档之家› 贪吃蛇课程设计2

贪吃蛇课程设计2

#include<stdio.h>#include <conio.h>#include<stdlib.h>#include<windows.h>#include <time.h>#define LEN 30#define WID 25int snake[LEN][WID]={0};char snake_dir = 'a';//记录蛇头的移动方向int snake_point_x,snake_point_y;//记录蛇头的位置int snake_len=3;//记录蛇的长度clock_t now_time;//记录当前时间int waiting_time=300;//记录自动移动的时间间隔int eat_apple = 1; //吃到苹果表示为0int bake_raod = 0; //背景颜色int level=1;int score=-1;int apple=-1;HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);void Set_Color(int color) //设置颜色{SetConsoleTextAttribute(hConsole, color);}void hid_guanbiao(){//隐藏光标CONSOLE_CURSOR_INFO cursor_info={1 , 0};SetConsoleCursorInfo(hConsole, &cursor_info);}void goto_point(int x,int y){//设置光标位置COORD point;point.X=x;point.Y=y;SetConsoleCursorPosition(hConsole , point);}void game_over() //蛇死了{goto_point(29,6);printf("GRAM OVER!!");Sleep(1000);system("pause > nul");exit(0);}void clear_snake(){//擦除贪吃蛇int x,y;for(y = 0;y < WID; y++){for(x = 0;x < LEN;x++){goto_point(x*2 , y);if(snake[x][y]==snake_len)printf(" ");}}}void rand_apple(){ //随机产生苹果int x,y;do{x=(rand()%(LEN-2))+1;y=(rand()%(WID-2))+1;}while(snake[x][y]);snake[x][y]=-1;goto_point(x*2,y);Set_Color(rand()%8+1);printf("●");eat_apple=0;}void Pri_News() //打印信息{Set_Color(0xe);goto_point(73,4);score += level;printf("%3d", score);goto_point(73, 6);printf("%3d", level);goto_point(73, 8);printf("%3d",snake_len);goto_point(73, 10);printf("0.%dm/s", 100-waiting_time/10);goto_point(75, 12);printf("%d", apple);}void level_snake() //设置等级系统、速度{if((apple-1)/10==level){++level;if(waiting_time>50)waiting_time-=50;else if(waiting_time>20)waiting_time-=20;else if(waiting_time>10)waiting_time-=10;elsewaiting_time-=1;}}void move_snake()// 让蛇动起来{int x,y;for(x=0;x<LEN;x++){ // 先标记蛇头for(y=0;y<WID;y++){if(snake[x][y]==1){switch(snake_dir){ // 根据新的蛇头方向标记蛇头case 'w':if(y!=1)//snake_point_y=WID-2;//elsesnake_point_y=y-1;snake_point_x=x;break;case 's':if(y!=WID-2)//snake_point_y=1;// elsesnake_point_y=y+1;snake_point_x=x;break;case 'a':if(x!=1)//snake_point_x=LEN-2;//elsesnake_point_x=x-1;snake_point_y=y;break;case 'd':if(x!=LEN-2)// snake_point_x=1;// elsesnake_point_x=x+1;snake_point_y=y;break;default:break;}}}}if(snake[snake_point_x][snake_point_y]!=0&& snake[snake_point_x][snake_point_y]!=-1) game_over();if(snake_point_x==0||snake_point_x==30||snake_point_y==0||snake_point_y==25)game_over(); //蛇死啦;if(snake[snake_point_x][snake_point_y]<0){//吃苹果++snake_len;eat_apple = 1;}for(x=0;x<LEN;x++){//处理蛇尾for(y=0;y<WID;y++){if(snake[x][y]>0){if( snake[x][y]!=snake_len)snake[x][y]+=1;elsesnake[x][y]=0;}}}snake[snake_point_x][snake_point_y] = 1; //处理蛇头}void print_snake(){int x,y,color;for(y=0;y<WID;y++){for(x=0;x<LEN;x++){if(snake[x][y] == 1){ //蛇头Set_Color(0xe);goto_point(x*2, y);printf("◆");}if(snake[x][y] == 2){ //蛇脖color=rand()%15+1;if(color==14)color-=rand()%13+1;Set_Color(color);goto_point(x*2,y);printf("●");}if(snake[x][y] == snake_len){goto_point(x*2, y);Set_Color(0xe);printf("●");}}}}void get_input() //控制蛇的移动方向{if(kbhit()){switch(getch()){case 'w':case 72 :if(snake_dir != 's') snake_dir = 'w';break;case 's':case 80 :if(snake_dir != 'w') snake_dir = 's';break;case 'a':case 75 :if(snake_dir != 'd') snake_dir = 'a';break;case 'd':case 77 :if(snake_dir != 'a') snake_dir = 'd';break;default:break;}}if(clock() - now_time > waiting_time){//蛇到时间自动行走clear_snake();move_snake();print_snake();now_time =clock();}}void set_out(){ //边框int i,j;Set_Color(6);for(i=0;i<LEN;i++){goto_point(i*2,0);//snake[i][0]=1;printf("▼");goto_point(i*2,WID-1);//snake[i][WID-1]=1;printf("▼");}for(i=5;i<LEN/2;i++){goto_point(i*2,0);printf("▼");goto_point(i*2,(WID-1)/2);printf("▼");}for(j=0;j<WID;j++){goto_point(0,j);//snake[0][j]=1;printf("▼");goto_point(2*(LEN-1),j);//snake[LEN-1][j]=1;printf("▼");}}void inset(){char str[1001];system("color 0c"); //初始化system("title:贪吃蛇宝宝");system("mode con: cols=80 lines=25");hid_guanbiao();goto_point(61 , 2);printf("You name:");scanf("%s",&str);goto_point(61 , 4);printf("You Score:");goto_point(61 , 6);printf("You Level:");goto_point(61 , 8);printf("The length:");goto_point(61 ,10);printf("The speed:");goto_point(61 ,12);printf("Eated apple:");int i;for(i = 0 ;i < snake_len;i++){snake[10+i][15]=i+1; // 生成蛇}int x,y; //打印蛇for(y = 0;y < WID; y++){for(x = 0;x < LEN; x++){Set_Color(snake[x][y]+bake_raod); // 设置背景颜色goto_point(x*2 , y);printf("■");}}set_out();}void jm() // 初始化{ system("cls");system("color 0d");char a[10];goto_point(25,10);printf("组长:杨帆组员:曹喜平张轶徐晓亮");printf("\n");printf(" ---------------------------------\n");printf(" 请输入您的名字,按enter进入。

相关主题