今天用C語言來實(shí)現(xiàn)一個打飛機(jī)的游戲!準(zhǔn)確的說應(yīng)該叫《防空車打飛機(jī)》一輛車在下面,三種類型的飛機(jī)在上空隨機(jī)速度飛過(不斷出現(xiàn)),而且飛機(jī)飛過的時候會往下方扔炸彈,在游戲上方也設(shè)置了分?jǐn)?shù)和車的生命值,如果你被炸到了那么就會生命值-1,打死一架飛機(jī)分?jǐn)?shù)就會加一,飛機(jī)不一樣得分也會不一樣。
游戲說明:
在游戲中,你操控你的防空車盡可能的多擊落飛機(jī),躲避飛機(jī)扔的炸彈。
游戲結(jié)束后,你可以選擇重新開始游戲。
簡單了解游戲后我們就來試試吧!
本項(xiàng)目編譯環(huán)境:Visual Studio 2019/2022,EasyX插件
代碼展示:(直接上源碼,大家可以看注釋)
/////////////////////////////////// // 程序名稱:防空車打飛機(jī) // 操作方式:左 左走 右 右走 空格 發(fā)射子彈 #include#include #include #include #pragma comment (lib,"MSIMG32.lib") #define KEY_DOWN(vk_c) (GetAsyncKeyState(vk_c)&0x8000?1:0) using namespace std; // IMAGE IMAGE buffer(500,400),background,player,missile,planes[3],planebomb; // 玩家x坐標(biāo),玩家y坐標(biāo),裝彈剩余時間,生命值,得分 int playerx=0,playery=286,firereload=5,strength=50,score=0; // 玩家防空導(dǎo)彈 struct PLAYERMISL { int x; int y; }; // 飛機(jī)類型 struct BOMBPLANE { int speed; // 飛機(jī)速度 int width; // 飛機(jī)寬度 int height; // 飛機(jī)高度 } bombplane[3]= {{15,85,22},{12,81,22},{8,105,50}}; // 炸彈 struct BOMB { int x; int y; }; // 飛機(jī) struct PLANE { int x; int y; int type; int reload;// 還有多少幀重新扔炸彈 }; vector misl; vector plane; vector bomb; // 封裝好的透明貼圖函數(shù) void putpicture(int x,int y,IMAGE img) { HDC dstDC = GetImageHDC(&buffer); HDC srcDC = GetImageHDC(&img); TransparentBlt(dstDC, x, y, img.getwidth(), img.getheight(), srcDC, 0, 0, img.getwidth(), img.getheight(), RGB(40,112,162)); } // 精確延時 void sleep(int ms) { static clock_t oldclock = clock(); // 靜態(tài)變量,記錄上一次 tick oldclock += ms * CLOCKS_PER_SEC / 1000; // 更新 tick if (clock() > oldclock) // 如果已經(jīng)超時,無需延時 { oldclock = clock(); } else while(clock() < oldclock) // 延時 { Sleep(1); // 釋放 CPU 控制權(quán),降低 CPU 占用率 } } // 矩形碰撞檢測函數(shù) bool collision(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2) { if((abs((x1 + w1 / 2) - (x2 + w2/2)) < (w1 + w2) / 2)&&abs((y1 + h1 / 2) - (y2 + h2/2) )< (h1 + h2) / 2) { return true; } else { return false; } } // 創(chuàng)建一顆防空導(dǎo)彈 void createmisl() { PLAYERMISL playermisl; playermisl.x=playerx+12; playermisl.y=playery-4; misl.push_back(playermisl); } // 創(chuàng)建一顆炸彈 void createbomb(PLANE p) { BOMB b; b.x=p.x+bombplane[p.type].width/2-4; b.y=p.y+bombplane[p.type].height+2; bomb.push_back(b); } // 創(chuàng)建一架飛機(jī) void createplane() { PLANE newplane; newplane.x=500; newplane.y=rand()%181+20; newplane.type=rand()%3; newplane.reload=10; // 炸彈發(fā)射間隔時間為10幀 plane.push_back(newplane); } // 加載圖片資源 void loadres() { loadimage(&background,"IMAGE","BG"); loadimage(&player,"IMAGE","PLAYER"); loadimage(&planebomb,"IMAGE","BOMB"); loadimage(&missile,"IMAGE","MISL"); char filename[10]; for(int i=0; i<3; i++) { sprintf(filename,"PLANE%d",i); loadimage(&planes[i],"IMAGE",filename); } } // 每幀繪圖 void render() { char info[25]; SetWorkingImage(&buffer); // 先在緩沖區(qū)繪圖 putimage(0,0,&background); putpicture(playerx,playery,player); // 顯示防空車 vector ::iterator it1; vector ::iterator it2; vector ::iterator it3; for(it1=misl.begin(); it1!=misl.end(); it1++) { putpicture(it1->x,it1->y,missile); // 顯示防空導(dǎo)彈 } for(it2=plane.begin(); it2!=plane.end(); it2++) { putpicture(it2->x,it2->y,planes[it2->type]); // 顯示飛機(jī) } for(it3=bomb.begin(); it3!=bomb.end(); it3++) { putpicture(it3->x,it3->y,planebomb); // 顯示炸彈 } sprintf(info,"生命值:%d 得分:%d",strength,score); outtextxy(0,0,info); // 顯示游戲數(shù)據(jù) SetWorkingImage(); putimage(0,0,&buffer); // 把緩沖區(qū)繪圖內(nèi)容一次性繪制上去,這樣能消除閃爍 } int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int) { srand(time(NULL)); loadres(); initgraph(500,400); setbkmode(TRANSPARENT); vector ::iterator it1; vector ::iterator it2; vector ::iterator it3; gamestart: bool eraseyes=false; while(true) { if(strength<1) { break; } if(plane.size()<3) { createplane(); } for(it1=misl.begin(); it1!=misl.end();) { it1->y-=10; if(it1->y<0) { it1=misl.erase(it1); } else { ++it1; } } for(it2=plane.begin(); it2!=plane.end();) { it2->x-=bombplane[it2->type].speed; if(it2->x+bombplane[it2->type].width<0) { plane.erase(it2); } else { if(it2->reload==0) { it2->reload=10; createbomb(*it2); } else { it2->reload-=1; } ++it2; } } for(it2=plane.begin(); it2!=plane.end();) { for(it1=misl.begin(); it1!=misl.end();) { if(collision(it1->x,it1->y,5,7,it2->x,it2->y,bombplane[it2->type].width,bombplane[it2->type].height)) { misl.erase(it1); plane.erase(it2); eraseyes=true; score+=1; } else { ++it1; } } if(eraseyes==false) { it2++; } else { eraseyes=false; } } for(it3=bomb.begin(); it3!=bomb.end();) { it3->y+=5; if(it3->y>400) { it3=bomb.erase(it3); } else { ++it3; } } for(it3=bomb.begin(); it3!=bomb.end();) { if(collision(it3->x,it3->y,5,7,playerx,playery,58,49)) { bomb.erase(it3); strength-=1; } else { ++it3; } } if(KEY_DOWN(VK_LEFT)&&playerx>=10) { playerx-=10; } if(KEY_DOWN(VK_RIGHT)&&playerx<=384) { playerx+=10; } if(KEY_DOWN(VK_SPACE)&&firereload==0) { createmisl(); firereload=5; } if(firereload>=1) { firereload-=1; } render(); sleep(33); } char result[50]; sprintf(result,"本次得分:%d分,請?jiān)俳釉賲?。是否重新開始游戲?",score); if(MessageBox(GetHWnd(),result,"游戲結(jié)束",MB_YESNO)==IDYES) { playerx=0; playery=286; strength=50; score=0; firereload=5; misl.clear(); plane.clear(); bomb.clear(); goto gamestart; } return 0; }
大家趕緊去動手試試吧!
審核編輯:湯梓紅
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。
舉報投訴
-
游戲
+關(guān)注
關(guān)注
2文章
788瀏覽量
27357 -
C語言
+關(guān)注
關(guān)注
183文章
7642瀏覽量
145124 -
編程
+關(guān)注
關(guān)注
90文章
3711瀏覽量
96983 -
源碼
+關(guān)注
關(guān)注
8文章
682瀏覽量
31186
原文標(biāo)題:C語言零基礎(chǔ)項(xiàng)目:打飛機(jī)游戲!300行源碼分享+詳細(xì)思路
文章出處:【微信號:cyuyanxuexi,微信公眾號:C語言編程學(xué)習(xí)基地】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
熱點(diǎn)推薦
C語言零基礎(chǔ)項(xiàng)目:涂格子(點(diǎn)燈)游戲!詳細(xì)思路+源碼分享
點(diǎn)燈游戲是一個十分有趣的智力游戲:有一行N行N列的燈,開始時全部是滅的,當(dāng)你點(diǎn)擊其中一盞燈時他的上下左右(若存在的話)狀態(tài)全部改變,現(xiàn)在要求你在限定的時間內(nèi)以最少地步數(shù),將全部的燈點(diǎn)亮
發(fā)表于 12-16 09:47
?1360次閱讀
基于FRDM-KL25Z開發(fā)板的仿微信打飛機(jī)游戲
版打飛機(jī)”游戲就誕生了。實(shí)際運(yùn)行效果不是很理想,控制不是很到位。當(dāng)然作為電控工程師,一個自己都不滿意的東西是不允許存在的,于是我又將觸摸滑條控制改為 “重力感應(yīng)”控制(FRDM-KL25Z上正好帶了重力
發(fā)表于 12-31 12:08
C語言零基礎(chǔ)項(xiàng)目:打字母游戲!詳細(xì)思路+源碼分享
今天就用C語言寫了這么個打字母的小程序,就是以前學(xué)習(xí)機(jī)上那種字母往下掉,然后按相應(yīng)鍵字母消失的游戲。
發(fā)表于 12-15 15:02
?1083次閱讀
C語言零基礎(chǔ)項(xiàng)目:生命游戲!詳細(xì)思路+源碼分享
C語言零基礎(chǔ)項(xiàng)目:黑白棋游戲!詳細(xì)思路+源碼分享
《黑白棋》也叫翻轉(zhuǎn)棋或者奧賽羅,其游戲過程是相互翻轉(zhuǎn)對方的棋子,最后以棋盤上誰的棋子多來判斷勝負(fù)。雖然規(guī)則簡單,但是變化復(fù)雜,是典型的易學(xué)難精,奧妙無窮,不信您就試試看吧!
C語言零基礎(chǔ)項(xiàng)目:對對碰(消除類)游戲!詳細(xì)思路+源碼分享
游戲中消除的對象為各種各樣的頭像,包括樹、小車、草莓和酒瓶等一些頭像。玩家通關(guān)移動這些頭像位置湊夠一定數(shù)量的相同圖標(biāo)即可消除。
C語言零基礎(chǔ)項(xiàng)目:吃豆人小游戲!詳細(xì)思路+源碼分享
《吃豆游戲》是一款休閑小游戲,和貪吃蛇,球球大作戰(zhàn)吃食物都是有差不多的游戲邏輯。
C語言項(xiàng)目:礦井逃生游戲(密室)!詳細(xì)思路+源碼分享
密室逃脫相信大部分都玩過了吧?本游戲就是一種用C語言寫的類似的游戲,因?yàn)橛檬蛛娡舱彰髡衣?,所以有點(diǎn)像礦工的樣子,還是叫它礦井逃生吧?。ㄒ韵率?b class='flag-5'>游戲
C語言零基礎(chǔ)項(xiàng)目:打飛機(jī)游戲!300行源碼分享+詳細(xì)思路
評論