-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame2main.c
259 lines (243 loc) · 8.13 KB
/
game2main.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
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
#include "main.h"
#include "game2.h"
int g2_point;
int tempPoint;
int g2_timetic;
int stage;
int game2main()
{
int i = 0, j = 0, key, a, b;
int blocktype, spinvalue, next_blocktype, next_spinvalue;
char*** graphic;//문자열을 원소로 갖는 2차원 배열
int** block_coors;//블럭의 상대좌표들
int** next_block_coors;
int* location;//블럭중심의 위치
int* tl;
clock_t starttime = clock();
clock_t start;
//graphic 동적할당
graphic = (char***)malloc(sizeof(char**) * 30);
for (i = 0; i < 30; i++)
{
graphic[i] = (char**)malloc(sizeof(char*) * 30);//30개의 열 확보
}
//block_coors 동적할당
block_coors = (int**)malloc(sizeof(int*) * 4);
for (i = 0; i < 4; i++) block_coors[i] = (int*)malloc(sizeof(int) * 2);
next_block_coors = (int**)malloc(sizeof(int*) * 4);
for (i = 0; i < 4; i++) next_block_coors[i] = (int*)malloc(sizeof(int) * 2);
//location 동적할당
location = (int*)malloc(sizeof(int) * 2);
tl = (int*)malloc(sizeof(int) * 2);
tl[0] = 7;
tl[1] = 24;
//기본세팅
for (i = 0; i < 30; i++)
{
for (j = 0; j < 30; j++)
{
if (i == 25 && j > 8 && j < 21) graphic[i][j] = "▩";
else if (j == 9 && i > 4 && i < 26) graphic[i][j] = "▩";
else if (j == 20 && i > 4 && i < 26) graphic[i][j] = "▩";
else graphic[i][j] = " ";
}
}
location[0] = 4;
location[1] = 14;//기준점이 생성되는 점
srand(time(NULL));
blocktype = rand() % 6;
spinvalue = rand() % 4;
next_blocktype = rand() % 6;
next_spinvalue = rand() % 4;
block_type_to_coors(block_coors, blocktype, spinvalue);
block_type_to_coors(next_block_coors, next_blocktype, next_spinvalue);
put_block(graphic, block_coors, location[0], location[1]);
put_block(graphic, next_block_coors, 7, 24);
system("cls");
g2show_graphic(graphic);
start = clock();
while (1)
{
//키가 눌린 경우
if (kbhit())
{
key = getch();
//방향키가 눌린경우
if (key == 224)
{
key = getch();
//상 키가 눌린경우
if (arrow(key) == 0)
{
del_block(graphic, block_coors, location[0], location[1]);//블럭을 지우고
while (1)
{
move_coors(location, 2);//기준점을 한칸 내림
if (can_put(graphic, block_coors, location))//변경된 기준점에 놓을 수 있다면
{
continue;
}
else//변경된 기준점에 놓을 수 없다면
{
move_coors(location, 0); //기준점 원상복귀
put_block(graphic, block_coors, location[0], location[1]);//블럭놓음
g2row_clear(graphic);
location[0] = 4;
location[1] = 14;//기준점이 생성되는 점
del_block(graphic, next_block_coors, 7, 24);
coor_to_coor(block_coors, next_block_coors);
blocktype = next_blocktype;
spinvalue = next_spinvalue;
next_blocktype = rand() % 6;
next_spinvalue = rand() % 4;//랜덤블럭정보입력
block_type_to_coors(next_block_coors, next_blocktype, next_spinvalue);//블럭좌표 파악
if (can_put(graphic, block_coors, location))//새 블럭을 놓을 수 있다면
{
put_block(graphic, block_coors, location[0], location[1]);//블럭놓음
put_block(graphic, next_block_coors, 7, 24);
system("cls");
g2show_graphic(graphic);//보여줌
}
else//새 블럭 생성할 자리가 없음
{
put_block(graphic, block_coors, location[0], location[1]);//블럭놓음
put_block(graphic, next_block_coors, 7, 24);
system("cls");
g2show_graphic(graphic);//보여줌
printf("\n G A M E O V E R");
printf("\n 총 게임시간 : %ld초", (clock() - starttime) / 1000);
printf("\n 최대 스테이지 : %d", stage);
return 0;
}
break;
}
}
}
//좌,하,우
else
{
del_block(graphic, block_coors, location[0], location[1]);//블럭을 지우고
move_coors(location, arrow(key));//기준점을 움직임
if (can_put(graphic, block_coors, location))//변경된 기준점에 놓을 수 있다면
{
put_block(graphic, block_coors, location[0], location[1]);
system("cls");
g2show_graphic(graphic);
}
else//변경된 기준점에 놓을 수 없다면
{
if (arrow(key) == 2)//아래로가 막힌 경우
{
move_coors(location, 0); //기준점 원상복귀
put_block(graphic, block_coors, location[0], location[1]);//블럭놓음
g2row_clear(graphic);
location[0] = 4;
location[1] = 14;//기준점이 생성되는 점
del_block(graphic, next_block_coors, 7, 24);
coor_to_coor(block_coors, next_block_coors);
blocktype = next_blocktype;
spinvalue = next_spinvalue;
next_blocktype = rand() % 6;
next_spinvalue = rand() % 4;//랜덤블럭정보입력
block_type_to_coors(next_block_coors, next_blocktype, next_spinvalue);//블럭좌표 파악
if (can_put(graphic, block_coors, location))//새 블럭을 놓을 수 있다면
{
put_block(graphic, block_coors, location[0], location[1]);//블럭놓음
put_block(graphic, next_block_coors, 7, 24);
system("cls");
g2show_graphic(graphic);//보여줌
}
else//새 블럭 생성할 자리가 없음
{
put_block(graphic, block_coors, location[0], location[1]);//블럭놓음
put_block(graphic, next_block_coors, 7, 24);
system("cls");
g2show_graphic(graphic);//보여줌
printf("\n 게임오버.");
printf("\n 총 게임시간 : %ld초", (clock() - starttime) / 1000);
printf("\n 최대 스테이지 : %d", stage);
return 0;
}
}
else//좌우가 막힌 경우
{
move_coors(location, anti_arrow(key)); //기준점 원상복귀
put_block(graphic, block_coors, location[0], location[1]);
system("cls");
g2show_graphic(graphic);//다시 찍고 보여줌
}
}
}
}
//회전키가 입력된 경우
else
{
del_block(graphic, block_coors, location[0], location[1]);//블럭을 지우고
spinvalue = spin(spinvalue);//블럭회전상수를 키우고
block_type_to_coors(block_coors, blocktype, spinvalue);//그에맞춰 블럭좌표들을 변환한 뒤
if (can_put(graphic, block_coors, location))//놓을 수 있다면
{
put_block(graphic, block_coors, location[0], location[1]);//블럭찍고
system("cls");
g2show_graphic(graphic);//보여주기
}
else //놓을 수 없다면
{
spinvalue = anti_spin(spinvalue);//블럭회전상수를 1 줄이고(원상복귀)
block_type_to_coors(block_coors, blocktype, spinvalue);
put_block(graphic, block_coors, location[0], location[1]);//블럭찍고
system("cls");
g2show_graphic(graphic);//보여주기
}
}
}
//단위시간마다 블럭을 떨어뜨림
if ((clock() - start) > g2_timetic)
{
start = clock();
del_block(graphic, block_coors, location[0], location[1]);//블럭을 지우고
move_coors(location, 2);//기준점을 움직임
if (can_put(graphic, block_coors, location))//변경된 기준점에 놓을 수 있다면
{
put_block(graphic, block_coors, location[0], location[1]);
system("cls");
g2show_graphic(graphic);
}
else//변경된 기준점에 놓을 수 없다면
{
move_coors(location, 0); //기준점 원상복귀
put_block(graphic, block_coors, location[0], location[1]);//블럭놓음
g2row_clear(graphic);
location[0] = 4;
location[1] = 14;//기준점이 생성되는 점
del_block(graphic, next_block_coors, 7, 24);
coor_to_coor(block_coors, next_block_coors);
blocktype = next_blocktype;
spinvalue = next_spinvalue;
next_blocktype = rand() % 6;
next_spinvalue = rand() % 4;//랜덤블럭정보입력
block_type_to_coors(next_block_coors, next_blocktype, next_spinvalue);//블럭좌표 파악
if (can_put(graphic, block_coors, location))//새 블럭을 놓을 수 있다면
{
put_block(graphic, block_coors, location[0], location[1]);//블럭놓음
put_block(graphic, next_block_coors, 7, 24);
system("cls");
g2show_graphic(graphic);//보여줌
}
else//새 블럭 생성할 자리가 없음
{
put_block(graphic, block_coors, location[0], location[1]);//블럭놓음
put_block(graphic, next_block_coors, 7, 24);
system("cls");
g2show_graphic(graphic);//보여줌
printf("\n G A M E O V E R");
printf("\n 총 게임시간 : %ld초", (clock() - starttime) / 1000);
printf("\n 최대 스테이지 %d", stage);
return 0;
}
}
}
}
//g2show_graphic(graphic);
return 0;
}