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

         **还有部分是c++语言哈**第一次写博客哈,有什么不足望大家指出我能写出来也参考网上的一些代码哈

#include <iomanip>
#include <strstream>
#include <cstdlib>
#include<stdlib.h>
#include<stdio.h>
#include<graphics.h>
#include<string.h>
#include<time.h>
#include<conio.h> //接收键盘输入输出
#include<windows.h>
#pragma comment(lib,“Winmm.lib”)
using namespace std;

enum color{blank, red, yellow, blue, green, white, orange, purple, shizijia, zhatan, qicai, alarm};
int HighScore = 0; //最高分
HANDLE hOut; //控制台句柄
int MaxT = 12; // 时间上限
int T = 10 * MaxT;// 时间速度
int V = 300; // 停留时间
clock_t start, now; // 控制时间
color gem[9][8]; // 地图
// IMAGE对象
IMAGE img[12], music_img[2], exit_img, jindutiao;
int Score, Time; // 成绩 时间
int Flag, Music = 1;// 是否加载 音乐

void load(); // 加载IMAGE对象
void Blank(); // 清空
void initjiemian(); // 初始化游戏界面
void scoreandjindutiao(); // 游戏界面右侧的得分
int ifxiaochu(); // 判断是否可以消除
int sosoublank(); // 搜索空格
int soudaojuorxiaochu(); // 搜索解法
void fall(); // 下落
void draw(); // 消除
void mousecontrol();/鼠标操作/
void play(); // 游戏过程
void gotoxy(int x,int y);/设置光标位置/
int colour(int c);/文字颜色函数/
void welcometogame();
void explation();
void File_in();
void File_out();
void endgame();

void load()
{

int i;
// 加载IMAGE对象loadimage(&img[0], "图片\\0.jpg");
loadimage(&img[1], "图片\\1.jpg");
loadimage(&img[2], "图片\\2.jpg");
loadimage(&img[3], "图片\\3.jpg");
loadimage(&img[4], "图片\\4.jpg");
loadimage(&img[5], "图片\\5.jpg");
loadimage(&img[6], "图片\\6.jpg");
loadimage(&img[7], "图片\\7.jpg");
loadimage(&img[8], "图片\\8.jpg");
loadimage(&img[9], "图片\\9.jpg");
loadimage(&img[10], "图片\\10.jpg");
loadimage(&img[11], "图片\\11.jpg");
loadimage(&music_img[0], "图片\\音乐关.jpg");
loadimage(&music_img[1], "图片\\音乐开.jpg");
loadimage(&exit_img, "图片\\退出.jpg");
loadimage(&jindutiao, "图片\\进度条.jpg");
// 加载音乐
mciSendString("open 背景音乐.mp3 alias mymusic", NULL, 0, NULL);// 随机种子
srand(unsigned(time(NULL)));

}

void Blank()
{

for
(int i = 1; i < 9; i++)for (int j = 0; j < 8; j++)gem[i][j] = blank;
initjiemian();
fall();

}

void initjiemian()//初始化游戏界面
{

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])]);
scoreandjindutiao();

}

/**

  • 游戏界面右侧的得分
    */

void scoreandjindutiao()// 分数和进度条
{

File_out();
static int old_score = -1, old_time = T;if (Score != old_score)  // 更新分数
{char s[15];ostrstream strout(s, 15);strout <<'$' <<setiosflags(ios::right) <<setw(9) <<Score <<ends;setfont(30, 0, "宋体");setcolor(RGB(255, 128, 0));outtextxy(480, 60, s);old_score = Score;}
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;}

}

int ifxiaochu()/*消除的方式:横的和竖的 */
{

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 1;
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 1;
return 0;

}

