c++编写消消乐游戏

2023-10-28 16:10
文章标签 c++ 编写 游戏 消消

本文主要是介绍c++编写消消乐游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <time.h>
using namespace sf;#define GAME_ROWS_COUNT  8
#define GAME_COLS_COUNT  8int 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 dowindow->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), "Rock-xiaoxiaole");// 设置刷新的最大帧率window.setFramerateLimit(60);Texture t1, t2;t1.loadFromFile("images/bg2.png");if (! t2.loadFromFile("images/t4.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;
}

  • <SFML/Graphics.hpp>:SFML 图形模块的头文件,用于创建窗口、渲染精灵等图形相关操作。
  • <SFML/Audio.hpp>:SFML 音频模块的头文件,用于音频播放和处理。
  • <time.h>:C 语言标准库中的时间头文件,在此代码中用于生成随机数种子

这段代码需要依赖其他 SFML 库文件和资源文件才能正常编译和运行。在编译和执行之前,请确保已正确配置 SFML 开发环境并添加了必要的依赖项。

  1. swap(Block p1, Block p2): This function swaps the position of two blocks (p1 and p2) by swapping their row and column values.

  2. doEvent(RenderWindow *window): This function handles user events, such as mouse clicks and window closures. It checks for mouse button presses and updates the positions of the clicked blocks accordingly.

  3. check(): This function checks for matches in the game grid. It iterates through each block and checks if there are three identical blocks in a row or column. If a match is found, it increments the match counter for those blocks.

  4. doMoving(): This function moves the blocks to their appropriate positions after a swap or match has occurred. It checks each block's position and adjusts it if it is not aligned with its row or column. It sets the isMoving flag to true if any blocks are still moving.

  5. xiaochu(): This function handles the removal of matched blocks by decreasing their alpha value (transparency). It sets the isMoving flag to true if any blocks are still being removed.

  6. huanYuan(): This function reverts the last swap if no match occurs as a result of the swap. It checks if a swap has occurred (isSwap flag) and if all blocks have finished moving (isMoving flag).

  7. updateGrid(): This function updates the game grid by moving blocks down if there are empty spaces below them and generating new random blocks at the top. It iterates through each column from bottom to top and replaces matched blocks with new random blocks.

  8. drawBlocks(Sprite *sprite, RenderWindow *window): This function draws the blocks on the game window using the provided sprite. It iterates through each block in the game grid, sets the sprite's texture rectangle and color based on the block's properties, and then draws the sprite on the window.

  9. initGrid(): This function initializes the game grid by assigning random block types to each block and setting their initial positions.

  10. main(): The main function of the program. It initializes the window, loads textures for the background and block sprites, calls initGrid() to initialize the game grid, and enters the main game loop. Inside the game loop, it calls the various functions in the correct order to handle events, update the game state, and render the game screen.

These functions together implement the logic and rendering of a basic match-three puzzle game using C++ and SFML.

这篇关于c++编写消消乐游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/294370

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

c++中std::placeholders的使用方法

《c++中std::placeholders的使用方法》std::placeholders是C++标准库中的一个工具,用于在函数对象绑定时创建占位符,本文就来详细的介绍一下,具有一定的参考价值,感兴... 目录1. 基本概念2. 使用场景3. 示例示例 1:部分参数绑定示例 2:参数重排序4. 注意事项5.

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表

使用C++实现单链表的操作与实践

《使用C++实现单链表的操作与实践》在程序设计中,链表是一种常见的数据结构,特别是在动态数据管理、频繁插入和删除元素的场景中,链表相比于数组,具有更高的灵活性和高效性,尤其是在需要频繁修改数据结构的应... 目录一、单链表的基本概念二、单链表类的设计1. 节点的定义2. 链表的类定义三、单链表的操作实现四、

使用C/C++调用libcurl调试消息的方式

《使用C/C++调用libcurl调试消息的方式》在使用C/C++调用libcurl进行HTTP请求时,有时我们需要查看请求的/应答消息的内容(包括请求头和请求体)以方便调试,libcurl提供了多种... 目录1. libcurl 调试工具简介2. 输出请求消息使用 CURLOPT_VERBOSE使用 C

C++实现获取本机MAC地址与IP地址

《C++实现获取本机MAC地址与IP地址》这篇文章主要为大家详细介绍了C++实现获取本机MAC地址与IP地址的两种方式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 实际工作中,项目上常常需要获取本机的IP地址和MAC地址,在此使用两种方案获取1.MFC中获取IP和MAC地址获取