C++ 테트리스 4차

pwy9679·2022년 5월 30일
0

CPP Tetris

목록 보기
4/4

구현기능

  1. 블록을 돌리거나 이동했을때 생기는 오류 해결
  2. 게임오버 구현
  3. 랭킹시스템 구현
  4. 게임 조작감 개선(스페이스바로 블록을 바로 놓을 수 있는 기능추가, 블록이동속도 개선, 돌리는속도 개선)
  5. 블록이 놓아질 위치 미리보는 기능추가

실행화면

메인코드

//main.cpp

#include "system.h"
#include "screen.h"
#include "game.h"
#include "score.h"
using std::thread;

int main() {
	int sel = 0;
	int a = 0;
	int stack = 0;
	int score = 0;
	std::string name;
	CursorView(false); //커서 안보이게하기
	system("mode con cols=130 lines=40 | title C++ Tetris"); //상태창 설정
	Screen screen; //메인화면 설정
	Game game; //게임에 필요한 요소 설정
	Score scores;
	while (sel != 4) {
		score = 0;
		stack = 21;
		sel = screen.MainMenu();
		if (sel == 1) {
			game.scoreset();
			while (sel == 1) {
				system("cls");
				stack = game.setBlock(stack);//다음 블록 선택
				if (stack == 100)break;
				stack = game.drawBlock();//블록의 위치를 테이블위에 정함
				game.drawTable();//블록을 테이블에 
				Sleep(25);//대기
			}
			score = game.returnScore();
			name = screen.GameOver(score);
			sel = 1;
			scores.rankingList(score,name);
			stack = 0;
			system("cls");
		}
		else if (sel == 2) {
			screen.how();//게임방법 출력
		}
		else if (sel == 3) {
			scores.outputRanking();
			screen.enterzero();
		}
		else if (sel == 4) {
			exit(0);//게임 종료
		}
	}
	//게임 실행
	return 0;
}

헤더파일

//game.h

#pragma once
#include<vector>
using namespace std;

class Game
{
private:
	int x, y;
	int num;
	int rotation;
	int shape[4][4][4];
	int Selected_block;
	int stack;
	int stop;
	int blockList[7];
	int blockList2[7];
	int rand_seed;
	int drop;
	int nextblock[3];
	int dropspeed;
	int removeblock;
	int removecombo;
	int backtoback;
	int stackblock;
	int rotationenable;
	int gameover;
	int score;
	int able;
	int spacebarable;
	int sideable;
	int hy;
	vector<vector<int>> table;
public:
	Game();
	void orderBlock();
	int setBlock(int stacks);
	void selectBlock();
	void drawTable();
	int drawBlock();
	int returnScore();
	void moveDrop();
	void drawNextBlock();
	void scoreset();
};

//score.h

#pragma once
#include "system.h"
class Score{
private:
	std::vector<int> score;
	std::vector<std::string> name;
public:
	Score();
	void rankingList(int score, std::string name);
	void outputRanking();
};

//screen.h

#pragma once
#include "system.h";

class Screen {
private:
	int sel, enter;
public:
	Screen();
	int MainMenu();
	void SelectMenu();
	void how();
	std::string GameOver(int score);
	void enterzero();
};

//system.h

#pragma once
#include <iostream>
#include <vector>
#include <conio.h>
#include <windows.h>
#include <thread>
#include<ctime>
#include <cstdlib>
#include<string>
void CursorView(char show); // 커서 설정
void gotoxy(int x, int y); // 커서 위치 설정

클래스/헤더 코드

//game.cpp

#include "game.h"
#include "system.h"
using namespace std;
#define TABLE_X 12
#define TABLE_Y 20

/*
* 테트리스 블록 선언
*/
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, 2, 0, 0},
                    {0, 2, 0, 0},
                    {0, 2, 0, 0},
                    {0, 2, 0, 0}
    },
    {
                    {0, 0, 0, 0},
                    {2, 2, 2, 2},
                    {0, 0, 0, 0},
                    {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}
    },
};