void fall()//下落
{

{int i, j, a[8] = {0};int sign = 0;for (j = 0; j < 8; j++)for (i = 8; i >= 1; i--)if (gem[i][j] == blank){a[j] = i;sign = 1;break;}if (sign == 0)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 (sosoublank())fall();
if (ifxiaochu())draw();

}

void draw()
{

{int i, j;int a[9][8] = {0};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] = 1;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] = 1;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;initjiemian();
}
fall();

}
int sosoublank()
{

for (int i = 1; i < 9; i++)for (int j = 0; j < 8; j++)if (gem[i][j] == blank)return 1;
return 0;

}
int soudaojuorxiaochu()
{

int i, j;
color t;
int flag = 0;
// 如果有一个道具则返回真
for (i = 1; i < 9; i++)for (j = 0; j < 8; j++)if (gem[i][j] >= shizijia)return 1;// 搜索解法
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 (ifxiaochu())flag = 1;t = gem[i][j];gem[i][j] = gem[i][j + 1];gem[i][j + 1] = t;if (flag)return 1;}
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 (ifxiaochu())flag = 1;t = gem[i][j];gem[i][j] = gem[i + 1][j];gem[i + 1][j] = t;if (flag)return 1;}
return 0;

}

void mousecontrol()
{

MOUSEMSG m;
int i, j, x, y, x1, y1;
char fx;
color t;
int sign;
if (Flag == 0)
{for (i = 0; i < 9; i++)for (j = 0; j < 8; j++)gem[i][j] = color(rand() % 7 + 1);Score = 0;Time = T;
}
cleardevice();/*清屏*/
setfont(30, 0, "微软雅黑");
setcolor(RGB(255, 128, 0));
outtextxy(480, 60, "$       0");
putimage(480, 400, &music_img[int(Music)]);
putimage(560, 400, &exit_img);
putimage(540, 140, &jindutiao);
initjiemian();
now = start = clock();
Flag = 1;
do
{if (sosoublank())fall();fx = 0;sign = 1;while (1){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;initjiemian();Sleep(V);fall();if (!soudaojuorxiaochu())Blank();break;case zhatan: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;initjiemian();Sleep(V);fall();if (!soudaojuorxiaochu())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;initjiemian();fall();if (!soudaojuorxiaochu())Blank();break;case alarm:Time = T;gem[x][y] = blank;initjiemian();fall();if (!soudaojuorxiaochu())Blank();break;}break;}else if (x1 > 480 && x1 < 560 && y1 > 400 && y1 < 480){if (Music)mciSendString("stop mymusic", NULL, 0, NULL);elsemciSendString("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--;scoreandjindutiao();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 = 0;break;}now = clock();if (now - start >= CLOCKS_PER_SEC * MaxT / T){start = now;Time--;scoreandjindutiao();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 (ifxiaochu()){draw();if (!soudaojuorxiaochu())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--;scoreandjindutiao();if (Time <= 0)return;}
}while (1);

}
void play()
{

 FILE *fp;   load(); // 加载IMAGE对象
// 打开界面
initgraph(640, 480);
// 游戏过程 if (Music)mciSendString("play mymusic from 0  repeat", NULL, 0, NULL);mousecontrol();if (Music)mciSendString("stop mymusic", NULL, 0, NULL);Flag = 0;mciSendString("close mymusic", NULL, 0, NULL);
closegraph();
endgame();

}
void Lostdraw()
{

system("cls");
int i;
gotoxy(45,2);
colour(6);
printf("\\\\\\|///");
gotoxy(43,3);
printf("\\\\");
gotoxy(47,3);
colour(15);
printf(".-.-");
gotoxy(54,3);
colour(6);
printf("//");gotoxy(44,4);
colour(14);
printf("(");gotoxy(47,4);
colour(15);
printf(".@.@");gotoxy(54,4);
colour(14);
printf(")");
gotoxy(17,5);
colour(11);
printf("+------------------------");
gotoxy(35,5);
colour(14);
printf("oOOo");
gotoxy(39,5);
colour(11);
printf("----------");
gotoxy(48,5);
colour(14);
printf("(_)");
gotoxy(51,5);
colour(11);
printf("----------");
gotoxy(61,5);
colour(14);
printf("oOOo");
gotoxy(65,5);
colour(11);
printf("-----------------+");
for(i = 6;i<=19;i++)        //竖边框
{gotoxy(17,i);printf("|");gotoxy(82,i);printf("|");
}gotoxy(17,20);
printf("+---------------------------------");gotoxy(52,20);
colour(14);
printf("☆☆☆〃");gotoxy(60,20);
colour(11);
printf("----------------------+");

}

void choose()
{

int n;
gotoxy(25,23);
colour(12);
printf("我要重新玩一局-------1");
gotoxy(52,23);
printf("不玩了,退出吧-------2");
gotoxy(46,25);
colour(11);
printf("选择:");
scanf("%d", &n);
switch (n)
{
case 1:system("cls");          //清屏//返回欢迎界面welcometogame();break;
case 2:exit(0);                //退出游戏break;
default:gotoxy(35,27);colour(12);printf("※※您的输入有误,请重新输入※※");system("pause >nul");endgame();choose();break;
}

}
/**

  • 结束游戏
    */

void endgame()
{

system("cls");Lostdraw();gotoxy(35,9);colour(12);printf("对不起,时间到了。游戏结束!");
gotoxy(43,12);
colour(13);
printf("您的得分是 %d\n",Score);gotoxy(43,13);
colour(13);printf("最高分是 %d",HighScore);
if(Score >= HighScore)
{colour(10);gotoxy(33,16);printf("创纪录啦!最高分被你刷新啦,真棒!!!");File_in();              //把最高分写进文件
}
else
{colour(10);gotoxy(33,16);printf("继续努力吧~ 你离最高分还差:%d",HighScore-Score);
}
choose();

}

/**

  • 设置光标位置
    */

void gotoxy(int x,int y)
{

COORD c;
c.X=x;
c.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);

}

