MainMenu Class
MainMenu 클래스에서는 게임 시작 화면 출력 기능을 넣었다.
class MainMenu
{
public:
MainMenu()
{
std::cout << "\n\n\n\n";
std::cout << "\t\t"; std::cout << "▦▦▦▦▦▦ ▦▦▦▦▦ ▦▦▦▦▦ ▦▦▦▦▦ ▦▦▦ ▦▦▦▦▦\n";
std::cout << "\t\t"; std::cout << " ▦▦ ▦ ▦ ▦ ▦ ▦ ▦ \n";
std::cout << "\t\t"; std::cout << " ▦▦ ▦ ▦ ▦ ▦ ▦ ▦ \n";
std::cout << "\t\t"; std::cout << " ▦▦ ▦▦▦▦▦ ▦ ▦▦▦▦ ▦ ▦▦▦▦▦\n";
std::cout << "\t\t"; std::cout << " ▦▦ ▦ ▦ ▦ ▦▦ ▦ ▦\n";
std::cout << "\t\t"; std::cout << " ▦▦ ▦ ▦ ▦ ▦ ▦ ▦\n";
std::cout << "\t\t"; std::cout << " ▦▦ ▦▦▦▦▦ ▦ ▦ ▦ ▦ ▦▦▦▦▦\n";
std::cout << "\t\t"; std::cout << " Press Any ket to start... \n";
std::cout << "\t\t"; std::cout << " TetrisGame 1.0 \n";
std::cout << "\t\t"; std::cout << "\n";
getchar(); // any key press
system("cls");
}
};
GameTable Class
GameTable 클래스에서는 테트리스 화면을 구성하였다.(사각판)
이를 위해 2차원 벡터를 이용하였다. 2차원 벡터가 각각 0의 값을 가지게 한 뒤에, GameTable 생성자를 통해 테트리스 게임판의 가로 세로길이를 입력받으면, 가장자리에 1을 저장하였다.
이후 DrawGameTable() 함수를 이용하여 2차원 벡터를 순회하고, 값을 1을 가지고 있으면 ▦을 출력하고, 값이 0인 경우에는 빈칸을 출력하여 사각판을 구성할 수 있다.
class GameTable
{
private:
int x; int y; // x: 가로 y : 세로
std::vector<std::vector<int>> table;
public:
GameTable(int x, int y)
{
this->x = x;
this->y = y;
for (int i = 0; i < y; i++)
{
std::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 = 0; i < y - 1; i++)
{
table[i][0] = 1;
table[i][x - 1] = 1;
}
}
void DrawGameTable()
{
for (int i = 0; i < y; i++)
{
for (int j = 0; j < x; j++)
{
if (table[i][j] == 1) std::cout << "▦";
else std::cout << " ";
}
std::cout << '\n';
}
}
};
main()
main 함수에서는 콘솔창의 크기와 파일명을 지정해주었으며, GameTable 생성자를 이용해 테트리스 게임판의 가로 세로크기를 전달해주었다. 이후 MainMenu 생성자를 이용하여 게임 시작화면을 콘솔창에 띄워준다. 아무 값이나 입력하게 되면 콘솔창이 clear 되고, DrawGameTable() 을 호출하여 게임 화면을 띄워준다.
#include <iostream>
#include <vector>
#include "MainMenu.h"
#define TABLE_X 20
#define TABLE_Y 40
int main()
{
system("mode con cols = 100 lines = 100 | title 테트리스 게임");
GameTable gt(TABLE_X, TABLE_Y);
MainMenu();
gt.DrawGameTable();
getchar();
return 0;
}
(1) https://javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/
(2) https://seokjin2.tistory.com/45?category=759924