C++ TETRIS | 1~2일차 | 메인화면 구현 & 블록 생성

2021. 7. 27. 23:04개발 | 프로젝트/C++ TETRIS

728x90

메인 화면
게임 틀 & 스타트 라인 & 랜덤한 정지 블록

#include<cstdio>
#include<iostream>
#include<windows.h>
#include<ctime>
#include<cstdlib>
#include<vector>
using namespace std;

#define map_x 10
#define map_y 20

char map[20][12],ch;
int x,y,clearline,score,rotation=1,number;
int block1[4][4][4]={
	{
		{0,0,0,0},
		{1,1,1,1},
		{0,0,0,0},
		{0,0,0,0}
	},
	{
		{0,0,1,0},
		{0,0,1,0},
		{0,0,1,0},
		{0,0,1,0}
	},
	{
		{0,0,0,0},
		{1,1,1,1},
		{0,0,0,0},
		{0,0,0,0}
	},
	{
		{0,0,1,0},
		{0,0,1,0},
		{0,0,1,0},
		{0,0,1,0}
	}
};
int block2[4][4][4]={
	{
		{0,0,0,0},
		{0,1,1,0},
		{0,1,1,0},
		{0,0,0,0}
	},
	{
		{0,0,0,0},
		{0,1,1,0},
		{0,1,1,0},
		{0,0,0,0}
	},
	{
		{0,0,0,0},
		{0,1,1,0},
		{0,1,1,0},
		{0,0,0,0}
	},
	{
		{0,0,0,0},
		{0,1,1,0},
		{0,1,1,0},
		{0,0,0,0}
	}
};
int block3[4][4][4]={
	{
		{0,0,0,0},
		{0,1,0,0},
		{0,1,0,0},
		{0,1,1,0}
	},
	{
		{0,0,0,0},
		{0,1,1,1},
		{0,1,0,0},
		{0,0,0,0}
	},
	{
		{0,0,0,0},
		{0,1,1,0},
		{0,0,1,0},
		{0,0,1,0}
	},
	{
		{0,0,0,0},
		{0,0,1,0},
		{1,1,1,0},
		{0,0,0,0}
	}
};
int block4[4][4][4]={
	{
		{0,0,0,0},
		{0,0,1,0},
		{0,0,1,0},
		{0,1,1,0}
	},
	{
		{0,0,0,0},
		{0,1,0,0},
		{0,1,1,1},
		{0,0,0,0}
	},
	{
		{0,0,0,0},
		{0,1,1,0},
		{0,1,0,0},
		{0,1,0,0}
	},
	{
		{0,0,0,0},
		{0,1,1,1},
		{0,0,0,1},
		{0,0,0,0}
	}
};
int block5[4][4][4]={
	{
		{0,0,0,0},
		{0,0,1,0},
		{0,1,1,0},
		{0,0,1,0}
	},
	{
		{0,0,0,0},
		{0,0,1,0},
		{0,1,1,1},
		{0,0,0,0}
	},
	{
		{0,0,0,0},
		{0,0,1,0},
		{0,0,1,1},
		{0,0,1,0}
	},
	{
		{0,0,0,0},
		{0,0,0,0},
		{0,1,1,1},
		{0,0,1,0}
	}
};
int block6[4][4][4]={
	{
		{0,0,0,0},
		{0,0,1,1},
		{0,1,1,0},
		{0,0,0,0}
	},
	{
		{0,0,0,0},
		{0,0,1,0},
		{0,0,1,1},
		{0,0,0,1}
	},
	{
		{0,0,0,0},
		{0,0,1,1},
		{0,1,1,0},
		{0,0,0,0}
	},
	{
		{0,0,0,0},
		{0,0,1,0},
		{0,0,1,1},
		{0,0,0,1}
	}
};
int block7[4][4][4]={
	{
		{0,0,0,0},
		{0,1,1,0},
		{0,0,1,1},
		{0,0,0,0}
	},
	{
		{0,0,0,0},
		{0,0,1,0},
		{0,1,1,0},
		{0,1,0,0}
	},
	{
		{0,0,0,0},
		{0,1,1,0},
		{0,0,1,1},
		{0,0,0,0}
	},
	{
		{0,0,0,0},
		{0,0,1,0},
		{0,1,1,0},
		{0,1,0,0}
	}
};

void gotoxy(int X,int Y){ //현재 좌표 설정 
	COORD pos={X,Y};
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);	
}

