服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++基于easyx图形库实现打砖块游戏

C++基于easyx图形库实现打砖块游戏

2022-11-28 11:54麦克学肖邦 C/C++

这篇文章主要为大家详细介绍了C++基于easyx图形库实现打砖块游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++基于easyx实现打砖块的具体代码,供大家参考,具体内容如下

C++基于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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include <graphics.h>
#include <ctime>
#include <iostream>
#include <cassert>
using namespace std;
 
class Board
{
public:
    int x;
    int y;
    int w;
    int h;
    COLORREF color;
};
 
class Board* createBoard(int x, int y, int w, int h, COLORREF color)
{
    Board* board = new Board;
    assert(board);
    board->x = x;
    board->y = y;
    board->w = w;
    board->h = h;
    board->color = color;
    return board;
}
 
class Ball
{
public:
    int x;
    int y;
    int r;
    int dx;
    int dy;
    unsigned long color;
};
 
class Ball* createBall(int x, int y, int r, int dx, int dy, unsigned long color)
{
    Ball* pBall = new Ball;
    assert(pBall);
    pBall->x = x;
    pBall->y = y;
    pBall->r = r;
    pBall->dx = dx;
    pBall->dy = dy;
    pBall->color = color;
    return pBall;
}
 
class Map
{
public:
    Map() {}
    
    //初始化地图
    void initMap(int map[][8], int row, int cols);
    //绘制地图
    void drawMap(int map[][8], int row, int cols);
    //绘制木板
    void drawBoard(Board* pBoard);
    //绘制球
    void drawBall(Ball* pBall);
};
 
 
 
class Move
{
public:
    
    //移动球
    void moveBall(Ball* pBall, Board* pBaord, int map[][8], int row, int cols);
    //球击中木板后
    int hitBoard(Ball* pBall, Board* pBoard);
    //球击中墙后
    int hitBricks(Ball* pBall, int map[][8], int row, int cols);
};
 
 
class System
{
public:
    //按键操作,控制木板左右移
    void keyDown(Board* pBoard);
 
    //计时器
    int Timer(int duration, int id);
 
    //游戏结束
    int gameOver(Ball* pBall, Board* pBoard);
 
    //游戏结束的文字显示
    void outtextxyInfo(int x, int y, const char* info);
 
    //游戏胜利
    int windGame(int map[][8], int row, int cols);
 
};
 
void Map::drawBoard(Board* pBoard)
{
    setfillcolor(pBoard->color);
    //木板的x , y 坐标 , 木板的坐标加上 宽度 和高度 
    solidrectangle(pBoard->x, pBoard->y, pBoard->x + pBoard->w, pBoard->y + pBoard->h);
}
 
void Map::initMap(int map[][8], int row, int cols)
{
    for (int i = 0; i < row; i++) 
 
    {
        for (int j = 0; j < cols; j++)
        {
            map[i][j] = rand() % 3 + 1; //[1,3]
        }
    }
}
 
void Map::drawMap(int map[][8], int row, int cols)
{
    setlinecolor(BLACK);
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            int x = 100 * j;
            int y = 25 * i;
            switch (map[i][j])
            {
            case 0:
                break;
            case 1:
                setfillcolor(RGB(255, 255, 85));
                fillrectangle(x, y, x + 100, y + 25);
                break;
            case 2:
                setfillcolor(RGB(85, 255, 85));
                fillrectangle(x, y, x + 100, y + 25);
                break;
            case 3:
                setfillcolor(RGB(85, 85, 255));
                fillrectangle(x, y, x + 100, y + 25);
                break;
            }
        }
    }
}
 
void System::keyDown(Board* pBoard) 
{
    //修改木板的坐标
    //_getch()
    //使用异步处理函数 , 按键控制
    if((GetAsyncKeyState('A') || GetAsyncKeyState(VK_LEFT)) && pBoard->x > 0) 
    {
        pBoard->x -= 1;
    }
    if((GetAsyncKeyState('D') || GetAsyncKeyState(VK_RIGHT))&& pBoard->x + pBoard->w < 800) 
    {
        pBoard->x += 1;
    }
}
 
void Map::drawBall(Ball* pBall) 
{
    //setfillcolor(pBall->color);
    setfillcolor(RGB(rand() % 255, rand() % 255 , rand() % 255)); //随机颜色
    solidcircle(pBall->x, pBall->y, pBall->r);
}
 
void Move::moveBall(Ball* pBall, Board* pBoard, int map[][8], int row, int cols) 
{
    //碰撞反弹, 
    /*if(pBall->x - pBall->r <= 0 || pBall->x + pBall->r >= 800) 
    {
        pBall->dx = -pBall->dx;
    }
    if(pBall->y - pBall->r <= 0 || pBall->y + pBall->r >=600 ) 
    {
        pBall->dy = -pBall->dy;
    }*/
    
    if (pBall->x - pBall->r <= 0 || pBall->x + pBall->r >= 800)
    {
        pBall->dx = -pBall->dx;
    }
 
#if 0
    if (pBall->y - pBall->r <= 0 || hitBoard(pBall, pBoard))
    {
        cout << "发生碰撞" << endl;
        cout << "没有发生反射" << endl;
        pBall->dy = -pBall->dy;
    }
#else
    if(pBall->y - pBall->r <= 0 
    || pBall->y + pBall->r >= 600 
    || hitBoard(pBall,pBoard)
    || hitBricks(pBall,map,row,cols))
    {
        cout << "发生碰撞" << endl;
        pBall->dy = -pBall->dy;
    }
#endif
    pBall->x += pBall->dx;
    pBall->y += pBall->dy;
     
}
 
