本文介绍了蛇游戏。身体没有被删除:(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 所以身体已经初始化到初始位置,并且在比赛开始时没有从棋盘上移除。离开这个作为一个障碍。 运行代码: gcc -o Snakes Snakes.c -lcucurses 然后 ./Snakes 我尝试过: #include< curses.h> #include< stdio.h> #include< time.h> #include< stdlib.h> #defined LEN 22 //这将是游戏的大小 #define DOWN 2 //按顺序定义移动值 #define UP 3 #define LEFT 4 #define RIGHT 5 / / 用于创建游戏地图的无效功能 void boardGeneration( int x, int y, char map [] [ LEN]) { int 行; int Cols; for (Rows = 0 ;行< ; LEN;行++) { for (Cols = 0 ; Cols < LEN; Cols ++) { mvaddch(Rows + 1,Cols + 1,map [Rows] [Cols]) ; } } mvaddch(y + 1,x + 1,' # ); // 蛇在baord周围移动 } // 确定食物下一个位置的功能 int foodLocation( void ){ srand(time(NULL)); // 生成随机种子 return (rand()%10); // 返回随机数的命令 } int main(){ int start = 1 ; printf( 你想开始玩Snakes吗?\ nnn 1代表Yes或2代表No: ); scanf( %d,& start); while (start == 1 ){ int 得分= 0 ; // 初始化游戏的得分值 char map [] [LEN] = { ------------- -------, // 定义地图边界 | |, | |, | |, | |, | |, | |, | |, | |, | |, | |, | |, | |, | |, | |, | |, | |, | |, | |, | |, | |, -------------- ------,}; map [foodLocation()] [foodLocation()] = ' $'; keypad(initscr(),TRUE); // 允许终端从键盘读取 curs_set( 0 ); // 删除光标符号 mvprintw( 2 , 23 , 您现在正在玩SNAKE'S); // 在游戏画面上打印评论 mvprintw( 3 , 23 , 四处移动你的屏幕箭头); mvprintw( 4 , 23 , 但是,要注意你的尾巴和墙壁!); mvprintw( 11 , 23 , 按'e'结束游戏); int PosX = 11 ; // 初始化位置到电路板中心 int PosY = 11 ; int inc = 1 ; int snakeLength = 5 ; // 使蛇初始长度 int xvalue [ 100 ]; // 存储以前的位置 int yvalue [ 100 ]; int i; int 运动; // 移动方向的占位符 char c; // 初始化的最终游戏字符 while (c!= ' e'){ mvprintw( 10 , 23 , 当前得分%d,得分); boardGeneration(PosX,PosY,map); // 更新显示的数组 c = getch(); // 从键盘读取单个字符 map [PosY] [PosX] = ' *'; // 更新数组中光标的位置 switch (c){ case UP: PosY--; movement = UP; break ; case DOWN: PosY ++; movement = DOWN; break ; case LEFT: PosX--; 运动=左; break ; case 右: PosX ++; 运动=正确; break ; } if (map [PosY] [PosX] == ' $'){ snakeLength = snakeLength + 1 ; // 增加蛇的长度 得分=得分+ 1 ; // 在食用食物时更新得分 if ((PosY + PosX)< = 20 ){ map [foodLocation()+ 10] [foodLocation()+ 10] = ' $'; // 更新地图上的食物位置 } else { map [foodLocation()] [foodLocation()] = ' $'; } } xvalue [ 0 ] = PosX; // 当前位置是数组中的第一个 yvalue [ 0 ] = POSY; for (i = snakeLength; i> 0; i - ){ // 用于在x位置传输过去位置的循环 xvalue [i] = xvalue [i-1]; } for (i = snakeLength; i> 0; i - ){ // 用于在y位置转移过去位置的循环 yvalue [i] = yvalue [i-1]; } if (inc == snakeLength){ map [yvalue [snakeLength]] [xvalue [snakeLength]] = ' '; // 删除旧位置 } if (inc< snakeLength){ inc = inc + 1; } if (map [PosY] [PosX] == ' - ' || map [PosY] [PosX] == ' |'){ // 游戏结束,如果xursor位置到达边界 break ; } if (map [PosY] [PosX] == ' *'&& snakeLength> 4){ // 如果游标进入正文游戏 break ; } } endwin(); if (map [PosY] [PosX] == ' - ' || map [PosY] [PosX] == ' |'){ // 打印命令告诉用户游戏结束的方式/原因 printf( \ n你刚碰到一堵墙就死了。你的分数是%d \ nn ,得分); } else if ( ' e'){ printf( \ n感谢玩耍,你的得分是%d \ n \\ n \\ n,得分); } else if (map [PosY] [PosX] == ' *'){ printf( \ n你只是想吃自己而死了。他的分数是%d \ n \\ n \\ n,得分); } printf( 你想再次玩Snakes吗?\ nType 1 for Yes或2代表否:); scanf( %d,& start); if (start == 2 ){ printf( \ n你选择不立即播放\ nBYE \ n); } } } 解决方案 '; 小键盘(initscr(),TRUE); // 允许终端从键盘读取 curs_set( 0 ); // 删除光标符号 mvprintw( 2 , 23 , 你现在正在玩SNAKE'S); // 在游戏画面上打印评论 mvprintw( 3 , 23 , 四处移动带箭头的屏幕); mvprintw( 4 , 23 , 但是,要注意你的尾巴和墙壁!); mvprintw( 11 , 23 , 按'e'结束游戏); int PosX = 11 ; // 初始化位置到电路板中心 int PosY = 11 ; int inc = 1 ; int snakeLength = 5 ; // 使蛇初始长度 int xvalue [ 100 ]; // 存储以前的位置 int yvalue [ 100 ]; int i; int 运动; // 移动方向的占位符 char c; // 初始化的最终游戏字符 while (c!= ' e'){ mvprintw( 10 , 23 , 当前得分%d,得分); boardGeneration(PosX,PosY,map); // 更新显示的数组 c = getch(); // 从键盘读取单个字符 map [PosY] [PosX] = ' *'; // 更新数组中光标的位置 switch (c){ case UP: PosY--; movement = UP; break ; case DOWN: PosY ++; movement = DOWN; break ; case LEFT: PosX--; 运动=左; break ; case 右: PosX ++; 运动=正确; break ; } if (map [PosY] [PosX] == ' '){ snakeLength = snakeLength + 1 ; // 增加蛇的长度 得分=得分+ 1 ; // 在食用食物时更新得分 if ((PosY + PosX)< = 20 ){ map [foodLocation()+ 10] [foodLocation()+ 10] = ' ; // 更新地图上的食物位置 } else { map [foodLocation()] [foodLocation()] = ' So the body has been initialised to the initial position and isn't being removed from the board when the game begins. Leaving to this acting as a an obstacle.To run the code:gcc -o Snakes Snakes.c -lncursesThen./SnakesWhat I have tried:#include <curses.h>#include <stdio.h>#include <time.h>#include <stdlib.h>#define LEN 22 //this will be the size of the game#define DOWN 2 //defines the movement value sequentially#define UP 3#define LEFT 4#define RIGHT 5//Void function for creating the games mapvoid boardGeneration(int x, int y, char map[][LEN]){int Rows;int Cols;for(Rows = 0; Rows < LEN ; Rows++){for(Cols = 0; Cols < LEN ; Cols++){mvaddch(Rows+1,Cols+1,map[Rows][Cols]);}}mvaddch(y+1, x+1, '#');//movement of the snakes head around the baord}//Function to determine next position of foodint foodLocation(void){srand(time(NULL)); //Generates random seedreturn (rand()%10); //Return comand of the random number}int main(){ int start = 1; printf("Do you want to start playing Snakes?\nType 1 for Yes or 2 for No: "); scanf("%d", &start); while (start == 1){int score=0;//Initialises score value for game char map[][LEN]={" -------------------- ", //Defines map boundaries "| |", "| |", "| |", "| |", "| |", "| |", "| |", "| |", "| |", "| |", "| |", "| |", "| |", "| |", "| |", "| |", "| |", "| |", "| |", "| |", " -------------------- ", };map[foodLocation()][foodLocation()]='$'; keypad(initscr(),TRUE);//Allows terminal to read from keypad curs_set(0); //Removes cursor symbolmvprintw(2,23," You are now playing SNAKE'S ");//Print comments on game screenmvprintw(3,23," Move around the screen with your arrows ");mvprintw(4,23," But, be mindful of your tail and the walls! ");mvprintw(11,23," End game by pressing 'e' "); int PosX=11; //initiaised location to the center of board int PosY=11; int inc=1; int snakeLength=5; //Make snake initial length int xvalue[100]; //stores previous position int yvalue[100]; int i; int movement; //place holder for movement direction char c; //initialised end game charactorwhile(c!='e'){mvprintw(10,23," Current score %d", score);boardGeneration(PosX,PosY,map); //updating displayed arrayc=getch(); //reading a single character from keyboardmap[PosY][PosX]='*';//updates position of curser in arrayswitch(c){case UP:PosY--;movement=UP;break;case DOWN:PosY++;movement=DOWN;break;case LEFT:PosX--;movement=LEFT;break;case RIGHT:PosX++;movement=RIGHT;break;}if(map[PosY][PosX]=='$'){snakeLength = snakeLength + 1;//increases the length of the snakescore = score + 1;//updates score when food is eatenif((PosY+PosX)<=20){map[foodLocation()+10][foodLocation()+10]='$';//updates food position on map}else{map[foodLocation()][foodLocation()]='$';}}xvalue[0]=PosX; //current position is first in the arrayyvalue[0]=PosY;for(i=snakeLength;i>0;i--){ //loop for transferring past postions in the x positionxvalue[i]=xvalue[i-1];}for(i=snakeLength;i>0;i--){//loop for transferring past postions in the y positionyvalue[i]=yvalue[i-1];}if(inc==snakeLength){map[yvalue[snakeLength]][xvalue[snakeLength]]=' '; //Removes old position}if(inc<snakeLength){inc=inc+1;}if(map[PosY][PosX]=='-'|| map[PosY][PosX]=='|'){ //game over if the xursor position hits the boundarybreak;}if(map[PosY][PosX]=='*' && snakeLength>4){ // game over if cursor runs into bodybreak;}}endwin();if (map[PosY][PosX]=='-'|| map[PosY][PosX]=='|'){// print comands to tell the user how/why the game endedprintf("\nYou just ran into a wall and died\nYour score was %d\n\n",score);}else if ('e'){printf("\nThanks for playing\nYour score was %d\n\n", score);}else if (map[PosY][PosX]=='*'){printf("\nYou just tried to eat yourself and died\nYor score was %d\n\n", score);}printf("Do you want to play Snakes again?\nType 1 for Yes or 2 for No: "); scanf("%d", &start); if (start == 2){ printf("\nYou chose not to play right now\nBYE\n"); }}} 解决方案 '; keypad(initscr(),TRUE);//Allows terminal to read from keypad curs_set(0); //Removes cursor symbolmvprintw(2,23," You are now playing SNAKE'S ");//Print comments on game screenmvprintw(3,23," Move around the screen with your arrows ");mvprintw(4,23," But, be mindful of your tail and the walls! ");mvprintw(11,23," End game by pressing 'e' "); int PosX=11; //initiaised location to the center of board int PosY=11; int inc=1; int snakeLength=5; //Make snake initial length int xvalue[100]; //stores previous position int yvalue[100]; int i; int movement; //place holder for movement direction char c; //initialised end game charactorwhile(c!='e'){mvprintw(10,23," Current score %d", score);boardGeneration(PosX,PosY,map); //updating displayed arrayc=getch(); //reading a single character from keyboardmap[PosY][PosX]='*';//updates position of curser in arrayswitch(c){case UP:PosY--;movement=UP;break;case DOWN:PosY++;movement=DOWN;break;case LEFT:PosX--;movement=LEFT;break;case RIGHT:PosX++;movement=RIGHT;break;}if(map[PosY][PosX]==''){snakeLength = snakeLength + 1;//increases the length of the snakescore = score + 1;//updates score when food is eatenif((PosY+PosX)<=20){map[foodLocation()+10][foodLocation()+10]='';//updates food position on map}else{map[foodLocation()][foodLocation()]=' 这篇关于蛇游戏。身体没有被删除:(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-29 19:06