C/명품 C언어 프로젝트(안기수 저)

명품 C언어 프로젝트 8.5장 말타고 화살쏘기 프로그램 응용

꾸준함. 2017. 9. 3. 11:47

[1번+2번 문제]

/*

응용 8.5.1에 대해 화살이 목표물에 맞았을 경우 소리가 나도록 프로그램을 수정하시오

*/

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <time.h>

#include <Windows.h>

 

void intro_game(void);

void horizontal_slide(int x, int y, char *c2);

void draw_rectangle(int r, int c);

void display_text1();

void display_text2(int count, int r_count);

void game_control(int *r_c, int rnd);

void gotoxy(int x, int y);

 

int main(void)

{

        int count = 0, rnd;

        int r_count = 0;

        char *target = "";

        srand((unsigned)time(NULL));

        intro_game();

        do

        {

               system("cls");

               draw_rectangle(20, 20);

               rnd = rand() % 35 + 4;

               gotoxy(rnd, 2);

               printf("%s", target);

               count++;

               display_text1();

               game_control(&r_count, rnd);

               display_text2(count, r_count);

               gotoxy(1, 24);

               printf("아무키나 누르면 다음 진행...");

               getch();

        } while (count < 10);

        gotoxy(1, 24);

        return 0;

}

 

void gotoxy(int x, int y)

{

        COORD Pos = { x - 1, y - 1 };

        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);

}

 

/*

직사각형 그리기 함수

*/

void draw_rectangle(int c, int r)

{

        int i, j;

        unsigned char a = 0xa6;

        unsigned char b[7];

        for (i = 1; i < 7; i++)

               b[i] = 0xa0 + i;

 

        printf("%c%c", a, b[3]);

        for (i = 0; i < c; i++)

               printf("%c%c", a, b[1]);

        printf("%c%c", a, b[4]);

        printf("\n");

        for (i = 0; i < r; i++)

        {

               printf("%c%c", a, b[2]);

               for (j = 0; j < c; j++)

                       printf("  ");

               printf("%c%c", a, b[2]);

               printf("\n");

        }

        printf("%c%c", a, b[6]);

        for (i = 0; i < c; i++)

               printf("%c%c", a, b[1]);

        printf("%c%c", a, b[5]);

        printf("\n");

}

 

 

/*

말타고 화살쏘기 게임의 제어 함수 game_control

*/

void game_control(int *r_c, int rnd)

{

        int i = 1, k = 1, y;

        char *horse = "", chr;

        do

        {

               i += k;

               if (i > 38)

                       k = -1;

               else if (i < 3)

                       k = +1;

               horizontal_slide(i + 1, 21, horse);

        } while (!kbhit());

        chr = getch();

        y = 21;

        if (chr == 32)

        {

               while (y > 2)

               {

                       y -= 1;

                       gotoxy(i + 1, y);

                       printf(""); //화살표시

                       Sleep(50); //지연시간

                       printf("\b  ");

               }

               if (((rnd - 2) <= i) && (i <= (rnd)))

               {

                       gotoxy(rnd - 1, 2);

                       printf("※쾅※");

                       int sound = calc_frequency(4, 0);

                       Beep(sound, 300); //맞췄을 때 소리가 난다

                       gotoxy(46, 8);

                       printf("맞았습니다.  ");

                       Sleep(50);

                       *r_c = *r_c + 1;

               }

        }

}

 

/*

게임에 대한 설명을 출력하는 함수 intro_game

*/

void intro_game(void)

{

        printf("말타며 화살쏘기\n\n");

        printf("말을 타고 이동하면서\n");

        printf("목표물()을 맞추는 게임입니다. \n");

        printf("화살은 스페이스키로 발사합니다. \n\n");

        printf("아무키나 누르면 시작합니다. ");

        getch();

}

 

/*

게임하는법 설명

*/

void display_text1()

{

        gotoxy(46, 2);

        printf("스페이스키를 누르면\n");

        gotoxy(46, 3);

        printf("화살이 발사됩니다.\n");

}

 

/*

시도한 횟수와 성공한 횟수를 화면에 출력하는 함수 display_text

*/

