欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C++小游戏BrickHit

程序员文章站 2022-12-11 10:08:43
打砖块小游戏。材料:EasyX图形库。 碰撞的处理,木板移动方法还需要优化。 2018-02-11 ......

打砖块小游戏。材料:EasyX图形库。

碰撞的处理,木板移动方法还需要优化。

  1 //定义 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     //小球起始位置
 15     void prtCirl() {
 16         setfillcolor(color);
 17         solidcircle(x0, y0, r);
 18     }
 19     //小球的移动
 20     void CirlMove() {
 21         setfillcolor(BLACK);
 22         solidcircle(x0, y0, r);
 23         x0 += mvX;
 24         y0 += mvY;
 25         setfillcolor(color);
 26         solidcircle(x0, y0, r);
 27     }
 28     //判断小球是否离开宽口内。
 29     //参数:窗口左上坐标、宽、高。
 30     //离开返回真。
 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     //砖块的绘制
 57     void prtBrick() {
 58         setfillcolor(color);
 59         solidrectangle(x0, y0, x0 + width, y0 + height);
 60     }
 61     //判断砖块是否与小球发生碰撞
 62     //参数:小球
 63     //发生碰撞返回真
 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     //砖块的清除
 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     //重载,判断木板是否与小球发生碰撞
 88     //参数:小球
 89     //发生碰撞返回真
 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     //木板移动
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)
107             x0 = max(x0 - mvX, floor);
108         else
109             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; //窗口大小
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;
}

2018-02-11