概述
用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);
else
gem[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);
else
mciSendString("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 '