void display_text2(int count, int r_count)

{

        gotoxy(46, 5);

        printf("횟수:%d", count);

        gotoxy(46, 6);

        printf("성공:%d", r_count);

}

 

/*

말의 위치를 좌우로 이동시키는 함수 horizontal_slide

*/

void horizontal_slide(int x, int y, char *c2)

{

        gotoxy(x, y);

        printf("%s", c2);

        Sleep(50); //시간지연

        printf("\b  ");

}

 

/*

피아노 건반 소리 계산

*/

int calc_frequency(int octave, int inx)

{

        double do_scale = 32.7032;

        double ratio = pow(2., 1 / 12.), temp;

        int i;

        temp = do_scale*pow(2, octave - 1);

        for (i = 0; i < inx; i++)

        {

               temp = (int)(temp + 0.5);

               temp *= ratio;

        }

        return (int)temp;

}


[3번 문제]

/*

응용 8.5.1의 목표물은 매 시행마다 한 개씩만 표시되지만 여러 개가 표시되도록 수정하고,

특정 목표물을 맞히면 보너스 점수를 주도록 프로그램을 수정하시오

*/

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <time.h>

#include <Windows.h>

 

void intro_game(void);

void horizontal_slide(int x, int y, char *c2);

void draw_rectangle(int r, int c);

void display_text1();

void display_text2(int count, int r_count, int score);

void game_control(int *r_c, int rnd[3], int *score);

void gotoxy(int x, int y);

 

int main(void)

{

        int count = 0, score=0, rnd[3];

        int r_count = 0;

        char *target[3] = { "", "", "" };

        srand((unsigned)time(NULL));

        intro_game();

        do

        {

               system("cls");

               draw_rectangle(20, 20);

               rnd[0] = rand() % 35 + 4;

               gotoxy(rnd[0], 2);

               printf("%s", target[0]);

               rnd[1] = rand() % 35 + 4;

               while (rnd[1] == rnd[0] || rnd[1] == (rnd[0] -1) || rnd[1] == (rnd[0]+1))

               {

                       rnd[1] = rand() % 35 + 4;

               }

               gotoxy(rnd[1], 2);

               printf("%s", target[1]);

               rnd[2] = rand() % 35 + 4;

               while (rnd[2] == rnd[1] || rnd[2] == (rnd[1] -1) || rnd[2] == (rnd[1] +1) || rnd[2] == rnd[0] || rnd[2] == (rnd[0] - 1) || rnd[2] == (rnd[0] + 1)) //확장코드이므로 겹치면 안되는 조건이 많다

               {

                       rnd[2] = rand() % 35 + 4;

               }

               gotoxy(rnd[2], 2);

               printf("%s", target[2]);

               count++;

               display_text1();

               game_control(&r_count, rnd, &score);

               display_text2(count, r_count, score);

               gotoxy(1, 24);

               printf("아무키나 누르면 다음 진행...");

               getch();

        } while (count < 10);

        gotoxy(1, 24);

        return 0;

}

 

void gotoxy(int x, int y)

{

        COORD Pos = { x - 1, y - 1 };

        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);

}

 

/*

직사각형 그리기 함수

*/

void draw_rectangle(int c, int r)

{

        int i, j;

        unsigned char a = 0xa6;

        unsigned char b[7];

        for (i = 1; i < 7; i++)

               b[i] = 0xa0 + i;

 

        printf("%c%c", a, b[3]);

        for (i = 0; i < c; i++)

               printf("%c%c", a, b[1]);

        printf("%c%c", a, b[4]);

        printf("\n");

        for (i = 0; i < r; i++)

        {

               printf("%c%c", a, b[2]);

               for (j = 0; j < c; j++)

                       printf("  ");

               printf("%c%c", a, b[2]);

               printf("\n");

        }

        printf("%c%c", a, b[6]);

        for (i = 0; i < c; i++)

               printf("%c%c", a, b[1]);

        printf("%c%c", a, b[5]);

        printf("\n");

}

 

 

/*

말타고 화살쏘기 게임의 제어 함수 game_control

*/

void game_control(int *r_c, int rnd[3], int *score)

