...
똥피하기 게임
C언어로 기말고사 작품으로 만든 게임 프로그램을 올려본다.
순수 문자열로만 이루어진 프로그램이며, 구조체를 이용하여 작성하였다.
소스 코드 :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <conio.h>
// 72
//75 80 77
#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
#define ENTER 0xd
#define ESC 0x1b
#define COL GetStdHandle(STD_OUTPUT_HANDLE)
#define BLACK SetConsoleTextAttribute(COL, 0x0000);
#define DARK_BLUE SetConsoleTextAttribute(COL, 0x0001);
#define GREEN SetConsoleTextAttribute(COL, 0x0002);
#define BLUE_GREEN SetConsoleTextAttribute(COL, 0x0003);
#define BLOOD SetConsoleTextAttribute(COL, 0x0004);
#define PURPLE SetConsoleTextAttribute(COL, 0x0005);
#define DARK_YELLOW SetConsoleTextAttribute(COL, 0x0006);
#define WHITE SetConsoleTextAttribute(COL, 0x0007);
#define GRAY SetConsoleTextAttribute(COL, 0x0008);
#define BLUE SetConsoleTextAttribute(COL, 0x0009);
#define LIGHT_GREEN SetConsoleTextAttribute(COL, 0x000a);
#define LIGHT_BLUE SetConsoleTextAttribute(COL, 0x000b);
#define RED SetConsoleTextAttribute(COL, 0x000c);
#define PLUM SetConsoleTextAttribute(COL, 0x000d);
#define YELLOW SetConsoleTextAttribute(COL, 0x000e);
#define LIGHT_WHITE SetConsoleTextAttribute(COL, 0x000f);
void gotoxy(int x,int y);
void map_ptf();
void move();
void ddong(int *n, int *ran);
void menu();
void move_menu();
int player_dead(int *ran, int *n);
struct cursor
{
int by, by_last;
int cx,cy;
};
struct game_source
{
int score,score_ptf;
int diff, life;
int begin_menu, menu_change, start_re;
char diff_ch[5];
};
//typedef struct cursor CURSOR;
//typedef struct game_source GAME_SOURCE;
struct cursor cursor1 = {17,17,12,18};
//by,by_last,cx,cy
struct game_source game_source1 = {0,1000,0,5,0,0,0,"초급"};
//score,score_ptf,diff,life,begin_menu,menu_change,start_re
//static int bx=16,by=17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,0,1,
1,0,0,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1
}};
int main()
{
system("title 똥 피하기");
system("mode con: cols=50 lines=27");//지역변수
int kb,kb1,n[20],ran[20],swit[19],sleep_num;
register int i,j;
srand(time(NULL));
while(1)
{
do
{
menu();
gotoxy(20,17);
if(game_source1.start_re==0)
{
LIGHT_BLUE printf(" 시작하기");
}
else
{
RED printf(" 다시하기");
}
gotoxy(20,20);
BLUE printf("난이도 설정");
gotoxy(20,23);
YELLOW printf(" 끝내기");
WHITE
gotoxy(16,cursor1.by);
printf("▷");
gotoxy(32,cursor1.by);
printf("◁");
while(1)
{
move_menu();
if(game_source1.menu_change == 1)//난이도 설정 눌렀을시
{
system("cls");
break;
}
else if(game_source1.begin_menu == 1)//시작하기 눌렀을시
break;
}
if(game_source1.menu_change == 1)//난이도 설정 눌렀을시
{
menu();
gotoxy(20,17);
GRAY printf(" 초급");
gotoxy(20,20);
DARK_YELLOW printf(" 중급");
gotoxy(20,23);
BLOOD printf(" 고급");
WHITE
cursor1.by = cursor1.by_last;
gotoxy(16,cursor1.by);
printf("▷");
gotoxy(32,cursor1.by);
printf("◁");
while(1)
{
move_menu();
if(game_source1.menu_change == 0)
{
system("cls");
break;
}
}
cursor1.by_last = cursor1.by;
cursor1.by = 20;
}
}while(game_source1.begin_menu==0);
//bx=18,
cursor1.by=17,cursor1.cx=12,cursor1.cy=18,game_source1.score=0,game_source1.life=5,game_source1.start_re=0,game_source1.begin_menu=0;
for(i=0;i<20;i++)
n[i]=1;
for(i=0;i<19;i++)
swit[i]=0;
gotoxy(0,0);
system("cls");
map_ptf(0);//맵출력
gotoxy(cursor1.cx,cursor1.cy);
BLUE_GREEN printf("%s", player);
/*최초 랜덤 초기화*/
for(i=0;i<20;i++)
{
do
{
ran[i] = rand()%19+2;
}while((ran[i] % 2 == 0 ? 1 : 0) == 0);//홀수가 나오면 네모 테두리칸이랑 똥칸이랑 맞질 않으니 짝수로 설정
}
/*목차*/
gotoxy(28,2);
PURPLE printf("난이도 : %s", game_source1.diff_ch);
gotoxy(28,6);
LIGHT_GREEN printf("점수 : ");
gotoxy(28,8);
RED printf("목숨 : ");
gotoxy(28,9);
BLUE printf("승리 조건 :");
gotoxy(40,9);
printf("%d점", game_source1.score_ptf);
gotoxy(28,11);
GRAY printf(" 조작키");
gotoxy(28,13);
printf(" ← →");
if(game_source1.diff == 0)
sleep_num = 22;
else if(game_source1.diff == 1)
sleep_num = 20;
else if(game_source1.diff == 2)
sleep_num = 10;
int j_count=0;
/*게임 실행중*/
for(i=1; ;i++)
{
Sleep(sleep_num);
if(kbhit())
move();
if(i % 100 == 0)
{
j_count++;
}
if(kbhit())
move();
for(j=0;j<=j_count;j++)
{
ddong(&n[j],&ran[j]);
}
if(kbhit())
move();
Sleep(sleep_num);
if(kbhit())
move();
for(j=0;j<20;j++)
{
int a;
a = player_dead(&ran[j],&n[j]);
if(a == 1)
{
if(game_source1.life==0)
{
break;
}
else
{
game_source1.life--;
game_source1.score -= 5;
}
}
}
if(kbhit())
move();
Sleep(sleep_num);
if(kbhit())
move();
gotoxy(35,6);
LIGHT_GREEN printf("%-4d", game_source1.score);
gotoxy(35,8);
RED printf("%d", game_source1.life);
gotoxy(48,26);
if(kbhit())
move();
Sleep(sleep_num);
if(kbhit())
move();
if(game_source1.score > game_source1.score_ptf)//승리조건
break;
else if(game_source1.life==0)//패배조건
break;
}
if(game_source1.score > game_source1.score_ptf)//승리조건
{
gotoxy(0,0);
map_ptf(1);
gotoxy(5,3);
RED printf("ESC - 돌아가기");
do
{
kb1 = getch();
}while(kb1 != ESC);
system("cls");
game_source1.start_re =1;
}
else if(game_source1.life == 0 && game_source1.score < game_source1.score_ptf)//패배조건
{
gotoxy(5,10);
RED printf(" 게임 오버");
gotoxy(5,11);
printf("ESC - 돌아가기");
do
{
kb1 = getch();
}while(kb1 != ESC);
system("cls");
game_source1.start_re =1;
}
}
printf("\n\n");
}
void gotoxy(int x,int y)
{
COORD Cur;
Cur.X=x;
Cur.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Cur);
}
void map_ptf(int k)
{
int i,j;
for(i=0;i<20;i++)
{
for(j=0;j<12;j++)
{
switch(map[k][i][j])
{
case 0 :
printf(" "); break;
case 1 :
PLUM printf("□"); break;
case 2 :
YELLOW printf("▒"); break;
}
}
putchar('\n');
}
}
void ddong(int *n, int *ran)
{
if(*n < 19)
{
if(*n != 1)
{
gotoxy(*ran,*n-1);
printf(" ");
}
gotoxy(*ran,*n);
YELLOW printf("♨");
(*n)++;
}
else
{
gotoxy(*ran,*n-1);
printf(" ");
gotoxy(cursor1.cx,cursor1.cy);
BLUE_GREEN printf("%s",player);//똥과 플레이어가 같은 위치에 위치될경우 사라지는 현상 버그 픽스
*n=1;
game_source1.score+=5;
do
{
*ran = rand()%19+2;
}while(((*ran) % 2 == 0 ? 1 : 0) == 0);
}
gotoxy(48,26);
}
void move()
{
register int kb;
kb = getch();
switch(kb)
{
case LEFT :
if(map[0][cursor1.cy][cursor1.cx/2-1] == 0)//이동할 위치에 배열이 있다.
//이동할 위치 = 배열값이니 충돌조건따진다.
{
gotoxy(cursor1.cx,cursor1.cy);//현재 캐릭터로 커서이동
printf(" ");//지우기
cursor1.cx-=2;//방향키따라 커서 값 변경
gotoxy(cursor1.cx,cursor1.cy);//커서 이동
BLUE_GREEN printf("%s", player);//캐릭터 출력
gotoxy(48,26);//커서 내보내기
}
break;
case RIGHT :
if(map[0][cursor1.cy][cursor1.cx/2+1] == 0)
{
gotoxy(cursor1.cx,cursor1.cy);
printf(" ");
cursor1.cx+=2;
gotoxy(cursor1.cx,cursor1.cy);
BLUE_GREEN printf("%s", player);
gotoxy(48,26);
}
break;
/*case UP :
gotoxy(cursor1.cx,cursor1.cy);
BLUE_GREEN printf("%s", player);
gotoxy(48,26);
break;*/
}
}
void move_menu()
{
int kb1 = getch();
switch(kb1)
{
case DOWN :
if(cursor1.by+3 <= 23)
{
gotoxy(16,cursor1.by);
printf(" ");
gotoxy(32,cursor1.by);
printf(" ");
cursor1.by+=3;
gotoxy(16,cursor1.by);
printf("▷");
gotoxy(32,cursor1.by);
printf("◁");
}
break;
case UP :
if(cursor1.by-3 >= 17)
{
gotoxy(16,cursor1.by);
printf(" ");
gotoxy(32,cursor1.by);
printf(" ");
cursor1.by-=3;
gotoxy(16,cursor1.by);
printf("▷");
gotoxy(32,cursor1.by);
printf("◁");
}
break;
case ENTER :
if(game_source1.menu_change == 0 && cursor1.by == 17)
game_source1.begin_menu=1;
else if(game_source1.menu_change == 0 && cursor1.by == 20)
{
//system("cls");
game_source1.menu_change = 1;
}
else if(game_source1.menu_change == 0 && cursor1.by == 23)
exit(0);
else if(game_source1.menu_change == 1 && cursor1.by == 17)
{
game_source1.score_ptf = 1000;
game_source1.diff = 0;
game_source1.menu_change = 0;
strcpy(game_source1.diff_ch, "초급");
}
else if(game_source1.menu_change == 1 && cursor1.by == 20)
{
game_source1.score_ptf = 3000;
game_source1.diff = 1;
game_source1.menu_change = 0;
strcpy(game_source1.diff_ch, "중급");
}
else if(game_source1.menu_change == 1 && cursor1.by == 23)
{
game_source1.score_ptf = 6000;
game_source1.diff = 2;
game_source1.menu_change = 0;
strcpy(game_source1.diff_ch, "고급");
}
break;
}
}
void menu()
{
LIGHT_GREEN
printf("\n\n▒▒▒▒▒ ▒▒▒▒▒ ▒ ▒ ▒▒▒ ▒\n");
printf("▒ ▒ ▒ ▒ ▒ ▒ ▒ ▒\n");
printf("▒▒▒▒▒ ▒ ▒ ▒ ▒▒▒▒ ▒ ▒ ▒\n");
printf(" ▒▒▒▒▒ ▒ ▒ ▒ ▒\n");
printf(" ▒ ▒ ▒▒▒ ▒ ▒\n");
printf("▒▒▒▒▒ ▒ ▒ ▒ ▒▒ ▒\n");
printf(" ▒▒▒ ▒ \n");
printf(" ▒ ▒ \n");
printf(" ▒ ▒ ▒ \n");
printf(" ▒ \n");
}
int player_dead(int *ran, int *n)
{
if(*ran == cursor1.cx && *n == 19)
{
gotoxy(cursor1.cx,cursor1.cy);
RED printf("\a※");
Sleep(300);
return 1;
}
else
return 0;
}
인용한 부분에 있어 만일 누락된 출처가 있다면 반드시 알려주시면 감사하겠습니다
이 글이 좋으셨다면 구독 & 좋아요
여러분의 구독과 좋아요는
저자에게 큰 힘이 됩니다.