Please enable JavaScript in your browser to load website properly.

Click here if you'd like to learn how.

도움 한번부탁드리겠습니다..! (C++ 2학년 수준) [2]

중령 폐허에서일어서다 | 21-03-20 17:15:40 | 조회 : 832 | 추천 : -



방향키 위 방향 누르면 별 하나 더 생성되고

아래방향누르면 별 하나 없어지되

별 갯수 5에서 더이상 안만들어지게 if(n<4)

갯수 1개에서 더이상 안줄어들게 if (n>=0)

설정해놓았는데 별 갯수가 무한대로 늘어나고 무한대로 줄어듭니다..

설정해놓은 별갯수 범위를 벗어난상태로 프로그램을 종료하면 이렇게 뜨네요.. 어떻게 해결해야될까요 너무 막막합니다 ㅠㅠ..........

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ이것은 소스코드입니다

 

// '*' 문자가 주기적으로 이동하는 프로그램

// 커서 이동, clock을 이용한 경과 시간 계산, 난수 발생(rand) 등 필요


#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

#include <Windows.h>

#include <conio.h>


#define KEY_ESC 27

#define KEY_UP (256 + 72)

#define KEY_DOWN (256 + 80)

#define KEY_LEFT (256 + 75)

#define KEY_RIGHT (256 + 77)


void CursorInvisible(void);

void GotoXY(int x, int y);

int GetKey(void);

double GetElapsedTime(clock_t initial);


typedef struct Point

{

int x;

int y;

int xspeed;                      // 오른쪽

int yspeed;                    // 아래쪽

}Point;


int main(void)

{

CursorInvisible();                      // 커서 안 보이게 설정

system("mode CON COLS=80 LINES=25");    // 콘솔창 크기 (가로80x세로25)

srand(time(NULL));


Point *stars = new Point[5];

stars[0].x = (rand() % 78) + 1;          // 1~78, 최초 시작점이 가장자리에 있지 않도록

stars[0].y = (rand() % 23) + 1;          // 1~23

stars[0].xspeed =  1;

stars[0].yspeed =  1;

GotoXY(stars[0].x, stars[0].y);

cout << "*";


int n = 0; //현재 별의 갯수 -1

clock_t initial_clock = clock();

bool stop = false;


while (true)

{

if (_kbhit())

{

int ch = GetKey();


if (ch == KEY_ESC)          // Esc 키를 누르면 종료

stop = !stop;


if (ch == KEY_UP)

{

if (n < 4);

{

n++;

stars[n].x = (rand() % 78) + 1;          // 1~78, 최초 시작점이 가장자리에 있지 않도록

stars[n].y = (rand() % 23) + 1;          // 1~23

stars[n].xspeed = 1;

stars[n].yspeed = 1;

}

}


if (ch == KEY_DOWN)

{

if (n >= 0);

{

GotoXY(stars[n].x, stars[n].y);

cout << " ";

n--;

}




}

}


if (stop)

break;


if (GetElapsedTime(initial_clock) >= 0.1)    // 0.1초가 지났다면

{

for (int i = 0; i <= n; i++)

{

GotoXY(stars[i].x, stars[i].y);

cout << " ";


if (stars[i].x <= 0 || stars[i].x >= 79)            // 좌우 벽에 부딪혔다면

stars[i].xspeed = -stars[i].xspeed;


if (stars[i].y <= 0 || stars[i].y >= 24)            // 상하 벽에 부딪혔다면

stars[i].yspeed = -stars[i].yspeed;


stars[i].x += stars[i].xspeed;

stars[i].y += stars[i].yspeed;


GotoXY(stars[i].x, stars[i].y);

cout << "*";

}

initial_clock = clock();

}

}


return 0;

}


double GetElapsedTime(clock_t initial)

{

clock_t current = clock();

return (double)(current - initial) / CLOCKS_PER_SEC;    // 초 단위 값 반환

}


void GotoXY(int x, int y)

{   // COORD 구조체 변수를 통해 이동할 위치 설정

COORD pos;

pos.X = x;

pos.Y = y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);

}


void CursorInvisible(void)

{

CONSOLE_CURSOR_INFO ci = { 100, FALSE };

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &ci);

}


int GetKey(void)

{

int ch = _getch();


if (ch == 0 || ch == 224)

// 방향키의 경우 0 또는 224의 값이 먼저 입력됨

ch = 256 + _getch();

// 그 다음에 해당 방향키에 따라 72(Up), 

// 80(Down), 75(Left), 77(Right) 값이 입력됨

return ch;

}

 

SNS로 공유하기
< 1 2 3 4 5 >