您好,登錄后才能下訂單哦!
小編給大家分享一下C++小游戲BrickHit的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
打磚塊小游戲。材料:EasyX圖形庫。
碰撞的處理,木板移動方法還需要優化。
//定義 Circle,Brick,Broad #include<cmath> #include<graphics.h> #ifndef _PROPERTY_H_ #define _PROPERTY_H_ struct Circle { int x0, y0, r; int mvX, mvY; COLORREF color; virtual ~Circle() {} Circle(int x0_, int y0_, int r_, int mvX_, int mvY_, COLORREF color_) :x0(x0_), y0(y0_), r(r_), mvX(mvX_), mvY(mvY_), color(color_) {} //小球起始位置 void prtCirl() { setfillcolor(color); solidcircle(x0, y0, r); } //小球的移動 void CirlMove() { setfillcolor(BLACK); solidcircle(x0, y0, r); x0 += mvX; y0 += mvY; setfillcolor(color); solidcircle(x0, y0, r); } //判斷小球是否離開寬口內。 //參數:窗口左上坐標、寬、高。 //離開返回真。 bool IsCirlQuit(int x, int y, int width, int height) { if (x0 - x <= r && mvX < 0) { mvX = -mvX; return false; } else if (x + width - x0 <= r && mvX > 0) { mvX = -mvX; return false; } else if (y0 - y <= r && mvY < 0) { mvY = -mvY; return false; } else if (y + height - y0 <= r) return true; return false; } }; struct Brick { int x0, y0; COLORREF color; int height, width; virtual ~Brick() {} Brick(int x0_, int y0_, int width_, int height_, COLORREF color_) :x0(x0_), y0(y0_), width(width_), height(height_), color(color_) {} //磚塊的繪制 void prtBrick() { setfillcolor(color); solidrectangle(x0, y0, x0 + width, y0 + height); } //判斷磚塊是否與小球發生碰撞 //參數:小球 //發生碰撞返回真 bool IsCrashCirl(Circle &arg) { if (arg.x0 + arg.r < x0 || x0 + width < arg.x0 - arg.r) return false; int disY = min(abs(y0 - arg.y0), abs(y0 + height - arg.y0)); if (disY <= arg.r) { arg.mvY = -arg.mvY; return true; } return false; } //磚塊的清除 void BrickClr() { setfillcolor(BLACK); solidrectangle(x0, y0, x0 + width, y0 + height); } }; struct Broad :public Brick{ int mvX; int floor, ceiling; virtual ~Broad() {} Broad(int x0_, int y0_, int width_, int height_, int mvX_, int floor_, int ceiling_, COLORREF color_) :Brick(x0_, y0_, width_, height_, color_), mvX(mvX_), floor(floor_), ceiling(ceiling_) {} //重載,判斷木板是否與小球發生碰撞 //參數:小球 //發生碰撞返回真 bool IsCrashCirl(Circle &arg) { if (arg.x0 + arg.r < x0 || x0 + width < arg.x0 - arg.r) return false; if (y0 - arg.y0 <= arg.r&&arg.mvY > 0) { arg.mvY = -arg.mvY; return true; } return false; } //木板移動 void BroadMove() { POINT point; GetCursorPos(&point); if (x0 <= point.x&&point.x <= x0) return; BrickClr(); if (point.x < x0) x0 = max(x0 - mvX, floor); else x0 = min(x0 + mvX, ceiling - width); setfillcolor(color); solidrectangle(x0, y0, x0 + width, y0 + height); } }; #endif // _PROPERTY_H_
//Main.cpp #include<list> #include<algorithm> #include"property.cpp" using namespace std; const int WndW = 400, WndH = 400; //窗口大小 list<Brick> CreatBricks(); bool theGame(list<Brick> &MyBrks, Broad &MyBrd, Circle&MyCirl); int main() { //brick布局 list<Brick> MyBrks = move(CreatBricks()); //broad:60*20,移速5,WHITE Broad MyBrd(WndW/2 - 30, WndH - 20, 60, 20, 5, 0, WndW, WHITE); //circle:半徑5,移速5,DARKGRAY Circle MyCirl(WndW/2 - 10, WndH - 20 - 10, 10, 5, 5, DARKGRAY); HWND Hwnd = initgraph(WndW, WndH); bool GameOver = theGame(MyBrks, MyBrd, MyCirl); if (GameOver) MessageBox(Hwnd, L"u Win!", L"BrickHit",MB_OK); else MessageBox(Hwnd, L"default!", L"BrickHit", MB_OK); closegraph(); return 0; } //bricks的實現 list<Brick> CreatBricks() { //brick信息:5行10列,40*10 int Row = 5, Col = 10; int BrickW = WndW / Col; int BrickH = 10; list<Brick> MyBrks; bool ColChoice = true; for (int i = Row - 1; i >= 0; i--) { ColChoice = !ColChoice; for (int j = 0; j < Col; j++) switch (ColChoice) { case true: MyBrks.push_back({ BrickW*j,BrickH*i,BrickW,BrickH,LIGHTGREEN }); ColChoice = !ColChoice; break; case false: MyBrks.push_back({ BrickW*j,BrickH*i,BrickW,BrickH,LIGHTCYAN }); ColChoice = !ColChoice; break; } } return MyBrks; } //游戲的實現 bool theGame(list<Brick> &MyBrks, Broad &MyBrd, Circle&MyCirl) { //游戲起始界面 for_each(MyBrks.begin(), MyBrks.end(), [](Brick it) { it.prtBrick(); }); MyBrd.prtBrick(); MyCirl.prtCirl(); //游戲循環 while (!MyBrks.empty()) { MyCirl.CirlMove(); MyBrd.BroadMove(); if (MyCirl.IsCirlQuit(0, 0, WndW, WndH)) return false; MyBrd.IsCrashCirl(MyCirl); auto theBrick = find_if(MyBrks.begin(), MyBrks.end(), [&MyCirl](Brick it) { return it.IsCrashCirl(MyCirl); }); if (theBrick != MyBrks.end()) { theBrick->BrickClr(); MyBrks.erase(theBrick); } Sleep(30); } return true; }
以上是“C++小游戲BrickHit的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。