//게임배경 설정
Game::Game() {
    int buffer;
    int randset;

	for (int i = 0; i < TABLE_Y; i++) {
		vector<int> temp;
		for (int j = 0; j < TABLE_X; j++) {
			temp.push_back(0);
		}
		table.push_back(temp);
	}
	for (int i = 0; i < TABLE_X; i++) {
		table[0][i] = 1;
		table[TABLE_Y - 1][i] = 1;
	}
	for (int i = 0; i < TABLE_Y-1; i++) {
		table[i][0] = 1;
		table[i][TABLE_X-1] = 1;
	}

    rand_seed = (unsigned int)time(NULL);
    num = 0;
    srand(rand_seed);
    drop = 1;
    score = 0;
    backtoback = 0;
    gameover = 0;
    able = 0;
    spacebarable = 0;
    sideable = 0;

    for (int i = 0; i < 7; i++) {
        blockList2[i] = i + 1;
    }
    for (int i = 0; i < 7; i++) {
        randset = rand() % (7 - i);
        buffer = blockList2[randset];
        blockList2[randset] = blockList2[6 - i];
        blockList2[6 - i] = buffer;
    }
    drawNextBlock();
}

//게임화면 출력 및 업데이트
void Game::drawTable() {
    gotoxy(15, 4);
    removecombo = 0;
	for (int i = 0; i < TABLE_Y; i++) {
        removeblock = 0;
		for (int j = 0; j < TABLE_X; j++) {
			if (table[i][j] == 1)cout << "▦";
			else if (table[i][j] == 2) { // 블록을 실시간으로 이동시키는기능
				cout << "■";
                if (stack > 20) {
                    table[i][j] = 3;
                    removeblock += 1;
                }
				else table[i][j] = 0;
			}
			else if (table[i][j] == 3) { // 블록 쌓이는기능
				cout << "■";
                removeblock += 1;
			}
            else if (table[i][j] == 4) {
                cout << "▧";
                table[i][j] = 0;
            }
			else cout << "  ";
		}
        gotoxy(15, 4 + 1 + i);
        if (removeblock == 10) {
            for (int l = 1; l < TABLE_X - 1; l++) {
                table[i][l] = table[i - 1][l];
                for (int m = i; m > 1; m--) {
                    if (table[m - 1][l] != 1 && table[m - 2][l] != 1) {
                        table[m - 1][l] = table[m - 2][l];
                        table[m-2][l] = 0;
                    }
                }
            }
            removecombo += 1;
        }
	}

    if (removecombo == 1) {
        score += 100;
        backtoback = 0;
        
        stackblock = 1;
    }
    else if (removecombo == 2) {
        score += 250;
        backtoback = 0;
        stackblock = 2;
    }
    else if (removecombo == 3) {
        score += 500;
        backtoback = 0;
        stackblock = 3;
        
    }
    else if (removecombo == 4) {
        if (backtoback == 1) {
            score += 1200;
            stackblock = 5;
            
        }
        else {
            score += 1000;
            stackblock = 4;
            
        }
        backtoback = 1;
    }

    if(stackblock == 1) cout << "single" << endl;
    else if (stackblock == 2) cout << "double" << endl;
    else if (stackblock == 3) cout << "triple" << endl;
    else if (stackblock == 4) cout << "tetris " << endl;
    else if (stackblock == 5) cout << "back to back tetris " << endl;

    gotoxy(15, 3);
    cout << "Score - " << score << " , back to back : " << backtoback;
}

//다음 블록을 화면에 그림
void Game::drawNextBlock() {
    for (int k = 0; k < 3; k++) {
        if (num + k+1 == 7) nextblock[k] = blockList2[0];
        else if (num + k+1 == 8) nextblock[k] = blockList2[1];
        else if (num + k+1 == 9) nextblock[k] = blockList2[2];
        else nextblock[k] = blockList[num + k + 1];
        if (nextblock[k] == 1) {
            for (int i = 0; i < 4; i++) {
                gotoxy(40, 5 + i+(5*k));
                for (int j = 0; j < 4; j++) {
                    if (Block_I[0][i][j] == 2)cout << "■";
                    else cout << "  ";
                }
            }
        }
        else if (nextblock[k] == 2) {
            for (int i = 0; i < 4; i++) {
                gotoxy(40, 5 + i+(5*k));
                for (int j = 0; j < 4; j++) {
                    if (Block_J[0][i][j] == 2)cout << "■";
                    else cout << "  ";
                }
            }
        }
        else if (nextblock[k] == 3) {
            for (int i = 0; i < 4; i++) {
                gotoxy(40, 5 + i+(5*k));
                for (int j = 0; j < 4; j++) {
                    if (Block_L[0][i][j] == 2)cout << "■";
                    else cout << "  ";
                }
            }
        }
        else if (nextblock[k] == 4) {
            for (int i = 0; i < 4; i++) {
                gotoxy(40, 5 + i+(5*k));
                for (int j = 0; j < 4; j++) {
                    if (Block_Z[0][i][j] == 2)cout << "■";
                    else cout << "  ";
                }
            }
        }
        else if (nextblock[k] == 5) {
            for (int i = 0; i < 4; i++) {
                gotoxy(40, 5 + i+(5*k));
                for (int j = 0; j < 4; j++) {
                    if (Block_S[0][i][j] == 2)cout << "■";
                    else cout << "  ";
                }
            }
        }
        else if (nextblock[k] == 6) {
            for (int i = 0; i < 4; i++) {
                gotoxy(40, 5 + i+(5*k));
                for (int j = 0; j < 4; j++) {
                    if (Block_T[0][i][j] == 2)cout << "■";
                    else cout << "  ";
                }
            }
        }
        else if (nextblock[k] == 7) {
            for (int i = 0; i < 4; i++) {
                gotoxy(40, 5 + i+(5*k));
                for (int j = 0; j < 4; j++) {
                    if (Block_O[0][i][j] == 2)cout << "■";
                    else cout << "  ";
                }
            }
        }
    }
}

