C语言零基础项目:对对碰(消除类)游戏!详细思路+源码分享

每天一个c语言小项目,提升你的编程能力!
游戏中消除的对象为各种各样的头像,包括树、小车、草莓和酒瓶等一些头像。玩家通关移动这些头像位置凑够一定数量的相同图标即可消除。
本程序的操作与 popcap 经典的宝石迷阵(bejeweled)游戏类似。
当然你自己也可以进一步去完善这个游戏(例如设置关卡,不同图像的不同分数,障碍物等等)
游戏中使用了大量图片、音乐、资源文件【https://codebus.cn/f/a/0/0/153/duiduipeng.zip】
本项目编译环境:visual studio 2019/2022,easyx插件
代码展示:
#include #include #include #include #include #include #pragma comment(lib, winmm.lib)using namespace std;/*******************************定义枚举类型*****************************/enum color{blank, red, yellow, blue, green, white, orange, purple, shizijia, zhadan, qicai, alarm};/*******************************定义全局变量*****************************/const int maxt = 12; // 时间上限const int t = 10 * maxt;// 时间速度const int v = 300; // 停留时间clock_t start, now; // 控制时间color gem[9][8]; // 地图 // image对象image img[12], music_img[2], exit_img, jindutiao;int score, time; // 成绩 时间bool flag, music = true;// 是否加载 音乐/**********************************函数声明*****************************/void load(void); // 加载image对象void blank(void); // 清空bool soso(void); // 搜索空格bool baidu(void); // 搜索解法void new(void); // 更新void print(void); // 输出bool judge(void); // 判断是否可以消除void fall(void); // 下落void draw(void); // 消除bool quit(void); // 是否重新开始void play(void); // 游戏过程/**********************************定义函数*****************************/void load(void){ char c[20]; int i; // 加载image对象 for (i = 0; i < 12; i++) { ostrstream strout(c, 50); strout <<图片\ <>music; if (flag) { hwnd wnd = gethwnd(); setwindowtext(wnd, 对对碰); if (messagebox(wnd, 是否继续上次游戏?, 游戏开始, mb_yesno | mb_iconquestion) == idyes) { fin >>score >>time; for (i = 0; i t; gem[i][j] = color(t); } } else flag = false; } fin.close();}void blank(void){ for (int i = 1; i < 9; i++) for (int j = 0; j < 8; j++) gem[i][j] = blank; print(); fall();}bool soso(void){ for (int i = 1; i < 9; i++) for (int j = 0; j < 8; j++) if (gem[i][j] == blank) return true; return false;}bool baidu(void){ int i, j; color t; bool flag = false; // 如果有一个道具则返回真 for (i = 1; i < 9; i++) for (j = 0; j = shizijia) return true; // 搜索解法 for (i = 1; i < 9; i++) for (j = 0; j < 7; j++) { t = gem[i][j]; gem[i][j] = gem[i][j + 1]; gem[i][j + 1] = t; if (judge()) flag = true; t = gem[i][j]; gem[i][j] = gem[i][j + 1]; gem[i][j + 1] = t; if (flag) return true; } for (i = 1; i < 7; i++) for (j = 0; j 999999999) { if (messagebox(gethwnd(), 恭喜你达到了最高分!是否重新开始?, 游戏结束, mb_yesno | mb_iconquestion) == idyes) score = 0; else exit(0); } char s[15]; ostrstream strout(s, 15); strout <<'$' < 0; i--) gem[i][j] = gem[i - 1][j]; if (rand() % 50 == 0) gem[0][j] = color(rand() % 4 + 8); else gem[0][j] = color(rand() % 7 + 1); } } // 加对大括号使递归时撤销内存空间 if (soso()) fall(); if (judge()) draw();}void draw(void){ { int i, j; bool a[9][8] = {false}; for (i = 1; i < 9; i++) for (j = 0; j < 6; j++) if (gem[i][j] == gem[i][j + 1] && gem[i][j] == gem[i][j + 2]) a[i][j] = a[i][j + 1] = a[i][j + 2] = true; for (i = 1; i < 7; i++) for (j = 0; j < 8; j++) if (gem[i][j] == gem[i + 1][j] && gem[i][j] == gem[i + 2][j]) a[i][j] = a[i + 1][j] = a[i + 2][j] = true; for (i = 1; i < 9; i++) for (j = 0; j t) time = t; print(); } // 加对大括号使递归时撤销内存空间 fall();}bool quit(void){ char str[50]; ostrstream strout(str, 50); strout <<得分: < 0) { gem[x - 1][y - 1] =blank; score += 10; } } if (y > 0) { gem[x][y - 1] = blank; if (x < 8) { gem[x + 1][y - 1] =blank; score += 10; } } if (x < 8) { gem[x + 1][y] = blank; if (y t) time = t; print(); sleep(v); fall(); if (!baidu()) blank(); break; case qicai: t = color(rand() % 7 + 1); putimage(60 * y, 60 * (x - 1), &img[int(t)]); sleep(v); for (i = 1; i 480 && x1 400 && y1 560 && x1 400 && y1 = clocks_per_sec * maxt / t) { start = now; time--; new(); if (time 30 || y - y1 > 30 || y1 - y > 30) { sign = false; break; } now = clock(); if (now - start >= clocks_per_sec * maxt / t) { start = now; time--; new(); if (time 30 && y1 / 60 > 0) fx = 'u'; if (y - y1 > 30 && y1 / 60 30 && x1 / 60 > 0) fx = 'l'; if (x - x1 > 30 && x1 / 60 < 7) fx = 'r'; x = y1 / 60 + 1; y = x1 / 60; x1 = y1 = 0; switch (fx) { case 'u':x1 = -1; break; case 'd':x1 = 1; break; case 'l':y1 = -1; break; case 'r':y1 = 1; break; case '�':continue; } for (i = 0; i < 60;) { putimage(60 * y, 60 * (x - 1), &img[0]); putimage(60 * y, 60 * (x - 1), &img[0]); i += 1; putimage(60 * (y + y1) - i * y1, 60 * (x - 1 + x1) - i * x1, &img[int(gem[x + x1][y + y1])]); putimage(60 * y + i * y1, 60 * (x - 1) + i * x1, &img[int(gem[x][y])]); sleep(2); } t = gem[x][y]; gem[x][y] = gem[x + x1][y + y1]; gem[x + x1][y + y1] = t; if (judge()) { draw(); if (!baidu()) blank(); } else { for (i = 0; i = clocks_per_sec * maxt / t) { start = now; time--; new(); if (time <= 0) return; } }while (true);}/***********************************主函数******************************/int apientry winmain(hinstance hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow){ try { // 加载素材 load(); } catch (int) { ofstream fout(存档.dat); fout < 大家赶紧去动手试试吧!


一种为倾角计子系统设计定制板支架的方法
中值滤波去除图像上存在孤立的噪声点
什么是GOPHER
基于Wave Panel的HID设备通讯接口设计
中国联通与5G网络共建共享顺利推进
C语言零基础项目:对对碰(消除类)游戏!详细思路+源码分享
分享几个大众的AGM、EFB蓄电池检测方法
研华推出专为批次控制设计的功能块
近五年来中文电子病历的命名实体识别研究进展
防雷电方式
比特大陆未来的发展规划探讨
聊聊芯片超净间的颗粒
谷歌等科技公司或与银行金融公司分享数据
USB双机通讯的原理
中国成为智能家居市场的主战场,跑马圈地竞争激烈
区块链里面的主角是谁
基于RC网络分相滤波器的镜频抑制混频器
vivoNEX3 5G曝光 屏占比高达99.6%?
大屏手机推荐:小米max2和三星note8值得拥有
5G太烧钱,联通混改成功,股价却依然下跌