C/C++百行代码实现热门游戏消消乐功能的示例代码

时间:2022-09-24 23:59:34

游戏设计

首先我们需要使用第三方框架,这里我使用的是sfml,不会使用sfml在我的上几篇文章当中-扫雷(上)有详细的开发环境搭建介绍

首先准备图片资源

一张背景图片,一张宝石图片

C/C++百行代码实现热门游戏消消乐功能的示例代码

窗口初始化加载图片

Texture t1;
t1.loadFromFile(“images/bg2.png”);

C/C++百行代码实现热门游戏消消乐功能的示例代码

当鼠标第一次单击时,记录下位置,第二次单击又记录一下位置,如果两个小方块相邻就交换位置,如果不相邻如图c的位置则,不发生变化

C/C++百行代码实现热门游戏消消乐功能的示例代码

判断行或列如果三张一样的图片相邻,清除一下图片,进行刷新

实列

?
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
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <time.h>
 
using namespace sf;
 
#define GAME_ROWS_COUNT 8
#define GAME_COLS_COUNT 8
 
int ts = 57; // 每一个游戏小方块区域的大小
 
bool isMoving = false;
bool isSwap = false;
 
// 相邻位置的第几次单击,第2次单击才交换方块
int click = 0;
 
Vector2i pos; //鼠标单击时的位置
Vector2i offset(15, 273);
 
int posX1, posY1; //第一次单击的位置(记录行和列的序号)
int posX2, posY2; //第二次单击的位置(记录行和列的序号)
 
struct Block {
    int x, y; //坐标值 x == col * ts y == row * ts;
    int row, col; //第几行,第几列
    int kind; //表示第几种小方块
    bool match; //表示是否成三
    int alpha; //透明度
 
    Block() {
        match = false;
        alpha = 255;
        kind = -1;
    }
} grid[GAME_ROWS_COUNT + 2][GAME_ROWS_COUNT + 2];
 
void swap(Block p1, Block p2) {
    std::swap(p1.col, p2.col);
    std::swap(p1.row, p2.row);
 
    grid[p1.row][p1.col] = p1;
    grid[p2.row][p2.col] = p2;
}
 
void doEvent(RenderWindow* window) {
    Event e;
    while (window->pollEvent(e)) {
        if (e.type == Event::Closed) {
            window->close();
        }
 
        if (e.type == Event::MouseButtonPressed) {
            if (e.key.code == Mouse::Left) {
                if (!isSwap && !isMoving) click++;
                pos = Mouse::getPosition(*window) - offset;
            }
        }
    }
 
    if (click == 1) {
        posX1 = pos.x / ts + 1;
        posY1 = pos.y / ts + 1;
    }
    else if (click == 2) {
        posX2 = pos.x / ts + 1;
        posY2 = pos.y / ts + 1;
 
        // 是相邻方块就交换位置
        if (abs(posX2 - posX1) + abs(posY2 - posY1) == 1) {
            // 交换相邻的两个小方块
            // 消消乐的方块,怎么表示?
            swap(grid[posY1][posX1], grid[posY2][posX2]);
            isSwap = 1;
            click = 0;
        }
        else {
            click = 1;
        }
    }
}
 
void check() {
    for (int i = 1; i <= GAME_ROWS_COUNT; i++) {
        for (int j = 1; j <= GAME_COLS_COUNT; j++) {
            if (grid[i][j].kind == grid[i + 1][j].kind &&
                grid[i][j].kind == grid[i - 1][j].kind) {
                //grid[i - 1][j].match++;
                //grid[i][j].match++;
                //grid[i + 1][j].match++;
                for (int k = -1; k <= 1; k++) grid[i + k][j].match++;
            }
 
            if (grid[i][j].kind == grid[i][j - 1].kind &&
                grid[i][j].kind == grid[i][j + 1].kind) {
                //grid[i][j - 1].match++;
                //grid[i][j + 1].match++;
                //grid[i][j].match++;
                for (int k = -1; k <= 1; k++) grid[i][j + k].match++;
            }
        }
    }
}
 
void doMoving() {
    isMoving = false;
 
    for (int i = 1; i <= GAME_ROWS_COUNT; i++) {
        for (int j = 1; j <= GAME_COLS_COUNT; j++) {
            Block& p = grid[i][j]; // 引用p, 就是grid[i][j]的别名
            int dx, dy;
 
            for (int k = 0; k < 4; k++) {
                dx = p.x - p.col * ts;
                dy = p.y - p.row * ts;
 
                if (dx) p.x -= dx / abs(dx);
                if (dy) p.y -= dy / abs(dy);
            }
 
            if (dx || dy) isMoving = true;
        }
    }
}
 
