C语言怎么实现贪吃蛇小游戏程序
创始人
2024-12-23 19:22:28
0次

代码如下:

#include #include #include #include #include    #define WALL_LENGTH 22 #define LEFT 0x4b #define RIGHT 0x4d #define DOWN 0x50 #define UP 0x48  struct Snakes{ int x; int y; struct Snakes *prev; struct Snakes *next;}; struct Food{ int x; int y;};  struct Snakes *header; struct Snakes *tailer; struct Food *food;  int wall[WALL_LENGTH][WALL_LENGTH];int direction = RIGHT;   /**/void init();void draw();void move();void doMove(int x1, int y1);void eat(); void keydown(); void foods();int isOver();int isDrawSnake(int x, int y);int isDrawFood(int x, int y);  int main(){ init();  while(1){    if(isOver()){ break; }     move();  eat(); draw();  _sleep( 100 ); keydown();    }  printf("GAME OVER!");  system("pause");}  void init(){ int y, x;  for(y=0; y < WALL_LENGTH; y++){  for(x=0; x < WALL_LENGTH; x++){ if(y == 0 || y == WALL_LENGTH - 1 || x == 0 || x == WALL_LENGTH - 1){  wall[y][x] = 1; } } }   header=(struct Snakes *)malloc(sizeof(struct Snakes)); header->x=10; header->y=10; header->prev=NULL;   tailer=(struct Snakes *)malloc(sizeof(struct Snakes)); tailer->x=9; tailer->y=10; tailer->next=NULL; tailer->prev=header;  header->next=tailer;  foods(); } void draw(){ int y, x;  system("cls"); for(y=0; y < WALL_LENGTH; y++){  for(x=0; x < WALL_LENGTH; x++){  if(wall[y][x] == 1){ printf("[]"); }else if(isDrawSnake(x, y)){ printf("[]"); }else if(isDrawFood(x, y)){ printf("()"); }else{ printf(" ");   } } printf("\n"); } } void move(){  switch(direction){ case LEFT :  doMove(-1, 0);  break; case RIGHT :  doMove(1, 0);  break; case UP :  doMove(0, -1);  break; case DOWN :  doMove(0, 1);  break; } } void doMove(int x1, int y1){  struct Snakes *temp_tailer = tailer->prev;  tailer->x=header->x + x1; tailer->y=header->y + y1;  tailer->next=header; tailer->prev->next = NULL; tailer->prev = NULL;  header->prev=tailer;  header = tailer; tailer = temp_tailer;  } void eat(){  if(header->x == food->x && header->y == food->y){ int x1=0, y1 =0; struct Snakes *temp_tailer = tailer;  tailer=(struct Snakes *)malloc(sizeof(struct Snakes));  switch(direction){ case LEFT :  x1 = -1; y1 = 0; break;  case RIGHT :  x1 = 1; y1 = 0; break;  case UP :  x1 = 0; y1 = -1; break;  case DOWN :  x1 = 0; y1 = 1; break; }  tailer->x=temp_tailer->x + x1; tailer->y=temp_tailer->y + y1; tailer->next=NULL; tailer->prev=temp_tailer;  temp_tailer->next = tailer;  foods(); } }  void foods(){  int y,x;  struct Snakes *temp = header;  _sleep(20);  srand((unsigned)time(NULL));  y=rand()%WALL_LENGTH;  x=rand()%WALL_LENGTH;   if(y == 0 || y == WALL_LENGTH - 1 || x == 0 || x == WALL_LENGTH - 1 ){  return foods(); }  do{ if(temp->x == x && temp->y == y){ return foods(); } temp = temp->next; }while(temp != NULL);    if(food == NULL){ food=(struct Food *)malloc(sizeof(struct Food)); } food->x = x; food->y = y; } void keydown(){ char keycode;  if(_kbhit()&&(keycode =_getch())) {    switch(keycode) {  case LEFT:  if(RIGHT!=direction) {  direction=LEFT; // move(); // draw(); } break;   case RIGHT:  if(LEFT!=direction) {  direction=RIGHT; // move(); // draw(); } break;   case UP:  if(DOWN!=direction) {  direction=UP; // move(); // draw(); } break;   case DOWN:  if(UP!=direction){  direction=DOWN; // move(); // draw(); } break;    }  }  }  int isDrawSnake(int x, int y){ struct Snakes *temp = header; do{ if(temp->x == x && temp->y == y){ return 1; } temp = temp->next; }while(temp != NULL); return 0;} int isDrawFood(int x, int y){ if(food->x == x && food->y == y){ return 1; } return 0;} int isOver(){ int x1=0, y1 =0; switch(direction){ case LEFT :  x1 = -1; y1 = 0; break;  case RIGHT :  x1 = 1; y1 = 0; break;  case UP :  x1 = 0; y1 = -1; break;  case DOWN :  x1 = 0; y1 = 1; break; }  if(header->x + x1 <= 0 || header->x + x1 >= WALL_LENGTH - 1  || header->y + y1 <= 0 || header->y + y1 >= WALL_LENGTH - 1){  return 1; } return 0; }

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...