#include < stdio.h >
#include < time.h >
#include < conio.h >
#include < stdlib.h >
#include < windows.h >
/*
* < 사격게임 >
*
* 호환성
* 이 프로그램은 Borland C++ 5.5 에서 컴파일하고 시험되었습니다.
*
* 게임설명
* 사격장은 각각 하나씩의 화살을 포함하고 있는 아홉개의 레인으로 만들어져 있습니다.
* 프로그램이 시작되면 레인의 오른쪽에 한개의 타겟(X)이 나타납니다.
* 당신의 과제는 화살들 중의 하나로 타겟을 맞추는 것 입니다.
* 화살을 쏘려면 레인 번호를 누르면 됩니다.
* 25개의 타겟이 나타난 후에, 게임은 끝나고, 최종점수가 출력됩니다.
*
* 난이도 변경하기
* 난이도를 변경하려면 ShootArrow(int)함수의 두번째 for루프를 변경해서 화살의 속도를 변경하면 됩니다.
* 화살이 천천히 나갈수록 맞추기까지의 시간을 소비하므로 난이도는 높아집니다.
* PlayGame(void)함수의 TIME_LIMIT값을 2초로 맞추어 놓았기 때문에
* 화살이 발사되는데 2초가 걸린다면 게임이 안되죠. 역시 제한시간도 마음대로 수정하세요.
*/
#define true 1
#define false 0
typedef int bool;
void ShowScore(int);
int PlayGame(void);
void EraseTarget(int);
void ShootArrow(int);
int ShowTarget(void);
void DrawScreen(void);
void gotoxy(int x, int y);
void clrscr(void);
int main(void) {
int score;
srand((unsigned) time(NULL));
DrawScreen();
score = PlayGame();
ShowScore(score);
return 0;
}
void DrawScreen(void) {
int x;
clrscr();
gotoxy(20, 11);
printf("게임을 시작하려면 아무키나 누르세요.");
getch();
clrscr();
for (x = 1; x <= 10; x++) { // 루프는 한번에 한 개의 헤인벽과 한개의 화살을 그린다.
gotoxy(1, x * 2 + 2);
printf("-----------------------------------");
if (x < 10) {
gotoxy(1, x * 2 + 3);
}
}
}
void EraseTarget(int target_position) {
gotoxy(60, target_position * 2 + 3);
}
int PlayGame(void) {
int score = 0;
int target_position;
long start_time;
bool shot_fired;
int num; // 값으로 저장하는 숫자키
const int TIME_LIMIT = 2; // 한 타겟당 제한시간 2초
int x;
for (x = 1; x <= 20; x++) { // 이 루프는 25개의 타겟을 부여한다.
target_position = ShowTarget();
start_time = time(NULL); // 여기에서 시작시간이 저장된다.
shot_fired = false;
// 제한시간과 남은 화살개수를 알려줌
gotoxy(44, 2);
printf("%s%d%s",
"남은 화살: ",
21 - x,
" ");
gotoxy(10, 2);
printf("%s%d%s",
"한 타겟 당 시간제한 ", TIME_LIMIT,
"초");
do { // 선수가 사격을 할 때까지 키 입력을 기다린다.
num = getch() - '0';
if (num >= 1 && num <= 9) {
ShootArrow(num);
shot_fired = true;
}
} while (!shot_fired);
// 시간 안에(2초) 타겟을 맞추었을 때 실행된다.
if ((time(NULL) < start_time + TIME_LIMIT) && num == target_position) {
putchar('\a');
++score;
}
EraseTarget(target_position);
}
return score;
}
void ShootArrow(int a) { // 파라미터 a는 발사한 화살의 번호
int x;
long delay;
for (x = 4; x <= 60; x++) {
gotoxy(x, a * 2 + 3); // 루프의 매 회마다 화살을 1문자씩 오른쪽으로 움직인다.
for (delay = 0; delay < 3999999; delay++) // 이 코드로 화살의 속도조절을 한다. 시스템의 성능에 따라 다르다.
continue;
gotoxy(x, a * 2 + 3);
}
gotoxy(4, a * 2 + 3);
}
void ShowScore(int score) {
gotoxy(60, 20);
gotoxy(60, 21);
printf("%s%d",
" Your score is ", score
);
gotoxy(60, 22);
}
int ShowTarget(void) {
int p = rand() % 9 + 1; // 이 난수는 타겟이 나타날 레인번호이다. 1 ~ 9
gotoxy(60, p * 2 + 3);
return p;
}
void gotoxy(int x, int y)
{
COORD pos={x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void clrscr(void)
{
COORD Cur= {0,0 };
unsigned long dwLen;
FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE) , ' ', 80*25, Cur, &dwLen);
}
댓글을 달아 주세요