{

        int i = 1, k = 1, y;

        char *horse = "", chr;

        do

        {

               i += k;

               if (i > 38)

                       k = -1;

               else if (i < 3)

                       k = +1;

               horizontal_slide(i + 1, 21, horse);

        } while (!kbhit());

        chr = getch();

        y = 21;

        if (chr == 32)

        {

               while (y > 2)

               {

                       y -= 1;

                       gotoxy(i + 1, y);

                       printf(""); //화살표시

                       Sleep(50); //지연시간

                       printf("\b  ");

               }

               if (((rnd[0] - 2) <= i) && (i <= (rnd[0])))

               {

                       gotoxy(rnd[0] - 1, 2);

                       printf("※쾅※");

                       int sound = calc_frequency(4, 0);

                       Beep(sound, 300); //맞췄을 때 소리가 난다

                       gotoxy(46, 8);

                       printf("맞았습니다.  ");

                       Sleep(50);

                       *r_c = *r_c + 1;

                       *score = *score + 100;

               }

               else if (((rnd[1] - 2) <= i) && (i <= rnd[1]))

               {

                       gotoxy(rnd[1] - 1, 2);

                       printf("※쾅※");

                       int sound = calc_frequency(4, 0);

                       Beep(sound, 300); //맞췄을 때 소리가 난다

                       gotoxy(46, 8);

                       printf("맞았습니다.  ");

                       Sleep(50);

                       *r_c = *r_c + 1;

                       *score = *score + 200;

               }

               else if(((rnd[2] - 2) <= i) && (i <= rnd[2]))

               {

                       gotoxy(rnd[2] - 1, 2);

                       printf("※쾅※");

                       int sound = calc_frequency(4, 0);

                       Beep(sound, 300); //맞췄을 때 소리가 난다

                       gotoxy(46, 8);

                       printf("맞았습니다.  ");

                       Sleep(50);

                       *r_c = *r_c + 1;

                       *score = *score + rand()%250+50;

               }

        }

}

 

/*

게임에 대한 설명을 출력하는 함수 intro_game

*/

void intro_game(void)

{

        printf("말타며 화살쏘기\n\n");

        printf("말을 타고 이동하면서\n");

        printf("목표물(, , )을 맞추는 게임입니다. \n");

        printf(": 100, : 200, : 랜덤 점수\n");

        printf("화살은 스페이스키로 발사합니다. \n\n");

        printf("아무키나 누르면 시작합니다. ");

        getch();

}

 

/*

게임하는법 설명

*/

void display_text1()

{

        gotoxy(46, 2);

        printf("스페이스키를 누르면\n");

        gotoxy(46, 3);

        printf("화살이 발사됩니다.\n");

}

 

/*

시도한 횟수와 성공한 횟수, 점수를 화면에 출력하는 함수 display_text

*/

void display_text2(int count, int r_count, int score)

{

        gotoxy(46, 5);

        printf("횟수:%d", count);

        gotoxy(46, 6);

        printf("성공:%d", r_count);

        gotoxy(46, 7);

        printf("점수:%d", score);

}

 

/*

말의 위치를 좌우로 이동시키는 함수 horizontal_slide

*/

void horizontal_slide(int x, int y, char *c2)

{

        gotoxy(x, y);

        printf("%s", c2);

        Sleep(50); //시간지연

        printf("\b  ");

}

 

/*

피아노 건반 소리 계산

*/

int calc_frequency(int octave, int inx)

{

        double do_scale = 32.7032;

        double ratio = pow(2., 1 / 12.), temp;

        int i;

        temp = do_scale*pow(2, octave - 1);

        for (i = 0; i < inx; i++)

        {

               temp = (int)(temp + 0.5);

               temp *= ratio;

        }

        return (int)temp;

}


[4번 문제]

/*

응용 8.5.1에서 한번 나타난 목표물은 위치가 고정되어 있으나 화살을 쏘기 전에

목표물이 좌우로 임의로 이동할 수 있도록 프로그램을 수정하시오

*/

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <time.h>

#include <Windows.h>

 

void intro_game(void);

void horizontal_slide(int x, int y, char *c2);

void horizontal_slide2(int x, char *c2);

