C++小游戏BrickHit实例代码
时间:2020-11-19 17:36:15|栏目:C代码|点击: 次
打砖块小游戏。材料: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; }
总结
上一篇:strings命令分析浅谈Go和C++编译时的一点小区别
栏 目:C代码
下一篇:C语言 解决不用+、-、×、÷数字运算符做加法的实现方法
本文标题:C++小游戏BrickHit实例代码
本文地址:http://www.codeinn.net/misctech/24167.html