C ++ games: BrickHit and brickhit
Brick games. Material: EasyX graphics library.
The Board movement method also needs to be optimized for collision handling.
1 // define Circle, Brick, Broad 2 # include <cmath> 3 # include <graphics. h> 4 5 # ifndef _ PROPERTY_H _ 6 # define _ PROPERTY_H _ 7 struct Circle {8 int x0, y0, r; 9 int mvX, mvY; 10 COLORREF color; 11 virtual ~ Circle () {} 12 Circle (int x0 _, int y0 _, int r _, int mvX _, int mvY _, COLORREF color _) 13: x0 (x0 _), y0 (y0 _), r (r _), mvX (mvX _), mvY (mvY _), color (color _) {} 14 // starting position of the ball 15 void prtCirl () {16 setfillcolor (color); 17 solidcircle (x0, y0, r ); 18} 19 // move the ball 20 void CirlMove () {21 setfillcolor (BLACK); 22 solidcircle (x0, y0, r); 23x0 + = mvX; 24 y0 + = mvY; 25 setfillcolor (color); 26 solidcircle (x0, y0, r); 27} 28 // determines whether the ball leaves the wide mouth. 29 // parameter: coordinates, width, and height on the upper left of the window. 30 // returns true if you leave. 31 bool IsCirlQuit (int x, int y, int width, int height) {32 if (x0-x <= r & mvX <0) {33 mvX =-mvX; 34 return false; 35} 36 else if (x + width-x0 <= r & mvX> 0) {37 mvX =-mvX; 38 return false; 39} 40 else if (y0-y <= r & mvY <0) {41 mvY =-mvY; 42 return false; 43} 44 else if (y + height-y0 <= r) 45 return true; 46 return false; 47} 48}; 49 struct Brick {50 int x0, y0; 51 COLORREF color; 52 int height, width; 53 virtual ~ Brick () {} 54 Brick (int x0 _, int y0 _, int width _, int height _, COLORREF color _) 55: x0 (x0 _), y0 (y0 _), width (width _), height (height _), color (color _) {} 56 // drawing of bricks 57 void prtBrick () {58 setfillcolor (color); 59 solidrectangle (x0, y0, x0 + width, y0 + height); 60} 61 // determine whether the bricks collide with the ball. 62 // parameter: ball 63 // returns true 64 bool IsCrashCirl (Circle & arg) {65 if (arg. x0 + arg. r <x0 | x0 + width <arg. x0-arg. r) 66 return false; 67 int disY = min (abs (y0-arg. y0), abs (y0 + height-arg. y0); 68 if (disY <= arg. r) {69 arg. mvY =-arg. mvY; 70 return true; 71} 72 return false; 73} 74 // clear 75 void BrickClr () {76 setfillcolor (BLACK); 77 solidrectangle (x0, y0, x0 + width, y0 + height); 78} 79}; 80 struct Broad: public Brick {81 int mvX; 82 int floor, ceiling; 83 virtual ~ Broad () {} 84 Broad (int x0 _, int y0 _, int width _, int height _, int mvX _, 85 int floor _, int ceiling _, COLORREF color _) 86: Brick (x0 _, y0 _, width _, height _, color _), mvX (mvX _), floor (floor _), ceiling (ceiling _) {} 87 // heavy load, judge whether the Board collided with the ball 88 // parameter: ball 89 // returns true 90 bool IsCrashCirl (Circle & arg) {91 if (arg. x0 + arg. r <x0 | x0 + width <arg. x0-arg. r) 92 return false; 93 if (y0-arg. y0 <= arg. r & arg. mvY> 0) {94 arg. mvY =-arg. mvY; 95 return true; 96} 97 return false; 98} 99 // board movement 100 void BroadMove () {101 POINT point; 102 GetCursorPos (& point ); 103 if (x0 <= point. x & point. x <= x0) 104 return; 105 BrickClr (); 106 if (point. x <x0) 107x0 = max (x0-mvX, floor); 108 else109 x0 = min (x0 + mvX, ceiling-width); 110 setfillcolor (color ); 111 solidrectangle (x0, y0, x0 + width, y0 + height); 112} 113}; 114 # endif // _ PROPERTY_H _
// Main. cpp # include <list> # include <algorithm> # include "property. cpp "using namespace std; const int WndW = 400, WndH = 400; // window size list <Brick> CreatBricks (); bool theGame (list <Brick> & MyBrks, broad & MyBrd, Circle & MyCirl); int main () {// brick layout list <Brick> MyBrks = move (CreatBricks (); // broad: 60*20, speed 5, WHITE Broad MyBrd (WndW/2-30, WndH-20, 60, 20, 5, 0, WndW, WHITE); // circle: radius 5, speed 5, DARKGRAY Circle M YCirl (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 ;}// list of bricks implementations <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;} // game implementation bool theGame (list <Brick> & MyBrks, Broad & MyBrd, Circle & MyCirl) {// game start interface for_each (MyBrks. begin (), MyBrks. end (), [] (Brick it) {it. prtBrick () ;}); MyBrd. prtBrick (); MyCirl. prtCirl (); // game loop 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 ;}