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