/**

  • 文字颜色函数
    */

int colour(int c)
{

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);        //更改文字颜色
return 0;

}

/**

  • 开始界面
    */

void welcometogame()
{

int n;
int i,j = 1;
gotoxy(43,10);
colour(11);
printf("对    对    碰 ");
colour(14);                     //黄色边框
for (i = 15; i <= 21; i++)       //输出上下边框┅
{for (j = 27; j <= 74; j++)  //输出左右边框┇{gotoxy(j, i);if (i == 15 || i == 21){printf("-");}else if (j == 27 || j == 74){printf("|");}}
}
colour(12);
gotoxy(35, 17);
printf("1.开始游戏");
gotoxy(55, 17);
printf("2.游戏说明");
gotoxy(35, 19);
printf("3.退出游戏");
gotoxy(29,22);
colour(3);
printf("请选择[1 2 3]:[ ]\b\b");        //\b为退格,使得光标处于[]中间
colour(14);
scanf("%d", &n);          //输入选项
switch (n)
{case 1:system("cls");play();break;case 2:explation();        //游戏说明函数break;case 3:exit(0);           //退出游戏break;default:                //输入非1~3之间的选项colour(12);gotoxy(40,28);printf("请输入1~3之间的数!");getch();          //输入任意键system("cls");     //清屏welcometogame();
}

}

/**

  • 储存最高分进文件
    */

void File_in()
{

FILE *fp;
fp = fopen("save.txt", "w+");       //以读写的方式建立一个名为save.txt的文件
fprintf(fp, "%d", Score);           //把分数写进文件中
fclose(fp);                         //关闭文件

}

/**

  • 在文件中读取最高分
    */

void File_out()
{

FILE *fp;
fp = fopen("save.txt", "r");       //打开文件save.txt
fscanf(fp, "%d", &HighScore);       //把文件中的最高分读出来
fclose(fp);                         //关闭文件

}
/*

  • 游戏说明
    */