void draw_rectangle(int r, int c);

void display_text1();

void display_text2(int count, int r_count);

void game_control(int *r_c, int rnd, char *target);

void gotoxy(int x, int y);

 

int main(void)

{

        int count = 0, rnd;

        int r_count = 0;

        char *target = "";

        srand((unsigned)time(NULL));

        intro_game();

        do

        {

               system("cls");

               draw_rectangle(20, 20);

               rnd = rand() % 35 + 4;

               count++;

               display_text1();

               game_control(&r_count, rnd, target);

               display_text2(count, r_count);

               gotoxy(1, 24);

               printf("아무키나 누르면 다음 진행...");

               getch();

        } while (count < 10);

        gotoxy(1, 24);

        return 0;

}

 

void gotoxy(int x, int y)

{

        COORD Pos = { x - 1, y - 1 };

        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);

}

 

/*

직사각형 그리기 함수

*/

void draw_rectangle(int c, int r)

{

        int i, j;

        unsigned char a = 0xa6;

        unsigned char b[7];

        for (i = 1; i < 7; i++)

               b[i] = 0xa0 + i;

 

        printf("%c%c", a, b[3]);

        for (i = 0; i < c; i++)

               printf("%c%c", a, b[1]);

        printf("%c%c", a, b[4]);

        printf("\n");

        for (i = 0; i < r; i++)

        {

               printf("%c%c", a, b[2]);

               for (j = 0; j < c; j++)

                       printf("  ");

               printf("%c%c", a, b[2]);

               printf("\n");

        }

        printf("%c%c", a, b[6]);

        for (i = 0; i < c; i++)

               printf("%c%c", a, b[1]);

        printf("%c%c", a, b[5]);

        printf("\n");

}

 

 

/*

말타고 화살쏘기 게임의 제어 함수 game_control

*/

void game_control(int *r_c, int rnd, char *target)

{

        int i = 1, k = 1, move=1, y;

        char *horse = "", chr;

        do

        {

               rnd += move;

               i += k;

               if (rnd > 38)

                       move = -1;

               else if (rnd < 3)

                       move = +1;

               if (i > 38)

                       k = -1;

               else if (i < 3)

                       k = +1;

               horizontal_slide(i + 1, 21, horse);

               horizontal_slide2(rnd, target);

        } while (!kbhit());

        chr = getch();

        y = 21;

        if (chr == 32)

        {

               while (y > 2)

               {

                       gotoxy(rnd, 2);

                       printf("%s", target);

                       y -= 1;

                       gotoxy(i + 1, y);

                       printf(""); //화살표시

                       Sleep(50); //지연시간

                       printf("\b  ");

               }

               if (((rnd - 2) <= i) && (i <= (rnd)))

               {

                       gotoxy(rnd - 1, 2);

                       printf("※쾅※");

                       int sound = calc_frequency(4, 0);

                       Beep(sound, 300); //맞췄을 때 소리가 난다

                       gotoxy(46, 8);

                       printf("맞았습니다.  ");

                       Sleep(50);

                       *r_c = *r_c + 1;

               }

        }

}

 

/*

게임에 대한 설명을 출력하는 함수 intro_game

*/

void intro_game(void)

{

        printf("말타며 화살쏘기\n\n");

        printf("말을 타고 이동하면서\n");

        printf("목표물()을 맞추는 게임입니다. \n");

        printf("화살은 스페이스키로 발사합니다. \n\n");

        printf("아무키나 누르면 시작합니다. ");

        getch();

}

 

/*

게임하는법 설명

*/

void display_text1()

{

        gotoxy(46, 2);

        printf("스페이스키를 누르면\n");

        gotoxy(46, 3);

        printf("화살이 발사됩니다.\n");

}

 

/*

시도한 횟수와 성공한 횟수를 화면에 출력하는 함수 display_text

*/

void display_text2(int count, int r_count)

{

        gotoxy(46, 5);

        printf("횟수:%d", count);

        gotoxy(46, 6);

        printf("성공:%d", r_count);

}

 

/*

말의 위치를 좌우로 이동시키는 함수 horizontal_slide

*/

