이제 구현한 게임을 상황(State)에 맞게 나눠 대기상태->게임상태->게임종료로 나눠야 하는데요,
그전에 코드를 조금 정리하고 진행하도록 하겠습니다.
게임전체 코드를 아래와 같이 헤더를 분리하고 함수별로 나눴습니다.
game.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | char PLAYER_STR[] = "└─●─┘"; typedef struct Position { int x, y; }Position; typedef struct Player { Position position; Position center; char *strPlayer;// = "└─●─┘";//주인공 캐릭터 int nLength; //주인공 캐릭터 전체 길이 }Player; typedef struct Ball { int isReady; Position position; clock_t moveTime; clock_t oldTime; }Ball; typedef struct GoalPost { Position position; int nLength; int nLineX[7]; int nDist; clock_t moveTime; clock_t oldTime; }GoalPost; | cs |
game.c - 선언부분
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include<stdio.h> #include<time.h> #include<conio.h> #include"screen.h" #include"fps.h" #include"game.h" #define GAME_MAX_WIDTH 40 FPSData* fpsData; Player g_Player; Ball g_Ball; GoalPost g_Post; int score = 0; | cs |
game.c -게임관련 함수
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | void InitGameData() { g_Player.position.y = 22; g_Player.position.x = 4; g_Player.center.x = 4; g_Player.center.y = 0; g_Player.nLength = strlen(PLAYER_STR); g_Player.strPlayer = (char*)malloc(sizeof(char)*g_Player.nLength); strcpy(g_Player.strPlayer, PLAYER_STR); //Ball 초기화 g_Ball.isReady = 1; g_Ball.position.x = g_Player.position.x; g_Ball.position.y = g_Player.position.y - 1; g_Ball.moveTime = 100; //Goal-Post 초기화 g_Post.position.x = 20; g_Post.position.y = 3; g_Post.nLength = 1; g_Post.moveTime = 100; g_Post.oldTime = clock(); g_Post.nDist = 1; int nLength = g_Post.nLength * 2 + 1; for (int i = 0; i < nLength; i++) g_Post.nLineX[i] = g_Post.position.x + 2 * (i + 1); } void ResetBall() { g_Ball.isReady = 1; g_Ball.position.x = g_Player.position.x; g_Ball.position.y = g_Player.position.y - 1; } void UpdateBall(clock_t CurTime) { int nLength = g_Post.nLength * 2 + 1; if (g_Ball.isReady == 0) { if ((CurTime - g_Ball.oldTime) > g_Ball.moveTime) { if (g_Ball.position.y > 0) { g_Ball.position.y--; g_Ball.oldTime = CurTime; //골대 라인충돌 시 if (g_Ball.position.x >= g_Post.nLineX[0] && g_Ball.position.x + 1 <= g_Post.nLineX[nLength - 1]) { if (g_Ball.position.y <= g_Post.position.y) { ResetBall(); score++; } } //골대 충돌 else if ((g_Ball.position.x >= g_Post.nLineX[0] - 2 && g_Ball.position.x <= g_Post.nLineX[0] - 1) || (g_Ball.position.x + 1 >= g_Post.nLineX[0] - 2 && g_Ball.position.x + 1 <= g_Post.nLineX[0] - 1) || (g_Ball.position.x >= g_Post.nLineX[nLength - 1] + 2 && g_Ball.position.x <= g_Post.nLineX[nLength - 1] + 3) || (g_Ball.position.x + 1 >= g_Post.nLineX[nLength - 1] + 2 && g_Ball.position.x + 1 <= g_Post.nLineX[nLength - 1] + 3)) { if (g_Ball.position.y <= g_Post.position.y) { ResetBall(); } } } else { ResetBall(); } } } } void UpdateGoalPost(clock_t CurTime) { //골대 처리 if (CurTime - g_Post.oldTime > g_Post.moveTime) { g_Post.oldTime = CurTime; if (g_Post.position.x + g_Post.nDist >= 0 && ((g_Post.nLineX[nLength - 1] + 3) + g_Post.nDist) <= GAME_MAX_WIDTH) { g_Post.position.x += g_Post.nDist; for (int i = 0; i < nLength; i++) { g_Post.nLineX[i] = g_Post.position.x + 2 * (i + 1); } } else { g_Post.nDist *= (-1); } } } void DrawGoalPost() { /*Draw Goal-post*/ ScreenPrint(g_Post.position.x, g_Post.position.y, "□"); int nLength = g_Post.nLength * 2 + 1; for (int i = 0; i < nLength; i++) ScreenPrint(g_Post.nLineX[i], g_Post.position.y, "━"); ScreenPrint(g_Post.nLineX[nLength - 1] + 2, g_Post.position.y, "□"); } void DrawPlayer() { /*Draw Player*/ char string[100] = { 0, }; sprintf(string, "Score: %d", score); ScreenPrint(0, 2, string); int printX = g_Player.position.x - g_Player.center.x; if (printX < 0) { ScreenPrint(0, g_Player.position.y, &g_Player.strPlayer[printX*-1]); } else if (g_Player.position.x + g_Player.center.x + 1 > GAME_MAX_WIDTH) { strncat(string, g_Player.strPlayer, g_Player.nLength - ((g_Player.position.x + g_Player.center.x + 1) - GAME_MAX_WIDTH)); ScreenPrint(printX, g_Player.position.y, string); } else { ScreenPrint(printX, g_Player.position.y, g_Player.strPlayer); } } | cs |
game.c - 메인 기능 함수
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | void Init() { InitFPSData(&fpsData); InitGameData(); } void Update() { //공의 이동처리 clock_t CurTime = clock(); UpdateBall(CurTime); UpdateGoalPost(CurTime); } void Render() { ScreenClear(); DrawFPS(&fpsData); DrawGoalPost(); DrawPlayer(); /*Draw Ball*/ ScreenPrint(g_Ball.position.x, g_Ball.position.y, "◎"); /*Draw Text*/ char string[100] = { 0, }; sprintf(string, "주인공 이동좌표 : %d, %d", g_Player.position.x, g_Player.position.y); ScreenPrint(0, 1, string); ScreenFlipping(); } void Release() { DestoyFPSData(&fpsData); } void WaitRender(clock_t OldTime) { clock_t CurTime; while (1) { CurTime = clock(); if (CurTime - OldTime > 13) break; } } int GetKeyEvent() { if (_kbhit()) return _getch(); return -1; } void KeyProcess(int key) { int remain; switch (key) { case 'j': if (g_Player.position.x <= 0) break; g_Player.position.x--; if (g_Player.position.x- g_Player.center.x < 0 || g_Player.position.x + (g_Player.center.x*2) + 1 >GAME_MAX_WIDTH) { g_Player.position.x--; } if (g_Ball.isReady) { ResetBall(); } break; case'l': if (g_Player.position.x + 1 >= GAME_MAX_WIDTH) break; g_Player.position.x++; if (g_Player.position.x+(g_Player.center.x*2)+1 >40||g_Player.position.x - g_Player.center.x < 0) { g_Player.position.x++; } if (g_Ball.isReady) { ResetBall(); } break; case'k': if (g_Ball.isReady) { ResetBall(); g_Ball.oldTime = clock(); g_Ball.isReady = 0; } break; } } | cs |
game.c - main()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | int main() { ScreenInit(); Init();//초기화 while (1) { int nKey = GetKeyEvent(); if (nKey == 'q') break; KeyProcess(nKey); Update();//데이터 갱신 Render();//화면 출력 WaitRender(clock()); } Release();//해제 ScreenRelease(); return 0; } | cs |
728x90
'Software Development > C, C++' 카테고리의 다른 글
[C++] 함수 오버로딩과 매개변수의 디폴트 값 (0) | 2018.11.25 |
---|---|
[C++] cin, cout을 이용한 입출력 (0) | 2018.11.25 |
C언어로 게임만들기 - 5. 첫번째 게임_4)충돌처리 (1) | 2017.03.21 |
C언어로 게임만들기 - 5. 첫번째 게임_3)움직이는 골대 만들기 (0) | 2017.03.20 |
C언어로 게임만들기 - 5. 첫번째 게임_2)Ball 만들기 (0) | 2017.03.19 |