本文章供自己复习使用,因此讲述的都是自己方便理解的东西。
使用对象实现,方便维护。
class Game
{
public:
int score;//游戏分数
int _id;//图形编号
int top;//最高点高度
int speed;//下落速度
Game();
void showMenu();//显示菜单
void showGround();//显示游戏界面
void gameOver();//游戏结束界面
void Run();//运行游戏
void sharpDraw(int id,bool show = false);//绘制图形
void keyControl();//键盘控制
bool move(int dir,int id);//移动判断
bool downSet(int id);//下落
void Turn(int id);//旋转
void clean();//消行
};
先构造基本框架,游戏界面
void Game::showMenu()
{
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 26; j++)
{
if ((i == 0 || i == 29) || (j == 0 || j == 25))
{
cout << "■";
}
else
{
cout << " ";
}
}
cout << endl;
}
SetPos(17, 8);
cout << "俄 罗 斯 方 块" << endl;
SetPos(13, 12);
cout << "↑旋转方块 ↓加速下滑" << endl;
SetPos(12, 14);
cout << "← →左右移动 空格 暂停" << endl;
SetPos(15, 20);
cout << "0 退出 Enter 开始" << endl;
while (1)
{
int select = _getch();
if (select == 13)
{
system("cls");
this->Run();
}
else if (select = 48)
{
system("cls");
exit(0);
}
}
}
这里用到放置光标函数SetPos,用来实现通过坐标定位光标,这样可以自己控制光标位置进行打印输出操作,函数实现为:
void SetPos(int i, int j)//控制光标位置, 列, 行
{
COORD pos = { i,j };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
SetConsoleCursorPosition()函数就是设置(set)控制台(console)光标(cursor)坐标(position)的意思,第一个参数获取一个句柄,获取(get)标准(std)句柄(handle),这里需要向屏幕输出光标,填入标准输出句柄STD_OUTPUT_HANDLE;另一个参数是获得一个坐标,用坐标结构体COORD创建一个坐标填入即可。
这样向函数填入横纵坐标即可将光标定位到我们想要打印输出的位置。
用主函数实例化一下这个类,然后利用对象调用这个函数,输出结果是这样的
键盘控制使用conio.h头文件里的_getch()函数,这个函数作用是等待用户输入一个键值并执行后续语句,但不像getchar()获得键值后还要按回车才能执行后续语句。为了防止用户按错键,就将_getch()函数和判断语句放到了一个死循环中,只有按正确的键才能继续程序。
退出就直接exit(0),如果想要二次确定就加一段判断就是了。选择开始就开始执行下一个函数运行游戏。
但我们要先构造游戏界面,效果如图
void Game:: showGround()
{
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 26; j++)
{
if ((i == 0 || i == 29) || (j == 0 || j == 25 || j==15))
{
cout << "■";
}
else if (i == 15 && j > 15)
{
cout << "■";
}
else
{
cout << " ";
}
}
cout << endl;
}
SetPos(31, 2);
cout << "下 个图形" << endl;
SetPos(31, 17);
cout << "当 前得分" << endl;
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 16; j++)
{
if ((i == 0 || i == 29) || (j == 0 || j == 15))
{
map[i][j] = 1;
}
else
{
map[i][j] = 0;
}
}
}
}
图形的可移动范围用二维数组map[30][16]表示,但是由于惯性思维依旧按照行、列的顺序建立的数组,这与坐标x为列y为行相反,所以后续使用map时都要将坐标反过来。
这里设置图形在28*14的范围内活动,map值为0,四边为1,为不可进入范围。
有了游戏界面,再构建19种图形,使用4*4的坐标来表示它们,并将它们的坐标放入一个数组中
int sharp[20][8] = {
{0,0,0,0,0,0,0,0},
//I形
{0,0,0,1,0,2,0,3},
{0,0,1,0,2,0,3,0},
//■形
{0,0,1,0,0,1,1,1},
//L形
{0,0,0,1,0,2,1,2},
{0,0,0,1,1,0,2,0},
{0,0,1,0,1,1,1,2},
{0,1,1,1,2,0,2,1},
//J形
{0,2,1,0,1,1,1,2},
{0,0,0,1,1,1,2,1},
{0,0,0,1,0,2,1,0},
{0,0,1,0,2,0,2,1},
//Z形
{0,0,1,0,1,1,2,1},
{0,1,0,2,1,0,1,1},
//S形
{0,1,1,0,1,1,2,0},
{0,0,0,1,1,1,1,2},
//T形
{0,1,1,0,1,1,2,1},
{0,0,0,1,0,2,1,1},
{0,0,1,0,1,1,2,0},
{0,1,1,0,1,1,1,2}
};
每排第偶数个数据就是横坐标数据,奇数个数据就是纵坐标数据,第0排全是0是方便后续编程用的
有了图形的坐标数据,接下来就是将其绘制在屏幕上
void Game::sharpDraw(int id, bool show)
{
int x, y;
if (show == true)
{
if (id > 0)
{
for (int i = 0; i < 4; i++)
{
x = 19 + sharp[id][2 * i];
y = 6 + sharp[id][2 * i + 1];
SetPos(2 * x, y);
cout << "■";
}
}
else
{
for (int i = 0; i < 4; i++)
{
x = 19 + sharp[-id][2 * i];
y = 6 + sharp[-id][2 * i + 1];
SetPos(2 * x, y);
cout << " ";
}
}
return;
}
if (id > 0)
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
SetPos(2 * x, y);
cout << "■";
}
}
else
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[- id][2 * i];
y = _y + sharp[- id][2 * i + 1];
SetPos(2 * x, y);
cout << " ";
}
}
return;
}
参数id是图形在sharp中的排数,参数show为true时就是绘制下一个将要出现的图形,为false就是绘制当前需要操作的图形,id为负数时就是擦除这个id代表的图形,为的是刷新这个图形的位置,_x,_y是一个全局变量,控制图形出现的位置和当前位置。
有了图形,就可以建立移动函数,因为要限制范围,所以这个函数是用来判断图形是否可以移动到下一个位置,返回一个布尔类的值
bool Game:: move(int dir,int id)
{
int x, y;
switch (dir)
{
case UP:
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
if (map[y][x] == 1)
{
return false;
}
}
break;
case DOWN:
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
if (map[y + 1][x] == 1)
{
return false;
}
}
}
break;
case RIGHT:
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
if (map[y][x + 1] == 1)
{
return false;
}
}
}
break;
case LEFT:
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
if (map[y][x - 1] == 1)
{
return false;
}
}
}
break;
default:
break;
}
return true;
}
第一个参数是移动方向,这里用枚举表示
enum DIR
{
UP,
RIGHT,
DOWN,
LEFT
};
方向向上是旋转命令,它只是改变了图形的id但并没有移动,所以对旋转后的图形进行检测即可,向下需要检测y+1位置的map是否为1,其他方向同理。
有了检测函数,就可以真正的进行键盘操作了
void Game:: keyControl()
{
if (!_kbhit())
return;
int key = _getch();
switch (key)
{
case 72:
Turn(_id);
break;
case 80:
if (move(DOWN, _id))
{
sharpDraw(-_id);
_y++;
}
break;
case 75:
if (move(LEFT, _id))
{
sharpDraw(-_id);
_x --;
}
break;
case 77:
if (move(RIGHT, _id))
{
sharpDraw(-_id);
_x ++;
}
break;
case 32:
{
for (int i = 5; i < 15; i++)
{
SetPos(1, i);
cout << " " << endl;
}
SetPos(10, 7);
cout << "游 戏 暂 停" << endl;
SetPos(3, 10);
cout << "0 返回菜单 回车 继续游戏" << endl;
while (1)
{
int select = _getch();
if (select == 13)
{
for (int i = 5; i < 15; i++)
{
SetPos(1, i);
cout << " " << endl;
}
break;
}
else if (select == 48)
{
system("cls");
showMenu();
}
}
}
default:
break;
}
}
_kbhit()函数是检测用户是否输入了键值,因为我们的游戏不能让程序停留在_getch()函数这里等待我们输入,而是进行实时操控,当然这里也可以用GetAsyncKeyState()函数来完成,参数为键的宏名,可以实现两个函数的效果
用key来获取_getch到的键值,上为72,下为80,左为75,右为77,空格为32
每个方向如果检测为真就在相应方向坐标进行加减,暂停同样的建立一个死循环等待用户输入键值。向上为旋转,那就加入旋转函数
void Game:: Turn(int id)
{
switch (id)
{
case 1:id++; break;
case 2:id--; break;
case 3: break;
case 4:id++; break;
case 5:id++; break;
case 6:id++; break;
case 7:id -= 3; break;
case 8:id++; break;
case 9:id++; break;
case 10:id++; break;
case 11:id -= 3; break;
case 12:id++; break;
case 13:id--; break;
case 14:id++; break;
case 15:id--; break;
case 16:id++; break;
case 17:id++; break;
case 18:id++; break;
case 19:id -= 3; break;
default:
break;
}
if (!move(UP, id))
{
return;
}
sharpDraw(-_id);
_id = id;
}
对不同图形进行的旋转也不同,先改变局部变量id,对这个id进行检测,如果可行就把局部变量赋值给成员变量_id,实现旋转。
移动实现了,接下来就是实现图形落到底的函数了
bool Game:: downSet(int id)
{
if (id == 0)
return true;
finish = clock();
if (finish - start < speed)
{
return false;
}
start = clock();
if (!move(DOWN,_id))
{
int x, y;
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
map[y][x] = 1;
if (y < top)
{
top = y;
}
if (top <= 1)
{
gameOver();
}
}
_x = 6;
_y = 1;
return true;
}
sharpDraw(-id);
_y++;
sharpDraw(id);
return false;
}
这里用到了时间函数,为的是控制图形下落速度,即每隔多少毫秒下落一格,start和finish都是time_t类的全局变量,speed是控制下落间隔的成员函数,使用clock()函数计时,以实现毫秒级控制。实现过程是当程序判断图形可以下落时使start重新计时,不可以时一直刷新finish,直到满足判断式
在时间上判断可以下落后,还要判断图形是否已经到底,这里用之前创建的move函数检测方向为DOWN是否可以执行,如果不能下落,说明已经到底,将该图形对应坐标的map值赋为1,然后将成员top更新,再将_x,_y重置,返回true以表示到底
如果没到底就让图形下降一格
最后是消行函数
void Game:: clean()
{
int n = -1;
int line = -1;
while (1)
{
for (int i = 28; i > 0; i--)
{
for (int j = 1; j < 15; j++)
{
line = i;
if (map[i][j] == 0)
{
line = -1;
break;
}
}
if (line != -1)
break;
}
if (line == -1)
break;
for (int i = line; i > 0; i--)
{
for (int j = 1; j < 15; j++)
{
if (i == 1)
map[i][j] = 0;
else
{
map[i][j] = map[i - 1][j];
SetPos(2 * j, i);
if (map[i][j] == 1)
cout << "■";
else
cout << " ";
}
}
}
top++;
n++;
}
if (n >= 0)
{
score += n * n * 100 + 100;
if (speed > 100)
speed = 1000 - score / 10;
}
}
n用来根据一次消了几行来计算得分,line用来定位消除的是哪一行。这里从下到上依次检测map每行是否都是1,如果出现一个0就说明没满,需要将line重新赋为-1,继续向上检测,一旦有某一行满了,line就不是-1,跳出循环,开始消行
消行就是将该行上面的各行都向下移动一行,然后将第1行赋0,并根据map重新绘制游戏界面来覆盖消行前的界面,最后将高度下移一格
函数齐全了就可以执行游戏运行函数了
void Game::Run()
{
score = 0;//重置各成员变量
_id = 0;
top = 58;
_x = 6;
_y = 1;
showGround();//游戏场景建立
start = clock();//开始计时
int new_id = rand() % 19 + 1;//随机生成下一个图形
while (1)
{
sharpDraw(_id); //绘制图形
keyControl();//键盘操作
if (downSet(_id))//图形到底后执行
{
sharpDraw(-new_id, 1);//清除右侧的下一个图形,准备绘制新的图形
_id = new_id;//更换图形
new_id = rand() % 19 + 1;//随机生成下一个图形
sharpDraw(new_id, 1);//在右侧绘制下一个图形
clean();//检测消行
}
SetPos(34, 20);
cout << score << endl;//输出分数
}
}
完整代码如下
#include
#include
#include
#include
#include
using namespace std;
enum DIR
{
UP,
RIGHT,
DOWN,
LEFT
};
time_t start = 0, finish = 0;
int _x = 6, _y = 1;//图形生成位置
int map[30][16] = { 0 };
int sharp[20][8] = {
{0,0,0,0,0,0,0,0},
//I形
{0,0,0,1,0,2,0,3},
{0,0,1,0,2,0,3,0},
//■形
{0,0,1,0,0,1,1,1},
//L形
{0,0,0,1,0,2,1,2},
{0,0,0,1,1,0,2,0},
{0,0,1,0,1,1,1,2},
{0,1,1,1,2,0,2,1},
//J形
{0,2,1,0,1,1,1,2},
{0,0,0,1,1,1,2,1},
{0,0,0,1,0,2,1,0},
{0,0,1,0,2,0,2,1},
//Z形
{0,0,1,0,1,1,2,1},
{0,1,0,2,1,0,1,1},
//S形
{0,1,1,0,1,1,2,0},
{0,0,0,1,1,1,1,2},
//T形
{0,1,1,0,1,1,2,1},
{0,0,0,1,0,2,1,1},
{0,0,1,0,1,1,2,0},
{0,1,1,0,1,1,1,2}
};
class Game
{
public:
int score;//游戏分数
int _id;//图形编号
int top;//最高点高度
int speed;//下落速度
Game();
void showMenu();//显示菜单
void showGround();//显示游戏界面
void gameOver();//游戏结束界面
void Run();//运行游戏
void sharpDraw(int id,bool show = false);//绘制图形
void keyControl();//键盘控制
bool move(int dir,int id);//移动判断
bool downSet(int id);//下落
void Turn(int id);//旋转
void clean();//消行
};
void SetPos(int i, int j)//控制光标位置, 列, 行
{
COORD pos = { i,j };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
int main()
{
CONSOLE_CURSOR_INFO cursor;
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
cursor.bVisible = 0; //这四行用来设置光标不显示
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
srand((unsigned)time(NULL));
Game game;
game.showMenu();
return 0;
}
Game::Game()
{
score = 0;
_id = 0;
top = 58;
speed = 1000;
}
void Game::showMenu()
{
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 26; j++)
{
if ((i == 0 || i == 29) || (j == 0 || j == 25))
{
cout << "■";
}
else
{
cout << " ";
}
}
cout << endl;
}
SetPos(17, 8);
cout << "俄 罗 斯 方 块" << endl;
SetPos(13, 12);
cout << "↑旋转方块 ↓加速下滑" << endl;
SetPos(12, 14);
cout << "← →左右移动 空格 暂停" << endl;
SetPos(15, 20);
cout << "0 退出 Enter 开始" << endl;
while (1)
{
int select = _getch();
if (select == 13)
{
system("cls");
this->Run();
}
else if (select = 48)
{
system("cls");
exit(0);
}
}
}
void Game:: showGround()
{
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 26; j++)
{
if ((i == 0 || i == 29) || (j == 0 || j == 25 || j==15))
{
cout << "■";
}
else if (i == 15 && j > 15)
{
cout << "■";
}
else
{
cout << " ";
}
}
cout << endl;
}
SetPos(31, 2);
cout << "下 个图形" << endl;
SetPos(31, 17);
cout << "当 前得分" << endl;
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 16; j++)
{
if ((i == 0 || i == 29) || (j == 0 || j == 15))
{
map[i][j] = 1;
}
else
{
map[i][j] = 0;
}
}
}
}
void Game:: gameOver()
{
for (int i = 5; i < 15; i++)
{
SetPos(1, i);
cout << " " << endl;
}
SetPos(8, 7);
cout << "G a m e O v e r" << endl;
SetPos(3, 10);
cout << "0 退出 Enter 重新开始" << endl;
while (1)
{
int select = _getch();
if (select == 13)
{
system("cls");
this->Run();
}
else if (select == 48)
{
system("cls");
exit(0);
}
}
}
void Game::Run()
{
score = 0;
_id = 0;
top = 58;
_x = 6;
_y = 1;
showGround();
start = clock();
int new_id = rand() % 19 + 1;
while (1)
{
sharpDraw(_id);
keyControl();
if (downSet(_id))
{
sharpDraw(-new_id, 1);
_id = new_id;
new_id = rand() % 19 + 1;
sharpDraw(new_id, 1);
clean();
}
SetPos(34, 20);
cout << score << endl;
}
}
void Game::sharpDraw(int id, bool show)
{
int x, y;
if (show == true)
{
if (id > 0)
{
for (int i = 0; i < 4; i++)
{
x = 19 + sharp[id][2 * i];
y = 6 + sharp[id][2 * i + 1];
SetPos(2 * x, y);
cout << "■";
}
}
else
{
for (int i = 0; i < 4; i++)
{
x = 19 + sharp[-id][2 * i];
y = 6 + sharp[-id][2 * i + 1];
SetPos(2 * x, y);
cout << " ";
}
}
return;
}
if (id > 0)
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
SetPos(2 * x, y);
cout << "■";
}
}
else
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[- id][2 * i];
y = _y + sharp[- id][2 * i + 1];
SetPos(2 * x, y);
cout << " ";
}
}
return;
}
bool Game:: downSet(int id)
{
if (id == 0)
return true;
finish = clock();
if (finish - start < speed)
{
return false;
}
start = clock();
if (!move(DOWN,_id))
{
int x, y;
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
map[y][x] = 1;
if (y < top)
{
top = y;
}
if (top <= 1)
{
gameOver();
}
}
_x = 6;
_y = 1;
return true;
}
sharpDraw(-id);
_y++;
sharpDraw(id);
return false;
}
bool Game:: move(int dir,int id)
{
int x, y;
switch (dir)
{
case UP:
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
if (map[y][x] == 1)
{
return false;
}
}
break;
case DOWN:
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
if (map[y + 1][x] == 1)
{
return false;
}
}
}
break;
case RIGHT:
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
if (map[y][x + 1] == 1)
{
return false;
}
}
}
break;
case LEFT:
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
if (map[y][x - 1] == 1)
{
return false;
}
}
}
break;
default:
break;
}
return true;
}
void Game:: Turn(int id)
{
switch (id)
{
case 1:id++; break;
case 2:id--; break;
case 3: break;
case 4:id++; break;
case 5:id++; break;
case 6:id++; break;
case 7:id -= 3; break;
case 8:id++; break;
case 9:id++; break;
case 10:id++; break;
case 11:id -= 3; break;
case 12:id++; break;
case 13:id--; break;
case 14:id++; break;
case 15:id--; break;
case 16:id++; break;
case 17:id++; break;
case 18:id++; break;
case 19:id -= 3; break;
default:
break;
}
if (!move(UP, id))
{
return;
}
sharpDraw(-_id);
_id = id;
}
void Game:: keyControl()
{
if (!_kbhit())
return;
int key = _getch();
switch (key)
{
case 72:
Turn(_id);
break;
case 80:
if (move(DOWN, _id))
{
sharpDraw(-_id);
_y++;
}
break;
case 75:
if (move(LEFT, _id))
{
sharpDraw(-_id);
_x --;
}
break;
case 77:
if (move(RIGHT, _id))
{
sharpDraw(-_id);
_x ++;
}
break;
case 32:
{
for (int i = 5; i < 15; i++)
{
SetPos(1, i);
cout << " " << endl;
}
SetPos(10, 7);
cout << "游 戏 暂 停" << endl;
SetPos(3, 10);
cout << "0 返回菜单 回车 继续游戏" << endl;
while (1)
{
int select = _getch();
if (select == 13)
{
for (int i = 5; i < 15; i++)
{
SetPos(1, i);
cout << " " << endl;
}
break;
}
else if (select == 48)
{
system("cls");
showMenu();
}
}
}
default:
break;
}
}
void Game:: clean()
{
int n = -1;
int line = -1;
while (1)
{
for (int i = 28; i > 0; i--)
{
for (int j = 1; j < 15; j++)
{
line = i;
if (map[i][j] == 0)
{
line = -1;
break;
}
}
if (line != -1)
break;
}
if (line == -1)
break;
for (int i = line; i > 0; i--)
{
for (int j = 1; j < 15; j++)
{
if (i == 1)
map[i][j] = 0;
else
{
map[i][j] = map[i - 1][j];
SetPos(2 * j, i);
if (map[i][j] == 1)
cout << "■";
else
cout << " ";
}
}
}
top++;
n++;
}
if (n >= 0)
{
score += n * n * 100 + 100;
if (speed > 100)
speed = 1000 - score / 10;
}
}