欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語(yǔ)言實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲

 更新時(shí)間:2022年06月08日 09:51:32   作者:JysinWee  
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C語(yǔ)言實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲的具體代碼,供大家參考,具體內(nèi)容如下

技術(shù)原型

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

技術(shù)路線

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

實(shí)現(xiàn)效果

花費(fèi)時(shí)間

約6小時(shí)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); //光標(biāo)定位函數(shù)
void print_plane(int x, int y); //打印飛機(jī)
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); //生成敵機(jī)
void print_target(nodeptr_t listnode); //打印敵機(jī)
nodeptr_t update_target(nodeptr_t listnode); //更新敵機(jī)位置
int collision_detection(nodeptr_t bulletlist, nodeptr_t targetlist); //碰撞檢測(cè)
bool is_gameover(int x,int y, nodeptr_t targetlist); // 游戲結(jié)束
void clear(nodeptr_t bulletlist, nodeptr_t targetlist);


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

?? ?nodeptr_t target = nullptr; //敵機(jī)鏈表
?? ?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; //分?jǐn)?shù)
?? ?int targetspeed = 0;
?? ?int bulletspeed = 0;

?? ?while(1)
?? ?{
?? ??? ?system("cls");
?? ??? ?time_t currenttime;
?? ??? ?currenttime = time(NULL);
?? ??? ?//每隔一秒生成一架敵機(jī)
?? ??? ?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;
?? ??? ?//開(kāi)火則生成子彈
?? ??? ?if (isfire)
?? ??? ?{
?? ??? ??? ?bullet = generate_bullet(bullet, plane_x, plane_y - 1);
?? ??? ??? ?isfire = 0;
?? ??? ?}
?? ??? ?//打印敵機(jī)
?? ??? ?print_target(target);
?? ??? ?targetspeed++;
?? ??? ?if(targetspeed % 2 == 0)
?? ??? ??? ?target = update_target(target);
?? ??? ?//打印子彈
?? ??? ?print_bullet(bullet);
?? ??? ?bulletspeed++;
?? ??? ?if (bulletspeed % 2 == 0)
?? ??? ??? ?bullet = update_bullet(bullet);
?? ??? ?//碰撞檢測(cè)
?? ??? ?grade = grade + collision_detection(bullet, target);
?? ??? ?gotoxy(0, 25);
?? ??? ?printf("SCORE: %d", grade);
?? ??? ?//打印飛機(jī)
?? ??? ?print_plane(plane_x, plane_y);
?? ??? ?//敵機(jī)本機(jī)是否相撞
?? ??? ?bool isgameover = is_gameover(plane_x, plane_y, target);
?? ??? ?//Sleep(100);
?? ??? ?//非阻塞鍵盤(pán)輸入
?? ??? ?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)//光標(biāo)定位函數(shù)
{
?? ?COORD p;//定義結(jié)構(gòu)體變量p
?? ?HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);//獲取當(dāng)前函數(shù)句柄
?? ?p.X = x;
?? ?p.Y = y;//將光標(biāo)的目標(biāo)移動(dòng)位置傳遞給結(jié)構(gòu)體
?? ?SetConsoleCursorPosition(handle, p);//移動(dòng)光標(biāo)
?? ?return;
}

