找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4820|回复: 2
打印 上一主题 下一主题
收起左侧

C语言对对碰游戏源码

[复制链接]
跳转到指定楼层
楼主
    对对碰是一款经典的消除类游戏,玩家只要通过点击砖块来使砖块之间互相还位,连成3个以上的砖块来消除得分。
    支持四种道具。
    背景音乐和图片可以自定义,但是注意不要改名。图片都是.jpg格式,背景音乐是.mp3格式。
    音乐:天空之城。
    ——作者:自然向日葵


源程序:
  1. ////////////////////////////////////////////////////////////
  2. //画素材的x和y都是反的,因为x表示行,但是画出来x表示列,y同
  3. ////////////////////////////////////////////////////////////

  4. /* 【自学去】网站收集 http://www.zixue7.com */

  5. #include <graphics.h>
  6. #include <fstream>
  7. #include <strstream>
  8. #include <iomanip>
  9. #include <cstdlib>
  10. #include <ctime>
  11. #pragma comment(lib, "Winmm.lib")
  12. using namespace std;


  13. /*******************************定义枚举类型*****************************/
  14. enum color{blank, red, yellow, blue, green, white, orange, purple, shizijia, zhadan, qicai, alarm};


  15. /*******************************定义全局变量*****************************/
  16. const int MaxT = 12;        // 时间上限
  17. const int T = 10 * MaxT;// 时间速度
  18. const int V = 300;                // 停留时间
  19. clock_t start, now;                // 控制时间
  20. color gem[9][8];                // 地图
  21.                                                 // IMAGE对象
  22. IMAGE img[12], music_img[2], exit_img, jindutiao;
  23. int Score, Time;                // 成绩 时间
  24. bool Flag, Music = true;// 是否加载 音乐


  25. /**********************************函数声明*****************************/
  26. void load(void);        // 加载IMAGE对象
  27. void Blank(void);        // 清空
  28. bool soso(void);        // 搜索空格
  29. bool baidu(void);        // 搜索解法
  30. void New(void);                // 更新
  31. void print(void);        // 输出
  32. bool judge(void);        // 判断是否可以消除
  33. void fall(void);        // 下落
  34. void draw(void);        // 消除
  35. bool quit(void);        // 是否重新开始
  36. void play(void);        // 游戏过程


  37. /**********************************定义函数*****************************/
  38. void load(void)
  39. {
  40.         char c[20];
  41.         int i;

  42.         // 加载IMAGE对象
  43.         for (i = 0; i < 12; i++)
  44.         {
  45.                 ostrstream strout(c, 50);
  46.                 strout <<"图片\\" <<i <<".jpg" <<ends;
  47.                 loadimage(&img[i], c);
  48.         }
  49.         loadimage(&music_img[0], "图片\\音乐关.jpg");
  50.         loadimage(&music_img[1], "图片\\音乐开.jpg");
  51.         loadimage(&exit_img, "图片\\退出.jpg");
  52.         loadimage(&jindutiao, "图片\\进度条.jpg");

  53.         // 加载音乐
  54.         mciSendString("open 背景音乐.mp3 alias mymusic", NULL, 0, NULL);

  55.         // 随机种子
  56.         srand(unsigned(time(NULL)));

  57.         // 打开文件
  58.         ifstream fin("存档.dat");
  59.         if (!fin)
  60.                 throw -1;        // 如果打开失败则抛出异常

  61.         // 读存档
  62.         fin >>Flag >>Music;
  63.         if (Flag)
  64.         {
  65.                 HWND wnd = GetHWnd();
  66.                 SetWindowText(wnd, "对对碰");
  67.                 if (MessageBox(wnd, "是否继续上次游戏?", "游戏开始", MB_YESNO | MB_ICONQUESTION) == IDYES)
  68.                 {
  69.                         fin >>Score >>Time;
  70.                         for (i = 0; i < 9; i++)
  71.                                 for (int j = 0; j < 8; j++)
  72.                                 {
  73.                                         int t;
  74.                                         fin >>t;
  75.                                         gem[i][j] = color(t);
  76.                                 }
  77.                 }
  78.                 else
  79.                         Flag = false;
  80.         }
  81.         fin.close();
  82. }

  83. void Blank(void)
  84. {
  85.         for (int i = 1; i < 9; i++)
  86.                 for (int j = 0; j < 8; j++)
  87.                         gem[i][j] = blank;
  88.         print();
  89.         fall();
  90. }

  91. bool soso(void)
  92. {
  93.         for (int i = 1; i < 9; i++)
  94.                 for (int j = 0; j < 8; j++)
  95.                         if (gem[i][j] == blank)
  96.                                 return true;
  97.         return false;
  98. }

  99. bool baidu(void)
  100. {
  101.         int i, j;
  102.         color t;
  103.         bool flag = false;

  104.         // 如果有一个道具则返回真
  105.         for (i = 1; i < 9; i++)
  106.                 for (j = 0; j < 8; j++)
  107.                         if (gem[i][j] >= shizijia)
  108.                                 return true;

  109.         // 搜索解法
  110.         for (i = 1; i < 9; i++)
  111.                 for (j = 0; j < 7; j++)
  112.                 {
  113.                         t = gem[i][j];
  114.                         gem[i][j] = gem[i][j + 1];
  115.                         gem[i][j + 1] = t;
  116.                         if (judge())
  117.                                 flag = true;
  118.                         t = gem[i][j];
  119.                         gem[i][j] = gem[i][j + 1];
  120.                         gem[i][j + 1] = t;
  121.                         if (flag)
  122.                                 return true;
  123.                 }
  124.         for (i = 1; i < 7; i++)
  125.                 for (j = 0; j < 8; j++)
  126.                 {
  127.                         t = gem[i][j];
  128.                         gem[i][j] = gem[i + 1][j];
  129.                         gem[i + 1][j] = t;
  130.                         if (judge())
  131.                                 flag = true;
  132.                         t = gem[i][j];
  133.                         gem[i][j] = gem[i + 1][j];
  134.                         gem[i + 1][j] = t;
  135.                         if (flag)
  136.                                 return true;
  137.                 }
  138.         return false;
  139. }

  140. void New(void)
  141. {
  142.         static int old_score = -1, old_time = T;
  143.         bool flag = false;
  144.         if (Score != old_score)        // 更新分数
  145.         {
  146.                 if (Score > 999999999)
  147.                 {
  148.                         if (MessageBox(GetHWnd(), "恭喜你达到了最高分!\n是否重新开始?", "游戏结束", MB_YESNO | MB_ICONQUESTION) == IDYES)
  149.                                 Score = 0;
  150.                         else
  151.                                 exit(0);
  152.                 }
  153.                 char s[15];
  154.                 ostrstream strout(s, 15);
  155.                 strout <<'

  156. 下载:
  157. c语言对对碰游戏源码.zip (4.13 MB, 下载次数: 14)

  158. <<setiosflags(ios::right) <<setw(9) <<Score <<ends;
  159.                 setfont(30, 0, "微软雅黑");
  160.                 setcolor(RGB(255, 128, 0));
  161.                 outtextxy(480, 60, s);
  162.                 old_score = Score;
  163.                 flag = true;
  164.         }
  165.         if (Time != old_time)        // 更新时间
  166.         {
  167.                 if (Time > old_time)
  168.                         putimage(540, 140, &jindutiao);
  169.                 setcolor(BLACK);
  170.                 for (int i = 0; i < T - Time; i++)
  171.                 {
  172.                         line(540, 140 + 2 * i, 580, 140 + 2 * i);
  173.                         line(540, 140 + 2 * i + 1, 580, 140 + 2 * i + 1);
  174.                 }
  175.                 old_time = Time;
  176.                 flag = true;
  177.         }
  178.         if (flag)                                // 写存档
  179.         {
  180.                 ofstream fout("存档.dat");
  181.                 fout <<Flag                <<'\t'
  182.                          <<Music        <<'\t'
  183.                          <<Score        <<'\t'
  184.                          <<Time                <<endl;
  185.                 for (int i = 0; i < 9; i++)
  186.                 {
  187.                         for (int j = 0; j < 8; j++)
  188.                                 fout <<int(gem[i][j]) <<'\t';
  189.                         fout <<endl;
  190.                 }
  191.                 fout.close();
  192.         }
  193. }

  194. void print(void)
  195. {
  196.         int i, j;
  197.         for (i = 1; i < 9; i++)
  198.                 for (j = 0; j < 8; j++)
  199.                         putimage(60 * j, 60 * (i - 1), &img[int(gem[i][j])]);
  200.         New();
  201. }

  202. bool judge(void)
  203. {
  204.         int i, j;
  205.         for (i = 1; i < 9; i++)
  206.                 for (j = 0; j < 6; j++)
  207.                         if (gem[i][j] == gem[i][j + 1] && gem[i][j] == gem[i][j + 2])
  208.                                 return true;
  209.         for (i = 1; i < 7; i++)
  210.                 for (j = 0; j < 8; j++)
  211.                         if (gem[i][j] == gem[i + 1][j] && gem[i][j] == gem[i + 2][j])
  212.                                 return true;
  213.         return false;
  214. }

  215. void fall(void)
  216. {
  217.         {
  218.                 int i, j, a[8] = {0};
  219.                 bool sign = false;
  220.                 for (j = 0; j < 8; j++)
  221.                         for (i = 8; i >= 1; i--)
  222.                                 if (gem[i][j] == blank)
  223.                                 {
  224.                                         a[j] = i;
  225.                                         sign = true;
  226.                                         break;
  227.                                 }
  228.                 if (sign == false)
  229.                         return;
  230.                 IMAGE im[8];
  231.                 for (j = 0; j < 8; j++)
  232.                         if (a[j] > 1)
  233.                                 getimage(&im[j], 60 * j, 0, 60, 60 * (a[j] - 1));
  234.                 for (i = 0; i < 60 ; i += 2)
  235.                         for (j = 0; j < 8; j++)
  236.                                 if (a[j] > 0)
  237.                                 {
  238.                                         if (a[j] > 1)
  239.                                                 putimage(60 * j, i + 2, &im[j]);
  240.                                         putimage(60 * j, i + 2 - 60, &img[int(gem[0][j])]);
  241.                                         Sleep(1);
  242.                                 }
  243.                 for (j = 0; j < 8; j++)
  244.                         if (a[j] > 0)
  245.                         {
  246.                                 for (i = a[j]; i > 0; i--)
  247.                                         gem[i][j] = gem[i - 1][j];
  248.                                 if (rand() % 50 == 0)
  249.                                         gem[0][j] = color(rand() % 4 + 8);
  250.                                 else
  251.                                         gem[0][j] = color(rand() % 7 + 1);
  252.                         }
  253.         }        // 加对大括号使递归时撤销内存空间
  254.         if (soso())
  255.                 fall();
  256.         if (judge())
  257.                 draw();
  258. }

  259. void draw(void)
  260. {
  261.         {
  262.                 int i, j;
  263.                 bool a[9][8] = {false};
  264.                 for (i = 1; i < 9; i++)
  265.                         for (j = 0; j < 6; j++)
  266.                                 if (gem[i][j] == gem[i][j + 1] && gem[i][j] == gem[i][j + 2])
  267.                                         a[i][j] = a[i][j + 1] = a[i][j + 2] = true;
  268.                 for (i = 1; i < 7; i++)
  269.                         for (j = 0; j < 8; j++)
  270.                                 if (gem[i][j] == gem[i + 1][j] && gem[i][j] == gem[i + 2][j])
  271.                                         a[i][j] = a[i + 1][j] = a[i + 2][j] = true;
  272.                 for (i = 1; i < 9; i++)
  273.                         for (j = 0; j < 8; j++)
  274.                                 if (a[i][j])
  275.                                 {
  276.                                         gem[i][j] = blank;
  277.                                         Score += 10;
  278.                                 }
  279.                 Sleep(V);
  280.                 Time += T / MaxT;
  281.                 if (Time > T)
  282.                         Time = T;
  283.                 print();
  284.         }        // 加对大括号使递归时撤销内存空间
  285.         fall();
  286. }

  287. bool quit(void)
  288. {
  289.         char str[50];
  290.         ostrstream strout(str, 50);
  291.         strout <<"得分:" <<Score <<"\n重新开始吗?" <<ends;
  292.         if (MessageBox(GetHWnd(), str, "游戏结束", MB_YESNO | MB_ICONQUESTION) == IDYES)
  293.                 return true;
  294.         return false;
  295. }

  296. void play(void)
  297. {
  298.         MOUSEMSG m;
  299.         int i, j, x, y, x1, y1;
  300.         char fx;
  301.         color t;
  302.         bool sign;
  303.         if (Flag == false)
  304.         {
  305.                 do
  306.                 {
  307.                         for (i = 0; i < 9; i++)
  308.                                 for (j = 0; j < 8; j++)
  309.                                         gem[i][j] = color(rand() % 7 + 1);
  310.                 }while (judge() || !baidu());
  311.                 Score = 0;
  312.                 Time = T;
  313.         }
  314.         setbkcolor(BLACK);
  315.         cleardevice();
  316.         setfont(30, 0, "微软雅黑");
  317.         setcolor(RGB(255, 128, 0));
  318.         outtextxy(480, 60, "$       0");
  319.         putimage(480, 400, &music_img[int(Music)]);
  320.         putimage(560, 400, &exit_img);
  321.         putimage(540, 140, &jindutiao);
  322.         print();
  323.         now = start = clock();
  324.         Flag = true;
  325.         do
  326.         {
  327.                 if (soso())
  328.                         fall();
  329.                 fx = 0;
  330.                 sign = true;
  331.                 while (true)
  332.                 {
  333.                         if (MouseHit())
  334.                         {
  335.                                 m = GetMouseMsg();
  336.                                 x1 = m.x;
  337.                                 y1 = m.y;
  338.                                 if (m.uMsg == WM_LBUTTONDOWN)
  339.                                 {
  340.                                         if (x1 < 480)
  341.                                         {
  342.                                                 x = y1 / 60 + 1;
  343.                                                 y = x1 / 60;
  344.                                                 switch (gem[x][y])
  345.                                                 {
  346.                                                 case shizijia:
  347.                                                         for (i = 1; i < 9; i++)
  348.                                                         {
  349.                                                                 gem[i][y] = blank;
  350.                                                                 Score += 10;
  351.                                                         }
  352.                                                         for (j = 0; j < 8; j++)
  353.                                                         {
  354.                                                                 gem[x][j] = blank;
  355.                                                                 Score += 10;
  356.                                                         }
  357.                                                         Score -= 20;
  358.                                                         Time += T / MaxT;
  359.                                                         if (Time > T)
  360.                                                                 Time = T;
  361.                                                         print();
  362.                                                         Sleep(V);
  363.                                                         fall();
  364.                                                         if (!baidu())
  365.                                                                 Blank();
  366.                                                         break;

  367.                                                 case zhadan:
  368.                                                         if (x > 1)
  369.                                                         {
  370.                                                                 gem[x - 1][y] = blank;
  371.                                                                 Score += 10;
  372.                                                                 if (y > 0)
  373.                                                                 {
  374.                                                                         gem[x - 1][y - 1] =blank;
  375.                                                                         Score += 10;
  376.                                                                 }
  377.                                                         }
  378.                                                         if (y > 0)
  379.                                                         {
  380.                                                                 gem[x][y - 1] = blank;
  381.                                                                 if (x < 8)
  382.                                                                 {
  383.                                                                         gem[x + 1][y - 1] =blank;
  384.                                                                         Score += 10;
  385.                                                                 }
  386.                                                         }
  387.                                                         if (x < 8)
  388.                                                         {
  389.                                                                 gem[x + 1][y] = blank;
  390.                                                                 if (y < 7)
  391.                                                                 {
  392.                                                                         gem[x + 1][y + 1] =blank;
  393.                                                                         Score += 10;
  394.                                                                 }
  395.                                                         }
  396.                                                         if (y < 7)
  397.                                                         {
  398.                                                                 gem[x][y + 1] = blank;
  399.                                                                 if (x > 1)
  400.                                                                 {
  401.                                                                         gem[x - 1][y + 1] =blank;
  402.                                                                         Score += 10;
  403.                                                                 }                                                       
  404.                                                         }
  405.                                                         gem[x][y] = blank;
  406.                                                         Time += T / MaxT;
  407.                                                         if (Time > T)
  408.                                                                 Time = T;
  409.                                                         print();
  410.                                                         Sleep(V);
  411.                                                         fall();
  412.                                                         if (!baidu())
  413.                                                                 Blank();
  414.                                                         break;

  415.                                                 case qicai:
  416.                                                         t = color(rand() % 7 + 1);
  417.                                                         putimage(60 * y, 60 * (x - 1), &img[int(t)]);
  418.                                                         Sleep(V);
  419.                                                         for (i = 1; i < 9; i++)
  420.                                                                 for (j = 0; j < 8; j++)
  421.                                                                         if (gem[i][j] == t)
  422.                                                                         {
  423.                                                                                 gem[i][j] = blank;
  424.                                                                                 putimage(60 * j, 60 * (i - 1), &img[0]);
  425.                                                                                 Score += 10;
  426.                                                                         }
  427.                                                         gem[x][y] = blank;
  428.                                                         Time += T / MaxT;
  429.                                                         if (Time > T)
  430.                                                                 Time = T;
  431.                                                         print();
  432.                                                         fall();
  433.                                                         if (!baidu())
  434.                                                                 Blank();
  435.                                                         break;

  436.                                                 case alarm:
  437.                                                         Time = T;
  438.                                                         gem[x][y] = blank;
  439.                                                         print();
  440.                                                         fall();
  441.                                                         if (!baidu())
  442.                                                                 Blank();
  443.                                                         break;
  444.                                                 }
  445.                                                 break;
  446.                                         }
  447.                                         else if (x1 > 480 && x1 < 560 && y1 > 400 && y1 < 480)
  448.                                         {
  449.                                                 if (Music)
  450.                                                         mciSendString("stop mymusic", NULL, 0, NULL);
  451.                                                 else
  452.                                                         mciSendString("play mymusic from 0 repeat", NULL, 0, NULL);
  453.                                                 Music = !Music;
  454.                                                 putimage(480, 400, &music_img[int(Music)]);
  455.                                         }
  456.                                         else if (x1 > 560 && x1 < 640 && y1 > 400 && y1 < 480)
  457.                                                 exit(0);
  458.                                 }
  459.                         }
  460.                         now = clock();
  461.                         if (now - start >= CLOCKS_PER_SEC * MaxT / T)
  462.                         {
  463.                                 start = now;
  464.                                 Time--;
  465.                                 New();
  466.                                 if (Time <= 0)
  467.                                         return;
  468.                         }
  469.                 }
  470.                 while (m.mkLButton && m.y < 480)
  471.                 {
  472.                         m = GetMouseMsg();
  473.                         x = m.x;
  474.                         y = m.y;
  475.                         if (x - x1 > 30 || x1 - x > 30 || y - y1 > 30 || y1 - y > 30)
  476.                         {
  477.                                 sign = false;
  478.                                 break;
  479.                         }
  480.                         now = clock();
  481.                         if (now - start >= CLOCKS_PER_SEC * MaxT / T)
  482.                         {
  483.                                 start = now;
  484.                                 Time--;
  485.                                 New();
  486.                                 if (Time <= 0)
  487.                                         return;
  488.                         }
  489.                 }
  490.                 if(sign)
  491.                 {
  492.                         putimage(x1 - x1 % 60, y1 - y1 % 60, &img[int(gem[y1 / 60 + 1][x1 / 60])], SRCPAINT);
  493.                         continue;
  494.                 }
  495.                 if (y1 - y > 30 && y1 / 60 > 0)
  496.                         fx = 'u';
  497.                 if (y - y1 > 30 && y1 / 60 < 7)
  498.                         fx = 'd';
  499.                 if (x1 - x > 30 && x1 / 60 > 0)
  500.                         fx = 'l';
  501.                 if (x - x1 > 30 && x1 / 60 < 7)
  502.                         fx = 'r';
  503.                 x = y1 / 60 + 1;
  504.                 y = x1 / 60;
  505.                 x1 = y1 = 0;
  506.                 switch (fx)
  507.                 {
  508.                         case 'u':x1 = -1;        break;
  509.                         case 'd':x1 = 1;        break;
  510.                         case 'l':y1 = -1;        break;
  511.                         case 'r':y1 = 1;        break;
  512.                         case '\0':continue;
  513.                 }
  514.                 for (i = 0; i < 60;)
  515.                 {
  516.                         putimage(60 * y, 60 * (x - 1), &img[0]);
  517.                         putimage(60 * y, 60 * (x - 1), &img[0]);
  518.                         i += 1;
  519.                         putimage(60 * (y + y1) - i * y1, 60 * (x - 1 + x1) - i * x1, &img[int(gem[x + x1][y + y1])]);
  520.                         putimage(60 * y + i * y1, 60 * (x - 1) + i * x1, &img[int(gem[x][y])]);
  521.                         Sleep(2);
  522.                 }
  523.                 t = gem[x][y];
  524.                 gem[x][y] = gem[x + x1][y + y1];
  525.                 gem[x + x1][y + y1] = t;
  526.                 if (judge())
  527.                 {
  528.                         draw();
  529.                         if (!baidu())
  530.                                 Blank();
  531.                 }
  532.                 else
  533.                 {
  534.                         for (i = 0; i < 60;)
  535.                         {
  536.                                 putimage(60 * y, 60 * (x - 1), &img[0]);
  537.                                 putimage(60 * y, 60 * (x - 1), &img[0]);
  538.                                 i += 1;
  539.                                 putimage(60 * (y + y1) - i * y1, 60 * (x - 1 + x1) - i * x1, &img[int(gem[x + x1][y + y1])]);
  540.                                 putimage(60 * y + i * y1, 60 * (x - 1) + i * x1, &img[int(gem[x][y])]);
  541.                                 Sleep(2);
  542.                         }
  543.                         t = gem[x][y];
  544.                         gem[x][y] = gem[x + x1][y + y1];
  545.                         gem[x + x1][y + y1] = t;
  546.                 }
  547.                 now = clock();
  548.                 if (now - start >= CLOCKS_PER_SEC * MaxT / T)
  549.                 {
  550.                         start = now;
  551.                         Time--;
  552.                         New();
  553.                         if (Time <= 0)
  554.                                 return;
  555.                 }
  556.         }while (true);
  557. }


  558. /***********************************主函数******************************/
  559. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  560. {
  561.         try
  562.         {
  563.                 // 加载素材
  564.                 load();
  565.         }
  566.         catch (int)
  567.         {
  568.                 ofstream fout("存档.dat");
  569.                 fout <<false <<'\t' <<true <<endl;
  570.         }

  571.         // 打开界面
  572.         initgraph(640, 480);

  573.         // 游戏过程
  574.         do
  575.         {
  576.                 if (Music)
  577.                         mciSendString("play mymusic from 0 repeat", NULL, 0, NULL);
  578.                 play();
  579.                 if (Music)
  580.                         mciSendString("stop mymusic", NULL, 0, NULL);
  581.                 Flag = false;
  582.         }while (quit());

  583.         // 关闭游戏
  584.         ofstream fout("存档.dat");
  585.         fout <<false <<'\t' <<Music <<endl;
  586.         fout.close();
  587.         mciSendString("close mymusic", NULL, 0, NULL);
  588.         closegraph();
  589.         return 0;
  590. }


  591. /***********************************THE END************************************/
复制代码


下载:


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:288791 发表于 2018-3-7 17:09 | 只看该作者
加油!!!!!
回复

使用道具 举报

板凳
ID:293258 发表于 2018-3-17 18:47 | 只看该作者
现在用C语言直接编游戏的很少了,基本都是用引擎
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表