开心六月综合激情婷婷|欧美精品成人动漫二区|国产中文字幕综合色|亚洲人在线成视频

    1. 
      
        <b id="zqfy3"><legend id="zqfy3"><fieldset id="zqfy3"></fieldset></legend></b>
          <ul id="zqfy3"></ul>
          <blockquote id="zqfy3"><strong id="zqfy3"><dfn id="zqfy3"></dfn></strong></blockquote>
          <blockquote id="zqfy3"><legend id="zqfy3"></legend></blockquote>
          打開(kāi)APP
          userphoto
          未登錄

          開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

          開(kāi)通VIP
          使用c語(yǔ)言實(shí)現(xiàn)飛機(jī)游戲

          在這里,我主要使用scanf函數(shù)和printf函數(shù)來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的飛機(jī)游戲,并且通過(guò)函數(shù)的形式實(shí)現(xiàn)。

          整體思路:main函數(shù)

          在這里,主要是使用一個(gè)簡(jiǎn)易的游戲框架,來(lái)減小開(kāi)發(fā)游戲時(shí)的難度

          int main()

          {

              startup();//初始化

              while (1//游戲循環(huán)進(jìn)行

              {

                  show();//顯示畫(huà)面  

                  updateWitoutIput();//與用戶(hù)輸入無(wú)關(guān)的更新

                  updateWithInput(); //與用戶(hù)輸入有關(guān)的更新

              }

              system('pause');

              return 0;

          }

          全局變量的定義

          為了方便之后代碼的書(shū)寫(xiě),在這里,我們使用宏定義代替部分全局變量

          //全局變量的定義

          #define HIGH 20  //游戲界面高度

          #define WIDTH 30  // 游戲界面寬度

          #define NUM 20  //敵機(jī)下落速度



          int position_x, position_y;  //飛機(jī)位置

          int bullet_x, bullet_y;  //子彈位置

          int enemy_x, enemy_y;  //敵機(jī)位置

          int score;  //得分

          全局變量的初始化

          void startup()//數(shù)據(jù)的初始化

          {

              //初始化飛機(jī)

              position_x = HIGH / 2//高度

              position_y = WIDTH / 2//寬度



              //初始化子彈

              bullet_x = -1;  //使子彈出現(xiàn)在屏幕外

              bullet_y = position_y;



              //初始化敵機(jī)

              enemy_x = 0;

              enemy_y = position_y;



              //初始化得分

              score = 0;

          }

          顯示函數(shù)show()

          輸出飛機(jī)、敵機(jī)、子彈等

          使用循環(huán)語(yǔ)句,在滿(mǎn)足飛機(jī)、敵機(jī)、子彈位置條件時(shí)輸出對(duì)應(yīng)的圖案(飛機(jī): * 敵機(jī): @ 子彈: | ),其余位置輸出‘ ’或‘\n’

          //輸出每一行每一列

          for (i = 0; i < HIGH; i++) //行  

              {

                  for (j = 0; j < WIDTH; j++) //列

                  {

                      if (i == position_x && j == position_y)//輸出飛機(jī)

                      {

                          printf('*');

                      }

                      else if (i == bullet_x && j == bullet_y)//輸出子彈

                      {

                          printf('|');

                      }

                      else if (i == enemy_x && j == enemy_y)//輸出敵機(jī)

                      {

                          printf('@');

                      }

                      else

                      {

                          printf(' ');

                      }

                  }

                  printf('\n');

              }

          輸出得分

          printf('得分:%d', score);

          清屏

          在輸出輸出飛機(jī)等圖標(biāo)時(shí),需要使用清屏函數(shù)使得光標(biāo)回到 (0,0) 處,方便下一次輸入

          清屏函數(shù)

          #include <windows.h>

          system('cls');

          使光標(biāo)回到 (0,0)

          gotoxy(00); 
          void gotoxy(int x, int y) //將光標(biāo)調(diào)整到(x,y)的位置

          {

              HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

              COORD pos;

              pos.X = x;

              pos.Y = y;

              SetConsoleCursorPosition(handle, pos);

          }

          隱藏光標(biāo)顯示

          為了防止光標(biāo)閃爍的過(guò)于頻繁,我們使用隱藏光標(biāo)顯示函數(shù)隱藏光標(biāo)

          oid HideCursor()//隱藏光標(biāo)顯示函數(shù)

          {

              CONSOLE_CURSOR_INFO cursor_info = { 10 };

              SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);

          }

          //該函數(shù)只需調(diào)用一次即可

          與用戶(hù)輸入無(wú)關(guān)的部分

          子彈上落

          if (bullet_x > -1//讓子彈向上落

          {

              bullet_x--;

          }

          敵機(jī)下落,到達(dá)底部后,生成新的敵機(jī)

          //創(chuàng)建一個(gè)靜態(tài)變量,當(dāng)靜態(tài)變量滿(mǎn)足一定條件時(shí),敵機(jī)下落一次,這樣可以控制敵機(jī)下降的速度同時(shí)不影響飛機(jī)移動(dòng)的速度

          static int speed = 0//控制敵機(jī)下落速度

          if (speed < NUM)  //每進(jìn)行NUM次敵機(jī)下落一次

          {

              speed++;

          }

          else

          {

              enemy_x++;  //敵機(jī)下落一次

              speed = 0;

          }

          if (enemy_x > HIGH)  //敵機(jī)一直下落到底部

          {

              enemy_x = -1;   //生成新的飛機(jī)

              enemy_y = rand() % WIDTH;

          }

          命中敵機(jī),得分+1,同時(shí)生成新的敵機(jī)

          if (bullet_x == enemy_x && bullet_y == enemy_y)  //命中

          {

              score++;  //得分+1



              enemy_x = -1;   //生成新的飛機(jī)

              enemy_y = rand() % WIDTH;



              bullet_x = -1;  //讓子彈直接出現(xiàn)屏幕外,直到下一次發(fā)射子彈

          }

          與用戶(hù)輸入有關(guān)的部分

          在這里,我們可以使用scanf函數(shù)或者getch函數(shù)實(shí)現(xiàn)在用戶(hù)輸入 ‘w’ ‘s’ ‘a(chǎn)’ ‘d’ 時(shí)對(duì)上下左右的控制

          scanf函數(shù)

          scanf('%c', &input);

          if (input == 'w')

              position_x--;

          if (input == 's')

              position_x++;

          if (input == 'a')

              position_y--;

          if (input == 'd')

              position_y++;

          if (input == ' ')

          {

              bullet_x = position_x - 1;

              bullet_y = position_y;

          }

          getch函數(shù)

          if (_kbhit())  //kbhit()函數(shù):檢查當(dāng)前是否有鍵盤(pán)輸入,若有則返回一個(gè)非0值,否則返回0,頭文件<conio.h>

          {

              input = _getch();  //getch()是一個(gè)不回顯函數(shù),當(dāng)用戶(hù)按下某個(gè)字符時(shí),函數(shù)自動(dòng)讀取,但是不會(huì)顯示在屏幕上,無(wú)需按回車(chē),

              if (input == 'w')

                  position_x--;

              if (input == 's')

                  position_x++;

              if (input == 'a')

                  position_y--;

              if (input == 'd')

                  position_y++;

              if (input == ' ')

              {

                  bullet_x = position_x - 1;

                  bullet_y = position_y;

              }

          }

          在這里,我們選擇使用getch()函數(shù),從而使得程序更加方便(減少用戶(hù)回車(chē)的輸入及屏幕上的顯示)

          注意事項(xiàng)

          在實(shí)現(xiàn)代碼時(shí)需要注意在使用各個(gè)函數(shù)時(shí)對(duì)頭文件的調(diào)用盡量減少對(duì)全局變量的創(chuàng)建

          完整代碼

          #include <stdio.h>

          #include <windows.h>

          #include <stdlib.h>

          #include <conio.h>

          #include <time.h>



          //全局變量的定義

          #define HIGH 20  //游戲界面高度

          #define WIDTH 30  // 游戲界面寬度

          #define NUM 10  //敵機(jī)下落速度



          int position_x, position_y;  //飛機(jī)位置

          int bullet_x, bullet_y;  //子彈位置

          int enemy_x, enemy_y;  //敵機(jī)位置

          int score;  //得分





          void gotoxy(int x, int y) //將光標(biāo)調(diào)整到(x,y)的位置

          {

              HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

              COORD pos;

              pos.X = x;

              pos.Y = y;

              SetConsoleCursorPosition(handle, pos);

          }



          void HideCursor()  //隱藏光標(biāo)顯示

          {

              CONSOLE_CURSOR_INFO cursor_info = { 10 };

              SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);

          }



          void startup()//數(shù)據(jù)的初始化

          {

              //初始化飛機(jī)

              position_x = HIGH / 2//高度

              position_y = WIDTH / 2//寬度



              //初始化子彈

              bullet_x = -1;

              bullet_y = position_y;



              //初始化敵機(jī)

              enemy_x = 0;

              enemy_y = position_y;



              //初始化得分

              score = 0;

          }



          void show()//顯示畫(huà)面

          {

              //system('cls'); //清屏函數(shù)  

              gotoxy(00); //使光標(biāo)回到0,0



              int i, j;

              for (i = 0; i < HIGH; i++) //行

              {

                  for (j = 0; j < WIDTH; j++) //列

                  {

                      if (i == position_x && j == position_y)//輸出飛機(jī)

                      {

                          printf('*');

                      }

                      else if (i == bullet_x && j == bullet_y)//輸出子彈

                      {

                          printf('|');

                      }

                      else if (i == enemy_x && j == enemy_y)//輸出敵機(jī)

                      {

                          printf('@');

                      }

                      else

                      {

                          printf(' ');

                      }

                  }

                  printf('\n');

              }

              printf('得分:%d', score);

          }



          void updateWitoutIput()//與用戶(hù)輸入無(wú)關(guān)的更新

          {

              if (bullet_x > -1//讓子彈向上落

              {

                  bullet_x--;

              }



              if (bullet_x == enemy_x && bullet_y == enemy_y) //命中敵機(jī)

              {

                  score++;  //得分+1



                  enemy_x = -1;   //生成新的飛機(jī)

                  enemy_y = rand() % WIDTH;



                  bullet_x = -1;  //讓子彈直接出現(xiàn)屏幕外,直到下一次發(fā)射子彈

              }



              static int speed = 0//控制敵機(jī)下落速度

              if (speed < NUM)  //每進(jìn)行NUM次敵機(jī)下落一次

              {

                  speed++;

              }

              else

              {

                  enemy_x++;

                  speed = 0;

              }

              if (enemy_x > HIGH)  //敵機(jī)一直下落到底部

              {

                  enemy_x = -1

                  enemy_y = rand() % WIDTH;

              }



          }



          void updateWithInput()//與用戶(hù)輸入有關(guān)的更新

          {

              //用戶(hù)輸入

              char input;

              if (_kbhit())

              {

                  input = _getch();

                  if (input == 'w')

                      position_x--;

                  if (input == 's')

                      position_x++;

                  if (input == 'a')

                      position_y--;

                  if (input == 'd')

                      position_y++;

                  if (input == ' ')

                  {

                      bullet_x = position_x - 1;

                      bullet_y = position_y;

                  }

              }

          }

          int main()

          {

              startup();//初始化

              HideCursor();

              srand((unsigned)time(NULL));

              while (1)

              {

                  show();//顯示畫(huà)面  

                  updateWitoutIput();//與用戶(hù)輸入無(wú)關(guān)的更新  //更新數(shù)據(jù)

                  updateWithInput(); //與用戶(hù)輸入有關(guān)的更新  //輸入分析

              }

              system('pause');

              return 0;

          }

          程序效果

          總結(jié)

          雖然完成了一個(gè)簡(jiǎn)單的飛機(jī)游戲,但是很多的功能都未能實(shí)現(xiàn),如改變飛機(jī)的形狀,增加游戲的難度,飛機(jī)生命的減少等等,任需要繼續(xù)的努力。

          --------------------- 

          作者:木頭i 

          本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
          打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
          猜你喜歡
          類(lèi)似文章
          【cocos2d-x入門(mén)實(shí)戰(zhàn)】微信飛機(jī)大戰(zhàn)之九:碰撞檢測(cè)
          用 Python 實(shí)現(xiàn)微信版飛機(jī)大戰(zhàn)
          unity3初學(xué)往鼠標(biāo)點(diǎn)擊的方向發(fā)射子彈
          飛機(jī)大戰(zhàn)編程
          Bite the bullet不是“咬子彈”,翻譯錯(cuò)就尷尬了!
          古老的子彈(7 photos)
          更多類(lèi)似文章 >>
          生活服務(wù)
          分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
          綁定賬號(hào)成功
          后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
          如果VIP功能使用有故障,
          可點(diǎn)擊這里聯(lián)系客服!

          聯(lián)系客服