void print_plane(int x, int y) //打印飛機(jī)
{
?? ?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;
?}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語(yǔ)言執(zhí)行時(shí),程序控制臺(tái)輸出窗口 一閃而過(guò)問(wèn)題及解決

    C語(yǔ)言執(zhí)行時(shí),程序控制臺(tái)輸出窗口 一閃而過(guò)問(wèn)題及解決

    這篇文章主要介紹了C語(yǔ)言執(zhí)行時(shí),程序控制臺(tái)輸出窗口 一閃而過(guò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Visual Studio Code (VSCode) 配置搭建 C/C++ 開(kāi)發(fā)編譯環(huán)境的流程

    Visual Studio Code (VSCode) 配置搭建 C/C++ 開(kāi)發(fā)編譯環(huán)境的流程

    記得N年前剛開(kāi)始接觸編程時(shí),使用的是Visual C++6.0,下面這個(gè)可愛(ài)的圖標(biāo)很多人一定很熟悉。不過(guò)今天想嘗鮮新的工具 Visual Studio Code 來(lái)搭建C/C++開(kāi)發(fā)環(huán)境,感興趣的朋友一起看看吧
    2021-09-09
  • c++ vector造成的內(nèi)存泄漏問(wèn)題

    c++ vector造成的內(nèi)存泄漏問(wèn)題

    這篇文章主要介紹了c++ vector造成的內(nèi)存泄漏問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 詳解C語(yǔ)言?xún)?nèi)核字符串拷貝與比較

    詳解C語(yǔ)言?xún)?nèi)核字符串拷貝與比較

    本文將探索一下字符串的拷貝與比較,與應(yīng)用層不同內(nèi)核字符串拷貝與比較也需要使用內(nèi)核專(zhuān)用的API函數(shù),字符串的拷貝往往伴隨有內(nèi)核內(nèi)存分配,我們將首先簡(jiǎn)單介紹內(nèi)核如何分配堆空間,然后再以此為契機(jī)簡(jiǎn)介字符串的拷貝與比較
    2022-09-09
  • 一波二叉樹(shù)遍歷問(wèn)題的C++解答實(shí)例分享

    一波二叉樹(shù)遍歷問(wèn)題的C++解答實(shí)例分享

    這篇文章主要介紹了一波二叉樹(shù)遍歷問(wèn)題的C++解答實(shí)例分享,包括節(jié)點(diǎn)打印和轉(zhuǎn)換為鏡像等問(wèn)題的解答,需要的朋友可以參考下
    2016-02-02
  • 詳解C++ bitset用法

    詳解C++ bitset用法

    這篇文章主要介紹了C++ bitset用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 基于C語(yǔ)言實(shí)現(xiàn)泛型編程詳解

    基于C語(yǔ)言實(shí)現(xiàn)泛型編程詳解

    對(duì)于C而言,想實(shí)現(xiàn)泛型編程并非易事,甚至可以說(shuō)非常繁瑣,一大堆坑。最主要也沒(méi)有現(xiàn)成的輪子可用。本文就來(lái)詳細(xì)為大家講講C語(yǔ)言如何實(shí)現(xiàn)泛型編程詳解,需要的可以參考一下
    2022-07-07
  • Linux環(huán)境下段錯(cuò)誤的產(chǎn)生原因及調(diào)試方法小結(jié)

    Linux環(huán)境下段錯(cuò)誤的產(chǎn)生原因及調(diào)試方法小結(jié)

    借此機(jī)會(huì)系統(tǒng)學(xué)習(xí)了一下,這里對(duì)Linux環(huán)境下的段錯(cuò)誤做個(gè)小結(jié),方便以后同類(lèi)問(wèn)題的排查與解決
    2011-11-11
  • C語(yǔ)言實(shí)現(xiàn)CRC校驗(yàn)算法的示例詳解

    C語(yǔ)言實(shí)現(xiàn)CRC校驗(yàn)算法的示例詳解

    CRC(Cyclic Redundancy Check,循環(huán)冗余校驗(yàn))是一種常用的錯(cuò)誤檢測(cè)技術(shù),用于驗(yàn)證數(shù)據(jù)在傳輸或存儲(chǔ)過(guò)程中是否發(fā)生了錯(cuò)誤,本文主要介紹了C語(yǔ)言如何實(shí)現(xiàn)CRC校驗(yàn)算法,需要的可以參考一下
    2023-08-08
  • C++鏈?zhǔn)蕉鏄?shù)深入分析

    C++鏈?zhǔn)蕉鏄?shù)深入分析

    二叉樹(shù)的鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)是指,用鏈表來(lái)表示一棵二叉樹(shù),即用鏈來(lái)指示元素的邏輯關(guān)系。通常的方法是鏈表中每個(gè)結(jié)點(diǎn)由三個(gè)域組成,數(shù)據(jù)域和左右指針域,左右指針?lè)謩e用來(lái)給出該結(jié)點(diǎn)左孩子和右孩子所在的鏈結(jié)點(diǎn)的存儲(chǔ)地址
    2022-06-06

最新評(píng)論