int System::Timer (int duration,int id) 
{
    static int startTime[10];    //静态变量自动初始化为零
    //用静态变量是因为要记录上一次的运行结果
 
    int endTime = clock();
    if(endTime - startTime[id] >= duration) 
    {
        //下一次开始的时间编程变成一次结束的时间
        startTime[id] = endTime;
        return 1;
    }
    return 0;
}
 
int Move::hitBoard(Ball* pBall, Board* pBoard)
{
 
#if 0
    if(pBall->y + pBall->r == pBoard->y) 
    {
        if(pBall->x > pBoard->x && pBall->x < pBoard->x + pBoard->w) 
        {
            cout << "发生碰撞" << endl;
            return 1;
        }
        else
        {
            cout << "碰撞出错" << endl;
        }
    }
#else
    if (pBall->y + pBall->r == pBoard->y)
    {
        if (pBall->x > pBoard->x && pBall->x <= pBoard->x + pBoard->w)
        {
            cout << "发生碰撞" << endl;
            return 1;
        }
        else
        {
            cout << "碰撞出错" << endl;
        }
    }
#endif
 
    return 0;
}
 
int Move::hitBricks(Ball* pBall,int map[][8],int row,int cols)
{
    int j = pBall->x / 100;
    int i = (pBall->y - pBall->r) / 25;
    if(i < row && j < cols && map[i][j] != 0) 
    {
        map[i][j] = 0;
        return 1;
    }
    return 0;
}
 
int System::gameOver(Ball* pBall,Board* pBoard)
{
    if(pBall->y + pBall->r > pBoard->y) 
    {
        return 1;
    }
    return 0;
}
 
int System::windGame(int map[][8], int row, int cols)
{
    for(int i = 0; i < row; ++i) 
    {
        for(int j = 0; j < cols; ++j) 
        {
            if(map[i][j] !=0) 
            {
                return 0;
            }
        }
    }
    return 1;
}
 
void System::outtextxyInfo(int x, int y, const char* info)
{
    settextstyle(45, 0, "楷体");
    settextcolor(RED);
    outtextxy(x, y, info);
}
 
int main() 
{
    Map init;
    Move move;
    System psystem;
    System judge;
    srand((unsigned int)time(NULL));
    initgraph(800, 600);
    int map[5][8];
    Board* pBoard = createBoard(300, 600 - 25, 200, 25, BLUE);
    Ball* pBall = createBall(400, 300, 10, 15, -5, RED);
    init.initMap(map, 5, 8);
    BeginBatchDraw();
    while (true)
    
        cleardevice();    
        init.drawMap(map, 5, 8);
        init.drawBoard(pBoard);
        init.drawBall(pBall);
        if (judge.Timer(20, 0))
            move.moveBall(pBall, pBoard, map, 5, 8);
        judge.keyDown(pBoard);
        //Sleep(10);        
        if (judge.gameOver(pBall, pBoard)) 
        {
            psystem.outtextxyInfo(300, 350, "游戏失败");
            FlushBatchDraw();
            break;
        }
        if (judge.windGame(map, 5, 8)) 
        {
            psystem.outtextxyInfo(300, 350, "游戏胜利");
            FlushBatchDraw();
            break;
        }
        FlushBatchDraw();   
    }
    Sleep(5000);
    EndBatchDraw();         
    
    closegraph();
    return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_66885166/article/details/123942097

延伸 · 阅读

精彩推荐
  • C/C++C程序中唯一序列号的生成实例详解

    C程序中唯一序列号的生成实例详解

    这篇文章主要介绍了C程序中唯一序列号的生成实例详解的相关资料,需要的朋友可以参考下...

    bianceng11512021-05-19
  • C/C++C语言实现数字雨效果

    C语言实现数字雨效果

    这篇文章主要为大家详细介绍了C语言实现数字雨效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    C语言教程网9062021-06-21
  • C/C++protobuf c++编程笔记

    protobuf c++编程笔记

    这篇文章主要介绍了Protobuf的c++编程笔记,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...

    龙海L7682021-12-21
  • C/C++bloom filter概念讲解以及代码分析

    bloom filter概念讲解以及代码分析

    Bloom filter 优点就是它的插入和查询时间都是常数,另外它查询元素却不保存元素本身,具有良好的安全性...

    C语言教程网8812020-12-30
  • C/C++C++利用 _findfirst与_findnext查找文件的方法

    C++利用 _findfirst与_findnext查找文件的方法

    这篇文章主要给大家介绍了关于C++利用 _findfirst与_findnext查找文件的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考...

    ranjiewen7032021-06-26
  • C/C++C/C++中extern "C" 的作用分析

    C/C++中extern "C" 的作用分析

    这篇文章主要介绍了C/C++中extern "C" 的作用,是在进行C/C++程序设计中非常常见的用法,需要的朋友可以参考下...

    C++教程网8712021-01-31
  • C/C++c++实现二路归并排序的示例代码

    c++实现二路归并排序的示例代码

    这篇文章主要介绍了c++实现二路归并排序的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们...

    DL&ML4982021-08-30
  • C/C++C语言程序设计谭浩强第五版课后答案(第三章习题答案)

    C语言程序设计谭浩强第五版课后答案(第三章习题答案)

    这篇文章主要介绍了C语言程序设计谭浩强第五版课后答案(第三章习题答案),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来...

    月已满西楼11872021-10-29