//출력될 블록 세팅
int Game::setBlock(int stacks) {
    if (gameover == 1) {
        return 100;
    }

    stack = stacks; 
if (stack > 20) {
    x = 4;
    y = 1;
    rotation = 0;
    stop = 0;
    stack = 0;
    if (num == 7) num = 0;
    if (num == 0) orderBlock();
    num++;
    selectBlock();
}
drawNextBlock();
return stack;
}

//다음에 나올 블럭을 설정함
void Game::selectBlock() {
    if (blockList[num] == 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_I[i][j][k];
                }
            }
        }
    }
    else if (blockList[num] == 2) {
        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_J[i][j][k];
                }
            }
        }
    }
    else if (blockList[num] == 3) {
        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];
                }
            }
        }
    }
    else if (blockList[num] == 4) {
        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_Z[i][j][k];
                }
            }
        }
    }
    else if (blockList[num] == 5) {
        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_S[i][j][k];
                }
            }
        }
    }
    else if (blockList[num] == 6) {
        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_T[i][j][k];
                }
            }
        }
    }
    else if (blockList[num] == 7) {
        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_O[i][j][k];
                }
            }
        }
    }
}

//블록을 화면상 출력하기위해 세팅
int Game::drawBlock() {
    char c;
    int error = 2;
    int spacebar = 0;
    int h = 0;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            if (shape[rotation][i][j] == 2 && table[y + i][x + j] == 3 && y <= 1) {
                gameover = 1;
            }
        }
    }

    if (drop > 5) {
        moveDrop();
        drop = 0;
    }

    if (stop != 1 && GetKeyState(VK_RIGHT) & 0x8000 && stack < 20 && sideable <= 0) {
        sideable = 2;
        x++;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (shape[rotation][i][j] == 2 && table[y + i][x + j] == 3) {
                    x--;
                }
                else if (shape[rotation][i][j] == 2 && table[y + i][x + j] == 1) {
                    x--;
                }
            }
        }
    }

    if (stop != 2 && GetKeyState(VK_LEFT) & 0x8000 && stack < 20 && sideable <=0) {
        sideable = 2;
        x--;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (shape[rotation][i][j] == 2 && table[y + i][x + j] == 3) {
                    x++;
                }
                else if (shape[rotation][i][j] == 2 && table[y + i][x + j] == 1) {
                    x++;
                }
            }
        }
    }

    //키를 입력받아서 블록을 돌림

    if (GetKeyState(VK_SPACE) & 0x8000 && spacebarable <= 0) {
        spacebar = 0;
        spacebarable = 4;
        while (spacebar == 0) {
            y++;
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    if (shape[rotation][i][j] == 2 && table[y + i][x + j] == 3) {
                        y--;
                        spacebar = 1;
                        stack = 21;
                    }
                    else if (shape[rotation][i][j] == 2 && table[y + i][x + j] == 1) {
                        y--;
                        spacebar = 1;
                        stack = 21;
                    }
                }
            }
        }
    }

    if ((GetKeyState('z') & 0x8000 || GetKeyState('Z') & 0x8000) && able <= 0) {
        rotation--;
        able = 3;
        if (rotation > 3) rotation = 0;
        else if (rotation < 0)rotation = 3;
        while (error != 1) {
            error = 1;
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    if (shape[rotation][i][j] == 2 && table[y + i][x + j] == 3) {
                        rotation++;
                        if (rotation > 3) rotation = 0;
                        else if (rotation < 0)rotation = 3;
                        if (shape[rotation][i][j] == 2 && table[y + i][x + j] == 3) {
                            y--;
                        }
                    }
                    else if (shape[rotation][i][j] == 2 && table[y + i][x + j] == 1) {
                        if (x > 7) {
                            x--;
                        }
                        else { 
                            x++;
                        }
                        if (y > 18) {
                            y--;
                        }
                        error = 2;
                    }
                }
            }
        }
    }
    else if ((GetKeyState('x') & 0x8000 || GetKeyState('X') & 0x8000) && able <= 0) {
        rotation++;
        able = 4;
        if (rotation > 3) rotation = 0;
        else if (rotation < 0)rotation = 3;

        while (error != 1) {
            error = 1;
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    if (shape[rotation][i][j] == 2 && table[y + i][x + j] == 3) {
                        rotation--;
                        if (rotation > 3) rotation = 0;
                        else if (rotation < 0)rotation = 3;
                        if (shape[rotation][i][j] == 2 && table[y + i][x + j] == 3) {
                            y--;
                        }
                    }
                    else if (shape[rotation][i][j] == 2 && table[y + i][x + j] == 1) {
                        if (x > 7) {
                            x--;
                        }
                        else {
                            x++;
                        }
                        if (y > 18) {
                            y--;
                        }
                        error = 2;
                    }
                }
            }
        }
    }

    

    

    //블록을 돌렸을때 오류해결

    while (error != 1) {
        error = 1;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (shape[rotation][i][j] == 2 && table[y + i][x + j] == 3) {
                    rotation--;
                    if (rotation > 3) rotation = 0;
                    else if (rotation < 0)rotation = 3;
                    if (shape[rotation][i][j] == 2 && table[y + i][x + j] == 3) {
                        y--;
                    }
                }
                else if (shape[rotation][i][j] == 2 && table[y + i][x + j] == 1) {
                    if (x > 7) {
                        x--;
                    }
                    else {
                        x++;
                    }
                    if (y > 18) {
                        y--;
                    }
                    error = 2;
                }
            }
        }
    }

    hy = y;
    while (h == 0) {
        hy++;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (shape[rotation][i][j] == 2 && table[hy + i][x + j] == 3) {
                    hy--;
                    h = 1;
                }
                else if (shape[rotation][i][j] == 2 && table[hy + i][x + j] == 1) {
                    hy--;
                    h = 1;
                }
            }
        }
    }

    stop = 0;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            if(shape[rotation][i][j] == 2){
                
                if (table[y + i][x + j] == 1) {
                    if (x < 4) {
                        x++;
                    }
                    if (y > 18)y--;
                    if (hy > 18)hy--;
                }
                table[hy + i][x + j] = 4;
                table[y + i][x + j] = 2;
                if (table[y + i + 1][x + j] == 1 || table[y + i + 1][x + j] == 3) {
                    stack += 1;
                    drop = 0;
                }
            }
        }
    }

    //아래키를 누르면 블록이 빠르게 떨어짐
    if (GetAsyncKeyState(VK_DOWN) & 0x8000) {
        drop += 5;
    }
    else drop++;
    able--;
    spacebarable--;
    sideable--;
    return stack;
}