void explation()
{

int i,j = 1;
system("cls");
colour(13);
gotoxy(44,3);
printf("游戏说明");
colour(2);
for (i = 6; i <= 22; i++)   //输出上下边框===
{for (j = 20; j <= 75; j++)  //输出左右边框||{gotoxy(j, i);if (i == 6 || i == 22) printf("=");else if (j == 20 || j == 75) printf("||");}
}
colour(3);
gotoxy(30,8);
printf("tip1: 点击砖块,砖块之间互相还位");
colour(10);
gotoxy(30,11);
printf("tip2: 3个及以上的砖块消除得分");
colour(14);
gotoxy(30,14);
printf("tip3:消除 一个砖块10分");
colour(11);
gotoxy(30,17);
printf("tip4:  音乐:天空之城");
colour(4);
gotoxy(30,20);
printf("tip5: Esc :退出游戏");
getch();                //按任意键返回主界面
system("cls");
welcometogame();

}

int main()
{

 welcometogame();system("pause");return 0;

}

  • List item
  • 游戏开始界面

游戏界面游戏说明
游戏结束

以上就是本文的全部内容,希望对大家的学习有所帮助

用90%的c语言开发对对碰游戏,希望大家指点哈相关推荐

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

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

  2. C语言实现对对碰游戏(附完整源码)

    C语言实现对对碰游戏 #include <graphics.h> #include <fstream> #include <strstream> #include ...

  3. C++开发对对碰游戏

    相信大家都玩过开心消消乐,对对碰游戏类似于开心消消乐,利用C++语言开发的小游戏,编译器vs2010版本开发. #include "stdafx.h" #include <g ...

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

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

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

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

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

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

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

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

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

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

  9. 使用 Go 语言开发大型 MMORPG 游戏服务器怎么样?(非常稳定、捕获所有异常、非常适合从头开始,但大公司已经有现成的C++框架、所以不会使用)

    使用 Go 语言开发大型 MMORPG 游戏服务器怎么样?(非常稳定.捕获所有异常.非常适合从头开始,但大公司已经有现成的C++框架.所以不会使用) 参考文章: (1)使用 Go 语言开发大型 MMO ...

最新文章

  1. python学好了能干啥_新手该如何学python怎么学好python?_python学好了能干什么
  2. 设计包含min函数的栈
  3. ksnapshot运行look up error undefined symbol错误解决方案
  4. php变量作用域(花括号、global、闭包)
  5. 一文彻底搞懂Java中的值传递和引用传递!
  6. mysql+时间串联_mysql时间操作
  7. SpringBoot(十六)_springboot整合JasperReport6.6.0
  8. python第五章课后答案汉诺塔_用python编写一个程序,得到汉诺塔的解决方案
  9. html入门(详细描述)2
  10. svn指定版本代码对比的方法
  11. go语言 过滤 html,golang 去除html标签-Go语言中文社区
  12. IntelliJ IDEA的maven如何提高下载速度
  13. 火车头免登录php代码,福利|火车采集器免登陆发布接口集合
  14. SaltStack之return与job管理
  15. jQuery菜鸟教程04
  16. 什么是金手指,金手指的设计要求有哪些?
  17. 前端程序员和后端程序员有什么不同?我来告诉你薪资待遇差多少
  18. 夏斌:半年宏观调控思路的建议
  19. TortoiseSVN的所有使用
  20. Windows Movie Maker视频制作

热门文章

  1. Nvidia发布全新计算卡Tesla P40/P4
  2. (转)Linux系统下PDF文件的编辑
  3. 【原创】JavaWeb仓库管理系统(Web仓库管理系统毕业设计)
  4. 软件测试——理论基础
  5. 基于SSM的宠物管理系统
  6. Android最新手机号正则判断(含最新166/198/199开头的手机号)
  7. vue 父链和子组件索引_Vue.js 学习笔记 第7章 组件详解
  8. Fluent常用模型介绍-流体模拟仿真ansys
  9. 高通平台5G注册问题分析
  10. 应用案例 | 12年雪佛兰科鲁兹网络故障