相信大家都玩过开心消消乐,对对碰游戏类似于开心消消乐,利用C++语言开发的小游戏,编译器vs2010版本开发。

#include "stdafx.h"
#include <graphics.h>
#include <fstream>
#include <strstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#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 <<"图片\\" <<i <<".jpg" <<ends;loadimage(&img[i], (LPCTSTR)c);}loadimage(&music_img[0], (LPCTSTR)"图片\\音乐关.jpg");loadimage(&music_img[1], (LPCTSTR)"图片\\音乐开.jpg");loadimage(&exit_img, (LPCTSTR)"图片\\退出.jpg");loadimage(&jindutiao, (LPCTSTR)"图片\\进度条.jpg");// 加载音乐mciSendString((LPCTSTR)"open 背景音乐.mp3 alias mymusic", NULL, 0, NULL);// 随机种子srand(unsigned(time(NULL)));// 打开文件ifstream fin("存档.dat");if (!fin)throw -1;   // 如果打开失败则抛出异常// 读存档fin >>Flag >>Music;if (Flag){HWND wnd = GetHWnd();SetWindowText(wnd, (LPCTSTR)"对对碰");if (MessageBox(wnd,(LPCTSTR) "是否继续上次游戏?",(LPCTSTR) "游戏开始", MB_YESNO | MB_ICONQUESTION) == IDYES){fin >>Score >>Time;for (i = 0; i < 9; i++)for (int j = 0; j < 8; j++){int t;fin >>t;gem[i][j] = color(t);}}elseFlag = 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 < 8; j++)if (gem[i][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 < 8; j++){t = gem[i][j];gem[i][j] = gem[i + 1][j];gem[i + 1][j] = t;if (judge())flag = true;t = gem[i][j];gem[i][j] = gem[i + 1][j];gem[i + 1][j] = t;if (flag)return true;}return false;
}void New(void)
{static int old_score = -1, old_time = T;bool flag = false;if (Score != old_score)  // 更新分数{if (Score > 999999999){if (MessageBox(GetHWnd(), (LPCTSTR)"恭喜你达到了最高分!\n是否重新开始?", (LPCTSTR)"游戏结束", MB_YESNO | MB_ICONQUESTION) == IDYES)Score = 0;elseexit(0);}char s[15];ostrstream strout(s, 15);strout <<'$' <<setiosflags(ios::right) <<setw(9) <<Score <<ends;setfont(30, 0, (LPCTSTR)"微软雅黑");setcolor(RGB(255, 128, 0));outtextxy(480, 60, (LPCTSTR)s);old_score = Score;flag = true;}if (Time != old_time)   // 更新时间{if (Time > old_time)putimage(540, 140, &jindutiao);setcolor(BLACK);for (int i = 0; i < T - Time; i++){line(540, 140 + 2 * i, 580, 140 + 2 * i);line(540, 140 + 2 * i + 1, 580, 140 + 2 * i + 1);}old_time = Time;flag = true;}if (flag)                // 写存档{ofstream fout("存档.dat");fout <<Flag      <<'\t'<<Music <<'\t'<<Score <<'\t'<<Time      <<endl;for (int i = 0; i < 9; i++){for (int j = 0; j < 8; j++)fout <<int(gem[i][j]) <<'\t';fout <<endl;}fout.close();}
}void print(void)
{int i, j;for (i = 1; i < 9; i++)for (j = 0; j < 8; j++)putimage(60 * j, 60 * (i - 1), &img[int(gem[i][j])]);New();
}bool judge(void)
{int i, j;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])return 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])return true;return false;
}void fall(void)
{{int i, j, a[8] = {0};bool sign = false;for (j = 0; j < 8; j++)for (i = 8; i >= 1; i--)if (gem[i][j] == blank){a[j] = i;sign = true;break;}if (sign == false)return;IMAGE im[8];for (j = 0; j < 8; j++)if (a[j] > 1)getimage(&im[j], 60 * j, 0, 60, 60 * (a[j] - 1));for (i = 0; i < 60 ; i += 2)for (j = 0; j < 8; j++)if (a[j] > 0){if (a[j] > 1)putimage(60 * j, i + 2, &im[j]);putimage(60 * j, i + 2 - 60, &img[int(gem[0][j])]);Sleep(1);}for (j = 0; j < 8; j++)if (a[j] > 0){for (i = a[j]; i > 0; i--)gem[i][j] = gem[i - 1][j];if (rand() % 50 == 0)gem[0][j] = color(rand() % 4 + 8);elsegem[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 < 8; j++)if (a[i][j]){gem[i][j] = blank;Score += 10;}Sleep(V);Time += T / MaxT;if (Time > T)Time = T;print();}   // 加对大括号使递归时撤销内存空间fall();
}bool quit(void)
{char str[50];ostrstream strout(str, 50);strout <<"得分:" <<Score <<"\n重新开始吗?" <<ends;if (MessageBox(GetHWnd(), (LPCTSTR)str,_T( "游戏结束"), MB_YESNO | MB_ICONQUESTION) == IDYES)return true;return false;
}void play(void)
{MOUSEMSG m;int i, j, x, y, x1, y1;char fx;color t;bool sign;if (Flag == false){do{for (i = 0; i < 9; i++)for (j = 0; j < 8; j++)gem[i][j] = color(rand() % 7 + 1);}while (judge() || !baidu());Score = 0;Time = T;}setbkcolor(BLACK);cleardevice();setfont(30, 0, (LPCTSTR)"微软雅黑");setcolor(RGB(255, 128, 0));outtextxy(480, 60, (LPCTSTR)"$       0");putimage(480, 400, &music_img[int(Music)]);putimage(560, 400, &exit_img);putimage(540, 140, &jindutiao);print();now = start = clock();Flag = true;do{if (soso())fall();fx = 0;sign = true;while (true){if (MouseHit()){m = GetMouseMsg();x1 = m.x;y1 = m.y;if (m.uMsg == WM_LBUTTONDOWN){if (x1 < 480){x = y1 / 60 + 1;y = x1 / 60;switch (gem[x][y]){case shizijia:for (i = 1; i < 9; i++){gem[i][y] = blank;Score += 10;}for (j = 0; j < 8; j++){gem[x][j] = blank;Score += 10;}Score -= 20;Time += T / MaxT;if (Time > T)Time = T;print();Sleep(V);fall();if (!baidu())Blank();break;case zhadan:if (x > 1){gem[x - 1][y] = blank;Score += 10;if (y > 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 < 7){gem[x + 1][y + 1] =blank;Score += 10;}}if (y < 7){gem[x][y + 1] = blank;if (x > 1){gem[x - 1][y + 1] =blank;Score += 10;}                          }gem[x][y] = blank;Time += T / MaxT;if (Time > 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 < 9; i++)for (j = 0; j < 8; j++)if (gem[i][j] == t){gem[i][j] = blank;putimage(60 * j, 60 * (i - 1), &img[0]);Score += 10;}gem[x][y] = blank;Time += T / MaxT;if (Time > T)Time = T;print();fall();if (!baidu())Blank();break;case alarm:Time = T;gem[x][y] = blank;print();fall();if (!baidu())Blank();break;}break;}else if (x1 > 480 && x1 < 560 && y1 > 400 && y1 < 480){if (Music)mciSendString((LPCTSTR)"stop mymusic", NULL, 0, NULL);elsemciSendString((LPCTSTR)"play mymusic from 0 repeat", NULL, 0, NULL);Music = !Music;putimage(480, 400, &music_img[int(Music)]);}else if (x1 > 560 && x1 < 640 && y1 > 400 && y1 < 480)exit(0);}}now = clock();if (now - start >= CLOCKS_PER_SEC * MaxT / T){start = now;Time--;New();if (Time <= 0)return;}}while (m.mkLButton && m.y < 480){m = GetMouseMsg();x = m.x;y = m.y;if (x - x1 > 30 || x1 - x > 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 <= 0)return;}}if(sign){putimage(x1 - x1 % 60, y1 - y1 % 60, &img[int(gem[y1 / 60 + 1][x1 / 60])], SRCPAINT);continue;}if (y1 - y > 30 && y1 / 60 > 0)fx = 'u';if (y - y1 > 30 && y1 / 60 < 7)fx = 'd';if (x1 - x > 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 '\0':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 < 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;}now = clock();if (now - start >= 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)int main()
{try{// 加载素材load();}catch (int){ofstream fout("存档.dat");fout <<false <<'\t' <<true <<endl;}// 打开界面initgraph(640, 480);// 游戏过程do{if (Music)mciSendString((LPCTSTR)"play mymusic from 0 repeat", NULL, 0, NULL);play();if (Music)mciSendString((LPCTSTR)"stop mymusic", NULL, 0, NULL);Flag = false;}while (quit());// 关闭游戏ofstream fout("存档.dat");fout <<false <<'\t' <<Music <<endl;fout.close();mciSendString((LPCTSTR)"close mymusic", NULL, 0, NULL);closegraph();return 0;
}

运行结果:

下载链接1:

链接:https://pan.baidu.com/s/1pQHmZjLprgMnCmunXEzd4g 
        提取码:2zpk

下载链接2:

https://download.csdn.net/download/EDU_YONG/85459115

下载链接3:

私信我;

C++开发对对碰游戏相关推荐

  1. 用90%的c语言开发对对碰游戏,希望大家指点哈

    用90%的c语言开发对对碰游戏,希望大家指点哈 **还有部分是c++语言哈**第一次写博客哈,有什么不足望大家指出我能写出来也参考网上的一些代码哈 #include <iomanip> # ...

  2. 程序员炫技:用c语言开发对对碰游戏,你打几分?

    我发现不少游戏都是这样,泡泡龙,对对碰,连连看,三重镇--众多游戏都是汇集三个单位便会进行消除.那么今天小编就用C语言开发出一款对对碰小游戏 这篇文章主要为大家分享了C语言对对碰游戏源码,具有一定的参 ...

  3. C#开发------对对碰游戏

    转载请标明是引用于 http://blog.csdn.net/chenyujing1234 例子代码: http://www.rayfile.com/zh-cn/files/9d09926e-736b ...

  4. 对对碰java_Java开发学习之用Java打造一款对对碰游戏(下篇)

    本文主要向大家介绍了Java开发学习之用Java打造一款对对碰游戏,通过具体的内容向大家展现,希望对大家Java开发的学习有所帮助. 在之前的文章介绍了对对碰游戏的理论部分和介绍了JLabel.JBu ...

  5. Java游戏开发——对对碰

    游戏介绍: 对对碰游戏在n*n的游戏池中进行,每个格子中有一个图案.鼠标连续选中两个横排或竖排相邻的图案,它们的位置会互换,互换后如果横排或者竖排有3个以上相同的图像,则可以消去该图像,并得分. 游戏 ...

  6. Java课设对对碰_第11章对对碰游戏(图形版)(Java游戏编程原理与实践教程课件).ppt...

    游戏界面和相关图片素材 11.3 程序设计的步骤 11.3.1 设计游戏窗口类(GameRoom.java) 游戏窗口类GameRoom实现游戏全部功能,继承JFrame组件实现的.是由上方Panel ...

  7. Unity 简易的对对碰游戏

    游戏规则:制作对对碰游戏,在4X4地图上随机生成8对不同(形状)的物体,第一次点击被选中的物体会变红色,第二次点击相同物体,则两个物体共同消失,第二次点击是不同物体则第一个物体变回原来的颜色,再次点击 ...

  8. java对对碰游戏设计报告_手把手带你用Java打造一款对对碰游戏(下篇)

    上篇文章介绍了对对碰游戏的理论部分和介绍了JLabel.JButton.JPanel.ImageIcon.JTextField.JProgressBar等组件的基本使用,介绍了进度条(JProgres ...

  9. MATLAB学习笔记 实现超简单对对碰游戏

    对对碰游戏,通过匹配相同的图像来赢得游戏.使用 uicontrol 中的按钮和一些图像来实现这个游戏. 设计有一个 2 x 2 矩阵,其中隐藏了 2 对等效图像(读取文件夹下的图片,至少含有两张rgb ...

最新文章

  1. shell编程_linux
  2. Eclipse详细设置护眼背景色和字体颜色并导出
  3. 中国电子银行网 神策数据:银行数字营销现状洞察报告
  4. WEB项目中的中文乱码问题
  5. wxWidgets:异形窗示例
  6. CSS 字体(font)实例
  7. linux 线程的运行时间吗,Linux下巧用ps得到运行线程个数和线程启动时间
  8. scala的stream流
  9. 四川名菜--水煮牛肉
  10. 线程池的使用及ThreadPoolExecutor的分析(一)
  11. 怎样使用 ASP.NET Optimization Bundling压缩样式表和脚本
  12. 字节跳动证实28岁员工离世;《英雄联盟》回应服务器崩了:官方直接回退了旧版本;Deno 1.19 发布|极客头条...
  13. 企业中个别机械类制图软件无法打印的问题汇总
  14. .net mysql和php mysql数据库连接_浅谈PHP连接MySQL数据库的三种方式
  15. javascript实现划词搜索功能(兼容IE,firefox,opera)
  16. [翻译]Visual Odmetry from scratch - A tutorial for beginners
  17. voc2007,voc2012数据集快速下载方法
  18. inav向STM32F401CCU开发板定制的过程(一)
  19. 每日一生信--blast2go本地化(终极版)
  20. Mysql分页Limt

热门文章

  1. 芒果数据库(MongoDB)学习记录(一)——安装
  2. 蓝桥杯真题:作物杂交
  3. Mysql 高可用部署实践
  4. 大一项目实训—学生成绩管理系统
  5. 全国计算机软考程序员考试大纲
  6. 舌尖上的广西 | 口馋者争相追求的中国美食梦!!!型男索女尝过吗?
  7. java编程文件传输_JAVA文件传输程序
  8. Wow魔兽世界服务器搭建详细教程,魔兽世界服务器配置要求
  9. 【随手记】PHP获取png图片主色系各色系色块RGB值
  10. FANUC系统5136报警维修