//블록을 한칸 아래로 내림
void Game::moveDrop() { 
    y += 1;
}

//블록을 양옆으로 이동시킴
int Game::returnScore() {
    

    for (int i = 0; i < TABLE_X; i++) {
        for (int j = 0; j < TABLE_Y; j++) {
            table[j][i] = 0;
        }
    }
    for (int i = 0; i < TABLE_X; i++) {
        table[0][i] = 1;
        table[TABLE_Y - 1][i] = 1;
    }
    for (int i = 0; i < TABLE_Y - 1; i++) {
        table[i][0] = 1;
        table[i][TABLE_X - 1] = 1;
    }
    orderBlock();
    num = 1;
    gameover = 0;
    return score;
}

//다음 7개의 블록의 순서를 결정함
void Game::orderBlock() {
    int buffer;
    int randset;
    rand_seed++;
    srand(rand_seed);
    for (int i = 0; i < 7; i++) {
        blockList[i] = blockList2[i];
    }
    for (int i = 0; i < 7; i++) {
        blockList2[i] = i + 1;
    }
    for (int i = 0; i < 7; i++) {
        randset = rand() % (7 - i);
        buffer = blockList2[randset];
        blockList2[randset] = blockList2[6 - i];
        blockList2[6 - i] = buffer;
    }
}

