Notice
Recent Posts
Recent Comments
250x250
Creative Code
c 본문
728x90
#include <stdio.h>
#include <windows.h>
#include <time.h>
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
#define SUBMIT 4 //선택(스페이스바)
void init(); //콘솔창 크기 설정함수
void gotoxy(int x, int y); //커서이동함수
void titleDraw(); //게임 시작 타이틀화면 출력함수
int keyControl(); //키 입력받는 함수
int menuSelect(); //게임 메뉴 선택함수
void gameInfo(); //게임 정보출력함수
int mapDifficulty(); //게임 난이도 설정함수
void difficultyEasy(); //난이도 EASY 게임실행 함수
void printMap(); //게임 실행후 맵 출력함수
void difficultyNormal(); //난이도 NORMAL 게임실행 함수
void difficultyHard(); //난이도 HARD 게임실행 함수
void createball();
void printBall();
typedef struct ball {
int x; //공이 생성된 x위치
int y; //공이 생성된 y위치
int direct_x; //공의 x방향 이동속도
int direct_y; //공의 y방향 이동속도
int type; // type이 1이면 위, 2면 아래, 3이면 왼쪽 4면 오른쪽에서 공이생성
}ball;
ball Ball[100]; //공 정보 저장
void init() { // 콘솔창 크기 설정하는 함수
system("mode con:cols=66 lines=30| title game");
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); //콘솔 핸들 가져오기
CONSOLE_CURSOR_INFO ConsoleCursor;
ConsoleCursor.bVisible = 0; //0이면 커서 숨기기 1이면 커서 보이기
ConsoleCursor.dwSize = 1; // 커서 크기
SetConsoleCursorInfo(consoleHandle, &ConsoleCursor);
}
void gotoxy1(int x, int y) {
}
void gotoxy(int x, int y) { // 커서 위치 이동함수
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); // 콘솔 핸들 가져오기
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(consoleHandle, pos);
}
void titleDraw() { // 타이틀 화면 출력하는 함수
printf(" \n");
printf(" \n");
printf(" \n");
printf(" ##### #### ### # #### ##### \n");
printf(" # # # # # # # # \n");
printf(" #### #### # ##### #### #### \n");
printf(" # # # # # # # \n");
printf(" ##### #### ### # # # ##### \n");
}
int keyControl() {
char temp = getch(); // getch함수는 엔터를 입력하지 않아도 키가 눌리면 바로 반환해주는 함수
if (temp == 'w' || temp == 'W') return UP;
else if (temp == 'a' || temp == 'A') return LEFT;
else if (temp == 's' || temp == 'S') return DOWN;
else if (temp == 'd' || temp == 'D') return RIGHT;
else if (temp == ' ') return SUBMIT;
}
int menuSelect() { // 메뉴선택 함수
int x = 25;
int y = 15;
gotoxy(25, 15);
printf("> 게임시작");
gotoxy(26, 16);
printf(" 게임정보");
gotoxy(26, 17);
printf(" 종료");
gotoxy(26, 14);
while (1) {
int select = keyControl();
switch (select) {
case UP: {
if (y > 15) {
gotoxy(x, y);
printf(" ");
gotoxy(x, --y);
printf(">");
}
else if (y == 15) {
gotoxy(x, y);
printf(" ");
gotoxy(x, y += 2);
printf(">");
}
break;
}
case DOWN: {
if (y < 17) {
gotoxy(x, y);
printf(" ");
gotoxy(x, ++y);
printf(">");
}
else if (y == 17) {
gotoxy(x, y);
printf(" ");
gotoxy(x, y -= 2);
printf(">");
}
break;
}
case SUBMIT: {
return y - 15;
}
}
}
}
void gameInfo() { // 게임 정보
system("cls");
printf("\n\n\n\n\n\n\n\n\n\n");
printf(" ");
printf(" <조작법> \n\n");
printf(" 이동 : W, A, S ,D \n");
printf(" 선택 : SPACE BAR \n");
printf(" 개 발 자 : harucode.tistory.com \n\n");
printf(" space bar를 누르면 메인화면으로 이동합니다. \n\n");
while (1) {
if (keyControl() == SUBMIT) {
break;
}
}
}
int mapDifficulty() { //난이도 설정함수
gotoxy(28, 12);
printf("<맵 난이도>");
gotoxy(28, 15);
printf("> EASY");
gotoxy(28, 16);
printf(" NORMAL");
gotoxy(28, 17);
printf(" HARD");
int x = 28;
int y = 15;
gotoxy(x, y);
while (1) {
int num = keyControl();
switch (num) {
case UP: {
if (y == 15) {
gotoxy(x, y);
printf(" ");
gotoxy(x, y += 2);
printf(">");
}
else if (y > 15) {
gotoxy(x, y);
printf(" ");
gotoxy(x, --y);
printf(">");
}
break;
}
case DOWN: {
if (y == 17) {
gotoxy(x, y);
printf(" ");
gotoxy(x, y -= 2);
printf(">");
}
else if (y < 17) {
gotoxy(x, y);
printf(" ");
gotoxy(x, ++y);
printf(">");
}
break;
}
case SUBMIT: {
return y - 15;
}
}
}
}
void printMap() { // 맵 출력
printf("▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣ ▣");
printf("▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣");
}
void difficultyEasy() { //난이도 easy
printMap();
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); //콘솔 핸들 가져오기
CONSOLE_CURSOR_INFO ConsoleCursor;
ConsoleCursor.bVisible = 0; //0이면 커서 숨기기 1이면 커서 보이기
ConsoleCursor.dwSize = 1; // 커서 크기
SetConsoleCursorInfo(consoleHandle, &ConsoleCursor);
gotoxy(33, 15);
printf("a");
int x = 33;
int y = 15;
while (1) {
int move = keyControl();
switch (move) {
case UP: {
gotoxy(x, y);
printf(" ");
gotoxy(x, --y);
if (y < 1) {
y = 1;
gotoxy(x, y);
}
printf("a");
break;
}
case DOWN: {
gotoxy(x, y);
printf(" ");
gotoxy(x, ++y);
if (y > 28) {
y = 28;
gotoxy(x, y);
}
printf("a");
break;
}
case LEFT: {
gotoxy(x, y);
printf(" ");
gotoxy(--x, y);
if (x < 2) {
x = 2;
gotoxy(x, y);
}
printf("a");
break;
}
case RIGHT: {
gotoxy(x, y);
printf(" ");
gotoxy(++x, y);
if (x > 63) {
x = 63;
gotoxy(x, y);
}
printf("a");
break;
}
}
createball();
printBall();
for (int j = 0; j < 100; j++) {
Ball[j].x += Ball[j].direct_x;
Ball[j].y += Ball[j].direct_y;
}
Sleep(10);
}
}
void createball() {
srand(time(NULL));
static int i = 0;
int type = rand() % 4 + 1;
switch (type) {
case 1: {
Ball[i].x = rand() % 61 + 3;
Ball[i].y = 2;
Ball[i].direct_x = rand() % 5 - 2;
Ball[i].direct_y = rand() % 2 + 1;
Ball[i].type = type;
if (Ball[i].direct_x == 0 || Ball[i].direct_y == 0) {
Ball[i].direct_x++;
Ball[i].direct_y++;
}
break;
}
case 2: {
Ball[i].x = rand() % 61 + 3;
Ball[i].y = 29;
Ball[i].direct_x = rand() % 5 - 2;
Ball[i].direct_y = -(rand() % 2 + 1);
Ball[i].type = type;
if (Ball[i].direct_x == 0 || Ball[i].direct_y == 0) {
Ball[i].direct_x++;
Ball[i].direct_y--;
}
break;
}
case 3: {
Ball[i].x = 3;
Ball[i].y = rand() % 29 + 2;
Ball[i].direct_x = rand() % 2 + 1;
Ball[i].direct_y = rand() % 5 - 2;
if (Ball[i].direct_y == 0) {
Ball[i].direct_y++;
}
Ball[i].type = type;
break;
}
case 4: {
Ball[i].x = 63;
Ball[i].y = rand() % 29 + 2;
Ball[i].direct_x = -(rand() % 2 + 1);
Ball[i].direct_y = rand() % 5 - 2;
if (Ball[i].direct_y == 0) {
Ball[i].direct_y--;
}
Ball[i].type = type;
break;
}
}
i++;
}
void printBall() { //탄알 출력
for (int i = 0; i < 100; i++) {
gotoxy(Ball[i].x, Ball[i].y);
printf("o");
}
}
void difficultyNormal() { //난이도 normal
printMap();
Sleep(5000);
}
void difficultyHard() { // 난이도 hard
printMap();
Sleep(5000);
}
int main() {
init(); //콘솔 설정
while (1) {
titleDraw(); //게임 시작화면 출력
int menuCode = menuSelect(); // 메뉴선택
system("cls");
if (menuCode == 0) {
int difficulty = mapDifficulty(); //난이도 선택
system("cls");
if (difficulty == 0) {
difficultyEasy(); // 난이도 EASY
}
else if (difficulty == 1) { // 난이도 NORMAL
}
else if (difficulty == 2) { // 난이도 HARD
}
}
else if (menuCode == 1) { // 게임 정보
gameInfo();
}
else if (menuCode == 2) { // 게임 종료
return 0;
}
system("cls");
}
return 0;
}
728x90
'혼자 만든 Code' 카테고리의 다른 글
[C]디데이 계산기 (0) | 2023.08.28 |
---|---|
[C++]디데이 계산기 (0) | 2023.08.28 |
[JAVA] 리모컨 프로그램 (0) | 2022.07.20 |
[java]거주가능한 행성을 찾는 프로그램 (0) | 2022.07.19 |
[JAVA] 로그인 프로그램 (0) | 2022.07.14 |