亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C語言鏈表實現(xiàn)貪吃蛇游戲

 更新時間:2017年10月09日 09:35:48   作者:J博士  
這篇文章主要為大家詳細介紹了C語言鏈表實現(xiàn)貪吃蛇游戲源碼,適合C語言入門者學習閱讀,具有一定的參考價值,感興趣的小伙伴們可以參考一下

閱讀學習了源代碼,并做了簡單的注釋和修改,里面只用了鏈表數(shù)據(jù)結構,非常適合C語言入門者學習閱讀。

程序可在VS2013下編譯運行。

#include<stdio.h>
#include<time.h>
#include<windows.h>
#include<stdlib.h>

#define U 1
#define D 2
#define L 3 
#define R 4  //蛇的狀態(tài),U:上 ;D:下;L:左 R:右

typedef struct SNAKE //蛇身的一個節(jié)點
{
 int x;
 int y;
 struct SNAKE *next;
}snake;

//全局變量//
int score = 0, add = 10;//總得分與每次吃食物得分。
int status, sleeptime = 200;//每次運行的時間間隔
snake *head, *food;//蛇頭指針,食物指針
snake *q;//遍歷蛇的時候用到的指針
int endGamestatus = 0; //游戲結束的情況,1:撞到墻;2:咬到自己;3:主動退出游戲。

//聲明全部函數(shù)//
void Pos();
void creatMap();
void initSnake();
int biteSelf();
void createFood();
void cantCrossWall();
void snakeMove();
void pause();
void runGame();
void initGame();
void endGame();
void gameStart();

void Pos(int x, int y)//設置光標位置
{
 COORD pos;
 HANDLE hOutput;
 pos.X = x;
 pos.Y = y;
 hOutput = GetStdHandle(STD_OUTPUT_HANDLE);//返回標準的輸入、輸出或錯誤的設備的句柄,也就是獲得輸入、輸出/錯誤的屏幕緩沖區(qū)的句柄
 SetConsoleCursorPosition(hOutput, pos);
}

void creatMap()//創(chuàng)建地圖
{
 int i;
 for (i = 0; i<58; i += 2)//打印上下邊框
 {
  Pos(i, 0);
  printf("■");//一個方塊占兩個位置
  Pos(i, 26);
  printf("■");
 }
 for (i = 1; i<26; i++)//打印左右邊框
 {
  Pos(0, i);
  printf("■");
  Pos(56, i);
  printf("■");
 }
}

void initSnake()//初始化蛇身
{
 snake *tail;
 int i;
 tail = (snake*)malloc(sizeof(snake));//從蛇尾開始,頭插法,以x,y設定開始的位置//
 tail->x = 24;
 tail->y = 5;
 tail->next = NULL;
 for (i = 1; i <= 4; i++)//初始長度為4
 {
  head = (snake*)malloc(sizeof(snake));
  head->next = tail;
  head->x = 24 + 2 * i;
  head->y = 5;
  tail = head;
 }
 while (tail != NULL)//從頭到為,輸出蛇身
 {
  Pos(tail->x, tail->y);
  printf("■");
  tail = tail->next;
 }
}
//??
int biteSelf()//判斷是否咬到了自己
{
 snake *self;
 self = head->next;
 while (self != NULL)
 {
  if (self->x == head->x && self->y == head->y)
  {
   return 1;
  } 
  self = self->next;
 }
 return 0;
}

void createFood()//隨機出現(xiàn)食物
{
 snake *food_1;
 srand((unsigned)time(NULL));//為了防止每次產(chǎn)生的隨機數(shù)相同,種子設置為time
 food_1 = (snake*)malloc(sizeof(snake));
 while ((food_1->x % 2) != 0) //保證其為偶數(shù),使得食物能與蛇頭對其
 {
  food_1->x = rand() % 52 + 2;
 }
 food_1->y = rand() % 24 + 1;
 q = head;
 while (q->next == NULL)
 {
  if (q->x == food_1->x && q->y == food_1->y) //判斷蛇身是否與食物重合
  {
   free(food_1);
   createFood();
  }
  q = q->next;
 }
 Pos(food_1->x, food_1->y);
 food = food_1;
 printf("■");
}

void cantCrossWall()//不能穿墻
{
 if (head->x == 0 || head->x == 56 || head->y == 0 || head->y == 26)
 {
  endGamestatus = 1;
  endGame();
 }
}

