구현기능
실행화면
메인코드
//main.cpp
#include <iostream>
#include <vector>
#include <conio.h>
#include <windows.h>
#include "game.h"
#include "system.h"
#define TABLE_X 12
#define TABLE_Y 20
using namespace std;
int main() {
CursorView(false); //커서 안보이게하기
system("mode con cols=100 lines=40 | title C++ Tetris"); //상태창 설정
GameSet gt(TABLE_X, TABLE_Y);//게임 테이블 설정
MainMenu();//메인메뉴 출력
gt.stack = 0;
while (1) {
gt.blockSet();//출력될 블록들 설정
while (gt.stack != 1) {
system("cls");//화면초기화
gt.blockDraw();//블록 위치 설정
gt.tableDraw();//설정된 값을 이용해 게임화면을 그리기
gt.down();//블록 위치를 한칸 아래로 내림
Sleep(200);
}
}
system("pause>null");
return 0;
}
Game 클래스
헤더
//game.h
#pragma once
#include<vector>
using namespace std;
//메인메뉴 출력 클래스
class MainMenu
{
public:
MainMenu();
};
//게임진행 클래스
class GameSet {
private:
int x, y;
int ax, ay;
vector<vector<int>> table;
int blockselect;
int blockList[7];
int shape[4][4][4];
int rotation;
public:
GameSet(int x, int y);
void tableDraw();
int stack;
void blockSet();
void blockSelect(int blockselect);
void blockDraw();
void down();
};
코드
//game.cpp
#include "game.h"
#include "system.h"
#include <iostream>
#include <cstdlib>
using namespace std;
/*
* 테트리스 블록 선언
*/
const int Block_I[4][4][4] = { // I블록
{
{0, 0, 0, 0},
{2, 2, 2, 2},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{0, 0, 2, 0},
{0, 0, 2, 0},
{0, 0, 2, 0},
{0, 0, 2, 0}
},
{
{0, 0, 0, 0},
{0, 0, 0, 0},
{2, 2, 2, 2},
{0, 0, 0, 0}
},
{
{0, 2, 0, 0},
{0, 2, 0, 0},
{0, 2, 0, 0},
{0, 2, 0, 0}
},
};
const int Block_L[4][4][4] = { //L블록
{
{0, 2, 0, 0},
{0, 2, 0, 0},
{0, 2, 2, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 2, 2, 2},
{0, 2, 0, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 2, 2, 0},
{0, 0, 2, 0},
{0, 0, 2, 0}
},
{
{0, 0, 0, 0},
{0, 0, 2, 0},
{2, 2, 2, 0},
{0, 0, 0, 0}
},
};
const int Block_J[4][4][4] = { //J블록
{
{0, 0, 2, 0},
{0, 0, 2, 0},
{0, 2, 2, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 2, 0, 0},
{0, 2, 2, 2},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 2, 2, 0},
{0, 2, 0, 0},
{0, 2, 0, 0}
},
{
{0, 0, 0, 0},
{2, 2, 2, 0},
{0, 0, 2, 0},
{0, 0, 0, 0}
},
};
const int Block_Z[4][4][4] = { //Z블록
{
{0, 0, 0, 0},
{0, 2, 0, 0},
{0, 2, 2, 0},
{0, 0, 2, 0}
},
{
{0, 0, 0, 0},
{0, 2, 2, 0},
{2, 2, 0, 0},
{0, 0, 0, 0}
},
{
{0, 0, 2, 0},
{0, 0, 2, 2},
{0, 0, 0, 2},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 2, 2},
{0, 2, 2, 0}
},
};
const int Block_S[4][4][4] = {//S블록
{
{0, 0, 0, 0},
{0, 0, 2, 0},
{0, 2, 2, 0},
{0, 2, 0, 0}
},
{
{0, 0, 0, 0},
{2, 2, 0, 0},
{0, 2, 2, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 2},
{0, 0, 2, 2},
{0, 0, 2, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 2, 2, 0},
{0, 0, 2, 2}
},
};
const int Block_O[4][4][4] = {//O블록
{
{0, 0, 0, 0},
{0, 2, 2, 0},
{0, 2, 2, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 2, 2, 0},
{0, 2, 2, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 2, 2, 0},
{0, 2, 2, 0},
{0, 0, 0, 0}
},
{
{0, 0, 0, 0},
{0, 2, 2, 0},
{0, 2, 2, 0},
{0, 0, 0, 0}
},
};
const int Block_T[4][4][4] = {//T블록
{
{0, 0, 0, 0},
{0, 2, 2, 2},
{0, 0, 2, 0},
{0, 0, 0, 0}
},
{
{0, 0, 2, 0},
{0, 2, 2, 0},
{0, 0, 2, 0},
{0, 0, 0, 0}
},
{
{0, 0, 2, 0},
{0, 2, 2, 2},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
{
{0, 0, 2, 0},
{0, 0, 2, 2},
{0, 0, 2, 0},
{0, 0, 0, 0}
},
};
//메인메뉴 출력
MainMenu::MainMenu() {
cout << "\n\n\n\n";
cout << "\t\t"; cout << "############ ######### ########### ######## # ###########\n";
cout << "\t\t"; cout << " # # # # # # # \n";
cout << "\t\t"; cout << " # # # # # # # \n";
cout << "\t\t"; cout << " # ######### # # # # ###########\n";
cout << "\t\t"; cout << " # # # # # # # #\n";
cout << "\t\t"; cout << " # # # # # # #\n";
cout << "\t\t"; cout << " # ######### # # # # ###########\n\n\n\n\n";
cout << "\t\t"; cout << " 게임을 시작하려면 아무키나 누르세요.\n\n\n\n\n\n\n";
system("pause>null");
system("cls");
}
//게임배경 설정
GameSet::GameSet(int x, int y) {
this->x = x;
this->y = y;
for (int i = 0; i < y; i++) {
vector<int> temp;
for (int j = 0; j < x; j++) {
temp.push_back(0);
}
table.push_back(temp);
}
for (int i = 0; i < x; i++) {
table[0][i] = 1;
table[y - 1][i] = 1;
}
for (int i = 1; i < y - 1; i++) {
table[i][0] = 1;
table[i][x - 1] = 1;
}
}
//게임화면 출력 및 업데이트
void GameSet::tableDraw() {
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
if (table[i][j] == 1)cout << "▦";
else if (table[i][j] == 2) { // 블록을 실시간으로 이동시키는기능
cout << "■";
if (stack == 1)table[i][j] = 3;
else table[i][j] = 0;
}
else if (table[i][j] == 3) { // 블록 쌓이는기능
cout << "■";
}
else cout << " ";
}
cout << "\n";
}
}
//출력될 블록 세팅
void GameSet::blockSet() {
stack = 0;
ax = 4;
ay = 1;
rotation = 0;
blockList[0] = 1;
blockSelect(blockList[0]);
}
//블록 선택
void GameSet::blockSelect(int blockList) {
if (blockList == 1) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
shape[i][j][k] = Block_L[i][j][k];
}
}
}
}
}
//블록을 화면상 출력하기위해 세팅
void GameSet::blockDraw() {
int bx = ax;
int by = ay;
int cy = 0;
for (int i = 0; i < 4; i++) {
if (stack == 1)break;
for (int j = 0; j < 4; j++) {
if (shape[rotation][i][j] == 2) {
cy = by + 1;
if(table[cy][bx] == 1 || table[cy][bx] == 3)stack = 1; //블록이 벽이나 다른 블록에 부딪히면 멈춤
table[by][bx] = 2;
}
bx += 1;
}
by += 1;
bx = ax;
}
}
//블록을 한칸 아래로 내림
void GameSet::down() {
ay += 1;
}
시스템
헤더
//system.h
#pragma once
void CursorView(char show); // 커서 설정
void gotoxy(int x, int y); // 커서 위치 설정
코드
//system.cpp
#include <iostream>
#include <windows.h>
#include "system.h"
using namespace std;
//커서 설정
void CursorView(char show) {
HANDLE hConsole;
CONSOLE_CURSOR_INFO ConsoleCursor;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
ConsoleCursor.bVisible = show;
ConsoleCursor.dwSize = 1;
SetConsoleCursorInfo(hConsole, &ConsoleCursor);
}
//커서 위치 지정
void gotoxy(int x, int y) {
COORD pos = { x,y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}