在游戏开始前会有一个游戏欢迎页面,整个页面会持续大概三秒,之后便进入游戏场景。在游戏欢迎页面中,除了将显示游戏名称外,还要修改窗口标题。
#include<graphics.h>
#include<stdio.h>
#include<time.h>
#include<conio.h>//kbhit()
int score = 0;//总分
int rank = 0;//等级
#define BLOCK_COUNT 5
#define BLOCK_WIDTH 5
#define BLOCK_HEIGHT 5
#define UNIT_SIZE 20//小方块宽度
#define START_X 130//方块降落框,方块降落起始位置
#define START_Y 30
#define KEY_UP 87//用户操作
#define KEY_LEFT 65
#define KEY_RIGHT 68
#define KEY_DOWN 83
#define KEY_SPACE 32
#define MinX 30//游戏左上角位置
#define MinY 30
int speed = 500;//方块降落速度
int NextIndex = -1;//下一个方块
int BlockIndex = -1;//当前方块
typedef enum {//方块朝向
BLOCK_UP,
BLOCK_RIGHT,
BLOCK_LEFT,
BLOCK_DOWN
}block_dir_t;
typedef enum {//方块移动方向
MOVE_DOWN,
MOVE_LEFT,
MOVE_RIGHT
}move_dir_t;
//方块颜色
int color[BLOCK_COUNT] = {
GREEN,
CYAN,
MAGENTA,
YELLOW,
BROWN
};
int visit[30][15];//访问数组visit[i][j] = 1表示该位置有方块
int markColor[30][15];//对应位置颜色
int block[BLOCK_COUNT * 4][BLOCK_WIDTH][BLOCK_HEIGHT] = {
// | 形方块
{ 0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0 },
// L 形方块
{ 0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0 },
// 田 形方块
{ 0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0 },
// T 形方块
{ 0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,1,0,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0 },
// Z 形方块
{ 0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0 },
};
/***************************
* 功能:欢迎页面
* 输入:
*无
* 返回:
*无
**************************/
void welcome() {
//1.初始化画布
initgraph(550, 660);
system("pause");
//2.设置窗口标题
HWND window = GetHWnd();//获得窗口,获得当前窗口
SetWindowText(window, _T("俄罗斯方块 小明来喽"));//设置标题
//3.设置游戏初始页面
setfont(40, 0, _T("微软雅黑"));//设置文本的字体样式(高,宽(0表示自适应),字体)
setcolor(WHITE);// 设置颜色
outtextxy(205, 200, _T("俄罗斯方法"));
setfont(20, 0, _T("楷体"));
setcolor(WHITE);// 设置颜色
outtextxy(175, 300, _T("编程,从俄罗斯方块开始"));
Sleep(3000);
}
/***************************
* 功能:初始化游戏场景
* 输入:
*无
* 返回:
*无
**************************/
void initGameSceen() {
char str[16];//存放分数
//1.清屏
cleardevice();
//2.画场景
rectangle(27, 27, 336, 635);//方块降落框外框
rectangle(29, 29, 334, 633);//方块降落框内框
rectangle(370, 50, 515, 195);//方块提示框
setfont(24, 0, _T("楷体"));//写“下一个”
setcolor(LIGHTGRAY);//灰色
outtextxy(405, 215, _T("下一个:"));
setcolor(RED);//写分数
outtextxy(405, 280, _T("分数:"));
//按指定格式,将score写入str
sprintf_s(str, 16, "%d", score);
//这里设置字符集为多字符,保证outtextxy可以写出变量str
outtextxy(415, 310, str);
outtextxy(405, 375, _T("等级:"));//等级
//按指定格式,将rank写入str
sprintf_s(str, 16, "%d", rank);
//这里设置字符集为多字符,保证outtextxy可以写出变量str
outtextxy(415, 405, str);
setcolor(LIGHTBLUE);//操作说明
outtextxy(390, 475, "操作说明:");
outtextxy(390, 500, "↑: 旋转");
outtextxy(390, 525, "↓: 下降");
outtextxy(390, 550, "←: 左移");
outtextxy(390, 575, "→: 右移");
outtextxy(390, 600, "空格: 暂停");
}
/*****************************************
* 功能:清空方块提示框里的方块
* 输入:
*无
* 返回:
*无
****************************************/
void clearBlock() {
setcolor(BLACK);
setfont(23, 0, "楷体");
for (int i = 0; i < BLOCK_HEIGHT; ++i) {
for (int j = 0; j < BLOCK_WIDTH; ++j) {
int x = 391 + j * UNIT_SIZE;
int y = 71 + i * UNIT_SIZE;
outtextxy(x, y, "■");
}
}
}
/*****************************************
* 功能:清除降落过程中的方块
* 输入:
*x,y - 方块的坐标(二维数组左上角位置)
*block_dir_t - 方块方向
* 返回:
*无
****************************************/
void clearBlock(int x, int y, block_dir_t blockDir) {
setcolor(BLACK);
//setfont(23, 0, "楷体");
int id = BlockIndex * 4 + blockDir;
for (int i = 0; i < BLOCK_HEIGHT; ++i) {
for (int j = 0; j < BLOCK_WIDTH; ++j) {
if (block[id][i][j] == 1) {
int x2 = x + j * UNIT_SIZE;
int y2 = y + i * UNIT_SIZE;
outtextxy(x2, y2, "■");
}
}
}
}
/*****************************************
* 功能:在提示框 与 降落框的起始位置画方块
* 输入:
*x,y - 方块的坐标(二维数组左上角位置)
* 返回:
*无
****************************************/
void drawBlock(int x, int y) {
setcolor(color[NextIndex]);
setfont(23, 0, "楷体");
for (int i = 0; i < BLOCK_HEIGHT; ++i) {
for (int j = 0; j < BLOCK_WIDTH; ++j) {
if (block[NextIndex * 4][i][j] == 1) {
int x2 = x + j * UNIT_SIZE;
int y2 = y + i * UNIT_SIZE;
outtextxy(x2, y2, "■");
}
}
}
}
/*****************************************
*功能:绘制下降过程中的方块
*输