最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言之飛機大戰(zhàn)游戲

 更新時間:2020年12月02日 08:46:46   作者:weixin_38554391  
這篇文章主要為大家詳細介紹了C語言之飛機大戰(zhàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C語言之飛機大戰(zhàn)游戲的具體代碼,供大家參考,具體內容如下

技術原型

1、void gotoxy(int x, int y) 函數,該函數可以使光標去到(x,y)的位置進行打??;
2、鏈表,用于存儲狀態(tài);
3、windows.h中有非阻塞輸入,_kbhit();
4、隨機生成數;
5、視覺暫留;
6、碰撞檢測;
7、清屏函數;
8、設置邊界;

技術路線

1、設置一個邊界;
2、維護一個子彈的列表;
3、維護一個敵機的列表;
4、初始化飛機的位置;
5、每隔一秒鐘生成一架敵機,生成位置x坐標隨機,坐標為0;
6、設置點擊空格生成子彈;
7、設置while(1)循環(huán),在其中每次進行清屏和更新;
8、每次更新遍歷子彈鏈表,使所有子彈的位置向上移動1;
9、每次更新遍歷敵機鏈表,使所有敵機的位置向下移動1;
10、碰撞檢測,遍歷子彈和敵機列表,發(fā)現碰撞則從各自的鏈表中移除碰撞的節(jié)點;
11、當有敵機碰撞到本機游戲結束;
12、當子彈或者敵機碰撞到邊界,則從鏈表中移除;
13、每次檢測到碰撞加一分。

實現效果

花費時間

約6小時30分鐘

代碼

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


struct node {
 int  x;
 int  y;
 struct node*  next;
};

typedef struct node node_t;
typedef struct node* nodeptr_t;

void gotoxy(int x, int y); //光標定位函數
void print_plane(int x, int y); //打印飛機
nodeptr_t generate_bullet(nodeptr_t listnode, int x, int y); //生成子彈
void print_bullet(nodeptr_t listnode); //打印子彈
nodeptr_t update_bullet(nodeptr_t listnode); //更新子彈位置
nodeptr_t generate_target(nodeptr_t listnode, int x); //生成敵機
void print_target(nodeptr_t listnode); //打印敵機
nodeptr_t update_target(nodeptr_t listnode); //更新敵機位置
int collision_detection(nodeptr_t bulletlist, nodeptr_t targetlist); //碰撞檢測
bool is_gameover(int x,int y, nodeptr_t targetlist); // 游戲結束
void clear(nodeptr_t bulletlist, nodeptr_t targetlist);


int main() {
 int plane_x = 0, plane_y = 19; //飛機位置
 char control; //輸入
 bool isfire = 0; //是否開火

 nodeptr_t target = nullptr; //敵機鏈表
 target = (nodeptr_t)malloc(sizeof(node_t));
 target->next = nullptr;
 target->x = 0;
 target->y = 0;

 nodeptr_t bullet = nullptr; //子彈鏈表
 bullet = (nodeptr_t)malloc(sizeof(node_t));
 bullet->next = nullptr;
 bullet->x = 0;
 bullet->y = 0;

 int subtime = 0;
 time_t starttime;
 starttime = time(NULL);
 int grade = 0; //分數
 int targetspeed = 0;
 int bulletspeed = 0;

 while(1)
 {
 system("cls");
 time_t currenttime;
 currenttime = time(NULL);
 //每隔一秒生成一架敵機
 if (currenttime - starttime - subtime > 0)
 {
  srand((unsigned)time(NULL));
  unsigned int target_y = rand() % 14 + 3;
  target = generate_target(target, target_y);
 }
 subtime = currenttime - starttime;
 //開火則生成子彈
 if (isfire)
 {
  bullet = generate_bullet(bullet, plane_x, plane_y - 1);
  isfire = 0;
 }
 //打印敵機
 print_target(target);
 targetspeed++;
 if(targetspeed % 2 == 0)
  target = update_target(target);
 //打印子彈
 print_bullet(bullet);
 bulletspeed++;
 if (bulletspeed % 2 == 0)
  bullet = update_bullet(bullet);
 //碰撞檢測
 grade = grade + collision_detection(bullet, target);
 gotoxy(0, 25);
 printf("SCORE: %d", grade);
 //打印飛機
 print_plane(plane_x, plane_y);
 //敵機本機是否相撞
 bool isgameover = is_gameover(plane_x, plane_y, target);
 //Sleep(100);
 //非阻塞鍵盤輸入
 if (isgameover)
 {
  clear(target, bullet);
  plane_x = 0;
  plane_y = 19;
  isfire = 0;
  system("cls");
  gotoxy(8, 8);
  printf("SCORE: %d", grade);
  gotoxy(8, 9);
  printf("GAME OVER");
  grade = 0;
  break;
 }
 if (_kbhit())
 {
  control = _getch();
  if (control == ' ')
  isfire = 1;
  else
  isfire = 0;
  if (control == 'w' && plane_y > 0)
  plane_y--;
  if (control == 's' && plane_y < 20)
  plane_y++;
  if (control == 'a' && plane_x > 0)
  plane_x--;
  if (control == 'd' && plane_x < 20)
  plane_x++;
 }


 }
 return 0;
}

void gotoxy(int x, int y)//光標定位函數
{
 COORD p;//定義結構體變量p
 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);//獲取當前函數句柄
 p.X = x;
 p.Y = y;//將光標的目標移動位置傳遞給結構體
 SetConsoleCursorPosition(handle, p);//移動光標
 return;
}

