forked from w1193/C-C-Player-s-Journey---project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
공받기.cpp
283 lines (229 loc) · 5.73 KB
/
공받기.cpp
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include "Screen.c"
#include <windows.h>
#include <time.h>
enum ControlKeys
{
SPACE = 32,
LEFT = 75,
RIGHT = 77
};
typedef struct _GOAL
{
int gMoveX, gMoveY;
int gLength; //골대의 길이
int gLineX[9]; //골대의 골인식 좌표
int gDistance; // 골대 이동거리
clock_t MoveTime;
clock_t OldTime;
} GOAL;
typedef struct _Ball
{
int ReadyB; // 1 = 준비, 0 = 슛
int bMoveX, bMoveY; //공 이동 좌표
clock_t MoveTime; // 이동중 걸린 시간
clock_t OldTime; // 이전 이동시간
} BALL;
typedef struct _Player
{
int CenterX, CenterY; //X, Y 좌표 센터
int MoveX, MoveY; // X, Y 좌표 이동
int X, Y; // X, Y 현재 출력 좌표 값
int Index; // 중심좌표의 인덱스
} PLAYER;
typedef struct _Position
{
int X, Y; // X, Y 현재 좌표
} Position;
GOAL Goal;
BALL Ball;
PLAYER Player;
char strPlayer[] = {"|__●__|"}; //캐릭터
int Length; // 주인공 캐릭터 길이
// 초기화
void Init() {
Player.CenterX = 3; //주인공 X 중심 좌표
Player.CenterY = 0; //주인공 Y 중심 좌표
Player.MoveX = 20; // 주인공 이동 ㅌ좌표 초기화
Player.Y = Player.MoveY = 22; // 주인공 이도 Y좌표 초기화
Player.X = Player.MoveX - Player.CenterX; //주인공 캐릭터 출력 좌표
Length = strlen(strPlayer); //주인공 전체 길이
Ball.ReadyB = 1;
Ball.bMoveX = Player.MoveX;
Ball.bMoveY = Player.MoveY - 1;
Ball.MoveTime = 100;
int gLength, i;
Goal.gMoveX = 20;
Goal.gMoveY = 2;
Goal.gLength = 2;
Goal.MoveTime = 100;
Goal.OldTime = clock();
Goal.gDistance = 1;
gLength = Goal.gLength*2 + 1;
for(i = 0; i < gLength; i++){
Goal.gLineX[i] = Goal.gMoveX + 2 * (i + 1);
}
}
// 데이터 갱신
void Update()
{
clock_t CurTime = clock();
int gLength = Goal.gLength * 2 + 1;
int i;
int BallCount = 0;
if(Ball.ReadyB == 0) {//이동중일 경우
if ((CurTime - Ball.OldTime) > Ball.MoveTime)
{
if(Ball.bMoveY - 1 > 0){
Ball.bMoveY--;
Ball.OldTime = CurTime;
}else{
Ball.ReadyB = 1;
Ball.bMoveX = Player.MoveX;
Ball.bMoveY = Player.MoveY - 1;
}
}
}else{
Ball.bMoveX = Player.MoveX;
}
if((CurTime - Goal.OldTime) > Goal.MoveTime)
{
Goal.OldTime = CurTime;
if(Goal.gMoveX + Goal.gDistance >= 0 && ((Goal.gLineX[gLength - 1] + 3) + Goal.gDistance) <= 79)
{
Goal.gMoveX += Goal.gDistance;
for(i = 0; i < gLength; i++)
{
Goal.gLineX[i] = Goal.gMoveX + 2 * (i + 1);
}
}else{
Goal.gDistance = Goal.gDistance * (-1); // 방향바꾸기
}
}
if(Ball.ReadyB == 0) //공이동중
{
if((CurTime - Ball.OldTime) > Ball.MoveTime){
if(Ball.bMoveY - 1 > 0){
Ball.bMoveY--;
Ball.OldTime = CurTime;
//골대의라인과 충돌
if(Ball.bMoveX >= Goal.gLineX[0] && Ball.bMoveX + 1 <= Goal.gLineX[gLength - 1]){
if(Ball.bMoveY <= Goal.gMoveY) //공 초기화
Ball.ReadyB = 1;
Ball.bMoveY = Player.MoveY - 1;
Ball.bMoveX = Player.MoveX;
BallCount++; //득점
printf("성공!");
}
//골대 충돌
}else if((Ball.bMoveX >= Goal.gLineX[0] - 2 && Ball.bMoveX <= Goal.gLineX[0] - 1) || (Ball.bMoveX + 1 >= Goal.gLineX[0] - 2) && (Ball.bMoveX + 1 <= Goal.gLineX[0] - 1) || (Ball.bMoveX >= Goal.gLineX[gLength - 1]) + 2 && (Ball.bMoveX <= Goal.gLineX[gLength - 1] + 3) || (Ball.bMoveX + 1 >= Goal.gLineX[gLength - 1]) + 2 && (Ball.bMoveX + 1 <= Goal.gLineX[gLength - 1] + 3)){
if(Ball.bMoveY <= Goal.gMoveY){
Ball.ReadyB = 1;
Ball.bMoveX = Player.MoveX;
Ball.bMoveY = Player.MoveY - 1;
}
}else {
Ball.ReadyB = 1;
Ball.bMoveX = Player.MoveX;
Ball.bMoveY = Player.MoveY - 1;
}
}
}else{
Ball.bMoveX = Player.MoveX;
}
}
// 출력
void Render()
{
char string[100] = {0};
ScreenClear();
int gLength, i;
// 출력 시작
// 좌우에 벽을 만듬 (클리핑)
if(Player.X < 0)
ScreenPrint(0, Player.MoveY, &strPlayer[Player.X * (-1)]);
else if(Player.MoveX + (Length - Player.MoveX + Player.CenterX + 1) > 79)
{
strncat_s(string, strPlayer, Length - ((Player.MoveX + Player.CenterX + 1) - 79));
ScreenPrint(Player.X, Player.Y, string);
}else{
ScreenPrint(Player.X, Player.Y, strPlayer);
}
ScreenPrint(Ball.bMoveX, Ball.bMoveY, "○");
sprintf_s(string, "주인공 이동좌표 : %d, %d", Player.MoveX, Player.Y);
ScreenPrint(0, 0, string);
//골대 출력
ScreenPrint(Goal.gMoveX, Goal.gMoveY, "ㅁ");
gLength = Goal.gLength * 2 + 1;
for(i = 0; i < gLength; i++)
ScreenPrint(Goal.gLineX[i], Goal.gMoveY, "ㅡ");
ScreenPrint(Goal.gLineX[gLength - 1] + 2, Goal.gMoveY, "ㅁ");
//출력 끝
ScreenFlipping();
}
//해제
void Release()
{
}
int main (void){
int Key, Remain;
clock_t CurTime, OldTime;
ScreenInit();
Init(); // 초기화
while( 1 ) //무한반복
{
if(_kbhit())
{
Key = _getch();
if(Key == 'q')
break;
switch(Key)
{
case LEFT :
if(Player.MoveX > 0){
Player.MoveX--;
Remain = Length - (Player.CenterX); // 남은길이 = 전체 길이 - (중심좌표 + 1)
if(Player.MoveX + Remain > 79 || Player.MoveX - Player.CenterX - 1 < 0)
Player.MoveX--;
Player.X = Player.MoveX - Player.CenterX;
}
break;
case RIGHT :
if(Player.MoveX + 1< 79){
Player.MoveX++;
Remain = Length - (Player.CenterX);
if(Player.MoveX + Remain >79 || Player.MoveX - Player.CenterX < 0)
Player.MoveX++;
Player.X = Player.MoveX - Player.CenterX;
}
break;
case SPACE :
if(Ball.ReadyB)
{
Ball.bMoveX = Player.MoveX;
Ball.bMoveY = Player.MoveY - 1;
Ball.OldTime = clock();
Ball.ReadyB = 0;
}
break;
}
}
Update();//데이터 갱신
Render(); //화면 출력
OldTime = clock();
while (1)
{
CurTime = clock();
if (CurTime - OldTime > 20)
{
OldTime = CurTime;
break;
}
}//대기시간
}
Release();
ScreenRelease();
return 0;
}