提示:
本游戏是基于EasyX图形库制作的,刚刚开始学习C++不是很完美,有不足的地方还请指出。
——————————————————————————————————————————
#include <cstdlib>
#include <graphics.h>
#include <conio.h>
#include <synchapi.h>
#include <time.h>
#define HEIGHT 50 // 高上一共50个小格子
#define WIDTH 4 // 宽上一共4个小格子
#define BLOCK_SIZE 75 // 每个小格子的宽大小
int origin_y = -4400; //原点Y坐标
int carrent = 5050;//当前方块坐标
char input; //键盘获取值
void main()
{
srand(time(0)); //重置随机序列
initgraph(300, 600); //新建绘图界面
setorigin(0, origin_y);//设置原点坐标
void topsd(void);
void Gemo_over(void);
using namespace std;
int coord[50][4]; //初始化方块数组
int position; //方块位置
int i;//循环参数
int j;//循环参数
float ck_wide = 300; //窗口宽
float ck_hot = 600;//窗口高
float fk_hot = 100;//方块高
int add_value = 25; //增量值
settextstyle(20, 16, _T("宋体")); // 设定文字大小、样式
outtextxy(40, carrent - 300, _T("从左至右分别以")); // 输出文字内容
outtextxy(50, carrent - 260, _T("D,F,J,K 控制")); // 输出文字内容
settextstyle(40, 16, _T("宋体")); // 设定文字大小、样式
outtextxy(40, carrent - 220, _T("点击任意键继续")); // 输出文字内容
_getch();
for (i = 0; i < 50; i++)
for (j = 0; j < 4; j++)
{
coord[i][j] = 0;
}
for (int ene = 1; ene <= 51; ene++)
{
if (add_value == 25) //第一次初始化
for (i = 0; i < HEIGHT; i++) // 对二维数组所有元素遍历
{
position = rand() % 4; //随机方块
for (j = 0; j < WIDTH; j++)
{
setlinecolor(RGB(0, 0, 0));
if (position == j)
{
coord[i][j] = 1;
setfillcolor(RGB(0, 0, 0));
}
else
setfillcolor(RGB(255, 255, 255)); // 根据元素值设定填充颜色,对应位置绘制对应颜色图像
fillrectangle(j * BLOCK_SIZE, i * BLOCK_SIZE + add_value - 25, (j + 1) * BLOCK_SIZE, (i + 1) * BLOCK_SIZE + add_value * 2);
}
add_value += 25;
}
else
{
input = _getch(); //获取键盘输入值
if (input == 'd')
{
carrent -= 100;
COLORREF d = getpixel(40, carrent);
cleardevice();
if (d == RGB(0, 0, 0))
{
origin_y = origin_y + 100;
setorigin(0, origin_y);//重设原点坐标
add_value = 25;//恢复附加值
for (i = 0; i < HEIGHT; i++) // 对二维数组所有元素遍历
{
for (j = 0; j < WIDTH; j++)
{
setlinecolor(RGB(0, 0, 0));
if (coord[i][j] == 1)
{
setfillcolor(RGB(0, 0, 0));
}
else
setfillcolor(RGB(255, 255, 255)); // 根据元素值设定填充颜色,对应位置绘制对应颜色图像
fillrectangle(j * BLOCK_SIZE, i * BLOCK_SIZE + add_value - 25, (j + 1) * BLOCK_SIZE, (i + 1) * BLOCK_SIZE + add_value * 2);
}
add_value += 25;
}
}
else
{
Gemo_over();
return;
}
}
else if (input == 'f')
{
carrent -= 100;
COLORREF f = getpixel(140, carrent);
cleardevice();
if (f == RGB(0, 0, 0))
{
origin_y = origin_y + 100;
setorigin(0, origin_y);//重设原点坐标
add_value = 25;//恢复附加值
for (i = 0; i < HEIGHT; i++) // 对二维数组所有元素遍历
{
for (j = 0; j < WIDTH; j++)
{
setlinecolor(RGB(0, 0, 0));
if (coord[i][j] == 1)
{
setfillcolor(RGB(0, 0, 0));
}
else
setfillcolor(RGB(255, 255, 255)); // 根据元素值设定填充颜色,对应位置绘制对应颜色图像
fillrectangle(j * BLOCK_SIZE, i * BLOCK_SIZE + add_value - 25, (j + 1) * BLOCK_SIZE, (i + 1) * BLOCK_SIZE + add_value * 2);
}
add_value += 25;
}
}
else
{
Gemo_over();
return;
}
}
else if (input == 'j')
{
carrent -= 100;
COLORREF j = getpixel(210, carrent);
cleardevice();
if (RGB(0, 0, 0) == j)
{
origin_y = origin_y + 100;
setorigin(0, origin_y);//重设原点坐标
add_value = 25;//恢复附加值
for (i = 0; i < HEIGHT; i++) // 对二维数组所有元素遍历
{
for (j = 0; j < WIDTH; j++)
{
setlinecolor(RGB(0, 0, 0));
if (coord[i][j] == 1)
{
setfillcolor(RGB(0, 0, 0));
}
else
setfillcolor(RGB(255, 255, 255)); // 根据元素值设定填充颜色,对应位置绘制对应颜色图像
fillrectangle(j * BLOCK_SIZE, i * BLOCK_SIZE + add_value - 25, (j + 1) * BLOCK_SIZE, (i + 1) * BLOCK_SIZE + add_value * 2);
}
add_value += 25;
}
}
else
{
Gemo_over();
return;
}
}
else if (input == 'k')
{
carrent -= 100;
COLORREF k = getpixel(280, carrent);
cleardevice();
if (RGB(0, 0, 0) == k)
{
origin_y = origin_y + 100;
setorigin(0, origin_y);//重设原点坐标
add_value = 25;//恢复附加值
for (i = 0; i < HEIGHT; i++) // 对二维数组所有元素遍历
{
for (j = 0; j < WIDTH; j++)
{
setlinecolor(RGB(0, 0, 0));
if (coord[i][j] == 1)
{
setfillcolor(RGB(0, 0, 0));
}
else
setfillcolor(RGB(255, 255, 255)); // 根据元素值设定填充颜色,对应位置绘制对应颜色图像
fillrectangle(j * BLOCK_SIZE, i * BLOCK_SIZE + add_value - 25, (j + 1) * BLOCK_SIZE, (i + 1) * BLOCK_SIZE + add_value * 2);
}
add_value += 25;
}
}
else
{
Gemo_over();
return;
}
}
}
}
//游戏胜利提示
settextcolor(RGB(255, 228, 181));// 设定文字颜色
settextstyle(80, 32, _T("宋体")); // 设定文字大小、样式
outtextxy(0, carrent - 300, _T("Game victory")); // 输出文字内容
settextstyle(40, 16, _T("宋体")); // 设定文字大小、样式
outtextxy(40, carrent - 220, _T("点击任意键退出")); // 输出文字内容
_getch();
}
void Gemo_over(void)
{
setbkmode(TRANSPARENT); // 文字字体透明
settextcolor(RGB(255, 0, 0));// 设定文字颜色
settextstyle(80, 32, _T("宋体")); // 设定文字大小、样式
outtextxy(8, carrent - 300, _T("Gemo over")); // 输出文字内容
settextstyle(40, 16, _T("宋体")); // 设定文字大小、样式
outtextxy(40, carrent - 220, _T("点击任意键退出")); // 输出文字内容
input = _getch();
closegraph();
cleardevice();
closegraph();
}
有不足地方还请各位前辈指出。