void print_plane(int x, int y) //打印飛機
{
 if (x == 0)
 {
 gotoxy(x, y);
 printf("*");
 gotoxy(x, y + 1);
 printf("***");
 gotoxy(x, y + 2);
 printf(" *");
 }
 else if (x == 1)
 {
 gotoxy(x, y);
 printf("*");
 gotoxy(x-1, y + 1);
 printf("****");
 gotoxy(x-1, y + 2);
 printf("* *");
 }
 else if (x == 20)
 {
 gotoxy(x, y);
 printf("*");
 gotoxy(x - 2, y + 1);
 printf("***");
 gotoxy(x - 1, y + 2);
 printf("* ");
 }
 else if (x == 19)
 {
 gotoxy(x, y);
 printf("*");
 gotoxy(x - 2, y + 1);
 printf("****");
 gotoxy(x - 1, y + 2);
 printf("* *");
 }
 else
 {
 gotoxy(x, y);
 printf("*");
 gotoxy(x - 2, y + 1);
 printf("*****");
 gotoxy(x - 1, y + 2);
 printf("* *");
 }
 return;
}

nodeptr_t generate_bullet(nodeptr_t listnode, int x, int y)
{
 nodeptr_t newbullet = nullptr; //子彈鏈表
 newbullet = (nodeptr_t)malloc(sizeof(node_t));
 newbullet->next = listnode->next;
 newbullet->x = x;
 newbullet->y = y;
 listnode->next = newbullet;
 return listnode;
}

void print_bullet(nodeptr_t listnode)
{
 nodeptr_t templist = listnode;
 while (templist->next != nullptr)
 {
 gotoxy((templist->next->x), templist->next->y);
 printf("|");
 templist = templist->next;
 }
 return;
}

nodeptr_t update_bullet(nodeptr_t listnode)
{
 nodeptr_t templist = listnode;
 while (templist->next != nullptr)
 {
 if (templist->next->y > 0)
  (templist->next->y)--;
 else
 {
  nodeptr_t tempnode = templist->next;
  templist->next = tempnode->next;
  tempnode->next = nullptr;
  free(tempnode);
 }
 if (templist->next != nullptr)
  templist = templist->next;
 else
  break;
 }
 return listnode;
}

nodeptr_t generate_target(nodeptr_t listnode, int x)
{
 nodeptr_t newtarget = nullptr; //子彈鏈表
 newtarget = (nodeptr_t)malloc(sizeof(node_t));
 newtarget->next = listnode->next;
 newtarget->x = x;
 newtarget->y = 0;
 listnode->next = newtarget;
 return listnode;
}

void print_target(nodeptr_t listnode)
{
 nodeptr_t templist = listnode;
 while(templist->next != nullptr)
 {
 gotoxy(templist->next->x, templist->next->y);
 printf("+");
 templist = templist->next;
 }
 return;
}

nodeptr_t update_target(nodeptr_t listnode)
{
 nodeptr_t templist = listnode;
 while (templist->next != nullptr)
 {
 if (templist->next->y < 21)
  (templist->next->y)++;
 else
 {
  nodeptr_t tempnode = templist->next;
  templist->next = tempnode->next;
  tempnode->next = nullptr;
  free(tempnode);
 }
 if (templist->next != nullptr)
  templist = templist->next;
 else
  break;
 }
 return listnode;
}

