国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

DOS簡易版C語言貪吃蛇

2020-01-26 11:44:55
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了C語言實(shí)現(xiàn)貪吃蛇的具體代碼,供大家參考,具體內(nèi)容如下

#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <time.h>#include <windows.h>   #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; } 

好久沒寫過C語言了,隨便寫個(gè)貪吃蛇玩一玩,BUG不少,當(dāng)記錄了。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 黎城县| 鸡东县| 江城| 祥云县| 宁晋县| 广南县| 义乌市| 玛纳斯县| 吉林市| 百色市| 老河口市| 东港市| 克什克腾旗| 开江县| 茂名市| 通河县| 聂拉木县| 加查县| 平舆县| 偏关县| 仪陇县| 宁夏| 诸暨市| 松溪县| 赤水市| 札达县| 元阳县| 贺州市| 建瓯市| 东安县| 丰顺县| 两当县| 长武县| 射阳县| 壶关县| 金坛市| 肥城市| 灵山县| 乐陵市| 肇庆市| 柳江县|