A mini-game for playing bricks. Material: EasyX graphics library.
The method of moving the boards also needs to be optimized for collision treatment.
//Definition Circle, Brick, Broad#include<cmath> #include<> #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_) {} //The starting position of the ball void prtCirl() { setfillcolor(color); solidcircle(x0, y0, r); } //The movement of the ball void CirlMove() { setfillcolor(BLACK); solidcircle(x0, y0, r); x0 += mvX; y0 += mvY; setfillcolor(color); solidcircle(x0, y0, r); } //Judge whether the ball is out of the wide mouth. //Parameters: upper left coordinate, width, height of the window. //Leave and return to true. 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_) {} //Painting of bricks void prtBrick() { setfillcolor(color); solidrectangle(x0, y0, x0 + width, y0 + height); } //Judge whether the brick collides with the ball //Parameters: small ball //A collision occurs and returns to true bool IsCrashCirl(Circle &arg) { if (arg.x0 + < x0 || x0 + width < arg.x0 - ) return false; int disY = min(abs(y0 - arg.y0), abs(y0 + height - arg.y0)); if (disY <= ) { = -; return true; } return false; } //Clearing of bricks 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_) {} //Hyperload, determine whether the board collided with the ball //Parameters: small ball //A collision occurs and returns to true bool IsCrashCirl(Circle &arg) { if (arg.x0 + < x0 || x0 + width < arg.x0 - ) return false; if (y0 - arg.y0 <= && > 0) { = -; return true; } return false; } //The wooden board moves void BroadMove() { POINT point; GetCursorPos(&point); if (x0 <= && <= x0) return; BrickClr(); if ( < 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_
// #include<list> #include<algorithm> #include"" using namespace std; const int WndW = 400, WndH = 400; //Window sizelist<Brick> CreatBricks(); bool theGame(list<Brick> &MyBrks, Broad &MyBrd, Circle&MyCirl); int main() { //brick layout list<Brick> MyBrks = move(CreatBricks()); //broad: 60*20, moving speed 5, WHITE Broad MyBrd(WndW/2 - 30, WndH - 20, 60, 20, 5, 0, WndW, WHITE); //circle: radius 5, speed of movement 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 implementationlist<Brick> CreatBricks() { //brick information: 5 rows and 10 columns, 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; } //The game implementationbool theGame(list<Brick> &MyBrks, Broad &MyBrd, Circle&MyCirl) { //Game start interface for_each((), (), [](Brick it) { (); }); (); (); //Game loop while (!()) { (); (); if ((0, 0, WndW, WndH)) return false; (MyCirl); auto theBrick = find_if((), (), [&MyCirl](Brick it) { return (MyCirl); }); if (theBrick != ()) { theBrick->BrickClr(); (theBrick); } Sleep(30); } return true; }
Summarize
The above is the sample code of the C++ mini game BrickHit introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!