C++小游戏BrickHit实例代码

时间:2022-09-19 12:49:17

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//定义 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_
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//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实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://www.cnblogs.com/WYK-paint/archive/2018/02/11/8441455.html