首次做关于游戏的小项目 一些想法和过程不是很巧妙还有待改良 仅供参考
#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <time.h>
// ●■★▓※
//1 ●
//2 ■
#define _HIGH_ 21
#define _WIDTH_ 12
#define _SHAPE_LEN_ 4
void initMap(); //初始背背景图
void printStaticMap(); //打印静态背景图
void gotoxy(); //控制打印光标位置
void printDynamicShape();//打印动态图形
void moveDown();//向下移动
int isCrashY(); //判断是否与下面碰撞
void addStaticMap(); //将动态图形添加到静态背景图中
void moveLeft(); //向左移动
int isCrashXL(); //判断是否与左侧碰撞,能够向左移动
void moveRight(); //向右移动
int isCrashXR(); //判断是否与右侧碰撞,能够向右移动
void drawNewShape();//描绘新图形
void changeShape();//变换图形
void clearFullLine();//清除满行
int isLineFull(int line);//判断该行是否已满
void clearOneLine(int line);//清除一行
void controlSpeed();//控制速度
void printGameOver();//打印输出游戏结束提示
void isRestartGame();//是否重启游戏
void pauseGame();//暂停游戏
void printPauseGame();//打印暂停游戏提示
void printHowToPlay();//打印游戏说明提示
void printNextBorder();//打印下一个图形外围边框
void drawNextShape();//描绘下一个图形
void printNextShape();//打印预示的下一个图形
int isGameOver();//判断游戏是否结束
int isChangeShape(int argy1,int argx1,int argy2,int argx2); //判断是否变形,argy1代表y坐标,argx1代表x坐标
void initGame();//游戏开始前初始化
void gameOverOrRestart();//游戏结束后是否需要重新开始
void printPicture();//打印一张图片,包括背景图、动态图形、下一个图形
void contrlGame();//控制游戏
struct coord //保存坐标点
{
int x;
int y;
};
int map[_HIGH_][_WIDTH_];//背景图
struct coord shape[_SHAPE_LEN_];//保存图形信息
struct coord nextOneShape[_SHAPE_LEN_];//保存下一个图形信息
int isAddMap = 0;//判断是否添加到静态背景图中
char input = 0;//存输入字符
int currentShape = 0;//保存当前图形编号
int nextShape = 0; //保存下一个图形编号
int i = 0; //用于for循环
int level = 1; //记录游戏难度水平
int score = 0; //记录游戏分数
int speed = 0; //记录游戏速度
int main()
{
initGame(); //游戏开始前初始化
while(1)
{
gameOverOrRestart();//游戏结束后是否需要重新开始
printPicture();//打印一张图片,包括背景图、动态图形、下一个图形
contrlGame(); //控制游戏
}
}
//初始化边框
void initMap()
{
int i,j;
for(i = 0; i < _HIGH_; i++)
{
map[i][0] = 1;
map[i][11] = 1;
for(j = 0; j < _WIDTH_; j++)
{
map[_HIGH_-1][j] = 1;
if(j != 0 && j != _WIDTH_-1 && i != _HIGH_-1)
{
map[i][j] = 0;
}
}
}
}
//打印静态背景图
void printStaticMap()
{
int i,j;
gotoxy(0,0);//一定要将打印光标移动至原点
for(i = 0; i < _HIGH_; i++)
{
for(j = 0; j < _WIDTH_; j++)
{
if(map[i][j] == 0)
printf(" ");
else if(map[i][j] == 1)
printf("★");
//printf("\033[0;31m★\033[0m ");
else if(map[i][j] == 2)
printf("■");
//printf("\033[0;32m■\033[0m ");
}
printf("\n");
}
printf("\n");
printf(" level = %d score = %d\n",level,score);
printNextBorder();
}
//控制光标位置
void gotoxy(int x,int y)
{ //让printf,回到x, y点,x, y是坐标,如果打印位置到原点,x 0,y 0,,gotoxy(0, 0);
int z=0x0b;
HANDLE hOutput;
COORD loc;
loc.X = x; loc.Y=y;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, loc);
}
//打印输出动态图形
void printDynamicShape()
{
int i;
for(i = 0; i < _SHAPE_LEN_; i++)
{
gotoxy(shape[i].x*2,shape[i].y);
printf("■");
//printf("\033[0;33m■\033[0m ");
}
}
//向下移动
void moveDown()
{
int i;
if(isCrashY())
{
addStaticMap();
clearFullLine();
currentShape = nextShape;
drawNewShape();
drawNextShape();
return;
}
else
{
for(i = 0; i < _SHAPE_LEN_; i++)
{
shape[i].y++;
}
}
}
//判断是否与下面碰撞,即不能向下移动
int isCrashY()
{
int i;
for(i = 0; i < _SHAPE_LEN_; i++)
{
if(map[shape[i].y+1][shape[i].x] != 0)
return 1;
}
return 0;
}
//添加到静态背景图中
void addStaticMap()
{
int i;
isAddMap = 1;
for(i = 0; i < _SHAPE_LEN_; i++)
{
map[shape[i].y][shape[i].x] = 2;
}
}
//向左移动
void moveLeft()
{
int i;
if(isCrashXL())
{
return;
}
else
{
for(i = 0; i < _SHAPE_LEN_; i++)
{
shape[i].x--;
}
}
}
//判断是否与左侧相碰撞
int isCrashXL()
{
int i;
for(i = 0; i < _SHAPE_LEN_; i++)
{
if(map[shape[i].y][shape[i].x-1] != 0)
return 1;
}
return 0;
}
//向右移动
void moveRight()
{
int i;
if(isCrashXR())
{
return;
}
else
{
for(i = 0; i < _SHAPE_LEN_; i++)
{
shape[i].x++;
}
}
}
//判断是否与右侧相碰撞
int isCrashXR()
{
int i;
for(i = 0; i < _SHAPE_LEN_; i++)
{
if(map[shape[i].y][shape[i].x+1] != 0)
return 1;
}
return 0;
}
//描绘新的图形
void drawNewShape()
{
switch(currentShape)
{
case 0:
shape[0].x = 5; //■■
shape[0].y = 0; //■■
shape[1].x = 6;
shape[1].y = 0;
shape[2].x = 5;
shape[2].y = 1;
shape[3].x = 6;
shape[3].y = 1;
break;
case 1:
shape[0].x = 4; //■■■■
shape[0].y = 0;
shape[1].x = 5;
shape[1].y = 0;
shape[2].x = 6;
shape[2].y = 0;
shape[3].x = 7;
shape[3].y = 0;
break;
case 2:
shape[0].x = 6; //■
shape[0].y = 0; //■
shape[1].x = 6; //■
shape[1].y = 1; //■
shape[2].x = 6;
shape[2].y = 2;
shape[3].x = 6;
shape[3].y = 3;
break;
case 3:
shape[0].x = 5; //■■
shape[0].y = 0; // ■
shape[1].x = 6; // ■
shape[1].y = 0;
shape[2].x = 6;
shape[2].y = 1;
shape[3].x = 6;
shape[3].y = 2;
break;
case 4:
shape[0].x = 6; // ■
shape[0].y = 0; //■■■
shape[1].x = 4;
shape[1].y = 1;
shape[2].x = 5;
shape[2].y = 1;
shape[3].x = 6;
shape[3].y = 1;
break;
case 5: // ■
shape[0].x = 5; // ■
shape[0].y = 0; // ■■
shape[1].x = 5;
shape[1].y = 1;
shape[2].x = 5;
shape[2].y = 2;
shape[3].x = 6;
shape[3].y = 2;
break;
case 6:
shape[0].x = 4;
shape[0].y = 0; //■■■
shape[1].x = 5; //■
shape[1].y = 0;
shape[2].x = 6;
shape[2].y = 0;
shape[3].x = 4;
shape[3].y = 1;
break;
case 7:
shape[0].x = 5;
shape[0].y = 0; //■■
shape[1].x = 6; //■
shape[1].y = 0; //■
shape[2].x = 5;
shape[2].y = 1;
shape[3].x = 5;
shape[3].y = 2;
break;
case 8:
shape[0].x = 4;
shape[0].y = 0; //■■■
shape[1].x = 5; // ■
shape[1].y = 0;
shape[2].x = 6;
shape[2].y = 0;
shape[3].x = 6;
shape[3].y = 1;
break;
case 9:
shape[0].x = 6;
shape[0].y = 0; // ■
shape[1].x = 6; // ■
shape[1].y = 1; //■■
shape[2].x = 6;
shape[2].y = 2;
shape[3].x = 5;
shape[3].y = 2;
break;
case 10:
shape[0].x = 4;
shape[0].y = 0;
shape[1].x = 4; //■
shape[1].y = 1; //■■■
shape[2].x = 5;
shape[2].y = 1;
shape[3].x = 6;
shape[3].y = 1;
break;
case 11: // ■
shape[0].x = 5; //■■■
shape[0].y = 0;
shape[1].x = 4;
shape[1].y = 1;
shape[2].x = 5;
shape[2].y = 1;
shape[3].x = 6;
shape[3].y = 1;
break;
case 12:
shape[0].x = 5; //■
shape[0].y = 0; //■■
shape[1].x = 5; //■
shape[1].y = 1;
shape[2].x = 6;
shape[2].y = 1;
shape[3].x = 5;
shape[3].y = 2;
break;
case 13:
shape[0].x = 4; //■■■
shape[0].y = 0; // ■
shape[1].x = 5;
shape[1].y = 0;
shape[2].x = 6;
shape[2].y = 0;
shape[3].x = 5;
shape[3].y = 1;
break;
case 14:
shape[0].x = 5; // ■
shape[0].y = 1; //■■
shape[1].x = 6; // ■
shape[1].y = 0;
shape[2].x = 6;
shape[2].y = 1;
shape[3].x = 6;
shape[3].y = 2;
break;
case 15: // ■■
shape[0].x = 5; //■■
shape[0].y = 0;
shape[1].x = 6;
shape[1].y = 0;
shape[2].x = 4;
shape[2].y = 1;
shape[3].x = 5;
shape[3].y = 1;
break;
case 16:
shape[0].x = 5; //■
shape[0].y = 0; //■■
shape[1].x = 5; // ■
shape[1].y = 1;
shape[2].x = 6;
shape[2].y = 1;
shape[3].x = 6;
shape[3].y = 2;
break;
case 17:
shape[0].x = 4; //■■
shape[0].y = 0; // ■■
shape[1].x = 5;
shape[1].y = 0;
shape[2].x = 5;
shape[2].y = 1;
shape[3].x = 6;
shape[3].y = 1;
break;
case 18:
shape[0].x = 6; // ■
shape[0].y = 0; //■■
shape[1].x = 5; //■
shape[1].y = 1;
shape[2].x = 6;
shape[2].y = 1;
shape[3].x = 5;
shape[3].y = 2;
break;
}
}
//变换图形
void changeShape()
{
switch(currentShape)
{
case 0:
break;
case 1:
// if(isChange01())
if(isChangeShape(-1,0,2,0))
{
shape[0].x = shape[1].x;
shape[0].y = shape[1].y-1;
shape[2].x = shape[1].x;
shape[2].y = shape[1].y+1;
shape[3].x = shape[1].x;
shape[3].y = shape[1].y+2;
currentShape = 2;
}
break;
case 2:
// if(isChange02())
if(isChangeShape(0,-1,0,2))
{
shape[0].x = shape[2].x-1;
shape[0].y = shape[2].y;
shape[1].x = shape[2].x+1;
shape[1].y = shape[2].y;
shape[3].x = shape[2].x+2;
shape[3].y = shape[2].y;
currentShape = 1;
}
break;
case 3:
if(isChangeShape(0,1,0,0))
{
shape[0].x = shape[1].x+1;
shape[0].y = shape[1].y-1;
shape[2].x = shape[1].x+1;
shape[2].y = shape[1].y;
shape[3].x = shape[1].x-1;
shape[3].y = shape[1].y;
currentShape = 4;
}
break;
case 4:
if(isChangeShape(1,0,0,0))
{
shape[0].x = shape[2].x+1;
shape[0].y = shape[2].y+1;
shape[1].x = shape[2].x;
shape[1].y = shape[2].y+1;
shape[3].x = shape[2].x;
shape[3].y = shape[2].y-1;
currentShape = 5;
}
break;
case 5:
if(isChangeShape(0,-1,0,0))
{
shape[0].x = shape[2].x-1;
shape[0].y = shape[2].y+1;
shape[1].x = shape[2].x-1;
shape[1].y = shape[2].y;
shape[3].x = shape[2].x+1;
shape[3].y = shape[2].y;
currentShape = 6;
}
break;
case 6:
if(isChangeShape(-1,0,0,0))
{
shape[0].x = shape[2].x-1;
shape[0].y = shape[2].y-1;
shape[1].x = shape[2].x;
shape[1].y = shape[2].y-1;
shape[3].x = shape[2].x;
shape[3].y = shape[2].y+1;
currentShape = 3;
}
break;
case 7:
if(isChangeShape(0,-1,0,0))
{
shape[0].x = shape[2].x+1;
shape[0].y = shape[2].y+1;
shape[1].x = shape[2].x+1;
shape[1].y = shape[2].y;
shape[3].x = shape[2].x-1;
shape[3].y = shape[2].y;
currentShape = 8;
}
break;
case 8:
if(isChangeShape(1,0,-1,0))
{
shape[0].x = shape[2].x-1;
shape[0].y = shape[2].y+1;
shape[1].x = shape[2].x;
shape[1].y = shape[2].y+1;
shape[3].x = shape[2].x;
shape[3].y = shape[2].y-1;
currentShape = 9;
}
break;
case 9:
if(isChangeShape(0,1,0,0))
{
shape[0].x = shape[2].x-1;
shape[0].y = shape[2].y-1;
shape[1].x = shape[2].x-1;
shape[1].y = shape[2].y;
shape[3].x = shape[2].x+1;
shape[3].y = shape[2].y;
currentShape = 10;
}
break;
case 10:
if(isChangeShape(1,0,0,0))
{
shape[0].x = shape[2].x+1;
shape[0].y = shape[2].y-1;
shape[1].x = shape[2].x;
shape[1].y = shape[2].y-1;
shape[3].x = shape[2].x;
shape[3].y = shape[2].y+1;
currentShape = 7;
}
break;
case 11:
if(isChangeShape(1,0,0,0))
{
shape[0].x = shape[2].x+1;
shape[0].y = shape[2].y;
shape[1].x = shape[2].x;
shape[1].y = shape[2].y-1;
shape[3].x = shape[2].x;
shape[3].y = shape[2].y+1;
currentShape = 12;
}
break;
case 12:
if(isChangeShape(0,-1,0,0))
{
shape[0].x = shape[2].x;
shape[0].y = shape[2].y+1;
shape[1].x = shape[2].x+1;
shape[1].y = shape[2].y;
shape[3].x = shape[2].x-1;
shape[3].y = shape[2].y;
currentShape = 13;
}
break;
case 13:
if(isChangeShape(1,0,0,0))
{
shape[0].x = shape[2].x-1;
shape[0].y = shape[2].y;
shape[1].x = shape[2].x;
shape[1].y = shape[2].y+1;
shape[3].x = shape[2].x;
shape[3].y = shape[2].y-1;
currentShape = 14;
}
break;
case 14:
if(isChangeShape(0,1,0,0))
{
shape[0].x = shape[2].x;
shape[0].y = shape[2].y-1;
shape[1].x = shape[2].x-1;
shape[1].y = shape[2].y;
shape[3].x = shape[2].x+1;
shape[3].y = shape[2].y;
currentShape = 11;
}
break;
case 15:
if(isChangeShape(1,0,0,0))
{
shape[0].x = shape[3].x+1;
shape[0].y = shape[3].y+1;
shape[1].x = shape[3].x+1;
shape[1].y = shape[3].y;
shape[2].x = shape[3].x;
shape[2].y = shape[3].y-1;
currentShape = 16;
}
break;
case 16:
if(isChangeShape(0,-1,0,0))
{
shape[0].x = shape[3].x;
shape[0].y = shape[3].y-1;
shape[1].x = shape[3].x+1;
shape[1].y = shape[3].y-1;
shape[2].x = shape[3].x-1;
shape[2].y = shape[3].y;
currentShape = 15;
}
break;
case 17:
if(isChangeShape(1,0,0,0))
{
shape[0].x = shape[2].x+1;
shape[0].y = shape[2].y-1;
shape[1].x = shape[2].x+1;
shape[1].y = shape[2].y;
shape[3].x = shape[2].x;
shape[3].y = shape[2].y+1;
currentShape = 18;
}
break;
case 18:
if(isChangeShape(0,-1,0,0))
{
shape[0].x = shape[2].x-1;
shape[0].y = shape[2].y-1;
shape[1].x = shape[2].x;
shape[1].y = shape[2].y-1;
shape[3].x = shape[2].x+1;
shape[3].y = shape[2].y;
currentShape = 17;
}
break;
}
}
//描绘下一个图形shape
void drawNextShape()
{
nextShape = rand()%19;
switch(nextShape)
{
case 0:
nextOneShape[0].x = 14; //x += 9 y += 3;
nextOneShape[0].y = 3;
nextOneShape[1].x = 15;
nextOneShape[1].y = 3;
nextOneShape[2].x = 14;
nextOneShape[2].y = 4;
nextOneShape[3].x = 15;
nextOneShape[3].y = 4;
break;
case 1:
nextOneShape[0].x = 13;
nextOneShape[0].y = 3;
nextOneShape[1].x = 14;
nextOneShape[1].y = 3;
nextOneShape[2].x = 15;
nextOneShape[2].y = 3;
nextOneShape[3].x = 16;
nextOneShape[3].y = 3;
break;
case 2:
nextOneShape[0].x = 14;
nextOneShape[0].y = 2;
nextOneShape[1].x = 14;
nextOneShape[1].y = 3;
nextOneShape[2].x = 14;
nextOneShape[2].y = 4;
nextOneShape[3].x = 14;
nextOneShape[3].y = 5;
break;
case 3:
nextOneShape[0].x = 14; //■■
nextOneShape[0].y = 3; // ■
nextOneShape[1].x = 15; // ■
nextOneShape[1].y = 3;
nextOneShape[2].x = 15;
nextOneShape[2].y = 4;
nextOneShape[3].x = 15;
nextOneShape[3].y = 5;
break;
case 4:
nextOneShape[0].x = 15; // ■
nextOneShape[0].y = 3; //■■■
nextOneShape[1].x = 13;
nextOneShape[1].y = 4;
nextOneShape[2].x = 14;
nextOneShape[2].y = 4;
nextOneShape[3].x = 15;
nextOneShape[3].y = 4;
break;
case 5: // ■
nextOneShape[0].x = 14; // ■
nextOneShape[0].y = 3; //■■
nextOneShape[1].x = 14;
nextOneShape[1].y = 4;
nextOneShape[2].x = 14;
nextOneShape[2].y = 5;
nextOneShape[3].x = 15;
nextOneShape[3].y = 5;
break;
case 6:
nextOneShape[0].x = 13;
nextOneShape[0].y = 3; //■■■
nextOneShape[1].x = 14; //■
nextOneShape[1].y = 3;
nextOneShape[2].x = 15;
nextOneShape[2].y = 3;
nextOneShape[3].x = 13;
nextOneShape[3].y = 4;
break;
case 7:
nextOneShape[0].x = 14;
nextOneShape[0].y = 3; //■■
nextOneShape[1].x = 15; //■
nextOneShape[1].y = 3; //■
nextOneShape[2].x = 14;
nextOneShape[2].y = 4;
nextOneShape[3].x = 14;
nextOneShape[3].y = 5;
break;
case 8:
nextOneShape[0].x = 13;
nextOneShape[0].y = 3; //■■■
nextOneShape[1].x = 14; // ■
nextOneShape[1].y = 3;
nextOneShape[2].x = 15;
nextOneShape[2].y = 3;
nextOneShape[3].x = 15;
nextOneShape[3].y = 4;
break;
case 9:
nextOneShape[0].x = 15;
nextOneShape[0].y = 3; // ■
nextOneShape[1].x = 15; // ■
nextOneShape[1].y = 4; //■■
nextOneShape[2].x = 15;
nextOneShape[2].y = 5;
nextOneShape[3].x = 14;
nextOneShape[3].y = 5;
break;
case 10:
nextOneShape[0].x = 13;
nextOneShape[0].y = 3;
nextOneShape[1].x = 13;//■
nextOneShape[1].y = 4; //■■■
nextOneShape[2].x = 14;
nextOneShape[2].y = 4;
nextOneShape[3].x = 15;
nextOneShape[3].y = 4;
break;
case 11: // ■
nextOneShape[0].x = 14; //■■■
nextOneShape[0].y = 3;
nextOneShape[1].x = 13;
nextOneShape[1].y = 4;
nextOneShape[2].x = 14;
nextOneShape[2].y = 4;
nextOneShape[3].x = 15;
nextOneShape[3].y = 4;
break;
case 12:
nextOneShape[0].x = 14; //■
nextOneShape[0].y = 3; //■■
nextOneShape[1].x = 14; //■
nextOneShape[1].y = 4;
nextOneShape[2].x = 15;
nextOneShape[2].y = 4;
nextOneShape[3].x = 14;
nextOneShape[3].y = 5;
break;
case 13:
nextOneShape[0].x = 13; //■■■
nextOneShape[0].y = 3; // ■
nextOneShape[1].x = 14;
nextOneShape[1].y = 3;
nextOneShape[2].x = 15;
nextOneShape[2].y = 3;
nextOneShape[3].x = 14;
nextOneShape[3].y = 4;
break;
case 14:
nextOneShape[0].x = 14; // ■
nextOneShape[0].y = 4; //■■
nextOneShape[1].x = 15; // ■
nextOneShape[1].y = 3;
nextOneShape[2].x = 15;
nextOneShape[2].y = 4;
nextOneShape[3].x = 15;
nextOneShape[3].y = 5;
break;
case 15: // ■■
nextOneShape[0].x = 14; //■■
nextOneShape[0].y = 3;
nextOneShape[1].x = 15;
nextOneShape[1].y = 3;
nextOneShape[2].x = 13;
nextOneShape[2].y = 4;
nextOneShape[3].x = 14;
nextOneShape[3].y = 4;
break;
case 16:
nextOneShape[0].x = 14; //■
nextOneShape[0].y = 3; //■■
nextOneShape[1].x = 14; // ■
nextOneShape[1].y = 4;
nextOneShape[2].x = 15;
nextOneShape[2].y = 4;
nextOneShape[3].x = 15;
nextOneShape[3].y = 5;
break;
case 17:
nextOneShape[0].x = 13; //■■
nextOneShape[0].y = 3; // ■■
nextOneShape[1].x = 14;
nextOneShape[1].y = 3;
nextOneShape[2].x = 14;
nextOneShape[2].y = 4;
nextOneShape[3].x = 15;
nextOneShape[3].y = 4;
break;
case 18:
nextOneShape[0].x = 15; // ■
nextOneShape[0].y = 3; //■■
nextOneShape[1].x = 14; //■
nextOneShape[1].y = 4;
nextOneShape[2].x = 15;
nextOneShape[2].y = 4;
nextOneShape[3].x = 14;
nextOneShape[3].y = 5;
break;
}
}
//清除满行
void clearFullLine()
{
int i;
for(i = _HIGH_-2; i >= 0; i--)
{
if(isLineFull(i))
{
clearOneLine(i);
score += 100;
i++;
}
}
}
//判断一行是否已满
int isLineFull(int line)
{
int j;
for(j = 1; j <= _WIDTH_-2; j++)
{
if(map[line][j] == 0)
{
return 0;
}
}
if(j == _WIDTH_-1)
{
return 1;
}
}
//清除一满行
void clearOneLine(int line)
{
int i,j;
for(i = line; i > 0; i--)
{
for(j = 1; j <= _WIDTH_-2; j++)
{
map[i][j] = map[i-1][j];
}
}
}
//判断是否能够进行旋转
int isChangeShape(int argy1,int argx1,int argy2,int argx2)
{
int i;
for(i = 0; i < _SHAPE_LEN_; i++)
{
if(map[shape[i].y+argy1][shape[i].x+argx1] != 0 || map[shape[i].y+argy2][shape[i].x+argx2] != 0)//不等于0,说明碰撞
{
return 0;
}
}
return 1;
}
////判断是否能够旋转图形—1
//int isChange01()
//{
// int i;
// for(i = 0; i < _SHAPE_LEN_; i++)
// {
// if(map[shape[i].y+2][shape[i].x] != 0 || map[shape[i].y-1][shape[i].x] != 0)//不等于0,说明碰撞
// {
// return 0;
// }
// }
// return 1;
//}
////判断是否能够旋转图形—2
//int isChange02()
//{
// int i;
// for(i = 0; i < _SHAPE_LEN_; i++)
// {
// if(map[shape[i].y][shape[i].x+2] != 0 || map[shape[i].y][shape[i].x-1] != 0)
// {
// return 0;
// }
// }
// return 1;
//}
//控制速度
void controlSpeed()
{
if(score >= 0 && score < 500)
{
level = 1;
speed = 20;
}
else if(score < 1000)
{
level = 2;
speed = 15;
}
else if(score < 1500)
{
level = 3;
speed = 10;
}
else if(score < 2000)
{
level = 4;
speed = 5;
}
}
//判断游戏是否结束
int isGameOver()
{
int i;
for(i = 1; i <= _WIDTH_ -2; i++)
{
if(map[0][i] != 0)
{
return 1;
}
}
return 0;
}
//打印输出游戏结束提示
void printGameOver()
{
gotoxy(2,5);
printf(" ");
gotoxy(2,6);
printf(" ▓※▓※▓※▓");
gotoxy(2,7);
printf(" ※ GAMEOVER ※");
gotoxy(2,8);
printf(" ▓※▓※▓※▓");
gotoxy(2,9);
printf(" ");
gotoxy(2,10);
printf(" ");
gotoxy(2,11);
printf(" 按回车键重新开始");
gotoxy(2,12);
printf(" ");
gotoxy(2,13);
printf(" 按其它键游戏结束");
gotoxy(2,14);
printf(" ");
}
//是否重新开始游戏
void isRestartGame()
{
printGameOver();
input = _getch();
if(input == 13)
{
initMap(); //初始化背景图
srand((int)time(0));//初始化随机种子
currentShape = rand()%19; //生成随机图形号
drawNewShape(); //描绘出画新图形
}
else
{
exit(-1);
}
}
//暂停游戏
void pauseGame()
{
char ch = 0;
int count = 0;
printPauseGame();
while(1)
{
if(_kbhit())
{
ch = _getch();
}
if(ch == 32)
{
while(1)
{
if(_kbhit())
{
ch = _getch();
}
if(ch == 32)
{
return;
}
}
}
}
}
//打印输出暂停游戏提示
void printPauseGame()
{
gotoxy(2,5);
printf(" ");
gotoxy(2,6);
printf(" ▓※▓※▓※▓");
gotoxy(2,7);
printf(" ※ PAUSE ※");
gotoxy(2,8);
printf(" ▓※▓※▓※▓");
gotoxy(2,9);
printf(" ");
gotoxy(2,10);
printf(" ");
gotoxy(2,11);
printf(" 第一次空格键暂停");
gotoxy(2,12);
printf(" ");
gotoxy(2,13);
printf(" 第二次空格键开始");
gotoxy(2,14);
printf(" ");
}
//打印输出游戏说明
void printHowToPlay()
{
initMap();
printStaticMap();
gotoxy(2,0);
printf("★★★★★★★★★★★");
gotoxy(2,2);
printf(" 俄 罗 斯 方 块");
gotoxy(2,5);
printf(" 游戏说明");
gotoxy(2,8);
printf(" ↑: 变换图形");
gotoxy(2,10);
printf(" ↓: 向下移动");
gotoxy(2,12);
printf(" ←: 向左移动");
gotoxy(2,14);
printf(" →: 向右移动");
gotoxy(2,16);
printf(" Enter: 开始游戏");
gotoxy(2,18);
printf(" Space: 暂停游戏");
while(1)
{
input = _getch();
if(input == 13)
{
break;
}
}
}
//打印输出下一个图形边框
void printNextBorder()
{
gotoxy(24,0);
printf("★★★★★★★");
gotoxy(24,1);
printf(" ★");
gotoxy(24,2);
printf(" ★");
gotoxy(24,3);
printf(" ★");
gotoxy(24,4);
printf(" ★");
gotoxy(24,5);
printf(" ★");
gotoxy(24,6);
printf(" ★");
gotoxy(24,7);
printf("★★★★★★★");
}
//打印输出下一个图形
void printNextShape()
{
int i;
for(i = 0; i < _SHAPE_LEN_; i++)
{
gotoxy(nextOneShape[i].x*2,nextOneShape[i].y);
printf("■");
}
}
//****************************************
//游戏开始前初始化
void initGame()
{
printHowToPlay(); //显示游戏说明
srand((int)time(0));//初始化随机种子
currentShape = rand()%19; //生成随机图形号
drawNewShape(); //描绘出当前新图形
drawNextShape(); //描绘下一个新图形
}
//游戏结束后是否需要重新开始
void gameOverOrRestart()
{
/*gotoxy(2*x,y++);
printf("■");*/
if(isGameOver())
{
isRestartGame();
}
/*if(isAddMap)
{
isAddMap = 0;
drawNewShape();
}*/
}
//打印一张图片,包括背景图、动态图形、下一个图形
void printPicture()
{
printStaticMap(); //打印静态背景图
printDynamicShape();//打印动态图形
printNextShape(); //打印下一个图形
}
//控制游戏
void contrlGame()
{
controlSpeed();
for(i= 0; i < speed; i++)
{
if(_kbhit())
{
input = _getch();
}
switch(input)
{
case 75: //向左键
moveLeft(); //向左移动
input = 0;
break;
case 77: //向右键
moveRight(); //向右移动
input = 0;
break;
case 80: //向下键
moveDown(); //向下移动
input = 0;
break;
case 72: //向下键
changeShape(); //变换图形
input = 0;
break;
case 32: //空格键
pauseGame(); //暂停游戏
input = 0;
break;
}
Sleep(25);
}
moveDown();
}