void horizontal_slide(int x, int y, char *c2)

{

        gotoxy(x, y);

        printf("%s", c2);

        Sleep(30); //시간지연

        printf("\b  ");

}

 

/*

목표물의 위치를 좌우로 임의로 이동시키는 함수 horizontal_slide2

*/

void horizontal_slide2(int x, char *c2)

{

        gotoxy(x, 2);

        printf("%s", c2);

        Sleep(20);

        printf("\b  ");

}

 

/*

피아노 건반 소리 계산

*/

int calc_frequency(int octave, int inx)

{

        double do_scale = 32.7032;

        double ratio = pow(2., 1 / 12.), temp;

        int i;

        temp = do_scale*pow(2, octave - 1);

        for (i = 0; i < inx; i++)

        {

               temp = (int)(temp + 0.5);

               temp *= ratio;

        }

        return (int)temp;

}


[6번 문제]

/*

응용문제 2를 응용하여 특정 목표물을 맞히면 여러 개의 화살을 동시에 발사할 수 있도록 프로그램을 수정하시오

*/

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <time.h>

#include <Windows.h>

 

void intro_game(void);

void horizontal_slide(int x, int y, char *c2);

void draw_rectangle(int r, int c);

void display_text1();

void display_text2(int count, int r_count, int score);

void game_control(int *r_c, int rnd[4], int *score);

void gotoxy(int x, int y);

 

int superArrow = 0; //여러개의 화살 유무

 

int main(void)

{

        int count = 0, score = 0, rnd[4];

        int r_count = 0;

        char *target[4] = { "", "", "", "" };

        srand((unsigned)time(NULL));

        intro_game();

        do

        {

               system("cls");

               draw_rectangle(20, 20);

               rnd[0] = rand() % 35 + 4;

               gotoxy(rnd[0], 2);

               printf("%s", target[0]);

               rnd[1] = rand() % 35 + 4;

               while (rnd[1] == rnd[0] || rnd[1] == (rnd[0] - 1) || rnd[1] == (rnd[0] + 1))

               {

                       rnd[1] = rand() % 35 + 4;

               }

               gotoxy(rnd[1], 2);

               printf("%s", target[1]);

               rnd[2] = rand() % 35 + 4;

               while (rnd[2] == rnd[1] || rnd[2] == (rnd[1] - 1) || rnd[2] == (rnd[1] + 1) || rnd[2] == rnd[0] || rnd[2] == (rnd[0] - 1) || rnd[2] == (rnd[0] + 1)) //확장코드이므로 겹치면 안되는 조건이 많다

               {

                       rnd[2] = rand() % 35 + 4;

               }

               gotoxy(rnd[2], 2);

               printf("%s", target[2]);

               rnd[3] = rand() % 35 + 4;

               while (rnd[3] == rnd[2] || rnd[3] == (rnd[2] - 1) || rnd[3] == (rnd[2] + 1) || rnd[3] == rnd[1] || rnd[3] == (rnd[1] - 1) || rnd[3] == (rnd[1] + 1) || rnd[3] == rnd[0] || rnd[3] == (rnd[0] - 1) || rnd[3] == (rnd[0] + 1))

               {

                       rnd[3] = rand() % 35 + 4;

               }

               gotoxy(rnd[3], 2);

               printf("%s", target[3]);

               count++;

               display_text1();

               game_control(&r_count, rnd, &score);

               display_text2(count, r_count, score);

               gotoxy(1, 24);

               printf("아무키나 누르면 다음 진행...");

               getch();

        } while (count < 10);

        gotoxy(1, 24);

        return 0;

}

 

void gotoxy(int x, int y)

{

        COORD Pos = { x - 1, y - 1 };

        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);

}

 

/*

직사각형 그리기 함수

*/

void draw_rectangle(int c, int r)

