글 수 3,386
특징?
1. '나'가 한칸 움직일 때 '적'은 두칸 움직입니다.
2. '적'은 움직였던 방향으로 다시 못갑니다.
↑
예) → 로 움직였다면 →의 반대방향인 ← 를 기억해두고 그 다음번엔 ←를 제외한 방향인 ◇ → 으로만 이동할 수 있습니다.
↓
*----------------------------------------------------*
#include<stdio.h>
#include<windows.h>
#include <stdlib.h>
#define LEFT 75 // 좌측방향키
#define RIGHT 77 // 우측방향키
#define UP 72 // 위쪽방향키
#define DOWN 80 // 아래방향키
#define Win 1
#define GameOver 0
#define MainMenu 2
#define Again 3
#define next 4
#define AGAIN 'a'
#define ESC 'q'
#define NextL 'w'
#define Urand(x) rand()%(x+1)
/*
rand();
rand()%((마지막 값 - 시작 값)+1)+시작 값
여기서는 시작값이 무조건 0
*/
void gotoxy(unsigned int x, unsigned int y);//커저 좌표 이동
void SetColor(int color , int bgcolor);//텍스트색상변경
/*SetColor 함수를 만들어주는 이유는
HANDLE hcon; // 콘솔의 핸들을 저장할 변수를 선언
hcon = GetStdHandle(STD_OUTPUT_HANDLE); // 콘솔의 핸들 획득
을 반복해서 쓰지 않고, 파라미터를 숫자만으로 할수있도록 하기위해서이다.
#include<windows.h>
의 SetConsoleTextAttribute를 쓰는 방법은 아래와 같다.
-첫번째 방법
#include<stdio.h>
#include<windows.h>
int main(void)
{
HANDLE hcon; // 콘솔의 핸들을 저장할 변수를 선언
hcon = GetStdHandle(STD_OUTPUT_HANDLE); // 콘솔의 핸들 획득
//FOREGROUND_BLUE Text color contains blue.
//FOREGROUND_GREEN Text color contains green.
//FOREGROUND_RED Text color contains red.
//FOREGROUND_INTENSITY Text color is intensified.
//BACKGROUND_BLUE Background color contains blue.
//BACKGROUND_GREEN Background color contains green.
//BACKGROUND_RED Background color contains red.
//BACKGROUND_INTENSITY Background color is intensified.
SetConsoleTextAttribute(hcon, FOREGROUND_BLUE);
printf("1 ");
SetConsoleTextAttribute(hcon, FOREGROUND_GREEN);
printf("2 ");
SetConsoleTextAttribute(hcon, FOREGROUND_GREEN | FOREGROUND_BLUE);
printf("3 ");
SetConsoleTextAttribute(hcon, FOREGROUND_RED);
printf("4 ");
SetConsoleTextAttribute(hcon, FOREGROUND_RED | FOREGROUND_BLUE);
printf("5 ");
SetConsoleTextAttribute(hcon, FOREGROUND_RED | FOREGROUND_GREEN);
printf("6 ");
SetConsoleTextAttribute(hcon, FOREGROUND_INTENSITY);
printf("8 ");
SetConsoleTextAttribute(hcon, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf("9 ");
SetConsoleTextAttribute(hcon, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("10 ");
SetConsoleTextAttribute(hcon, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf("11 ");
SetConsoleTextAttribute(hcon, FOREGROUND_RED | FOREGROUND_INTENSITY);
printf("12 ");
SetConsoleTextAttribute(hcon, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf("13 ");
SetConsoleTextAttribute(hcon, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("14 ");
SetConsoleTextAttribute(hcon, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY | FOREGROUND_RED);
printf("15 ");
return 0;
//배경과 글자색을 동시에 바꾸려면
//SetConsoleTextAttribute(hcon, 글자색 | 배경색); 으로 한다.(순서 상관없음)
//예) SetConsoleTextAttribute(hcon, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY | FOREGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
}
-두번째 방법
#include <windows.h>
#include <stdio.h>
int main()
{
HANDLE hcon; // 콘솔의 핸들을 저장할 변수를 선언
hcon = GetStdHandle(STD_OUTPUT_HANDLE); // 콘솔의 핸들 획득
SetConsoleTextAttribute(hcon, 1);
printf("1 ");
SetConsoleTextAttribute(hcon, 2);
printf("2 ");
SetConsoleTextAttribute(hcon, 3);
printf("3 ");
SetConsoleTextAttribute(hcon, 4);
printf("4 ");
SetConsoleTextAttribute(hcon, 5);
printf("5 ");
SetConsoleTextAttribute(hcon, 6);
printf("6 ");
SetConsoleTextAttribute(hcon, 7);
printf("7 ");
SetConsoleTextAttribute(hcon, 8);
printf("8 ");
SetConsoleTextAttribute(hcon, 9);
printf("9 ");
SetConsoleTextAttribute(hcon, 10);
printf("10 ");
SetConsoleTextAttribute(hcon, 11);
printf("11 ");
SetConsoleTextAttribute(hcon, 12);
printf("12 ");
SetConsoleTextAttribute(hcon, 13);
printf("13 ");
SetConsoleTextAttribute(hcon, 14);
printf("14 ");
SetConsoleTextAttribute(hcon, 15);
printf("15 ");
return 0;
//배경과 글자색을 동시에 바꾸려면
//SetConsoleTextAttribute(hcon, 글자색 | 배경색); 으로 한다.(순서 상관없음)
//배경색은 16부터 시작.
//예) SetConsoleTextAttribute(hcon, 16 | 15);
}
-세번째 방법
#include<stdio.h>
#include<windows.h>
void SetColor(int color , int bgcolor )
{
color &= 0xf;
bgcolor &= 0xf ;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (bgcolor << 4) | color );
}
int main(void)
{
SetColor( 0 , 15);
printf("pacman game");
return 0;
//SetColor(글자색, 배경색);
}
여기서는 세번째 방법으로 했음
*/
int beginningLevel();//초급
void BColor(int n);//초급 메뉴 색을 출력하는 함수
int intermediateLevel();//중급
void IColor(int n);//중급 메뉴 색을 출력하는 함수
int advancedLevel();//고급
void AColor(int n);//고급 메뉴 색을 출력하는 함수
//<색을 출력하는 함수>
//메뉴에서 밝은 빨간색을 1, 어두운 빨간색은 0으로 하는데
//0과 1은 파라미터로 받은 n으로 결정
int PrintWin(int number);//이겼을 때의 화면을 축력하는 함수
/*
파라미터로 받은 number은 아래와 같은 문장을 상황에 따라 다르게 출력하기 위해서 쓰인다.
초급과 중급같은경우는 왼쪽 아래에 아래와 같이 출력된다.
printf("메인메뉴 : q, 다시하기 : a, 다음레벨 : w");
고급에서는 다음단계가 없으므로 초급과 중급과 달리 아래와 같이 출력된다.
printf("메인메뉴 : q, 다시하기 : a");
*/
int PrintGameOver();
int main(void)
{
char ch;//목록을 표현하기 위해 키보드로부터 입력받은 up, down 을 저장하는 변수
int result;//게임의 결과들을 저장하는 변수
int selected = 0 ;//초급/중급/고급 인지를 저장하는 변수
//game이 끝나서 콘솔창이 자동으로 닫히지 않도록 하기 위해서 while문을 써서 계속 반복함
//예를 들어 메뉴 -> 게임 -> 게임 -> 메뉴...
while(1)//main while
{
//맨위의 화면
gotoxy(23, 4);
SetColor(0, 14);
printf(" ");
gotoxy(23 , 5);
SetColor(0, 15);
printf(" pacman game ");
gotoxy(23, 6);
SetColor(0, 14);
printf(" ");
//처음 메뉴화면이 보이면 무조건 초급메뉴(제일 위)가 밝은 빨간색으로 하기위해 썼다.
BColor(1);
IColor(0);
AColor(0);
//목록
//엔터를 쳐야지만 빠져나가고 아니면 계속 목록의
//색깔들만 바꿈(밝은 빨간색 or 어두운 빨간색)
while( 1 )//menu while
{
ch = getch();
if(ch == '\r')//엔터를 처야지만 while을 빠져나감
break;
switch( ch )
{
case UP:
selected--;
if( selected < 0 )
selected = 0;
//마이너스가 되면 안되므로 0(초급)보다 작아지면 다시 0(초급)으로 만든다.
//초급에서 up해봤자 초급이게 하기위해서
break;
case DOWN:
selected++;
if( selected > 2 )
selected = 2;
//2(고급)보다 크면 안되므로 2(고급)보다 커지면 다시 2(고급)으로 만든다.
//고급에서 down해봤자 고급이게 하기위해서
break;
}
if(selected == 0)//초급이 선택되어 있을 때 초급만 밝은 빨간색
{
BColor(1);
IColor(0);
AColor(0);
}
else if(selected == 1)//중급이 선택되어 있을 때 중급만 밝은 빨간색
{
BColor(0);
IColor(1);
AColor(0);
}
else//곡급이 선택되어있을 때 고급만 밝은 빨간색
{
BColor(0);
IColor(0);
AColor(1);
}
//엔터를 치지 않았으므로 계속 반복
}//end of menu while
SetColor(0, 0);//배경색이 바뀌었을 경우 다시 검정색 배경으로 바꾸기 위해 사용
system("cls");//화면 지우기
result = Again;
//처음에는 무조건 반복을 기본으로함
//다음단계를 실행할 수 있게 while을 사용
while(1)//game while
{
switch(selected)//selected(지금의 단계)에 맞는 게임을 실행
{
case 0://초급
while(result == Again)//Again이면 게임에서 빠져나갈수 없다
{
result = beginningLevel();//초급게임 실행
if(result == Win)
result = PrintWin(1);
//파라미터 1은 다음단계를 출력하기 위해
//이겼을때의 화면을 출력하고 메인메뉴로이동 or 다음단계 or 다시하기
//중 선택한것을 다시 result에 저장
else if(result == GameOver)
result = PrintGameOver();
//졌을때의 화면을 출력하고 메인메뉴로이동 or 다시하기
//중 선택한것을 다시 result에 저장
//다음단계 하기
if(result == next)
{
selected = 1;//중급
result = Again;//result를 next -> Again으로 바꿈
break;
}
}
break;
case 1://중급
while(result == Again){
result = intermediateLevel();//중급게임 실행
if(result == Win)
result = PrintWin(1);
else if(result == GameOver)
result = PrintGameOver();
if(result == next)
{
selected = 2;//고급
result = Again;
break;
}
}
break;
case 2://고급
while(result == Again){
result = advancedLevel();//고급단계실행
if(result == Win)
result = PrintWin(0);
//메인메뉴로이동 or 다시하기
if(result == GameOver)
result = PrintGameOver();
}
break;
//고급이 마지막 단계이므로 next 는 없다.
}
if(result == MainMenu)
break;
//메인메뉴로 이동이므로 이곳의 while에서 벗어나 제일 바깥의 while처음으로 감
}//end of game while
system("cls");//화면 지우기
selected = 0;//메인메뉴화면은 무조건 초급이 밝은 빨간색일때부터 시작이므로
//selected 는 0이 되어야 한다.
}//end of main while
return 0 ;
}
// 커서좌표이동
void gotoxy(unsigned int x, unsigned int y)
{
COORD xy = {x, y} ;
SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE) , xy );
}
// 테스트색상변경
void SetColor(int color , int bgcolor )
{
color &= 0xf;
bgcolor &= 0xf ;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (bgcolor << 4) | color );
}
void BColor(int n)
{
if(n == 1){
gotoxy(28, 9);
SetColor(0, 12);
printf(" ");
gotoxy(28 , 10);
printf(" 초급 ");
gotoxy(28, 11);
printf(" ");
}
else if(n == 0){
gotoxy(28, 9);
SetColor(0, 4);
printf(" ");
gotoxy(28 , 10);
printf(" 초급 ");
gotoxy(28, 11);
printf(" ");
}
}
void IColor(int n)
{
if(n == 0){
gotoxy(28, 13);
SetColor(0, 4);
printf(" ");
gotoxy(28 , 14);
printf(" 중급 ");
gotoxy(28, 15);
printf(" ");
}
else if(n == 1){
gotoxy(28, 13);
SetColor(0, 12);
printf(" ");
gotoxy(28 , 14);
printf(" 중급 ");
gotoxy(28, 15);
printf(" ");
}
}
void AColor(int n)
{
if(n == 1){
gotoxy(28, 17);
SetColor(0, 12);
printf(" ");
gotoxy(28 , 18);
printf(" 고급 ");
gotoxy(28, 19);
printf(" ");
}
else if(n == 0){
gotoxy(28, 17);
SetColor(0, 4);
printf(" ");
gotoxy(28 , 18);
printf(" 고급 ");
gotoxy(28, 19);
printf(" ");
}
}
//초급
int beginningLevel()
{
int i, j;//for
int xp=9;//나의 x좌표
int yp=1;//나의 y좌표
int ch = 0;
int num=0;//먹이 개수
int score = 0;//점수
int ey=19;//적의 y좌표
int ex=1;//적의 x좌표
int moving, count = 0;
int before = 5;//0, 1, 2, 3 을 제외한 아무 숫자
int beforeMap = 2;//적이 처음 이동할때 적이 있었던 자리는 빈칸이됨
char map[11][21] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 0},
{0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0},
{0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
{0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0},
{0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0},
{0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
system("cls");//화면 지우기
//먹이를 랜덤으로 만듬
for(i=0; i<11; i++){
for(j=0; j<21; j++)
if(map[i][j]==1)
{
map[i][j] = Urand(1);//Urand()로 먹이의 개수 조절
if(map[i][j] == 1)
num++;
else
map[i][j] =2;
}
}//end of for
num = num*10;//총점수. 먹이가 rand로 바뀌므로 그때그때 점수가 다름
while(1){
//왼쪽 아래
gotoxy(3, 23);
SetColor(15, 0);
printf("메인메뉴 : q , 다시하기 : a");
//점수 출력, 오른쪽 아래
gotoxy(60, 23);
printf("점수 : %d / %d", score, num);
if(score == num)
return Win;
//키보드 입력시 무조건적으로 맵 출력
for(i=0; i<11; i++){
gotoxy(17, (6+i));
for(j=0; j<21; j++){
switch(map[i][j]){
case 0://벽
SetColor(9 ,15);
printf("■");
break;
case 1://먹이
SetColor(12, 0);
printf("♡");
break;
case 2://빈칸
SetColor(0, 0);
printf(" ");
break;
case 3://나
SetColor(14 ,0);
if(ch == LEFT)
printf("◀");
else if(ch == RIGHT)
printf("▶");
else if(ch == UP)
printf("▲");
else if(ch == DOWN)
printf("▼");
else
printf("●");
break;
case 4://적
SetColor(10, 0);
printf("◆");
break;
}//end of switch
}//end of j_for
SetColor(0, 0);
}//end of i_for
ch = getch();
if(ch == ESC)
return MainMenu;
//result에 MainMenu를 반환하고 종료. 메인메뉴로 이동
else if(ch == AGAIN)
return Again;
//result에 Again를 반환하고 종료. 게임 다시시작
//적의 이동
while(count == 0)//이동하기 전은 0, 이동한 후는 1
{
//벽이어서 이동하지 못하면 count는 0
moving = Urand(3);//갈 방향 4개를 랜덤으로 정함
switch(moving)
{
case 0://left
if(map[ex][ey-1] != 0 && moving != before)
//벽이 아니고 전에 움직인곳(before)이 아닐경우
{
if(map[ex][ey-1] == 1)//먹이일경우
{
map[ex][--ey] = 4;
map[ex][ey+1] = beforeMap;
//적이 움직이기 전에 있었던 자리에 기억해뒀던 beforeMap이 됨
count = 1;//이동한 후로 바꿈
before = 1;//오른쪽에서 왼쪽으로 이동했으므로 before은 오른쪽이 됨
beforeMap = 1;//적이 움직인 후의 자리에 있었던 것을 기억
}
else if(map[ex][ey-1] == 2)//빈칸일경우
{
map[ex][--ey] = 4;
map[ex][ey+1] = beforeMap;
count = 1;
before = 1;//오른쪽에서 왼쪽으로 이동했으므로 before은 오른쪽이 됨
beforeMap = 2;
}
else if(map[ex][ey-1] == 3)//적 == 나 일 경우
return GameOver;
}
break;
case 1://light
if(map[ex][ey+1] != 0 && moving != before)
{
if(map[ex][ey+1] == 1)
{
map[ex][++ey] = 4;
map[ex][ey-1] = beforeMap;
count = 1;
before = 0;//왼쪽에서 오른쪽으로 이동했으므로 before은 왼쪽이 됨
beforeMap = 1;
}
else if(map[ex][ey+1] == 2)
{
map[ex][++ey] = 4;
map[ex][ey-1] = beforeMap;
count = 1;
before = 0;//왼쪽에서 오른쪽으로 이동했으므로 before은 왼쪽이 됨
beforeMap = 2;
}
else if(map[ex][ey+1] == 3)
return GameOver;
}
break;
case 2://down
if(map[ex-1][ey] != 0 && moving != before)
{
if(map[ex-1][ey] == 1)
{
map[--ex][ey] = 4;
map[ex+1][ey] = beforeMap;
count = 1;
before = 3;//위쪽에서 아래쪽으로 이동했으므로 before은 위쪽이 됨
beforeMap = 1;
}
else if(map[ex-1][ey] == 2)
{
map[--ex][ey] = 4;
map[ex+1][ey] = beforeMap;
count = 1;
before = 3;//위쪽에서 아래쪽으로 이동했으므로 before은 위쪽이 됨
beforeMap = 2;
}
else if(map[ex-1][ey] == 3)
return GameOver;
}
break;
case 3://up
if(map[ex+1][ey] != 0 && moving != before)
{
if(map[ex+1][ey] == 1)
{
map[++ex][ey] = 4;
map[ex-1][ey] = beforeMap;
count = 1;
before = 2;//아래쪽에서 위쪽으로 이동했으므로 before은 아래쪽이 됨
beforeMap = 1;
}
else if(map[ex+1][ey] == 2)
{
map[++ex][ey] = 4;
map[ex-1][ey] = beforeMap;
count = 1;
before = 2;//아래쪽에서 위쪽으로 이동했으므로 before은 아래쪽이 됨
beforeMap = 2;
}
else if(map[ex+1][ey] == 3)
return GameOver;
}
break;
}
}//end of while
count = 0;//적이 이동한후이므로 무조건 다시 이동전으로 바꿈
switch(ch)
{
case LEFT:
if(map[xp][yp-1] != 0)//벽일 아닐 경우
{
if(map[xp][yp-1] == 4)//적 == 나 일 경우
{
map[xp][yp] = 2;
map[xp][--yp] = 4;
return GameOver;//main의 result에 GameOver 반환
}
if(map[xp][yp-1] == 1)//먹이일 경우
score+=10;//점수 증가
map[xp][yp] = 2;//나의 이동
map[xp][--yp] = 3;
//이동했던곳이 먹이이든 빈칸이든 나 가 지나간 자리는 무조건 빈칸
}//end of if
break;
case RIGHT:
if(map[xp][yp+1] != 0)
{
if(map[xp][yp+1] == 4)
{
map[xp][yp] = 2;
map[xp][++yp] = 4;
return GameOver;
}
if(map[xp][yp+1] == 1)
score+=10;
map[xp][yp] = 2;
map[xp][++yp] = 3;
}//end of if
break;
case UP:
if(map[xp-1][yp] != 0)
{
if(map[xp-1][yp] == 4)
{
map[xp][yp] = 2;
map[--xp][yp] = 4;
return GameOver;
}
if(map[xp-1][yp] == 1)
score+=10;
map[xp][yp] = 2;
map[--xp][yp] = 3;
}//end of if
break;
case DOWN:
if(map[xp+1][yp] != 0)
{
if(map[xp+1][yp] == 4)
{
map[xp][yp] = 2;
map[++xp][yp] = 4;
return GameOver;
}
if(map[xp+1][yp] == 1)
score+=10;
map[xp][yp] = 2;
map[++xp][yp] = 3;
}//end of if
break;
}//end of switch
}//end of while
return 0;
}//end of beginningLevel method
//중급
int intermediateLevel(){
int i, j;
int xp=13;
int yp=1;
int ch=0;
int num=0;//먹이 개수
int score = 0;//점수
int ey=33;
int ex=1;
int moving, count = 0, before = 5;
int beforeMap = 2;
char map[15][35] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0,},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 0,},
{0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0 ,0, 0, 1, 0,},
{0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1 ,1, 0, 1, 0,},
{0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0 ,1, 0, 1, 0,},
{0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 ,1, 1, 1, 0,},
{0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 ,1, 0, 1, 0,},
{0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ,1, 0, 1, 0,},
{0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 ,1, 0, 1, 0,},
{0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 ,1, 1, 1, 0,},
{0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0 ,1, 0, 1, 0,},
{0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1 ,1, 0, 1, 0,},
{0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0 ,0, 0, 1, 0,},
{0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0,}};
system("cls");
for(i=0; i<15; i++){
for(j=0; j<35; j++)
if(map[i][j]==1)
{
map[i][j] = Urand(2);
if(map[i][j] == 1)
num++;
else
map[i][j] =2;
}
}//end of for
num = num*10;//총점수
while(1){
gotoxy(3, 23);
SetColor(15, 0);
printf("메인메뉴 : q , 다시하기 : a");
//점수 출력
gotoxy(60, 23);
printf("점수 : %d / %d", score, num);
if(score == num)
return Win;
//맵 출력
for(i=0; i<15; i++){
gotoxy(5, (4+i));
for(j=0; j<35; j++){
switch(map[i][j]){
case 0:
SetColor(9 ,15);
printf("■");
break;
case 1:
SetColor(12, 0);
printf("♡");
break;
case 2:
SetColor(0, 0);
printf(" ");
break;
case 3:
SetColor(14 ,0);
if(ch == LEFT)
printf("◀");
else if(ch == RIGHT)
printf("▶");
else if(ch == UP)
printf("▲");
else if(ch == DOWN)
printf("▼");
else
printf("●");
break;
case 4:
SetColor(13, 0);
printf("◆");
break;
}//end of switch
}//end of j_for
SetColor(0, 0);
printf("\n");
}//end of i_for
ch = getch();
if(ch == ESC)
return MainMenu;
else if(ch == AGAIN)
return Again;
//이동할 곳이 벽인지 아닌지 검사
while(count == 0){
moving = Urand(3);
switch(moving)
{
case 0:
if(map[ex][ey-1] != 0 && moving != before)
{
if(map[ex][ey-1] == 1)
{
map[ex][--ey] = 4;
map[ex][ey+1] = beforeMap;
count = 1;
before = 1;
beforeMap = 1;
}
else if(map[ex][ey-1] == 2)
{
map[ex][--ey] = 4;
map[ex][ey+1] = beforeMap;
count = 1;
before = 1;
beforeMap = 2;
}
else if(map[ex][ey-1] == 3)
return GameOver;
}
break;
case 1:
if(map[ex][ey+1] != 0 && moving != before)
{
if(map[ex][ey+1] == 1)
{
map[ex][++ey] = 4;
map[ex][ey-1] = beforeMap;
count = 1;
before = 0;
beforeMap = 1;
}
else if(map[ex][ey+1] == 2)
{
map[ex][++ey] = 4;
map[ex][ey-1] = beforeMap;
count = 1;
before = 0;
beforeMap = 2;
}
else if(map[ex][ey+1] == 3)
return GameOver;
}
break;
case 2:
if(map[ex-1][ey] != 0 && moving != before)
{
if(map[ex-1][ey] == 1)
{
map[--ex][ey] = 4;
map[ex+1][ey] = beforeMap;
count = 1;
before = 3;
beforeMap = 1;
}
else if(map[ex-1][ey] == 2)
{
map[--ex][ey] = 4;
map[ex+1][ey] = beforeMap;
count = 1;
before = 3;
beforeMap = 2;
}
else if(map[ex-1][ey] == 3)
return GameOver;
}
break;
case 3:
if(map[ex+1][ey] != 0 && moving != before)
{
if(map[ex+1][ey] == 1)
{
map[++ex][ey] = 4;
map[ex-1][ey] = beforeMap;
count = 1;
before = 2;
beforeMap = 1;
}
else if(map[ex+1][ey] == 2)
{
map[++ex][ey] = 4;
map[ex-1][ey] = beforeMap;
count = 1;
before = 2;
beforeMap = 2;
}
else if(map[ex+1][ey] == 3)
return GameOver;
}
break;
}
}//end of while
count = 0;
switch(ch)
{
case LEFT:
if(map[xp][yp-1] != 0)
{
if(map[xp][yp-1] == 4)
{
map[xp][yp] = 2;
map[xp][--yp] = 4;
return GameOver;
}
if(map[xp][yp-1] == 1)
score+=10;
map[xp][yp] = 2;
map[xp][--yp] = 3;
}//end of if
break;
case RIGHT:
if(map[xp][yp+1] != 0)
{
if(map[xp][yp+1] == 4)
{
map[xp][yp] = 2;
map[xp][++yp] = 4;
return GameOver;
}
if(map[xp][yp+1] == 1)
score+=10;
map[xp][yp] = 2;
map[xp][++yp] = 3;
}//end of if
break;
case UP:
if(map[xp-1][yp] != 0)
{
if(map[xp-1][yp] == 4)
{
map[xp][yp] = 2;
map[--xp][yp] = 4;
return GameOver;
}
if(map[xp-1][yp] == 1)
score+=10;
map[xp][yp] = 2;
map[--xp][yp] = 3;
}//end of if
break;
case DOWN:
if(map[xp+1][yp] != 0)
{
if(map[xp+1][yp] == 4)
{
map[xp][yp] = 2;
map[++xp][yp] = 4;
return GameOver;
}
if(map[xp+1][yp] == 1)
score+=10;
map[xp][yp] = 2;
map[++xp][yp] = 3;
}//end of if
break;
}//end of switch
}//end of while
return 0;
}
//고급
int advancedLevel(){
int i, j;
int xp=13;
int yp=1;
int ch = 0;
int num=0;//먹이 개수
int score = 0;//점수
int Gey=1, Gex = 1, Gmoving, Gcount = 0, Gbefore = 5, GbeforeMap = 2;
int Pex = 13, Pey = 33, Pmoving, Pcount = 0, Pbefore = 5, PbeforeMap = 2;
char map[15][35] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0,},
{0, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,},
{0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0 ,0, 0, 1, 0,},
{0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1 ,1, 0, 1, 0,},
{0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0 ,1, 0, 1, 0,},
{0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 ,1, 1, 1, 0,},
{0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 ,1, 0, 1, 0,},
{0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ,1, 0, 1, 0,},
{0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 ,1, 0, 1, 0,},
{0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 ,1, 1, 1, 0,},
{0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0 ,1, 0, 1, 0,},
{0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1 ,1, 0, 1, 0,},
{0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0 ,0, 0, 1, 0,},
{0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0,}};
system("cls");
for(i=0; i<15; i++){
for(j=0; j<35; j++)
if(map[i][j]==1)
{
map[i][j] = Urand(2);
if(map[i][j] == 1)
num++;
else
map[i][j] =2;
}
}//end of for
num = num*10;//총점수
while(1){
gotoxy(3, 23);
SetColor(15, 0);
printf("메인메뉴 : q , 다시하기 : a");
//점수 출력
gotoxy(60, 23);
printf("점수 : %d / %d", score, num);
if(score == num)
return Win;
//맵 출력
for(i=0; i<15; i++){
gotoxy(5, (4+i));
for(j=0; j<35; j++){
switch(map[i][j]){
case 0:
SetColor(9 ,15);
printf("■");
break;
case 1:
SetColor(12, 0);
printf("♡");
break;
case 2:
SetColor(0, 0);
printf(" ");
break;
case 3:
SetColor(14 ,0);
if(ch == LEFT)
printf("◀");
else if(ch == RIGHT)
printf("▶");
else if(ch == UP)
printf("▲");
else if(ch == DOWN)
printf("▼");
else
printf("●");
break;
case 4:
SetColor(10, 0);
printf("◆");
break;
case 5:
SetColor(13, 0);
printf("◆");
break;
}//end of switch
}//end of j_for
SetColor(0, 0);
printf("\n");
}//end of i_for
ch = getch();
if(ch == ESC)
return MainMenu;
else if(ch == AGAIN)
return Again;
//이동할 곳이 벽인지 아닌지 검사
while(Gcount == 0){
Gmoving = Urand(3);
switch(Gmoving)
{
case 0:
if(map[Gex][Gey-1] != 0 && Gmoving != Gbefore)
{
if(map[Gex][Gey-1] == 1)
{
map[Gex][--Gey] = 4;
map[Gex][Gey+1] = GbeforeMap;
Gcount = 1;
Gbefore = 1;
GbeforeMap = 1;
}
else if(map[Gex][Gey-1] == 2)
{
map[Gex][--Gey] = 4;
map[Gex][Gey+1] = GbeforeMap;
Gcount = 1;
Gbefore = 1;
GbeforeMap = 2;
}
else if(map[Gex][Gey-1] == 5)
{
map[Gex][--Gey] = 5;
map[Gex][Gey+1] = GbeforeMap;
}
else if(map[Gex][Gey-1] == 3)
return GameOver;
}
break;
case 1:
if(map[Gex][Gey+1] != 0 && Gmoving != Gbefore)
{
if(map[Gex][Gey+1] == 1)
{
map[Gex][++Gey] = 4;
map[Gex][Gey-1] = GbeforeMap;
Gcount = 1;
Gbefore = 0;
GbeforeMap = 1;
}
else if(map[Gex][Gey+1] == 2)
{
map[Gex][++Gey] = 4;
map[Gex][Gey-1] = GbeforeMap;
Gcount = 1;
Gbefore = 0;
GbeforeMap = 2;
}
else if(map[Gex][Gey+1] == 5)
{
map[Gex][++Gey] = 5;
map[Gex][Gey-1] = GbeforeMap;
}
else if(map[Gex][Gey+1] == 3)
return GameOver;
}
break;
case 2:
if(map[Gex-1][Gey] != 0 && Gmoving != Gbefore)
{
if(map[Gex-1][Gey] == 1)
{
map[--Gex][Gey] = 4;
map[Gex+1][Gey] = GbeforeMap;
Gcount = 1;
Gbefore = 3;
GbeforeMap = 1;
}
else if(map[Gex-1][Gey] == 2)
{
map[--Gex][Gey] = 4;
map[Gex+1][Gey] = GbeforeMap;
Gcount = 1;
Gbefore = 3;
GbeforeMap = 2;
}
else if(map[Gex-1][Gey] == 5)
{
map[--Gex][Gey] = 5;
map[Gex+1][Gey] = GbeforeMap;
}
else if(map[Gex-1][Gey] == 3)
return GameOver;
}
break;
case 3:
if(map[Gex+1][Gey] != 0 && Gmoving != Gbefore)
{
if(map[Gex+1][Gey] == 1)
{
map[++Gex][Gey] = 4;
map[Gex-1][Gey] = GbeforeMap;
Gcount = 1;
Gbefore = 2;
GbeforeMap = 1;
}
else if(map[Gex+1][Gey] == 2)
{
map[++Gex][Gey] = 4;
map[Gex-1][Gey] = GbeforeMap;
Gcount = 1;
Gbefore = 2;
GbeforeMap = 2;
}
else if(map[Gex+1][Gey] == 5)
{
map[++Gex][Gey] = 5;
map[Gex-1][Gey] = GbeforeMap;
}
else if(map[Gex+1][Gey] == 3)
return GameOver;
}
break;
}
}//end of while
Gcount = 0;
while(Pcount == 0){
Pmoving = Urand(3);
switch(Pmoving)
{
case 0:
if(map[Pex][Pey-1] != 0 && Pmoving != Pbefore)
{
if(map[Pex][Pey-1] == 1)
{
map[Pex][--Pey] = 5;
map[Pex][Pey+1] = PbeforeMap;
Pcount = 1;
Pbefore = 1;
PbeforeMap = 1;
}
else if(map[Pex][Pey-1] == 2)
{
map[Pex][--Pey] = 5;
map[Pex][Pey+1] = PbeforeMap;
Pcount = 1;
Pbefore = 1;
PbeforeMap = 2;
}
else if(map[Pex][Pey-1] ==4)
{
map[Pex][--Pey] = 5;
map[Pex][Pey+1] = PbeforeMap;
}
else if(map[Pex][Pey-1] == 3)
return GameOver;
}
break;
case 1:
if(map[Pex][Pey+1] != 0 && Pmoving != Pbefore)
{
if(map[Pex][Pey+1] == 1)
{
map[Pex][++Pey] = 5;
map[Pex][Pey-1] = PbeforeMap;
Pcount = 1;
Pbefore = 0;
PbeforeMap = 1;
}
else if(map[Pex][Pey+1] == 2)
{
map[Pex][++Pey] = 5;
map[Pex][Pey-1] = PbeforeMap;
Pcount = 1;
Pbefore = 0;
PbeforeMap = 2;
}
else if(map[Pex][Pey+1] == 4)
{
map[Pex][++Pey] = 5;
map[Pex][Pey-1] = PbeforeMap;
}
else if(map[Pex][Pey+1] == 3)
return GameOver;
}
break;
case 2:
if(map[Pex-1][Pey] != 0 && Pmoving != Pbefore)
{
if(map[Pex-1][Pey] == 1)
{
map[--Pex][Pey] = 5;
map[Pex+1][Pey] = PbeforeMap;
Pcount = 1;
Pbefore = 3;
PbeforeMap = 1;
}
else if(map[Pex-1][Pey] == 2)
{
map[--Pex][Pey] = 5;
map[Pex+1][Pey] = PbeforeMap;
Pcount = 1;
Pbefore = 3;
PbeforeMap = 2;
}
else if(map[Pex-1][Pey] == 4)
{
map[--Pex][Pey] = 5;
map[Pex+1][Pey] = PbeforeMap;
}
else if(map[Pex-1][Pey] == 3)
return GameOver;
}
break;
case 3:
if(map[Pex+1][Pey] != 0 && Pmoving != Pbefore)
{
if(map[Pex+1][Pey] == 1)
{
map[++Pex][Pey] = 5;
map[Pex-1][Pey] = PbeforeMap;
Pcount = 1;
Pbefore = 2;
PbeforeMap = 1;
}
else if(map[Pex+1][Pey] == 2)
{
map[++Pex][Pey] = 5;
map[Pex-1][Pey] = PbeforeMap;
Pcount = 1;
Pbefore = 2;
PbeforeMap = 2;
}
else if(map[Pex+1][Pey] == 4)
{
map[++Pex][Pey] = 5;
map[Pex-1][Pey] = PbeforeMap;
}
else if(map[Pex+1][Pey] == 3)
return GameOver;
}
break;
}
}//end of while
Pcount = 0;
switch(ch)
{
case LEFT:
if(map[xp][yp-1] != 0)
{
if(map[xp][yp-1] == 4 || map[xp][yp-1] == 5)
{
map[xp][yp] = 2;
map[xp][--yp] = 5;
return GameOver;
}
if(map[xp][yp-1] == 1)
score+=10;
map[xp][yp] = 2;
map[xp][--yp] = 3;
}//end of if
break;
case RIGHT:
if(map[xp][yp+1] != 0)
{
if(map[xp][yp+1] == 4 || map[xp][yp+1] == 5)
{
map[xp][yp] = 2;
map[xp][++yp] = 5;
return GameOver;
}
if(map[xp][yp+1] == 1)
score+=10;
map[xp][yp] = 2;
map[xp][++yp] = 3;
}//end of if
break;
case UP:
if(map[xp-1][yp] != 0)
{
if(map[xp-1][yp] == 4 || map[xp-1][yp] == 5)
{
map[xp][yp] = 2;
map[--xp][yp] = 5;
return GameOver;
}
if(map[xp-1][yp] == 1)
score+=10;
map[xp][yp] = 2;
map[--xp][yp] = 3;
}//end of if
break;
case DOWN:
if(map[xp+1][yp] != 0)
{
if(map[xp+1][yp] == 4 || map[xp+1][yp] == 5)
{
map[xp][yp] = 2;
map[++xp][yp] = 5;
return GameOver;
}
if(map[xp+1][yp] == 1)
score+=10;
map[xp][yp] = 2;
map[++xp][yp] = 3;
}//end of if
break;
}//end of switch
}//end of while
return 0;
}
int PrintWin(int number)
{
char ch;
system("cls");
SetColor(11, 9);
gotoxy(24, 10);
printf(" ");
gotoxy(24 , 11);
printf(" Win ");
gotoxy(24, 12);
printf(" ");
SetColor(0, 0);
gotoxy(3, 23);
SetColor(15, 0);
if(number == 0)
printf("메인메뉴 : q, 다시하기 : a");
else if(number == 1)
printf("메인메뉴 : q, 다시하기 : a, 다음레벨 : w");
while(1)
{
ch = getch();
if(ch == ESC)
return MainMenu;
else if(ch == AGAIN)
return Again;
else if(ch == NextL)
return next;
else
fflush(stdin);
}
return 0;
}
int PrintGameOver()
{
char ch;
system("cls");
SetColor(0, 12);
gotoxy(24, 10);
printf(" ");
gotoxy(24 , 11);
printf(" Game Over ");
gotoxy(24, 12);
printf(" ");
SetColor(0, 0);
gotoxy(3, 23);
SetColor(15, 0);
printf("메인메뉴 : q, 다시하기 : a");
while(1)
{
ch = getch();
if(ch == ESC)
return MainMenu;
else if(ch == AGAIN)
return Again;
else
fflush(stdin);
}
return 0;
}
*--------------------------------------*
참 쓸말이 없네요....
궁금하신 점 있으시면 언제든지 댓글 남겨주세요~
언제 잠적할지는 모르겠지만....확인하도록 노력하겠습니다!
그럼 저는 이만 시험 공부하러 가겠습니다ㅜ_ㅜ...





실행파일도 올려주세요~ ^^