void snakeMove()//蛇前進,上U,下D,左L,右R
{
 snake * nexthead;
 cantCrossWall();

 nexthead = (snake*)malloc(sizeof(snake));
 if (status == U)
 {
  nexthead->x = head->x;
  nexthead->y = head->y - 1;
  if (nexthead->x == food->x && nexthead->y == food->y)//如果下一個有食物//
  {
   nexthead->next = head;
   head = nexthead;
   q = head;
   while (q != NULL)
   {
    Pos(q->x, q->y);
    printf("■");
    q = q->next;
   }
   score = score + add;
   createFood();
  }
  else            //如果沒有食物//
  {
   nexthead->next = head;
   head = nexthead;
   q = head;
   while (q->next->next != NULL)
   {
    Pos(q->x, q->y);
    printf("■");
    q = q->next;
   }
   Pos(q->next->x, q->next->y);
   printf(" ");
   free(q->next);
   q->next = NULL;
  }
 }
 if (status == D)
 {
  nexthead->x = head->x;
  nexthead->y = head->y + 1;
  if (nexthead->x == food->x && nexthead->y == food->y) //有食物
  {
   nexthead->next = head;
   head = nexthead;
   q = head;
   while (q != NULL)
   {
    Pos(q->x, q->y);
    printf("■");
    q = q->next;
   }
   score = score + add;
   createFood();
  }
  else        //沒有食物
  {
   nexthead->next = head;
   head = nexthead;
   q = head;
   while (q->next->next != NULL)
   {
    Pos(q->x, q->y);
    printf("■");
    q = q->next;
   }
   Pos(q->next->x, q->next->y);
   printf(" ");
   free(q->next);
   q->next = NULL;
  }
 }
 if (status == L)
 {
  nexthead->x = head->x - 2;
  nexthead->y = head->y;
  if (nexthead->x == food->x && nexthead->y == food->y)//有食物
  {
   nexthead->next = head;
   head = nexthead;
   q = head;
   while (q != NULL)
   {
    Pos(q->x, q->y);
    printf("■");
    q = q->next;
   }
   score = score + add;
   createFood();
  }
  else        //沒有食物
  {
   nexthead->next = head;
   head = nexthead;
   q = head;
   while (q->next->next != NULL)
   {
    Pos(q->x, q->y);
    printf("■");
    q = q->next;
   }
   Pos(q->next->x, q->next->y);
   printf(" ");
   free(q->next);
   q->next = NULL;
  }
 }
 if (status == R)
 {
  nexthead->x = head->x + 2;
  nexthead->y = head->y;
  if (nexthead->x == food->x && nexthead->y == food->y)//有食物
  {
   nexthead->next = head;
   head = nexthead;
   q = head;
   while (q != NULL)
   {
    Pos(q->x, q->y);
    printf("■");
    q = q->next;
   }
   score = score + add;
   createFood();
  }
  else           //沒有食物
  {
   nexthead->next = head;
   head = nexthead;
   q = head;
   while (q->next->next != NULL)
   {
    Pos(q->x, q->y);
    printf("■");
    q = q->next;
   }
   Pos(q->next->x, q->next->y);
   printf(" ");
   free(q->next);
   q->next = NULL;
  }
 }
 if (biteSelf() == 1)  //判斷是否會咬到自己
 {
  endGamestatus = 2;
  endGame();
 }
}

void pause()//暫停
{
 while (1)
 {
  Sleep(300);
  if (GetAsyncKeyState(VK_SPACE))
  {
   break;
  }

 }
}

void runGame()//控制游戲  
{

 Pos(64, 15);
 printf("不能穿墻,不能咬到自己\n");
 Pos(64, 16);
 printf("用↑.↓.←.→分別控制蛇的移動.");
 Pos(64, 17);
 printf("F1 為加速,F(xiàn)2 為減速\n");
 Pos(64, 18);
 printf("ESC :退出游戲.space:暫停游戲.");
 Pos(64, 20);
 printf("C語言研究中心 www.clang.cc");
 status = R;
 while (1)
 {
  Pos(64, 10);
  printf("得分:%d ", score);
  Pos(64, 11);
  printf("每個食物得分:%d分", add);
  if (GetAsyncKeyState(VK_UP) && status != D)
  {
   status = U;
  }
  else if (GetAsyncKeyState(VK_DOWN) && status != U)
  {
   status = D;
  }
  else if (GetAsyncKeyState(VK_LEFT) && status != R)
  {
   status = L;
  }
  else if (GetAsyncKeyState(VK_RIGHT) && status != L)
  {
   status = R;
  }
  else if (GetAsyncKeyState(VK_SPACE))
  {
   pause();
  }
  else if (GetAsyncKeyState(VK_ESCAPE))
  {
   endGamestatus = 3;
   break;
  }
  else if (GetAsyncKeyState(VK_F1))
  {
   if (sleeptime >= 50)
   {
    sleeptime = sleeptime - 30;
    add = add + 2;
    if (sleeptime == 320)
    {
     add = 2;//防止減到1之后再加回來有錯
    }
   }
  }
  else if (GetAsyncKeyState(VK_F2))
  {
   if (sleeptime<350)
   {
    sleeptime = sleeptime + 30;
    add = add - 2;
    if (sleeptime == 350)
    {
     add = 1; //保證最低分為1
    }
   }
  }
  Sleep(sleeptime);
  snakeMove();
 }
}