{

        int i, j;

        unsigned char a = 0xa6;

        unsigned char b[7];

        for (i = 1; i < 7; i++)

               b[i] = 0xa0 + i;

 

        printf("%c%c", a, b[3]);

        for (i = 0; i < c; i++)

               printf("%c%c", a, b[1]);

        printf("%c%c", a, b[4]);

        printf("\n");

        for (i = 0; i < r; i++)

        {

               printf("%c%c", a, b[2]);

               for (j = 0; j < c; j++)

                       printf("  ");

               printf("%c%c", a, b[2]);

               printf("\n");

        }

        printf("%c%c", a, b[6]);

        for (i = 0; i < c; i++)

               printf("%c%c", a, b[1]);

        printf("%c%c", a, b[5]);

        printf("\n");

}

 

 

/*

말타고 화살쏘기 게임의 제어 함수 game_control

*/

void game_control(int *r_c, int rnd[4], int *score)

{

        int i = 1, k = 1, y;

        char *horse = "", chr;

        do

        {

               i += k;

               if (i > 38)

                       k = -1;

               else if (i < 3)

                       k = +1;

               horizontal_slide(i + 1, 21, horse);

        } while (!kbhit());

        chr = getch();

        y = 21;

        if (chr == 32)

        {

               if (!superArrow) //아이템 무

               {

                       while (y > 2)

                       {

                              y -= 1;

                              gotoxy(i + 1, y);

                              printf(""); //화살표시

                              Sleep(50); //지연시간

                              printf("\b  ");

                       }

                       if (((rnd[0] - 2) <= i) && (i <= (rnd[0])))

                       {

                              gotoxy(rnd[0] - 1, 2);

                              printf("※쾅※");

                              int sound = calc_frequency(4, 0);

                              Beep(sound, 300); //맞췄을 때 소리가 난다

                              gotoxy(46, 8);

                              printf("맞았습니다.  ");

                              Sleep(50);

                              *r_c = *r_c + 1;

                              *score = *score + 100;

                       }

                       else if (((rnd[1] - 2) <= i) && (i <= rnd[1]))

                       {

                              gotoxy(rnd[1] - 1, 2);

                              printf("※쾅※");

                              int sound = calc_frequency(4, 0);

                              Beep(sound, 300); //맞췄을 때 소리가 난다

                              gotoxy(46, 8);

                              printf("맞았습니다.  ");

                              Sleep(50);

                              *r_c = *r_c + 1;

                              *score = *score + 200;

                       }

                       else if (((rnd[2] - 2) <= i) && (i <= rnd[2]))

                       {

                              gotoxy(rnd[2] - 1, 2);

                              printf("※쾅※");

                              int sound = calc_frequency(4, 0);

                              Beep(sound, 300); //맞췄을 때 소리가 난다

                              gotoxy(46, 8);

                              printf("맞았습니다.  ");

                              Sleep(50);

                              *r_c = *r_c + 1;

                              *score = *score + rand() % 250 + 50;

                       }

                       else if (((rnd[3] - 2) <= i) && (i <= rnd[3]))

                       {

                              gotoxy(rnd[3] - 1, 2);

                              printf("※쾅※");

                              int sound = calc_frequency(4, 0);

                              Beep(sound, 300); //맞췄을 때 소리가 난다

                              gotoxy(46, 8);

                              printf("맞았습니다.  ");

                              Sleep(50);

                              *r_c = *r_c + 1;

                              superArrow = 1;

                       }

               }

               else if (superArrow) //아이템 먹은 후

               {

                       while (y > 2)

                       {

                              y -= 1;

                              gotoxy(i + 1, y);

                              printf("↑↑↑"); //화살표시

                              Sleep(50); //지연시간

                              printf("\b\b\b\b\b    ");

                       }

                       if (((rnd[0] - 2) <= i) && (i <= (rnd[0])) || ((rnd[0] - 4) <= i) && (i <= (rnd[0] - 2)) || ((rnd[0]) <= i) && (i <= (rnd[0] + 2)))

                       {

                              gotoxy(rnd[0] - 1, 2);

                              printf("※쾅※");

                              int sound = calc_frequency(4, 0);

                              Beep(sound, 300); //맞췄을 때 소리가 난다

                              gotoxy(46, 8);

                              printf("맞았습니다.  ");

                              Sleep(50);

                              *r_c = *r_c + 1;

                              *score = *score + 100;

                       }

                       if (((rnd[1] - 2) <= i) && (i <= (rnd[1])) || ((rnd[1] - 4) <= i) && (i <= (rnd[1] - 2)) || ((rnd[1]) <= i) && (i <= (rnd[1] + 2)))

                       {

                              gotoxy(rnd[1] - 1, 2);

                              printf("※쾅※");

                              int sound = calc_frequency(4, 0);

                              Beep(sound, 300); //맞췄을 때 소리가 난다

                              gotoxy(46, 8);

                              printf("맞았습니다.  ");

                              Sleep(50);

                              *r_c = *r_c + 1;

                              *score = *score + 200;

                       }

                       if (((rnd[2] - 2) <= i) && (i <= (rnd[2])) || ((rnd[2] - 4) <= i) && (i <= (rnd[2] - 2)) || ((rnd[2]) <= i) && (i <= (rnd[2] + 2)))

                       {

                              gotoxy(rnd[2] - 1, 2);

                              printf("※쾅※");

                              int sound = calc_frequency(4, 0);

                              Beep(sound, 300); //맞췄을 때 소리가 난다

                              gotoxy(46, 8);

                              printf("맞았습니다.  ");

                              Sleep(50);

                              *r_c = *r_c + 1;

                              *score = *score + rand() % 250 + 50;

                       }

                       if (((rnd[3] - 2) <= i) && (i <= (rnd[3])) || ((rnd[3] - 4) <= i) && (i <= (rnd[3] - 2)) || ((rnd[3]) <= i) && (i <= (rnd[3] + 2)))

                       {

                              gotoxy(rnd[3] - 1, 2);

                              printf("※쾅※");

                              int sound = calc_frequency(4, 0);

                              Beep(sound, 300); //맞췄을 때 소리가 난다

                              gotoxy(46, 8);

                              printf("맞았습니다.  ");

                              Sleep(50);

                              *r_c = *r_c + 1;

                       }

               }

        }

}

 