void Game::scoreset() {
    score = 0;
}

//mainscreen.cpp

#include "screen.h"
#include "system.h"
#include "game.h"
using namespace std;
Screen::Screen() {
    sel = 1;
    enter = 0;
}
//메인화면 출력
int Screen::MainMenu() {
    cout << "\n\n\n\n";
    cout << "\t\t" << "□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□\n";
    cout << "\t\t" << "□■■■■□□■□■□□□□■■■■■■■■□□□■■■■■■□□■□□□□□□□□■□□□□□\n";
    cout << "\t\t" << "□■□□□□□■□■□□□□■□□□□□□□□□□□□□□□■□□■□□□□□□□□■□□□□□\n";
    cout << "\t\t" << "□■□□□□□■□■□□□□■□□□□□□□□□□□□□□□■□□■□□□□□□□□■□□□□□\n";
    cout << "\t\t" << "□■□□□□□■□■□□□□■■■■■■■■□□□□□□□□■□□■□□□□□□□■□■□□□□\n";
    cout << "\t\t" << "□■■■□■■■□■□□□□■□□□□□□□□□□■■■■■■□□■□□□□□□■□□□■□□□\n";
    cout << "\t\t" << "□■□□□□□■□■□□□□■□□□□□□□□□□■□□□□□□□■□□□□■■□□□□□■■□\n";
    cout << "\t\t" << "□■□□□□□■□■□□□□■■■■■■■■□□□■□□□□□□□■□□□□□□□□□□□□□□\n";
    cout << "\t\t" << "□■□□□□□■□■□□□□□□□□□□□□□□□■□□□□□□□■□□□□□□□□□□□□□□\n";
    cout << "\t\t" << "□■□□□□□■□■□□□□□□□□□□□□□□□■□□□□□□□■□□□□□□□□□□□□□□\n";
    cout << "\t\t" << "□■■■■■□■□■□□□■■■■■■■■■■□□■■■■■■■□■□□□■■■■■■■■■■□\n";
    cout << "\t\t" << "□□□□□□□■□■□□□□□□□□□□□□□□□□□□□□□□□■□□□□□□□□□□□□□□\n";
    cout << "\t\t" << "□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□\n";
    if (sel == 1) {//게임시작 선택
        cout << "\n\n\t\t\t\t\t\t\t" << "> 게임시작\n";
        cout << "\n\n\t\t\t\t\t\t\t" << "게임방법\n";
        cout << "\n\n\t\t\t\t\t\t\t" << "게임순위\n";
        cout << "\n\n\t\t\t\t\t\t\t" << "게임종료\n";
    }
    else if (sel == 2) {//게임방법 선택
        cout << "\n\n\t\t\t\t\t\t\t" << "게임시작\n";
        cout << "\n\n\t\t\t\t\t\t\t" << "> 게임방법\n";
        cout << "\n\n\t\t\t\t\t\t\t" << "게임순위\n";
        cout << "\n\n\t\t\t\t\t\t\t" << "게임종료\n";
    }
    else if (sel == 3) {//게임종료선택
        cout << "\n\n\t\t\t\t\t\t\t" << "게임시작\n";
        cout << "\n\n\t\t\t\t\t\t\t" << "게임방법\n";
        cout << "\n\n\t\t\t\t\t\t\t" << "> 게임순위\n";
        cout << "\n\n\t\t\t\t\t\t\t" << "게임종료\n";
    }
    else if (sel == 4) {//게임종료선택
        cout << "\n\n\t\t\t\t\t\t\t" << "게임시작\n";
        cout << "\n\n\t\t\t\t\t\t\t" << "게임방법\n";
        cout << "\n\n\t\t\t\t\t\t\t" << "게임순위\n";
        cout << "\n\n\t\t\t\t\t\t\t" << "> 게임종료\n";
    }

    if (enter == 1) {//선택한 메뉴를 실행
        system("cls");
        return sel;
    }
    else {
        SelectMenu();//위아래로 움직이며 메뉴를 선택
        system("cls");
        return 0;
    } 
}

