1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
#include <windows.h>
#include <conio.h>
#include <iostream>
using std::cout;
using std::endl;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
class Bads
{
public:
Bads(int x = 0, int y = 0)
: X(x), Y(y)
{ }
int X, Y;
Bads operator + (const Bads &right) const
{
return Bads(X + right.X, Y + right.Y);
}
};
const char *TITLE = "########## THE TETRIS ###########";
const char *SCORE_FILE = "c:\\tetrisScore.txt";
const char *FAIL_STR = "GAME OVER!";
const int FIELD_WIDTH = 14, FIELD_HEIGHT = 19;
const int HORIZONTAL_DIVIDE = 4, VERTICAL_DIVIDE = 20;
const int BOARD_WIDTH = 35, BOARD_HEIGHT = FIELD_HEIGHT + HORIZONTAL_DIVIDE + 1;
const Bads FIELD_START((VERTICAL_DIVIDE - FIELD_WIDTH)/2,HORIZONTAL_DIVIDE + 1);
const Bads NEXT_PIECE_POS(VERTICAL_DIVIDE + (BOARD_WIDTH - VERTICAL_DIVIDE)/2,HORIZONTAL_DIVIDE + 4);
const Bads SCORE_POS(VERTICAL_DIVIDE + 2,NEXT_PIECE_POS.Y + 4);
char field[FIELD_HEIGHT * FIELD_WIDTH];
bool exiting = false;
bool downHeld = false;
int score = 0, highScore = 0 ;
void welcome (){
system("color b1");
cout<<"\n\n\n\t\t _____ _ _____ _____ (_) ___ \n";
system("color bd");
Sleep(200);
cout<<"\t\t|_ _| |_ ___ |_ _|___|_ _|/\\_ _ ( _) \n";
system("color bc");
Sleep(200);
cout<<"\t\t | | | ' \\/ -_) | | \/ -_) | | | __|| |_\\ \\ \n";
system("color b0");
Sleep(200);
cout<<"\t\t |_| |_||_\\___| |_| \\___| |_| |_| |_|(___) \n\n";
system("color b1");
Sleep(200);
cout<<"\t\t\tThe Tetris\n";
system("color b3");
Sleep(500);
cout<<"\t\t\tby: John Singson \n";
system("color b1");
Sleep(500);
cout<<"\t\t\tPress any key to continue. . .";
getch();
system("cls");
}
void Reset();
char &GetField(const Bads &pos)
{
return field[pos.Y*FIELD_WIDTH + pos.X];
}
char &GetField(int x, int y)
{
return field[y*FIELD_WIDTH + x];
}
void SetConsoleCursor(const Bads &pos)
{
COORD c = {(short)pos.X, (short)pos.Y};
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition( h, c );
}
class Piece
{
public:
Piece()
{ }
Piece(const Bads &b1, const Bads &b2, const Bads &b3, const Bads &b4)
{
Blocks[0] = b1;
Blocks[1] = b2;
Blocks[2] = b3;
Blocks[3] = b4;
}
void SetPosition(const Bads &pos)
{
Position = pos;
}
Bads &GetPosition()
{
return Position;
}
bool Step()
{
++Position.Y;
if (Collide())
{
--Position.Y;
Freeze();
return true;
}
return false;
}
void Rotate()
{
for (int i = 0; i < 4; ++i)
{
Bads pos = Blocks[i];
Blocks[i] = Bads(-pos.Y, pos.X);
}
if (Collide())
{
UnRotate();
}
}
void UnRotate()
{
for (int i = 0; i < 4; ++i)
{
Bads pos = Blocks[i];
Blocks[i] = Bads(-pos.Y, pos.X);
}
}
void Draw(const Bads &fieldStart, bool clamp = true, char character = 'O')
{
for (int i = 0; i < 4; ++i)
{
Bads pos = Position + Blocks[i];
if (pos.Y >= 0 || !clamp)
{
SetConsoleCursor(fieldStart + pos);
cout<<character;
}
}
}
void UnDraw(const Bads &fieldStart, bool clamp = true)
{
Draw(fieldStart, clamp, ' ');
}
bool Collide()
{
for (int i = 0; i < 4; ++i)
{
Bads pos = Position + Blocks[i];
if (pos.X < 0 || pos.X >= FIELD_WIDTH || (pos.Y >= 0 && GetField(pos) == '#'))
return true;
}
return false;
}
private:
void Freeze()
{
for (int i = 0; i < 4; ++i)
GetField(Position + Blocks[i]) = '#';
}
Bads Position;
Bads Blocks[6];
};
const int PIECE_COUNT = 7;
Piece pieces[PIECE_COUNT];
Piece activePiece, nextPiece;
void SetupPieces()
{
pieces[0] = Piece(Bads(0,2), Bads(0,1), Bads(0,0), Bads(0,-1));
pieces[1] = Piece(Bads(0,0), Bads(0,1), Bads(1,0), Bads(1,1));
pieces[2] = Piece(Bads(-1,0), Bads(0,0), Bads(1,0), Bads(0,-1));
pieces[3] = Piece(Bads(-1,1), Bads(-1,0), Bads(0,0), Bads(0,-1));
pieces[4] = Piece(Bads(1,1), Bads(1,0), Bads(0,0), Bads(0,-1));
pieces[5] = Piece(Bads(1,1), Bads(1,0), Bads(0,0), Bads(-1,0));
pieces[6] = Piece(Bads(-1,1), Bads(1,0), Bads(0,0), Bads(-1,0));
}
int max(int a, int b)
{
return a>b ? a : b;
}
int RandInt(int low, int high)
{
int range = high - low;
return low + rand() % range;
}
void LoadHighScore()
{
FILE *file = fopen(SCORE_FILE, "r");
if (file)
{
fscanf(file, "%i", &highScore);
fclose(file);
}
}
void SaveHighScore()
{
FILE *file = fopen(SCORE_FILE, "w");
if (file)
{
fprintf(file, "%i", highScore);
fclose(file);
}
}
void Fail()
{
int failLen = (int)strlen(FAIL_STR);
Bads pos = FIELD_START + Bads((FIELD_WIDTH - failLen)/2,FIELD_HEIGHT/2);
SetConsoleCursor(pos);
cout<<FAIL_STR;
while (GetKeyState(VK_SPACE) > -100 && GetKeyState(VK_RETURN) > -100)
{}
highScore = max(highScore, score);
SaveHighScore();
Reset();
}
|