/*
Author : 周天涯
email : [email protected]
blog : http://www.cppblog.com/menjitianya/
Description : 即興創作,《C控制台 俄羅斯方塊》,歡迎交流與探討,直接將代碼粘貼到VC6.0的環境下即可運行。
← 左移
→ 右移
↓ 加速
↑ 旋轉
連續消去1行得1分、2行得3分、3行得5分、4行得7分。
積分達到一定程度,會有換命的活動,命最多6條。
難度會隨積分的上升逐漸上升,最多到6的難度。
*/
#include <iostream>
#include <windows.h>
#include <vector>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
using namespace std;
#define GameW 10
#define GameH 20
const int CtrlLeft = GameW*2+4 + 3;
struct Point {
Point(){}
Point(int x, int y) {_x = x, _y = y;}
int _x, _y;
};
HANDLE g_hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE g_hInput = GetStdHandle(STD_INPUT_HANDLE);
Point g_ptCursor(0,0);
BOOL isChecking = FALSE;
BOOL g_bGameOver = FALSE;
int g_nGameBack[GameH][GameW], Case;
int nowKeyInfo = -1;
int g_nDiff = 1;
int g_nLife = 2;
int g_nScore = 0;
void SetCursor(COORD cd) {
SetConsoleCursorPosition(g_hOutput, cd);
}
void SetCursor(int x, int y){
COORD cd = {x, y};
SetCursor(cd);
}
void SetBlockCursor(int x, int y){
COORD cd = {2*x + 2, y + 1};
SetCursor(cd);
}
void SetBack(int x, int y, BOOL bk) {
SetBlockCursor(x, y);
if (bk)
printf("%s", "■");
else
printf(" ");
}
bool Out(int x, int y) {
return x < 0 || y < 0 || x >= GameW || y >= GameH;
}
struct xBlock {
public:
int len;
int nowRotateID;
BOOL mask[4][4][4];
static vector <xBlock> List;
xBlock() { len = 0; }
xBlock(int l, char *str) {
int i, j, k;
len = l;
memset(mask, FALSE, sizeof(mask));
for(i = 0; i < l; i++) {
for(j = 0; j < l; j++) {
mask[0][i][j] = str[i*l + j] - 0;
}
}
for(k = 1; k < 4; k++) {
for(i = 0; i < len; i++) {
for(j = 0; j < len; j++) {
mask[k][i][j] = mask[k-1][j][len-1-i];
}
}
}
nowRotateID = rand() % 4;
}
void rotate() {
nowRotateID ++;
if (nowRotateID >= 4)
nowRotateID = 0;
}
BOOL getUnit(int x, int y, int roID) {
if (roID == -1) {
roID = nowRotateID;
}
return mask[roID][y][x];
}
};
vector <xBlock> xBlock::List;
class Block {
public:
int x, y;
int ID;
xBlock bk;
void reset(xBlock *pbk) {
bk = *pbk;
x = 4, y = 0;
ID = ++ Case;
if(collide(0,0)) {
lifeDown();
}
draw();
*pbk = xBlock::List[rand() % xBlock::List.size()];
}
void lifeDown() {
int i, j;
for(i = 0; i < GameH; i++) {
for(j = 0; j < GameW; j++) {
SetBack(j, i, TRUE);
Sleep(10);
}
}
if(g_nLife) {
g_nLife --;
for(i = g_nLife; i < 6; i++) {
SetCursor(CtrlLeft + i, 15);
printf("%c", );
}
for(i = GameH-1; i >= 0; i--) {
for(j = GameW-1; j >= 0; j--) {
SetBack(j, i, FALSE);
Sleep(10);
g_nGameBack[i][j] = 0;
}
}
}else {
g_bGameOver = TRUE;
}
}
void erase() {
int i, j;
for(i = 0; i < bk.len; i++) {
for(j = 0; j <