技術原型
1、void gotoxy(int x, int y) 函式,該函式可以使游標去到(x,y)的位置進行列印;
2、鍊錶,用於儲存狀態;
3、windows.h中有非阻塞輸入,_kbhit();
4、隨機生成數;
5、視覺暫留;
6、碰撞檢測;
7、清屏函式;
8、設定邊界;
技術路線
1、設定乙個邊界;
2、維護乙個子彈的列表;
3、維護乙個敵機的列表;
4、初始化飛機的位置;
5、每隔一秒鐘生成一架敵機,生成位置x座標隨機,座標為0;
6、設定點選空格生成子彈;
7、設定while(1)迴圈,在其中每次進行清屏和更新;
8、每次更新遍歷子彈鍊錶,使所有子彈的位置向上移動1;
9、每次更新遍歷敵機鍊錶,使所有敵機的位置向下移動1;
10、碰撞檢測,遍歷子彈和敵機列表,發現碰撞則從各自的鍊錶中移除碰撞的節點;
11、當有敵機碰撞到本機遊戲結束;
12、當子彈或者敵機碰撞到邊界,則從鍊錶中移除;
13、每次檢測到碰撞加一分。
實現效果
花費時間
約6小時30分鐘
**#include
#include
#include
#include
#include
#include
struct node ;
ty程式設計客棧pedef 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()
subtime = currenttime - starttime;
//**則生成子彈
if (isfire)
//列印敵機
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)
if (_kbhit()) }
return 0;
}void gotoxy(int x, int y)//游標定位函式
void print_plane(int x, int y) //列印飛機
else if (x == 1)
else if (x == 20)
else if (x == 19)
else
return;
}nodeptr_t generate_bullet(nodeptr_t listnode, int x, int y)
void print_bullet(nodeptr_t listnode)
return;
}nodeptr_t update_bullet(nodeptr_t listnode)
if (templist->next != nullptr)
templist = templist->next;
else
break;
} return listnode;
}nodeptr_t generate_tawww.cppcns.comrget(nodeptr_t listnode, int x)
void print_target(nodeptr_t listnode)
return;
}nodeptr_t update_target(nodeptr_t listnode)
if (templist->next != nullptr)
templist = templist->next;
else
break;
} return listnode;
}int collision_detection(nodeptr_t bulletlist, nodeptr_t targetlist)
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)
else if(tempsub == 1 && temptargetlist->next->y > y + 1)
else if (tempsub == 2 && temptargetlist->next->y > y + 1)
temptargetlist = temptargetlist->next;
} return 0;
} void clear(nodeptr_t bulletlist, nodeptr_t targetlist)
while (targetlist->next != nullptr)
return;
}本文標題: c語言之飛機大戰遊戲
本文位址:
C語言版飛機大戰遊戲
c語言版飛機大戰,供大家參考,具體內容如下 不多說直接上 include include include include include using namespace std all the structures typedef struct frame frame all the functio...
C語言飛機大戰
include include int zdj x,zdj y int zd x,zd y int dj x,dj y int speed int map 21 41 int score int h 20,c 40 牆是1 飛機2 子彈3 敵機4 void start 設定初始值 void star...
C語言實現飛機大戰遊戲Demo
o表示玩家,w表示敵機 檔案 ifndef console h define console h 游標跳轉到指定位置 void gotoxy int x,int y handle hout getstdhandle std output handle 獲取標準輸出裝置控制代碼 setconsolec...