void initGame()//開始界面
{
 Pos(40, 12);

 system("title C語言研究中心 www.clang.cc");
 printf("歡迎來到貪食蛇游戲!");
 Pos(40, 25);
 printf("    C語言研究中心 www.clang.cc.\n");
 system("pause");
 system("cls");
 Pos(25, 12);
 printf("用↑.↓.←.→分別控制蛇的移動, F1 為加速,2 為減速\n");
 Pos(25, 13);
 printf("加速將能得到更高的分數(shù)。\n");
 system("pause");
 system("cls");
}

void endGame()//結束游戲
{

 system("cls");
 Pos(24, 12);
 if (endGamestatus == 1)
 {
  printf("對不起,您撞到墻了。游戲結束.");
 }
 else if (endGamestatus == 2)
 {
  printf("對不起,您咬到自己了。游戲結束.");
 }
 else if (endGamestatus == 3)
 {
  printf("您的已經(jīng)結束了游戲。");
 }
 Pos(24, 13);
 printf("您的得分是%d\n", score);
 while (getchar() != 'y')
 { 
  printf("close?[y]");
 }
 exit(0);
}

void gameStart()//游戲初始化
{
 system("mode con cols=100 lines=30");
 initGame();
 creatMap();
 initSnake();
 createFood();
}

int main()
{
 gameStart();
 runGame();
 endGame();
 return 0;
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 解析C++編程中的選擇結構和switch語句的用法

    解析C++編程中的選擇結構和switch語句的用法

    這篇文章主要介紹了解析C++編程中的選擇結構和switch語句的用法,是C++入門學習中的基礎知識,需要的朋友可以參考下
    2015-09-09
  • C/C++?Qt數(shù)據(jù)庫與SqlTableModel組件應用教程

    C/C++?Qt數(shù)據(jù)庫與SqlTableModel組件應用教程

    SqlTableModel?組件可以將數(shù)據(jù)庫中的特定字段動態(tài)顯示在TableView表格組件中,這篇文章將主要介紹SqlTableModel組件一些常用的操作,需要的朋友可以參考一下
    2021-12-12
  • c語言/c++溢出問題淺談

    c語言/c++溢出問題淺談

    這篇文章主要給大家介紹了關于c語言/c++溢出問題的相關資料,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-02-02
  • C語言關于include順序不同導致編譯結果不同的問題

    C語言關于include順序不同導致編譯結果不同的問題

    這篇文章主要介紹了在日常調試C語言中include的順序不同從而影響最后編譯結果不同的問題,究其原因是寫代碼的習慣所導致,下面跟小編一起來看看吧
    2022-04-04
  • C++?requires關鍵字簡單介紹

    C++?requires關鍵字簡單介紹

    requires?是?C++20?中引入的一個新關鍵字,用于在函數(shù)模板或類模板中聲明所需的一組語義要求,它可以用來限制模板參數(shù),類似于?typename?和?class?關鍵字,這篇文章主要介紹了C++?requires關鍵字簡介,需要的朋友可以參考下
    2023-05-05
  • C++ 容器適配器仿函數(shù)與priority_queue的使用

    C++ 容器適配器仿函數(shù)與priority_queue的使用

    本文主要介紹了C++ 容器適配器仿函數(shù)與priority_queue的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-09-09
  • 在C/C++與Python之間實現(xiàn)通信的常見方法

    在C/C++與Python之間實現(xiàn)通信的常見方法

    在C/C++與Python之間實現(xiàn)通信的方式有很多,本文給大家介紹了一些常見的方法,文中通過代碼示例介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2023-12-12
  • C語言深入淺出講解直接插入排序算法的實現(xiàn)

    C語言深入淺出講解直接插入排序算法的實現(xiàn)

    插入排序也是最簡單的一類排序方法,我今天介紹的也是插入排序里最直觀且淺顯易懂的直接插入排序。對這個很簡單的排序,記得當時也是花了近兩個晚上才搞懂它的原理的,接下來就來介紹一下
    2022-05-05
  • C語言線程對象和線程存儲的實現(xiàn)

    C語言線程對象和線程存儲的實現(xiàn)

    這篇文章主要介紹了C語言線程對象和線程存儲的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • C語言字符函數(shù)、內存函數(shù)功能及實現(xiàn)代碼

    C語言字符函數(shù)、內存函數(shù)功能及實現(xiàn)代碼

    這篇文章主要介紹了C語言字符函數(shù)、內存函數(shù) 功能及實現(xiàn),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02

最新評論