//조작법 및 플레이방법
void Screen::how() {
    cout << "\n\n\n\n";
    cout << "\t\t\t" << "조작법" << endl;
    cout << "\n\n\t\t\t" << "←→ 좌우로 움직이기" << endl;
    cout << "\n\t\t\t" << "↓ 빠르게 떨어뜨리기" << endl;
    cout << "\n\t\t\t" << "z 오른쪽으로 회전" << endl;
    cout << "\n\t\t\t" << "x 왼쪽으로 회전" << endl;
    cout << "\n\t\t\t" << "spacebar 바로 떨어뜨리기" << endl;
    cout << "\n\n\n\t\t\t" << "How to play" << endl;
    cout << "\n\n\t\t\t" << "1. 떨어지는 블록을 쌓습니다." << endl;
    cout << "\n\t\t\t" << "2. 줄을 빈틈없이 쌓으면 줄이 사라지며 점수를 얻을 수 있습니다." << endl;
    cout << "\n\t\t\t" << "3. 여러줄을 동시에 없애면 추가 점수를 얻을 수 있습니다." << endl;
    cout << "\n\t\t\t" << "4. 블록이 천장에 닿이면 게임오버입니다." << endl;
    system("pause>null");
    system("cls");
    enter = 0;
    sel = 1;
}

void Screen::enterzero() {
    enter = 0;
    sel = 1;
}

//키보드를 이용해 메뉴를 선택
void Screen::SelectMenu() {
    char c;
    c = _getch();
    if (c == 80) {
        if (sel < 4)sel += 1;
    }
    else if (c == 72) {
        if (sel > 1)sel -= 1;
    }
    if (c == 13) {
        enter = 1;
    }
}

std::string Screen::GameOver(int score) {
    string name;
    rewind(stdin);
    while (getchar() != '\n')system("cls");
    system("cls");
    cout << "\n\n\n\n\n";
    cout << "\t\t" << "                                                                                         " << endl;
    cout << "\t\t" << "   _|_|_|    _|_|    _|      _|  _|_|_|_|        _|_|    _|      _|  _|_|_|_|  _|_|_|    " << endl;
    cout << "\t\t" << " _|        _|    _|  _|_|  _|_|  _|            _|    _|  _|      _|  _|        _|    _|  " << endl;
    cout << "\t\t" << " _|  _|_|  _|_|_|_|  _|  _|  _|  _|_|_|        _|    _|  _|      _|  _|_|_|    _|_|_|    " << endl;
    cout << "\t\t" << " _|    _|  _|    _|  _|      _|  _|            _|    _|    _|  _|    _|        _|    _|  " << endl;
    cout << "\t\t" << "   _|_|_|  _|    _|  _|      _|  _|_|_|_|        _|_|        _|      _|_|_|_|  _|    _|  " << endl;
    cout << "\t\t" << "                                                                                         " << endl;
    cout << "\n\t\t\t" << "                              점수 - " << score << endl;
    cout << "\n\t\t" << "                              이름을 입력하세요 - ";
    cin >> name;
    cout << "\n\t\t" << "                   ~아무키나 누르면 메인화면으로 돌아갑니다.~                            " << endl;
    enter = 0;
    system("pause>null");
    return name;
}

//score.cpp

#include "score.h"
#include "system.h"
#include "screen.h"
#include <algorithm>
using namespace std;

Score::Score() {
	vector<int> score = {0};
	vector<std::string> name = {"0"};
}

void Score::rankingList(int scores, std::string names) {
	score.push_back(scores);
	name.push_back(names);
	vector<int>::iterator it = score.begin();
	vector<std::string>::iterator itn = name.begin();
	for (int i = 0; i > score.size(); i--) {
		if (score[i] < scores) {
			score.insert(it + i, scores);
			name.insert(itn +i, names);
			break;
		}
	}
	//sort(score.begin(), score.end());
}

void Score::outputRanking() {
	int a = 1;
	
	system("cls");
	cout << "\n\n\n\n";
	cout << "\t\t\t\t" << "순위" << endl;
	for (int i = score.size(); i > 0; i--) {
		cout << "\n\t\t\t\t" << a << ".  "<< name[i-1]<< "   " << score[i - 1] << endl;
		a++;
	}
	system("pause>null");
	system("cls");
}

//system.cpp

#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);
}
profile
다양한 분야 해보는중

0개의 댓글