#include <iostream>
#include <Windows.h>
#include <conio.h>
using namespace std;
void cursor_move(int x, int y);
void frame();
void produce_diamonds(int x);
void body();
void move();
void line_delete();
bool b[15][10] = { };
bool judge[15] = { };
char cleck = 0;
int point = 8;
int line = 3;
int timer = 0;
int score = 0;
char a;
int main()
{
srand(time(0));
for (int i = 0; i <= 9; i++)
{
b[0][i] = 1;
}
frame();
cursor_move(30, 7);
cout << "得分:";
cursor_move(8, 14);
cout << "■";
produce_diamonds(1);
produce_diamonds(2);
produce_diamonds(3);
body();
}
void cursor_move(int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(HANDLE(GetStdHandle(STD_OUTPUT_HANDLE)), pos);
CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void frame()
{
SetConsoleTextAttribute(HANDLE(GetStdHandle(STD_OUTPUT_HANDLE)), 11);
for (int i = 0; i <= 15; i++)
{
for (int j = 0; j <= 20; j += 2)
{
if (i == 0 || i == 15 || j == 0 || j == 20)
{
cursor_move(j, i);
cout << "■";
}
}
}
}
void produce_diamonds(int x)
{
for (int i = 2; i <= 18; i += 2)
{
if (rand() % 3 < 2)
{
cursor_move(i, x);
b[x][i/2] = 1;
cout << "■";
}
}
}
void body()
{
while (1)
{
if (_kbhit())
{
cleck = _getch();
move();
}
cursor_move(36, 7);
cout << score;
timer++;
if (timer == 10)
{
line++;
produce_diamonds(line);
timer = 0;
}
line_delete();
if (line == 14)
{
cursor_move(40, 7);
cout << "菜的抠脚";
a = _getch();
cursor_move(0, 17);
break;
}
Sleep(20);
}
}
void move()
{
switch ((int)cleck)
{
case 77:
point += 2;
if (point == 20)
{
point = 18;
break;
}
cursor_move(point - 2, 14);
cout << " ";
cursor_move(point, 14);
cout << "■";
break;
case 75:
point -= 2;
if (point == 0)
{
point = 2;
break;
}
cursor_move(point + 2, 14);
cout << " ";
cursor_move(point, 14);
cout << "■";
break;
case 32:
for (int i = line; i >= 0; i--)
{
if (b[i][point/2])
{
cursor_move(point, i + 1);
cout << "■";
b[i + 1][point/2] = 1;
if (i == line)
{
line++;
}
break;
}
}
break;
}
}
void line_delete()
{
int sum = 0;
for (int i = 1; i <= line; i++)
{
for (int j = 1; j <= 9; j ++)
{
if (b[i][j])
{
sum++;
}
}
if (sum == 9)
{
judge[i] = true;
}
sum = 0;
}
for (int i = 1; i <= line; i++)
{
if (judge[i])
{
judge[i] = false;
for (int j = i; j <= line; j++)
{
for (int k = 2; k <= 18; k += 2)
{
cursor_move(k, j);
cout << " ";
b[j][k/2] = 0;
if (b[j + 1][k/2])
{
cursor_move(k, j);
cout << "■";
b[j][k/2] = 1;
}
}
}
line--;
score++;
break;
}
}
}