/*

게임에 대한 설명을 출력하는 함수 intro_game

*/

void intro_game(void)

{

        printf("말타며 화살쏘기\n\n");

        printf("말을 타고 이동하면서\n");

        printf("목표물(, , , )을 맞추는 게임입니다. \n");

        printf(": 100, : 200, : 랜덤 점수, : 0점 하지만 여러개 화살 쏠 수 있는 아이템 증정\n");

        printf("화살은 스페이스키로 발사합니다. \n\n");

        printf("아무키나 누르면 시작합니다. ");

        getch();

}

 

/*

게임하는법 설명

*/

void display_text1()

{

        gotoxy(46, 2);

        printf("스페이스키를 누르면\n");

        gotoxy(46, 3);

        printf("화살이 발사됩니다.\n");

        gotoxy(46, 4);

        printf("아이템 유무: ");

        if (!superArrow)

               printf("");

        else

               printf("");

}

 

/*

시도한 횟수와 성공한 횟수, 점수를 화면에 출력하는 함수 display_text

*/

void display_text2(int count, int r_count, int score)

{

        gotoxy(46, 5);

        printf("횟수:%d", count);

        gotoxy(46, 6);

        printf("성공:%d", r_count);

        gotoxy(46, 7);

        printf("점수:%d", score);

}

 

/*

말의 위치를 좌우로 이동시키는 함수 horizontal_slide

*/

void horizontal_slide(int x, int y, char *c2)

{

        gotoxy(x, y);

        printf("%s", c2);

        Sleep(50); //시간지연

        printf("\b  ");

}

 

/*

피아노 건반 소리 계산

*/

int calc_frequency(int octave, int inx)

{

        double do_scale = 32.7032;

        double ratio = pow(2., 1 / 12.), temp;

        int i;

        temp = do_scale*pow(2, octave - 1);

        for (i = 0; i < inx; i++)

        {

               temp = (int)(temp + 0.5);

               temp *= ratio;

        }

        return (int)temp;

}



개발환경:Visual Studio 2017


지적, 조언, 질문 환영입니다! 댓글 남겨주세요~


[참고] 명품 C언어 프로젝트 안기수 저


*다른 응용문제도 차근차근 풀고 업로드하도록 하겠습니다!

반응형