|
|
@ -1,132 +1,133 @@
|
|
|
|
/********头文件*********/
|
|
|
|
/*******头 文 件*******/
|
|
|
|
#include<stdio.h>
|
|
|
|
#include <stdio.h> //标准输入输出函数库(printf、scanf)
|
|
|
|
#include<windows.h>
|
|
|
|
#include <windows.h> //控制dos界面(获取控制台上坐标位置、设置字体颜色)
|
|
|
|
#include<conio.h>
|
|
|
|
#include <conio.h> //接收键盘输入输出(kbhit()、getch())
|
|
|
|
#include<time.h>
|
|
|
|
#include <time.h> //用于获得随机数
|
|
|
|
|
|
|
|
/*******宏 定 义*******/
|
|
|
|
|
|
|
|
#define FrameX 13 //游戏窗口左上角的X轴坐标为13
|
|
|
|
/********宏定义********/
|
|
|
|
#define FrameY 3 //游戏窗口左上角的Y轴坐标为13
|
|
|
|
#define FrameX 13
|
|
|
|
#define Frame_height 20 //游戏窗口的高度为20
|
|
|
|
#define FrameY 3
|
|
|
|
#define Frame_width 18 //游戏窗口的宽度为18
|
|
|
|
#define Frame_height 20 //定义游戏窗口高度
|
|
|
|
/*******定 义 全 局 变 量 *******/
|
|
|
|
#define Frame_width 18//定义游戏窗口宽度
|
|
|
|
int i,j,Temp,Temp1,Temp2; //temp,temp1,temp2用于记住和转换方块变量的值
|
|
|
|
|
|
|
|
//标记游戏屏幕的图案:2,1,0分别表示该位置为游戏边框、方块、无图案;初始化为无图案
|
|
|
|
|
|
|
|
int a[80][80]={0};
|
|
|
|
/********全局变量*********/
|
|
|
|
int b[4]; //标记4个"口"方块:1表示有方块,0表示无方块
|
|
|
|
int i,j,Temp,Temp1,Temp2;
|
|
|
|
struct Tetris //声明俄罗斯方块的结构体
|
|
|
|
int a[80][80]={0};//a数组中2,1,0分别代表边框,方块和无图案
|
|
|
|
|
|
|
|
int b[4];//每种方块都由四个█组成,b数组用来标记四个█的位置以构造图形
|
|
|
|
|
|
|
|
struct Tetris
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int x; //中心方块的x轴坐标
|
|
|
|
int x; //中心方块的x轴坐标
|
|
|
|
int y; //中心方块的y轴坐标
|
|
|
|
int y; //中心方块的y轴坐标
|
|
|
|
int flag; //标记方块类型的序号
|
|
|
|
int flag; //标记方块类型的序号
|
|
|
|
int next;//下一个方块类型的序号
|
|
|
|
int next; //下一个俄罗斯方块类型的序号
|
|
|
|
int speed;//俄罗斯方块产生的速度
|
|
|
|
int speed; //俄罗斯方块移动的速度
|
|
|
|
int number; //产生俄罗斯方块的个数
|
|
|
|
int number; //产生俄罗斯方块的个数
|
|
|
|
int score;//游戏分数
|
|
|
|
int score; //游戏的分数
|
|
|
|
int level;//游戏等级
|
|
|
|
int level; //游戏的等级
|
|
|
|
};
|
|
|
|
};
|
|
|
|
HANDLE hOut; //控制台句柄
|
|
|
|
HANDLE hOut; //控制台句柄
|
|
|
|
|
|
|
|
/*******函 数 声 明 *******/
|
|
|
|
|
|
|
|
int color(int c); //设置文字颜色
|
|
|
|
/******函数声明*******/
|
|
|
|
void gotoxy(int x, int y); //光标移到指定位置
|
|
|
|
int color(int c);
|
|
|
|
void DrwaGameframe(); //绘制游戏边框
|
|
|
|
static void gotoxy(int x, int y);//移动光标位置
|
|
|
|
void Flag(struct Tetris *); //随机产生方块类型的序号
|
|
|
|
void DrawGameframe(); //绘制游戏边框
|
|
|
|
|
|
|
|
void Flag(struct Tetris * );//随机产生俄罗斯方块序号
|
|
|
|
|
|
|
|
void MakeTetris(struct Tetris *); //制作俄罗斯方块
|
|
|
|
void MakeTetris(struct Tetris *); //制作俄罗斯方块
|
|
|
|
void PrintTetris(struct Tetris *); //打印俄罗斯方块
|
|
|
|
void PrintTetris(struct Tetris *); //打印俄罗斯方块
|
|
|
|
void CleanTetris(struct Tetris *); //清除俄罗斯方块的痕迹
|
|
|
|
void CleanTetris(struct Tetris *); //清除俄罗斯方块的痕迹
|
|
|
|
int ifMove(struct Tetris *);//判断能否移动
|
|
|
|
int ifMove(struct Tetris *); //判断是否能移动,返回值为1,能移动,否则,不能移动
|
|
|
|
void Del_Fullline(struct Tetris *);//判断是否满行
|
|
|
|
void Del_Fullline(struct Tetris *); //判断是否满行,并删除满行的俄罗斯方块
|
|
|
|
void GamePlay();//开始游戏
|
|
|
|
void Gameplay(); //开始游戏
|
|
|
|
void regulation(); //游戏规则
|
|
|
|
void regulation(); //游戏规则
|
|
|
|
void explation(); //按键说明
|
|
|
|
void explation(); //按键说明
|
|
|
|
void welcome();//欢迎界面
|
|
|
|
void welcom(); //欢迎界面
|
|
|
|
void Replay(struct Tetris *);//重新开始游戏
|
|
|
|
void Replay(struct Tetris * tetris); //重新开始游戏
|
|
|
|
void title();//欢迎界面上方标题
|
|
|
|
void title(); //欢迎界面上方的标题
|
|
|
|
void close();//关闭游戏
|
|
|
|
void flower(); //欢迎界面上的字符装饰花
|
|
|
|
|
|
|
|
void close();
|
|
|
|
|
|
|
|
/**
|
|
|
|
/*****文字颜色函数******/
|
|
|
|
* 文字颜色函数 此函数的局限性:1、只能Windows系统下使用 2、不能改变背景颜色
|
|
|
|
int color(int c){
|
|
|
|
*/
|
|
|
|
|
|
|
|
int color(int c)
|
|
|
|
|
|
|
|
{
|
|
|
|
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); //更改文字颜色
|
|
|
|
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); //更改文字颜色
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
} //关闭游戏
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取屏幕光标位置
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void gotoxy(int x, int y)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
COORD pos;
|
|
|
|
|
|
|
|
pos.X = x; //横坐标
|
|
|
|
|
|
|
|
pos.Y = y; //纵坐标
|
|
|
|
|
|
|
|
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 欢迎界面上方的标题
|
|
|
|
/*****获取屏幕光标位置*******/
|
|
|
|
*/
|
|
|
|
static void gotoxy(int x, int y){
|
|
|
|
void title()
|
|
|
|
COORD point = { x, y };//光标要设置的位置x,y
|
|
|
|
{
|
|
|
|
HANDLE HOutput = GetStdHandle(STD_OUTPUT_HANDLE);//使用GetStdHandle(STD_OUTPUT_HANDLE)来获取标准输出的句柄
|
|
|
|
color(15); //亮白色
|
|
|
|
SetConsoleCursorPosition(HOutput, point);//设置光标位置
|
|
|
|
gotoxy(28,3);
|
|
|
|
}
|
|
|
|
printf("趣 味 俄 罗 斯 方 块\n"); //输出标题
|
|
|
|
|
|
|
|
color(11); //亮蓝色
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****欢迎界面标题********/
|
|
|
|
|
|
|
|
void title(){
|
|
|
|
|
|
|
|
color(15);
|
|
|
|
|
|
|
|
gotoxy(30,3);
|
|
|
|
|
|
|
|
printf("趣味俄罗斯方块\n");
|
|
|
|
|
|
|
|
//标题
|
|
|
|
|
|
|
|
color(11);
|
|
|
|
|
|
|
|
gotoxy(18,5);
|
|
|
|
gotoxy(18,5);
|
|
|
|
printf("█");
|
|
|
|
printf("■"); //■
|
|
|
|
gotoxy(18,6);
|
|
|
|
gotoxy(18,6); //■■
|
|
|
|
printf("██");
|
|
|
|
printf("■■"); //■
|
|
|
|
gotoxy(18,7);
|
|
|
|
gotoxy(18,7);
|
|
|
|
printf("█");
|
|
|
|
printf("■");
|
|
|
|
//T字方块
|
|
|
|
|
|
|
|
color(14);
|
|
|
|
color(14); //黄色
|
|
|
|
gotoxy(26,6);
|
|
|
|
gotoxy(26,6);
|
|
|
|
printf("██");
|
|
|
|
printf("■■"); //■■
|
|
|
|
gotoxy(28,7);
|
|
|
|
gotoxy(28,7); // ■■
|
|
|
|
printf("██");
|
|
|
|
printf("■■");
|
|
|
|
//Z字方块
|
|
|
|
|
|
|
|
color(10);
|
|
|
|
color(10); //绿色
|
|
|
|
gotoxy(36,6);
|
|
|
|
gotoxy(36,6); //■■
|
|
|
|
printf("██");
|
|
|
|
printf("■■"); //■■
|
|
|
|
gotoxy(36,7);
|
|
|
|
gotoxy(36,7);
|
|
|
|
printf("██");
|
|
|
|
printf("■■");
|
|
|
|
//田字方块
|
|
|
|
|
|
|
|
color(13);
|
|
|
|
color(13); //粉色
|
|
|
|
gotoxy(45,5);
|
|
|
|
gotoxy(45,5);
|
|
|
|
printf("█");
|
|
|
|
printf("■"); //■
|
|
|
|
gotoxy(45,6);
|
|
|
|
gotoxy(45,6); //■
|
|
|
|
printf("█");
|
|
|
|
printf("■"); //■
|
|
|
|
gotoxy(45,7);
|
|
|
|
gotoxy(45,7); //■
|
|
|
|
printf("█");
|
|
|
|
printf("■");
|
|
|
|
gotoxy(45,8);
|
|
|
|
gotoxy(45,8);
|
|
|
|
printf("█");
|
|
|
|
printf("■");
|
|
|
|
//直线方块
|
|
|
|
|
|
|
|
color(12);
|
|
|
|
color(12); //亮红色
|
|
|
|
gotoxy(56,6);
|
|
|
|
gotoxy(56,6);
|
|
|
|
printf("█");
|
|
|
|
printf("■"); // ■
|
|
|
|
gotoxy(52,7);
|
|
|
|
gotoxy(52,7); //■■■
|
|
|
|
printf("███\n");
|
|
|
|
printf("■■■");
|
|
|
|
//7字方块
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 菜单选项边框
|
|
|
|
/*******菜单选项边框*******/
|
|
|
|
*/
|
|
|
|
void welcome()
|
|
|
|
void welcom()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
int n;
|
|
|
|
int i,j = 1;
|
|
|
|
int i,j = 1;
|
|
|
|
color(14);
|
|
|
|
color(14); //黄色边框
|
|
|
|
for(i=9;i<=20;i++){
|
|
|
|
for (i = 9; i <= 20; i++) //循环y轴坐标,打印输出上下边框===
|
|
|
|
for(j=15;j<=60;j++){
|
|
|
|
{
|
|
|
|
|
|
|
|
for (j = 15; j <= 60; j++) //循环x轴坐标,打印输出左右边框||
|
|
|
|
|
|
|
|
{
|
|
|
|
gotoxy(j, i);
|
|
|
|
gotoxy(j, i);
|
|
|
|
if(i==9||i==20) printf("=");
|
|
|
|
if (i == 9 || i == 20) printf("="); //输出上下边框===
|
|
|
|
else if(j==15||j==59) printf("::");
|
|
|
|
else if (j == 15 || j == 59) printf("||"); //输出左右边框||
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*******菜单选项的文字********/
|
|
|
|
/**
|
|
|
|
color(12);
|
|
|
|
* 菜单选项的文字
|
|
|
|
gotoxy(25,12);
|
|
|
|
*/
|
|
|
|
printf("1.开始游戏");
|
|
|
|
color(12); //设置字体颜色为红色
|
|
|
|
|
|
|
|
gotoxy(25, 12); //设置显示位置
|
|
|
|
|
|
|
|
printf("1.开始游戏"); //输出文字“1.开始游戏”
|
|
|
|
gotoxy(40, 12);
|
|
|
|
gotoxy(40, 12);
|
|
|
|
printf("2.按键说明");
|
|
|
|
printf("2.按键说明");
|
|
|
|
gotoxy(25, 17);
|
|
|
|
gotoxy(25, 17);
|
|
|
@ -134,83 +135,87 @@ void welcome()
|
|
|
|
gotoxy(40, 17);
|
|
|
|
gotoxy(40, 17);
|
|
|
|
printf("4.退出");
|
|
|
|
printf("4.退出");
|
|
|
|
gotoxy(21,22);
|
|
|
|
gotoxy(21,22);
|
|
|
|
printf("请选择[1,2,3,4]:[ ]\b\b");
|
|
|
|
color(3);
|
|
|
|
scanf("%d",&n);
|
|
|
|
printf("请选择[1 2 3 4]:[ ]\b\b");
|
|
|
|
switch(n){
|
|
|
|
color(14);
|
|
|
|
case 1:
|
|
|
|
scanf("%d", &n); //输入选项
|
|
|
|
system("cls");
|
|
|
|
switch (n)
|
|
|
|
DrawGameframe();
|
|
|
|
{
|
|
|
|
|
|
|
|
case 1: //输入1
|
|
|
|
|
|
|
|
system("cls"); //清屏
|
|
|
|
|
|
|
|
DrwaGameframe();
|
|
|
|
Gameplay();
|
|
|
|
Gameplay();
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
case 2: //输入2
|
|
|
|
|
|
|
|
explation();
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
case 3:
|
|
|
|
|
|
|
|
regulation(); //输入3
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
case 4:
|
|
|
|
|
|
|
|
close(); //输入4
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 制作游戏窗口
|
|
|
|
/*******游戏窗口*******/
|
|
|
|
*/
|
|
|
|
void DrawGameframe()
|
|
|
|
void DrwaGameframe()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
/***********游戏名称***********/
|
|
|
|
gotoxy(FrameX+Frame_width-5,FrameY-2); //设置游戏名称的显示位置
|
|
|
|
gotoxy(FrameX+Frame_width-7,FrameY-2);
|
|
|
|
color(11); //将字体颜色设置为亮蓝色
|
|
|
|
color(11);
|
|
|
|
printf("趣味俄罗斯方块"); //打印游戏名称
|
|
|
|
printf("趣味俄罗斯方块");
|
|
|
|
gotoxy(FrameX+2*Frame_width+3,FrameY+7); //设置上边框的显示位置
|
|
|
|
/**********下一个出现方块********/
|
|
|
|
color(2); //将字体颜色设置为深绿色
|
|
|
|
gotoxy(FrameX+2*Frame_width+3,FrameY+7);
|
|
|
|
printf("**********"); //打印下一个出现方块的上边框
|
|
|
|
color(2);
|
|
|
|
|
|
|
|
printf("**********");
|
|
|
|
|
|
|
|
gotoxy(FrameX+2*Frame_width+13,FrameY+7);
|
|
|
|
gotoxy(FrameX+2*Frame_width+13,FrameY+7);
|
|
|
|
color(3);
|
|
|
|
color(3); //将字体颜色设置为深蓝绿色
|
|
|
|
printf("下一个出现方块");
|
|
|
|
printf("下一出现方块:");
|
|
|
|
gotoxy(FrameX+2*Frame_width+3,FrameY+13);
|
|
|
|
gotoxy(FrameX+2*Frame_width+3,FrameY+13);
|
|
|
|
color(2);
|
|
|
|
color(2);
|
|
|
|
printf("**********");
|
|
|
|
printf("**********"); //打印下一个出现方块的下边框
|
|
|
|
/**********按键说明***********/
|
|
|
|
|
|
|
|
gotoxy(FrameX+2*Frame_width+3,FrameY+17);
|
|
|
|
gotoxy(FrameX+2*Frame_width+3,FrameY+17);
|
|
|
|
color(14);
|
|
|
|
color(14); //将字体颜色设置为黄色
|
|
|
|
printf("↑键:旋转");
|
|
|
|
printf("↑键:旋转");
|
|
|
|
gotoxy(FrameX+2*Frame_width+3,FrameY+19);
|
|
|
|
gotoxy(FrameX+2*Frame_width+3,FrameY+19);
|
|
|
|
printf("空格:暂停游戏");
|
|
|
|
printf("空格:暂停游戏");
|
|
|
|
gotoxy(FrameX+2*Frame_width+3,FrameY+15);
|
|
|
|
gotoxy(FrameX+2*Frame_width+3,FrameY+15);
|
|
|
|
printf("Esc:退出游戏");
|
|
|
|
printf("Esc :退出游戏");
|
|
|
|
/*********游戏边框***********/
|
|
|
|
|
|
|
|
gotoxy(FrameX,FrameY);
|
|
|
|
gotoxy(FrameX,FrameY);
|
|
|
|
color(12);
|
|
|
|
color(12);
|
|
|
|
printf("╔");
|
|
|
|
printf("╔"); //打印框角
|
|
|
|
gotoxy(FrameX+2*Frame_width-2,FrameY);
|
|
|
|
gotoxy(FrameX+2*Frame_width-2,FrameY);
|
|
|
|
printf("╗");
|
|
|
|
printf("╗");
|
|
|
|
gotoxy(FrameX,FrameY+Frame_height);
|
|
|
|
gotoxy(FrameX,FrameY+Frame_height);
|
|
|
|
printf("╚");
|
|
|
|
printf("╚");
|
|
|
|
gotoxy(FrameX+2*Frame_width-2,FrameY+Frame_height);
|
|
|
|
gotoxy(FrameX+2*Frame_width-2,FrameY+Frame_height);
|
|
|
|
printf("╝");
|
|
|
|
printf("╝");
|
|
|
|
a[FrameX][FrameY+Frame_height]=2;
|
|
|
|
a[FrameX][FrameY+Frame_height]=2; //记住该处已有图案
|
|
|
|
a[FrameX+2*Frame_width-2][FrameY+Frame_height]=2;//防止方块出界
|
|
|
|
a[FrameX+2*Frame_width-2][FrameY+Frame_height]=2;
|
|
|
|
for(i=2;i<2*Frame_width-2;i+=2){
|
|
|
|
for(i=2;i<2*Frame_width-2;i+=2)
|
|
|
|
|
|
|
|
{
|
|
|
|
gotoxy(FrameX+i,FrameY);
|
|
|
|
gotoxy(FrameX+i,FrameY);
|
|
|
|
printf("═");
|
|
|
|
printf("═"); //打印上横框
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(i=2;i<2*Frame_width-2;i+=2){
|
|
|
|
for(i=2;i<2*Frame_width-2;i+=2)
|
|
|
|
|
|
|
|
{
|
|
|
|
gotoxy(FrameX+i,FrameY+Frame_height);
|
|
|
|
gotoxy(FrameX+i,FrameY+Frame_height);
|
|
|
|
printf("═");
|
|
|
|
printf("═"); //打印下横框
|
|
|
|
a[FrameX+i][FrameY+Frame_height]=2;
|
|
|
|
a[FrameX+i][FrameY+Frame_height]=2; //标记下横框为游戏边框,防止方块出界
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(i=1;i<Frame_height;i++){
|
|
|
|
for(i=1;i<Frame_height;i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
gotoxy(FrameX,FrameY+i);
|
|
|
|
gotoxy(FrameX,FrameY+i);
|
|
|
|
printf("║");
|
|
|
|
printf("║"); //打印左竖框
|
|
|
|
a[FrameX][FrameY+i]=2;
|
|
|
|
a[FrameX][FrameY+i]=2; //标记左竖框为游戏边框,防止方块出界
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(i=1;i<Frame_height;i++){
|
|
|
|
for(i=1;i<Frame_height;i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
gotoxy(FrameX+2*Frame_width-2,FrameY+i);
|
|
|
|
gotoxy(FrameX+2*Frame_width-2,FrameY+i);
|
|
|
|
printf("║");
|
|
|
|
printf("║"); //打印右竖框
|
|
|
|
a[FrameX+2*Frame_width-2][FrameY+i]=2;
|
|
|
|
a[FrameX+2*Frame_width-2][FrameY+i]=2; //标记右竖框为游戏边框,防止方块出界
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 制作俄罗斯方块
|
|
|
|
* 制作俄罗斯方块
|
|
|
|
*/
|
|
|
|
*/
|
|
|
@ -243,8 +248,8 @@ void MakeTetris(struct Tetris *tetris)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
color(13);
|
|
|
|
color(13);
|
|
|
|
a[tetris->x][tetris->y-1]=b[1];
|
|
|
|
a[tetris->x][tetris->y-1]=b[1];
|
|
|
|
a[tetris->x][tetris->y+1]=b[2];
|
|
|
|
a[tetris->x][tetris->y-2]=b[2];
|
|
|
|
a[tetris->x][tetris->y+2]=b[3];
|
|
|
|
a[tetris->x][tetris->y+1]=b[3];
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 4: /*T字方块 ■■■
|
|
|
|
case 4: /*T字方块 ■■■
|
|
|
@ -401,40 +406,44 @@ void MakeTetris(struct Tetris *tetris)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 打印俄罗斯方块
|
|
|
|
|
|
|
|
*/
|
|
|
|
/******打印俄罗斯方块*******/
|
|
|
|
void PrintTetris(struct Tetris *tetris)
|
|
|
|
void PrintTetris(struct Tetris *tetris){
|
|
|
|
{
|
|
|
|
for(i=0;i<4;i++) b[i]=1;
|
|
|
|
for(i=0;i<4;i++) //数组b[4]中有4个元素,循环这4个元素,让每个元素的值都为1
|
|
|
|
MakeTetris(tetris);
|
|
|
|
{
|
|
|
|
for(i=tetris->x-2;i<=tetris->x+4;i+=2){
|
|
|
|
b[i]=1; //数组b[4]的每个元素的值都为1
|
|
|
|
for(j=tetris->y-1;j<=tetris->y+1;j++){
|
|
|
|
}
|
|
|
|
if(a[i][j]==1&&j>FrameY){
|
|
|
|
MakeTetris(tetris); //制作游戏窗口
|
|
|
|
|
|
|
|
for( i=tetris->x-2; i<=tetris->x+4; i+=2 )
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
for(j=tetris->y-2;j<=tetris->y+1;j++) //循环方块所有可能出现的位置
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if( a[i][j]==1 && j>FrameY ) //如果这个位置上有方块
|
|
|
|
|
|
|
|
{
|
|
|
|
gotoxy(i,j);
|
|
|
|
gotoxy(i,j);
|
|
|
|
printf("█");
|
|
|
|
printf("■"); //打印边框内的方块
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//打印菜单信息
|
|
|
|
//打印菜单信息
|
|
|
|
gotoxy(FrameX+2*Frame_width+3,FrameY+1);
|
|
|
|
gotoxy(FrameX+2*Frame_width+3,FrameY+1); //设置打印位置
|
|
|
|
color(4);
|
|
|
|
color(4);
|
|
|
|
printf("level : ");
|
|
|
|
printf("level : ");
|
|
|
|
color(12);
|
|
|
|
color(12);
|
|
|
|
printf("%d",tetris->level);
|
|
|
|
printf(" %d",tetris->level); //输出等级
|
|
|
|
gotoxy(FrameX+2*Frame_width+3,FrameY+3);
|
|
|
|
gotoxy(FrameX+2*Frame_width+3,FrameY+3);
|
|
|
|
color(4);
|
|
|
|
color(4);
|
|
|
|
printf("score : ");
|
|
|
|
printf("score : ");
|
|
|
|
color(12);
|
|
|
|
color(12);
|
|
|
|
printf("%d",tetris->score);
|
|
|
|
printf(" %d",tetris->score); //输出分数
|
|
|
|
gotoxy(FrameX+2*Frame_width+3,FrameY+3);
|
|
|
|
gotoxy(FrameX+2*Frame_width+3,FrameY+5);
|
|
|
|
color(4);
|
|
|
|
color(4);
|
|
|
|
printf("speed");
|
|
|
|
printf("speed : ");
|
|
|
|
color(12);
|
|
|
|
color(12);
|
|
|
|
printf("%dms",tetris->speed);
|
|
|
|
printf(" %dms",tetris->speed); //输出速度
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 判断是否可移动
|
|
|
|
* 判断是否可移动
|
|
|
|
*/
|
|
|
|
*/
|
|
|
@ -494,11 +503,12 @@ int ifMove(struct Tetris *tetris)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 清除俄罗斯方块的痕迹
|
|
|
|
|
|
|
|
*/
|
|
|
|
void CleanTetris(struct Tetris *tetris)
|
|
|
|
void CleanTetris(struct Tetris *tetris)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
int n;
|
|
|
|
for(i=0;i<4;i++) //数组b[4]中有4个元素,循环这4个元素,让每个元素的值都为0
|
|
|
|
for(i=0;i<4;i++) //数组b[4]中有4个元素,循环这4个元素,让每个元素的值都为0
|
|
|
|
{
|
|
|
|
{
|
|
|
|
b[i]=0; //数组b[4]的每个元素的值都为0
|
|
|
|
b[i]=0; //数组b[4]的每个元素的值都为0
|
|
|
@ -519,13 +529,12 @@ void CleanTetris(struct Tetris *tetris)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 判断是否满行并删除满行的俄罗斯方块
|
|
|
|
* 判断是否满行并删除满行的俄罗斯方块
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
void Del_Fullline(struct Tetris *tetris) //当某行有Frame_width-2个方块时,则满行消除
|
|
|
|
void Del_Fullline(struct Tetris *tetris) //当某行有Frame_width-2个方块时,则满行消除
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
int n;
|
|
|
|
int k,del_rows=0; //分别用于记录某行方块的个数和删除方块的行数的变量
|
|
|
|
int k,del_rows=0; //分别用于记录某行方块的个数和删除方块的行数的变量
|
|
|
|
for(j=FrameY+Frame_height-1;j>=FrameY+1;j--)
|
|
|
|
for(j=FrameY+Frame_height-1;j>=FrameY+1;j--)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -555,7 +564,7 @@ void Del_Fullline(struct Tetris *tetris) //
|
|
|
|
printf(" ");
|
|
|
|
printf(" ");
|
|
|
|
a[i][k+1]=1;
|
|
|
|
a[i][k+1]=1;
|
|
|
|
gotoxy(i,k+1);
|
|
|
|
gotoxy(i,k+1);
|
|
|
|
printf("█");
|
|
|
|
printf("■");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -571,20 +580,41 @@ void Del_Fullline(struct Tetris *tetris) //
|
|
|
|
tetris->speed-=20;
|
|
|
|
tetris->speed-=20;
|
|
|
|
tetris->level++;
|
|
|
|
tetris->level++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tetris->speed==0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
system("cls"); //清屏
|
|
|
|
|
|
|
|
gotoxy(17,18);
|
|
|
|
|
|
|
|
color(14);
|
|
|
|
|
|
|
|
printf("您已通关");
|
|
|
|
|
|
|
|
gotoxy(32,20);
|
|
|
|
|
|
|
|
printf("选择【1/2】:");
|
|
|
|
|
|
|
|
color(11);
|
|
|
|
|
|
|
|
scanf("%d", &n); //输入1或2
|
|
|
|
|
|
|
|
switch (n) //分支语句
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
|
|
system("cls");
|
|
|
|
|
|
|
|
Replay(tetris); //重新开始游戏
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/******随机产生俄罗斯方块类型的序号*******/
|
|
|
|
|
|
|
|
void Flag(struct Tetris *tetris){
|
|
|
|
|
|
|
|
tetris->number++;
|
|
|
|
|
|
|
|
srand(time(NULL));
|
|
|
|
|
|
|
|
if(tetris->number==1){
|
|
|
|
|
|
|
|
tetris->flag=rand()%19+1;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tetris->next=rand()%19+1;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 随机产生俄罗斯方块类型的序号
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void Flag(struct Tetris *tetris)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
tetris->number++; //记住产生方块的个数
|
|
|
|
|
|
|
|
srand(time(NULL)); //初始化随机数
|
|
|
|
|
|
|
|
if(tetris->number==1)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
tetris->flag = rand()%19+1; //记住第一个方块的序号
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
tetris->next = rand()%19+1; //记住下一个方块的序号
|
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 开始游戏
|
|
|
|
* 开始游戏
|
|
|
|
*/
|
|
|
|
*/
|
|
|
@ -607,7 +637,8 @@ void Gameplay()
|
|
|
|
PrintTetris(tetris); //调用打印俄罗斯方块方法
|
|
|
|
PrintTetris(tetris); //调用打印俄罗斯方块方法
|
|
|
|
tetris->x=FrameX+Frame_width; //获得游戏窗口中心方块x坐标
|
|
|
|
tetris->x=FrameX+Frame_width; //获得游戏窗口中心方块x坐标
|
|
|
|
tetris->y=FrameY-1; //获得游戏窗口中心方块y坐标
|
|
|
|
tetris->y=FrameY-1; //获得游戏窗口中心方块y坐标
|
|
|
|
tetris->flag=Temp; //按键操作
|
|
|
|
tetris->flag=Temp;
|
|
|
|
|
|
|
|
//按键操作
|
|
|
|
while(1) //控制方块方向,直至方块不再下移
|
|
|
|
while(1) //控制方块方向,直至方块不再下移
|
|
|
|
{
|
|
|
|
{
|
|
|
|
label:PrintTetris(tetris); //打印俄罗斯方块
|
|
|
|
label:PrintTetris(tetris); //打印俄罗斯方块
|
|
|
@ -626,17 +657,6 @@ void Gameplay()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
tetris->x+=2;
|
|
|
|
tetris->x+=2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(ch==80) //按 ↓键则加速下落
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(ifMove(tetris)!=0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
tetris->y+=2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ifMove(tetris)==0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
tetris->y=FrameY+Frame_height-2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ch==72) //按 ↑键则变体,即当前方块顺时针转90度
|
|
|
|
if(ch==72) //按 ↑键则变体,即当前方块顺时针转90度
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if( tetris->flag>=2 && tetris->flag<=3 ) //如果是直线方块
|
|
|
|
if( tetris->flag>=2 && tetris->flag<=3 ) //如果是直线方块
|
|
|
@ -670,6 +690,17 @@ void Gameplay()
|
|
|
|
tetris->flag+=16;
|
|
|
|
tetris->flag+=16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ch==80) //按 ↓键则加速下落
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(ifMove(tetris)!=0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
tetris->y+=2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ifMove(tetris)==0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
tetris->y=FrameY+Frame_height-2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if(ch == 32) //按空格键,暂停
|
|
|
|
if(ch == 32) //按空格键,暂停
|
|
|
|
{
|
|
|
|
{
|
|
|
|
PrintTetris(tetris);
|
|
|
|
PrintTetris(tetris);
|
|
|
@ -688,9 +719,9 @@ void Gameplay()
|
|
|
|
if(ch == 27)
|
|
|
|
if(ch == 27)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
system("cls");
|
|
|
|
system("cls");
|
|
|
|
memset(a,0,6400*sizeof(int));
|
|
|
|
memset(a,0,6400*sizeof(int)); //初始化a数组
|
|
|
|
title(); //初始化a数组
|
|
|
|
title();
|
|
|
|
welcome();
|
|
|
|
welcom();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(ifMove(tetris)==0) //如果不可动,上面操作无效
|
|
|
|
if(ifMove(tetris)==0) //如果不可动,上面操作无效
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -710,20 +741,13 @@ void Gameplay()
|
|
|
|
Del_Fullline(tetris);
|
|
|
|
Del_Fullline(tetris);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}//游戏结束条件:方块触到框顶位置
|
|
|
|
}
|
|
|
|
|
|
|
|
//游戏结束条件:方块触到框顶位置
|
|
|
|
for(i=tetris->y-2;i<tetris->y+2;i++)
|
|
|
|
for(i=tetris->y-2;i<tetris->y+2;i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(i==FrameY)
|
|
|
|
if(i==FrameY)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
system("cls"); //清屏
|
|
|
|
system("cls"); //清屏
|
|
|
|
gotoxy(29,7); //设置显示位置
|
|
|
|
|
|
|
|
printf(" \n");
|
|
|
|
|
|
|
|
color(12); //红色
|
|
|
|
|
|
|
|
printf("\t\t\t■■■■ ■ ■ ■■ \n");
|
|
|
|
|
|
|
|
printf("\t\t\t■ ■■ ■ ■ ■ \n");
|
|
|
|
|
|
|
|
printf("\t\t\t■■■ ■ ■ ■ ■ ■ \n");
|
|
|
|
|
|
|
|
printf("\t\t\t■ ■ ■ ■ ■ ■ \n");
|
|
|
|
|
|
|
|
printf("\t\t\t■■■■ ■ ■ ■■ \n");
|
|
|
|
|
|
|
|
gotoxy(17,18);
|
|
|
|
gotoxy(17,18);
|
|
|
|
color(14);
|
|
|
|
color(14);
|
|
|
|
printf("我要重新玩一局-------1");
|
|
|
|
printf("我要重新玩一局-------1");
|
|
|
@ -752,21 +776,104 @@ for(i=tetris->y-2;i<tetris->y+2;i++)
|
|
|
|
CleanTetris(tetris);
|
|
|
|
CleanTetris(tetris);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 重新开始游戏
|
|
|
|
/*******重新开始游戏函数*******/
|
|
|
|
*/
|
|
|
|
void Replay(struct Tetris *tetris){
|
|
|
|
void Replay(struct Tetris *tetris)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
system("cls"); //清屏
|
|
|
|
|
|
|
|
memset(a,0,6400*sizeof(int)); //初始化a数组,否则不会正常显示方块,导致游戏直接结束
|
|
|
|
|
|
|
|
DrwaGameframe(); //制作游戏窗口
|
|
|
|
|
|
|
|
Gameplay(); //开始游戏
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 按键说明
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void explation()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int i,j = 1;
|
|
|
|
|
|
|
|
system("cls"); //清屏
|
|
|
|
|
|
|
|
color(13); //粉色
|
|
|
|
|
|
|
|
gotoxy(32,3); //设置显示位置
|
|
|
|
|
|
|
|
printf("按键说明");
|
|
|
|
|
|
|
|
color(2);
|
|
|
|
|
|
|
|
for (i = 6; i <= 16; i++) //输出上下边框===
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
for (j = 15; j <= 60; j++) //输出左右边框||
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
gotoxy(j, i);
|
|
|
|
|
|
|
|
if (i == 6 || i == 16) printf("=");
|
|
|
|
|
|
|
|
else if (j == 15 || j == 59) printf("||");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
color(3);
|
|
|
|
|
|
|
|
gotoxy(18,7);
|
|
|
|
|
|
|
|
printf("tip1: 玩家可以通过 ← →方向键来移动方块");
|
|
|
|
|
|
|
|
color(10);
|
|
|
|
|
|
|
|
gotoxy(18,9);
|
|
|
|
|
|
|
|
printf("tip2: 通过 ↑使方块旋转");
|
|
|
|
|
|
|
|
color(14);
|
|
|
|
|
|
|
|
gotoxy(18,11);
|
|
|
|
|
|
|
|
printf("tip3: 通过 ↓加速方块下落");
|
|
|
|
|
|
|
|
color(11);
|
|
|
|
|
|
|
|
gotoxy(18,13);
|
|
|
|
|
|
|
|
printf("tip4: 按空格键暂停游戏,再按空格键继续");
|
|
|
|
|
|
|
|
color(4);
|
|
|
|
|
|
|
|
gotoxy(18,15);
|
|
|
|
|
|
|
|
printf("tip5: 按ESC退出游戏");
|
|
|
|
|
|
|
|
getch(); //按任意键返回主界面
|
|
|
|
|
|
|
|
system("cls"); //清屏
|
|
|
|
|
|
|
|
main(); //返回主函数
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 游戏规则
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void regulation()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int i,j = 1;
|
|
|
|
system("cls");
|
|
|
|
system("cls");
|
|
|
|
memset(a,0,6400*sizeof(int));
|
|
|
|
color(13);
|
|
|
|
DrawGameframe();
|
|
|
|
gotoxy(34,3);
|
|
|
|
Gameplay();
|
|
|
|
printf("游戏规则");
|
|
|
|
|
|
|
|
color(2);
|
|
|
|
|
|
|
|
for (i = 6; i <= 18; i++) //输出上下边框===
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
for (j = 12; j <= 70; j++) //输出左右边框||
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
gotoxy(j, i);
|
|
|
|
|
|
|
|
if (i == 6 || i == 18) printf("=");
|
|
|
|
|
|
|
|
else if (j == 12 || j == 69) printf("||");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
color(12);
|
|
|
|
|
|
|
|
gotoxy(16,7);
|
|
|
|
|
|
|
|
printf("tip1: 不同形状的小方块从屏幕上方落下,玩家通过调整");
|
|
|
|
|
|
|
|
gotoxy(22,9);
|
|
|
|
|
|
|
|
printf("方块的位置和方向,使他们在屏幕底部拼出完整的");
|
|
|
|
|
|
|
|
gotoxy(22,11);
|
|
|
|
|
|
|
|
printf("一行或几行");
|
|
|
|
|
|
|
|
color(14);
|
|
|
|
|
|
|
|
gotoxy(16,13);
|
|
|
|
|
|
|
|
printf("tip2: 每消除一行,积分增加100");
|
|
|
|
|
|
|
|
color(11);
|
|
|
|
|
|
|
|
gotoxy(16,15);
|
|
|
|
|
|
|
|
printf("tip3: 每累计1000分,会提升一个等级");
|
|
|
|
|
|
|
|
color(10);
|
|
|
|
|
|
|
|
gotoxy(16,17);
|
|
|
|
|
|
|
|
printf("tip4: 提升等级会使方块下落速度加快,游戏难度加大");
|
|
|
|
|
|
|
|
getch(); //按任意键返回主界面
|
|
|
|
|
|
|
|
system("cls");
|
|
|
|
|
|
|
|
welcom();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 退出
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
void close()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*******主函数*******/
|
|
|
|
|
|
|
|
int main(){
|
|
|
|
int main(){
|
|
|
|
title();
|
|
|
|
title();
|
|
|
|
welcome();
|
|
|
|
welcom();
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|