int collision_detection(nodeptr_t bulletlist, nodeptr_t targetlist)
{
 int grade = 0;
 nodeptr_t tempbulletlist = bulletlist;
 while(tempbulletlist->next != nullptr)
 {
 nodeptr_t temptargetlist = targetlist;
  while(temptargetlist->next != nullptr) 
  {
  
  if(temptargetlist->next->x == (tempbulletlist->next->x) && temptargetlist->next->y > tempbulletlist->next->y)
  {
    nodeptr_t tempnode = temptargetlist->next;
  temptargetlist->next = tempnode->next;
  tempnode->next = nullptr;
  free(tempnode);

  tempnode = tempbulletlist->next;
  tempbulletlist->next = tempnode->next;
  tempnode->next = nullptr;

   free(tempnode);
  grade++;
  break;

  }
  if (temptargetlist->next != nullptr)
  temptargetlist = temptargetlist->next;
  else
  break;
  }
 if (tempbulletlist->next != nullptr)
  tempbulletlist = tempbulletlist->next;
 else
  break;
 }
 return grade;
}

 bool is_gameover(int x, int y, nodeptr_t targetlist)
 {
 nodeptr_t temptargetlist = targetlist;
 while (temptargetlist->next != nullptr)
 {
  int tempsub = abs((temptargetlist->next->x) - x);
  if (tempsub == 0 && temptargetlist->next->y > y)
  {
  return 1;
  }
  else if(tempsub == 1 && temptargetlist->next->y > y + 1)
  {
  return 1;
  }
  else if (tempsub == 2 && temptargetlist->next->y > y + 1)
  {
  return 1;
  }
  temptargetlist = temptargetlist->next;
 }
 return 0;
 }

 void clear(nodeptr_t bulletlist, nodeptr_t targetlist)
 {
 while (bulletlist->next != nullptr)
 {
  nodeptr_t temp = bulletlist->next;
  bulletlist->next = temp->next;
  //temp->next = nullptr;
  free(temp);
 }
 while (targetlist->next != nullptr)
 {
  nodeptr_t temp = targetlist->next;
  targetlist->next = temp->next;
  //temp->next = nullptr;
  free(temp);
 }
 return;
 }

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

相關文章

  • C++二維數組螺旋加密信息

    C++二維數組螺旋加密信息

    大家好,本篇文章主要講的是C++二維數組螺旋加密信息,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • C語言遞歸實現線索二叉樹

    C語言遞歸實現線索二叉樹

    這篇文章主要介紹了C語言遞歸實現線索二叉樹,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • cin.get()和cin.getline()之間的區(qū)別

    cin.get()和cin.getline()之間的區(qū)別

    以下是對cin.get()和cin.getline()的區(qū)別進行了詳細的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2013-09-09
  • OpenCV4.1.0+VisualStudio2019開發(fā)環(huán)境搭建(超級簡單)

    OpenCV4.1.0+VisualStudio2019開發(fā)環(huán)境搭建(超級簡單)

    這篇文章主要介紹了OpenCV4.1.0+VisualStudio2019開發(fā)環(huán)境搭建(超級簡單),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • C++簡明圖解this指針的使用

    C++簡明圖解this指針的使用

    this 指針在C++類和對象中是個很方便實用的關鍵字,可以簡化對象成員屬性的調用,使代碼表達的含義更加準確;在之前的學習中我們都可以判斷變量所占內存空間大小,那么我們創(chuàng)建的類對象所占的內存空間怎么計算呢?想知道this的妙用和類對象占用的內存空間就來跟我學習吧
    2022-06-06
  • c++ 如何實現線程注入

    c++ 如何實現線程注入

    本文主要介紹了各種API遠程線程注入的方法,分別是 遠程線程注入,普通消息鉤子注入,全局消息鉤子注入,APC應用層異步注入,ZwCreateThreadEx強力注入,純匯編實現的線程注入等
    2021-06-06
  • C語言實現時區(qū)轉換函數的實例

    C語言實現時區(qū)轉換函數的實例

    這篇文章主要介紹了C語言實現時區(qū)轉換函數的實例的相關資料,這里分析需求并提供實現代碼,需要的朋友可以參考下
    2017-08-08
  • Visual?C++?6.0添加一個對話框的實現步驟

    Visual?C++?6.0添加一個對話框的實現步驟

    VC6.0是微軟公司推出的一款集成開發(fā)環(huán)境,本文主要介紹了Visual?C++?6.0添加一個對話框的實現步驟,具有一定的參考價值,感興趣的可以了解一下
    2024-06-06
  • C++實現哈夫曼樹編碼解碼

    C++實現哈夫曼樹編碼解碼

    這篇文章主要為大家詳細介紹了C++實現哈夫曼樹編碼解碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • 從匯編看c++中函數里面的static關鍵字的使用說明

    從匯編看c++中函數里面的static關鍵字的使用說明

    c++中的static關鍵字使得函數里面的局部變量的存活期不在局限于函數里面,而是變?yōu)樵谡麄€程序生命期里面都有效
    2013-05-05

最新評論

上思县| 昌图县| 西充县| 博爱县| 苏尼特左旗| 扶沟县| 什邡市| 陆良县| 伊通| 古浪县| 石狮市| 乐昌市| 南投县| 宁国市| 斗六市| 汉寿县| 漯河市| 赤水市| 长治市| 多伦县| 五峰| 深州市| 民乐县| 瑞丽市| 曲阳县| 成安县| 和顺县| 临高县| 夏河县| 任丘市| 涿州市| 临澧县| 攀枝花市| 疏勒县| 荃湾区| 团风县| 渝北区| 东平县| 武穴市| 莱芜市| 洛隆县|