请问各位大佬怎么修改才能实现不同图标的牌消除呀.
如有帮助,望采纳
#include <stdio.h>
#include <graphics.h> //easyx
#include <time.h>
#include <mmsystem.h>
#define BLOCK_KINDS_1 3
#define WIN_W 504
#define WIN_H 900
#define BLOCK_W 61
#define BLOCK_H 68
//定义第一关的方格数
IMAGE imgBg; //用来表示背景图片
IMAGE imgBlocks[BLOCK_KINDS_1];
int map[3][3];
int chao[7] = { 0 };
struct location {
int row;
int col;
};
void initMap() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
map[i][j] = i + 1; // 1 2 3
}
}
// 1 1 1
// 2 2 2
// 3 3 3
// 打乱
srand(time(NULL)); //配置随机种子
for (int i = 0; i < 20; i++) {
int row1 = rand() % 3; //0,1,2
int col1 = rand() % 3;
int row2 = rand() % 3;
int col2 = rand() % 3;
int tmp = map[row1][col1];
map[row1][col1] = map[row2][col2];
map[row2][col2] = tmp;
}
}
void gameInit() {
// 加载游戏的资源
// 背景图片
loadimage(&imgBg, "res/bg.png");
//小方块
char fileName[256];
for (int i = 0; i < BLOCK_KINDS_1; i++) {
sprintf_s(fileName,
sizeof(fileName),
"res/%d.png", i + 1);
loadimage(&imgBlocks[i], fileName);
}
// 初始化地图
initMap();
// 创建游戏的窗口
initgraph(WIN_W, WIN_H, EW_SHOWCONSOLE);//控制台
}
bool userClick(struct location* loc) {
ExMessage msg;
if (peekmessage(&msg) && msg.message == WM_LBUTTONDOWN) {
// 判断具体的位置
int off = 10;
int marginY = 270;
int marginX = (WIN_W - BLOCK_W * 3 - off * 2) / 2;
float x = (msg.x - marginX) * 1.0 / (BLOCK_W + off);
// 0.0 - n.n
// 0.09 最左边方块的左侧10%左右
// 1.09
// 0.99
// 0
int col = (x + 1) - 0.1;
float tail = (x + 1) - 0.1 - col; //得到小数部分
if (col < 1 || col > 3 || tail > 0.6) {
return false;
}
float y = (msg.y - marginY) * 1.0 / (BLOCK_H + off);
float y2 = y + 1 - 0.1;
int row = y2;
tail = y2 - row;
if (row < 1 || row > 3 || tail > 0.6) {
return false;
}
loc->row = row;
loc->col = col;
printf("[%d,%d]", row, col);
return true;
}
return false;
}
void update() {
BeginBatchDraw();
//先刷新背景图片,再去刷新其他部分
// 问:怎样在游戏开发中,修改指定区域的图片
// 答案:不需要修改,所有的游戏,都是全部重新渲染出来的,就是覆盖!
putimage(0, 0, //图片的左上角的坐标位置
&imgBg);
// 绘制游戏的中心区域的, 多个小方块
/*
1 1 1
2 2 2
3 3 3
再随机打乱100次
*/
// 第一关
int off = 10;
int marginX = (WIN_W - BLOCK_W * 3 - off * 2) / 2;
int marginY = 270;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (map[i][j] == 0) continue;//如果为0则不渲染
// 位置的计算
int x = marginX + j * (BLOCK_W + off);
int y = marginY + i * (BLOCK_H + off);
// 到底画哪一个
putimage(x, y, &imgBlocks[map[i][j] - 1]);
}
}
marginX = 26;
marginY = 691;
for (int i = 0; i < 7; i++) {
if (chao[i]) {
int x = marginX + i * 64.7;
int y = marginY + 5;
putimage(x, y, &imgBlocks[chao[i] - 1]);
}
}
EndBatchDraw();
}
//点击谁就把谁放在羊槽,下面第一一个数组。
//如果被点击数字,上面变成0则删掉,下面添加相应数字,然后渲染
void work(struct location* loc) {
int index = map[loc->row - 1][loc->col - 1];
map[loc->row - 1][loc->col - 1] = 0;
// 放到羊槽的合适位置
// 0 0 0 0 0 0 0
// 2 1 0 0 0 0 0
//从左向右数,直到遇到第一个为0的位置
int i = 0;
for (; chao[i] && i < 7; i++);
if (i < 7) {
chao[i] = index;
}
}
int main(void) {
gameInit(); //游戏的初始化
struct location loc; //表示玩家有效点击的位置
while (1) {
//刷新游戏窗口
// 按键处理: 接口设计
if (userClick(&loc)) {
work(&loc);
}
update();
//clear(&loc);
}
return 0;
}
三张相同是
a==b && b==c
三张不同是
a!=b && a!=c && b!=c
讲每种图做上标识然后给组件设置标识的名称。再通过判断看三者是否相等
同层的同图标给一个相同的标志,判断是否相等,相等的消除
提供一个类似题型的思路供你参考【卡片消除游戏 java版(代码+讲解)】,链接:https://blog.csdn.net/qq_54262820/article/details/127086644
羊了个羊的程序java版本https://blog.csdn.net/luoxueyong/article/details/126951367 ,有问题随时私信我
标记+判断,判断相同还是不同,进行标记。
具体的可以去看一下站内的相关消除游戏程序。