void xiaochu() {
    for (int i = 1; i <= GAME_ROWS_COUNT; i++) {
        for (int j = 1; j <= GAME_COLS_COUNT; j++) {
            if (grid[i][j].match && grid[i][j].alpha > 10) {
                grid[i][j].alpha -= 10;
                isMoving = true;
            }
        }
    }
}
 
void huanYuan() {
    if (isSwap && !isMoving) {
        // 如果此时没有产生匹配效果,就要还原
        int score = 0;
        for (int i = 1; i <= GAME_ROWS_COUNT; i++) {
            for (int j = 1; j <= GAME_COLS_COUNT; j++) {
                score += grid[i][j].match;
            }
        }
 
        if (score == 0) {
            swap(grid[posY1][posX1], grid[posY2][posX2]);
        }
 
        isSwap = false;
    }
}
 
void updateGrid() {
    for (int i = GAME_ROWS_COUNT; i > 0; i--) {
        for (int j = 1; j <= GAME_COLS_COUNT; j++) {
            if (grid[i][j].match) {
                for (int k = i - 1; k > 0; k--) {
                    if (grid[k][j].match == 0) {
                        swap(grid[k][j], grid[i][j]);
                        break;
                    }
                }
 
            }
        }
    }
 
    for (int j = 1; j <= GAME_COLS_COUNT; j++) {
        int n = 0;
        for (int i = GAME_ROWS_COUNT; i > 0; i--) {
            if (grid[i][j].match) {
                grid[i][j].kind = rand() % 7;
                grid[i][j].y = -ts * n;
                n++;
                grid[i][j].match = false;
                grid[i][j].alpha = 255;
            }
        }
    }
}
 
void drawBlocks(Sprite* sprite, RenderWindow* window) {
    for (int i = 1; i <= GAME_ROWS_COUNT; i++) {
        for (int j = 1; j <= GAME_COLS_COUNT; j++) {
            Block p = grid[i][j];
            sprite->setTextureRect(
                IntRect(p.kind * 52, 0, 52, 52));
            // 设置透明度
            sprite->setColor(Color(255, 255, 255, p.alpha));
            sprite->setPosition(p.x, p.y);
            // 因为数组gird中的Block, 每个Block的行标,列标是从1计算的,
            // 并根据行标和列表来计算的x,y坐标
            // 所以坐标的偏移,需要少便宜一些,也就是相当于在正方形区域的左上角的左上角方向偏移一个单位
            // 在这个位置开发存放第0行第0列(实际不绘制第0行第0列)
            sprite->move(offset.x - ts, offset.y - ts); // to do
            window->draw(*sprite);
        }
    }
}
 
void initGrid() {
    for (int i = 1; i <= GAME_ROWS_COUNT; i++) {
        for (int j = 1; j <= GAME_COLS_COUNT; j++) {
            grid[i][j].kind = rand() % 3;
            grid[i][j].col = j;
            grid[i][j].row = i;
            grid[i][j].x = j * ts;
            grid[i][j].y = i * ts;
        }
    }
}
 
int main(void) {
    srand(time(0));
 
    RenderWindow window(VideoMode(485, 917), "canxin-xiaoxiaole");
    // 设置刷新的最大帧率
    window.setFramerateLimit(60);
 
    Texture t1, t2;
    t1.loadFromFile("images/bg2.png");
    if (!t2.loadFromFile("images/1.png")) {
        return -1;
    }
    Sprite spriteBg(t1);
    Sprite spriteBlock(t2);
 
    initGrid();
 
    while (window.isOpen()) {
        // 处理用户的点击事件
        doEvent(&window);
 
        // 检查匹配情况
        check();
 
        // 移动处理
        doMoving();
 
        // 消除
        if (!isMoving) {
            xiaochu();
        }
 
        // 还原处理
        huanYuan();
 
        if (!isMoving) {
            updateGrid();
        }
 
        // 渲染游戏画面
        window.draw(spriteBg);
        // 渲染所有的小方块
        drawBlocks(&spriteBlock, &window);
        // 显示
        window.display();
    }
 
    return 0;
}

总结

到此这篇关于C/C++百行代码实现热门游戏消消乐功能的示例代码的文章就介绍到这了,更多相关C++ 消消乐内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_46464021/article/details/107027567