void set(){
	system("mode con:cols=100 lines=25");
	system("title TETRIS_made_by_kty");
	x=33,y=10;
	gotoxy(x,y);
	printf("==================================");
    gotoxy(x,y+1);
    printf("==== Welcome to TETRIS world! ====");
    gotoxy(x,y+2);    
    printf("==================================");
    gotoxy(x,y+3);
    printf("엔터 키를 누르면 게임이 시작됩니다"); 
}

void start(){
	char ch;
	while(true){
		cin.get(ch);
		if(ch=='\n'){
			system("cls");
			return;
		}
	}
} 

void table(int x,int y){
	cout<<endl;
	for(int i=0;i<y;i++){
		for(int j=0;j<x;j++){
			if(j==0||j==11||i==19){
				cout<<"▩";
			}
			else if(i==0){
				cout<<"ㅡ";
			}
			else{
				cout<<"  ";
			}
		}
		cout<<endl;
	}
}

void one(){
	int point_x=map_x/2+3;
	int point_y=1;
	rotation=0;
	for(int i=0;i<4;i++){
		gotoxy(point_x,point_y);
		for(int j=0;j<4;j++){
			if(block1[number][i][j]!=0){
				cout<<"▣";
			}
		}
		point_y++;
	}
}

void two(){
	int point_x=map_x/2+3;
	int point_y=1;
	rotation=0;
	for(int i=0;i<4;i++){
		gotoxy(point_x,point_y);
		for(int j=0;j<4;j++){
			if(block2[number][i][j]!=0){
				cout<<"▣";
			}
		}
		point_y++;
	}
}

void three(){
	int point_x=map_x/2+3;
	int point_y=1;
	rotation=0;
	for(int i=0;i<4;i++){
		gotoxy(point_x,point_y);
		for(int j=0;j<4;j++){
			if(block3[number][i][j]!=0){
				cout<<"▣";
			}
		}
		point_y++;
	}
}

void four(){
	int point_x=map_x/2+3;
	int point_y=1;
	rotation=0;
	for(int i=0;i<4;i++){
		gotoxy(point_x,point_y);
		for(int j=0;j<4;j++){
			if(block4[number][i][j]!=0){
				cout<<"▣";
			}
		}
		point_y++;
	}
}

void five(){
	int point_x=map_x/2+3;
	int point_y=1;
	rotation=0;
	for(int i=0;i<4;i++){
		gotoxy(point_x,point_y);
		for(int j=0;j<4;j++){
			if(block5[number][i][j]!=0){
				cout<<"▣";
			}
		}
		point_y++;
	}
}

void six(){
	int point_x=map_x/2+3;
	int point_y=1;
	rotation=0;
	for(int i=0;i<4;i++){
		gotoxy(point_x,point_y);
		for(int j=0;j<4;j++){
			if(block6[number][i][j]!=0){
				cout<<"▣";
			}
		}
		point_y++;
	}
}

void seven(){
	int point_x=map_x/2+3;
	int point_y=1;
	rotation=0;
	for(int i=0;i<4;i++){
		gotoxy(point_x,point_y);
		for(int j=0;j<4;j++){
			if(block7[number][i][j]!=0){
				cout<<"▣";
			}
		}
		point_y++;
	}
}

void createblock(){
	srand((unsigned int)time(NULL)); //현재 시간 기준 랜덤 설정 
	int random=rand()%7; //랜덤값 설정 
	if(random==1||random==0){
		one();
	}
	else if(random==2){
		two();
	}
	else if(random==3){
		three();
	}
	else if(random==4){
		four();
	}
	else if(random==5){
		five();
	}
	else if(random==6){
		six();
	}
	else{
		seven();
	}
}

void playgame(){
	table(map_x+2,map_y);
	while(true){
		if(rotation){
			createblock(); //블록 생성 
		}
	}
}

int main(){
	set(); //메인화면 세팅 
	start(); //엔터 입력 감지 & 콘솔창 클리어 
	playgame(); // 게임 플레이 
}

 

예전에 구글 공룡 게임을 코드로 구현해본 이후, 콘솔 창 게임 제작에 손을 대지 않았지만....

프로젝트 제작 겸 연습 겸 해서 테트리스 게임 만들기에 도전해봤습니다.

 

코드를 좀 비효율적으로 짠 것 같긴 하지만, 우선 오늘은 이 정도로 만족하겠습니다.

(비효율적으로 짰다는 증거 : 구현만 했는데도 코드가 389줄...)

이후에는 초당 블록이 한 칸씩 내려가고,

키보드 방향키 입력에 따라서 블록을 컨트롤하는 기능을 만들어보겠습니다.

728x90