C++實(shí)現(xiàn)簡單射擊小游戲
使用c++制作簡單的橫板射擊小游戲,供大家參考,具體內(nèi)容如下
#include <easyx.h> #include <time.h> #include <conio.h> class Bullet; class Tank; class E_Bullet; class Boss; bool dead = false; bool wined = false; struct pos//坐標(biāo)類 { int a; int b; }; class E_Bullet//敵人打出的子彈 { public: clock_t d; int x; int y; bool on = false; pos show()//畫出新的位置 { setfillcolor(RGB(255, 180, 20)); fillrectangle(x - 5, y - 5, x + 5, y + 5); return pos{ x,y }; } pos del()//覆蓋原來的位置 { setfillcolor(0); setlinecolor(0); fillrectangle(x - 5, y - 5, x + 5, y + 5); rectangle(x - 5, y - 5, x + 5, y + 5); return pos{ x,y }; } pos move()//左移 { x -= 3; return pos{ x,y }; } }; class Bullet//玩家打出的子彈,同上 { public: clock_t d; int x; int y; bool on = false; pos show() { setfillcolor(RGB(150, 180, 210)); fillrectangle(x - 5, y - 5, x + 5, y + 5); return pos{ x,y }; } pos del() { setfillcolor(0); setlinecolor(0); fillrectangle(x - 5, y - 5, x + 5, y + 5); rectangle(x - 5, y - 5, x + 5, y + 5); return pos{ x,y }; } pos move()//右移 { x += 3; return pos{ x,y }; } }; class Boss//敵人 { public: bool hurting = false; clock_t d_hurt; COLORREF clr = RGB(0, 130, 125); int x; int y; int hp = 100;//生命 clock_t d;//判斷舉例上一次執(zhí)行某一函數(shù)過了多久 clock_t att_d; bool angle = false;//方向 pos show() { setfillcolor(clr); fillrectangle(x - 20, y - 40, x + 20, y + 40); return pos{ x,y }; } pos del() { setfillcolor(0); setlinecolor(0); rectangle(x - 20, y - 40, x + 20, y + 40); fillrectangle(x - 20, y - 40, x + 20, y + 40); return pos{ x,y }; } void fire(E_Bullet& but)//攻擊 { but.on = true;//放置一個(gè)子彈 but.x = x - 20; but.y = y; but.d = clock(); } void move()//上上下下得移動(dòng) { if (angle == true) y -= 5; if (angle == false) y += 5; if (y >= 440) angle = true; if (y <= 40) angle = false; } void hurt()//受傷 { hp -= 4; d_hurt = clock(); setfillcolor(0); setlinecolor(WHITE); fillrectangle(160, 485, 560, 510);//更新血條 rectangle(160, 485, 160 + hp * 4, 510); setfillcolor(RGB(230, 0, 1)); setlinecolor(RGB(255, 255, 255)); fillrectangle(160, 485, 160 + hp * 4, 510); rectangle(160, 485, 160 + hp * 4, 510); hurting = true; if (hp <= 0)//死亡 { wined = true; } } }; class Tank//玩家類,同上 { public: bool hurting = false; int hp = 100; int x; COLORREF clr = RGB(150, 180, 210); int y; clock_t d_hurt; Tank() {} Tank(int _x, int _y) { x = _x; y = _y; } Tank operator=(pos p) { x = p.a; y = p.a; } pos show() { setfillcolor(clr); fillrectangle(x - 25, y - 25, x + 25, y + 25); setfillcolor(RGB(100, 200, 180)); fillrectangle(x, y + 5, x + 40, y - 5); return pos{ x,y }; } pos del() { setfillcolor(0); setlinecolor(0); fillrectangle(x - 25, y - 25, x + 25, y + 25); rectangle(x - 25, y - 25, x + 25, y + 25); fillrectangle(x, y + 5, x + 40, y - 5); rectangle(x, y + 5, x + 40, y - 5); return pos{ x,y }; } void fire(Bullet& but) { but.on = true; but.x = x + 45; but.y = y; but.d = clock(); but.show(); } void hurt() { hp -= 2; d_hurt = clock(); setfillcolor(0); setlinecolor(WHITE); fillrectangle(160, 515, 560, 540); rectangle(160, 515, 560, 540); rectangle(160, 515, 160 + hp * 4, 540); setfillcolor(RGB(0, 255, 1)); setlinecolor(RGB(255, 255, 255)); fillrectangle(160, 515, 160 + hp * 4, 540); rectangle(160, 515, 160 + hp * 4, 540); hurting = true; if (hp <= 0) dead = true; } }; #define BT_MAX 8 int main() { initgraph(640, 550, 4);//初始化屏幕 settextcolor(RGB(0, 254, 0)); settextstyle(35, 0, _T("黑體")); outtextxy(150, 200, _T("W,S移動(dòng),K攻擊")); Sleep(3000); setlinecolor(0); setfillcolor(0); rectangle(0, 0, 640, 550); fillrectangle(0, 0, 640, 550); setlinecolor(RGB(255, 255, 255)); setfillcolor(RGB(255, 255, 255)); clock_t delay = clock();//玩家移動(dòng)的延時(shí) clock_t d_f = clock();//玩家開火的延時(shí) line(0, 481, 640, 481);//分割畫面與血條 Bullet bt[BT_MAX];//玩家的子彈 Tank tk(30, 30);//玩家 Boss bo;//敵人 bo.x = 580; bo.y = 240; E_Bullet ebt[BT_MAX];//敵人的子彈 bo.d = clock();//初始化延時(shí) bo.att_d = clock(); tk.show(); settextstyle(20, 0, _T("黑體")); outtextxy(10, 485, _T("BOSS的生命值:")); setfillcolor(RGB(230, 0, 1)); fillrectangle(160, 485, 560, 510);//敵人血條 outtextxy(10, 520, _T("玩家的生命值:")); setfillcolor(RGB(0, 255, 1)); fillrectangle(160, 515, 560, 540);//玩家血條 while (1)//主循環(huán) { if (wined || dead)//玩家死了或者敵人死了 break; if (GetAsyncKeyState('W') & 0x8000)//玩家移動(dòng) { if (tk.y > 28 && (clock() - delay) >= 40) { tk.del(); tk.y -= 3; tk.show(); delay = clock(); } } if (GetAsyncKeyState('w') & 0x8000)//玩家移動(dòng) { if (tk.y > 28 && (clock() - delay) >= 40) { tk.del(); tk.y -= 3; tk.show(); delay = clock(); } } if (GetAsyncKeyState('k') & 0x8000)//玩家開火 { for (int i = 0; i < BT_MAX; i++) { if (bt[i].on == false && (clock() - d_f) > 800) { bt[i].on = true; tk.fire(bt[i]); d_f = clock(); break; } } } if (GetAsyncKeyState('K') & 0x8000)//玩家開火 { for (int i = 0; i < BT_MAX; i++) { if (bt[i].on == false && (clock() - d_f) > 800) { tk.fire(bt[i]); d_f = clock(); break; } } } if (GetAsyncKeyState('S') & 0x8000)//玩家移動(dòng) { if (tk.y < 452 && (clock() - delay) >= 40) { tk.del(); tk.y += 3; tk.show(); delay = clock(); } } if (GetAsyncKeyState('s') & 0x8000)//玩家移動(dòng) if (tk.y < 452 && (clock() - delay) >= 40) { tk.del(); tk.y += 3; tk.show(); delay = clock(); } for (int i = 0; i < BT_MAX; i++)//遍歷子彈,使子彈刷新 { if (bt[i].on == true && (clock() - bt[i].d) > 20) { bt[i].del(); bt[i].move(); bt[i].show(); bt[i].d = clock(); if (bt[i].x >= 635) bt[i].on = false, bt[i].del();//到達(dá)了屏幕最右端 if ((bt[i].x + 5 >= bo.x - 20 && bt[i].x - 5 <= bo.x + 20) && (bt[i].y - 5 < bo.y + 40 && bt[i].y + 5 > bo.y - 40)) //擊中敵人 bt[i].on = false, bo.hurt(), bt[i].del(); } } if (clock() - bo.att_d > 700)//敵人自動(dòng)開火 { for (int i = 0; i < BT_MAX; i++) { if (ebt[i].on == false) { bo.fire(ebt[i]); break; } } bo.att_d = clock(); } for (int i = 0; i < BT_MAX; i++)//敵人子彈刷新,同上 { if (ebt[i].on == true && (clock() - ebt[i].d > 20)) { ebt[i].del(); ebt[i].move(); ebt[i].show(); ebt[i].d = clock(); if (ebt[i].x < 5) ebt[i].del(), ebt[i].on = false; if (ebt[i].x - 5 < tk.x + 25 && ebt[i].x + 5 > tk.x - 25 && ebt[i].y - 5 < tk.y + 25 && ebt[i].y + 5 > tk.y - 25) { ebt[i].on = false, tk.hurt(), ebt[i].del(); } } } if (tk.hurting == true)//玩家受傷閃爍0.1秒 if (clock() - tk.d_hurt > 100) { tk.clr = RGB(150, 180, 210), tk.show(), tk.hurting = false; } else tk.clr = RGB(255, 0, 0), tk.show(); if (bo.hurting == true)//敵人受傷閃爍0.1秒 if (clock() - bo.d_hurt > 100) { bo.clr = RGB(0, 130, 125), bo.show(), bo.hurting = false; } else bo.clr = RGB(0, 255, 0), bo.show(); if (clock() - bo.d > 50)//敵人移動(dòng)延時(shí); bo.del(), bo.move(), bo.show(), bo.d = clock(); } if (wined)//勝負(fù)已分 { settextcolor(RGB(0, 254, 0)); settextstyle(35, 0, _T("黑體")); outtextxy(150, 200, _T("你打敗了boss!你贏了?。?)); } else { settextcolor(RGB(254, 0, 0)); settextstyle(35, 0, _T("黑體")); outtextxy(140, 200, _T("你被boss打敗了!")); } Sleep(5000); closegraph(); return 0; }
游戲截圖
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C語言
下一篇:使用mmap實(shí)現(xiàn)多進(jìn)程對(duì)大文件拷貝
本文標(biāo)題:C++實(shí)現(xiàn)簡單射擊小游戲
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/183.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實(shí)現(xiàn)方法


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)