-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutiTubes.cpp
More file actions
218 lines (185 loc) · 3.93 KB
/
Copy pathmutiTubes.cpp
File metadata and controls
218 lines (185 loc) · 3.93 KB
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
#include <easyx.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#define GRAPH_HEIGHT 512
#define GRAPH_WIDTH 288
#define PI 3.14159265359
// the width and height depend on the resolution ratio of backgroundImage
/*
IMAGE base;
IMAGE back;
IMAGE TubeUp;
IMAGE TubeDown;
int tubeGap = 75;
int tubeWidth = 50;
int startTime = (unsigned int) time(NULL);
int tubeMoveStep = 50;
int currentBase = 0;
int baseMoveStep = 10;
typedef struct TubeStruct {
int xLeft;
int width;
int xRight;
int gap[2];
int gapHeight;
} Tube;
typedef struct TubesNodeStruct {
Tube* tube;
TubesNodeStruct* next;
} TubesNode;
typedef struct TubesStruct {
int num;
TubesNode* head;
} Tubes;
Tubes tubes = {
0, NULL
};
void moveGround();
void loadTestImages();
void drawTubes(Tube*); // *********
void initRandom();
void getRandomGap(Tube*);
void createConstantTube();
void moveAllTubes();
void drawBackground();
void drawBase();
void drawAllTubes();
void draw();
void move();
TubesNode* deleteFirstTube();
// test function
void continueCreation();
void main() {
loadTestImages();
initgraph(GRAPH_WIDTH, GRAPH_HEIGHT);
initRandom();
int baseHeight = base.getheight();
putimage(0, 0, &back);
putimage(0, GRAPH_HEIGHT - baseHeight, &base);
for (int i = 0; i < 100; i++) {
createConstantTube();
draw();
_getch();
move();
draw();
_getch();
move();
draw();
_getch();
move();
}
createConstantTube();
_getch();
drawAllTubes();
_getch();
}
void drawBackground() {
putimage(0, 0, &back);
}
void drawBase() {
int y = GRAPH_HEIGHT - base.getheight();
putimage(currentBase, y, &base);
putimage(GRAPH_WIDTH + currentBase, y, &base);
}
void draw() {
drawBackground();
drawBase();
drawAllTubes();
// draw Bird
}
void move() {
moveGround();
moveAllTubes();
}
TubesNode* deleteFirstTube() {
if (tubes.head == NULL) {
return NULL;
}
TubesNode* next = tubes.head->next;
free(tubes.head);
tubes.head = next;
tubes.num--;
return next;
}
void moveAllTubes() {
TubesNode* node = tubes.head;
while (node != NULL) {
node->tube->xLeft -= tubeMoveStep;
node->tube->xRight -= tubeMoveStep;
if (node->tube->xRight <= 0) {
node = deleteFirstTube();
}
else {
node = node->next;
}
}
}
void drawAllTubes() {
TubesNode *node = tubes.head;
while (node != NULL) {
drawTubes(node->tube);
node = node->next;
}
}
void createConstantTube() {
Tube *tube = (Tube*)malloc(sizeof(Tube));
TubesNode *node = (TubesNode*)malloc(sizeof(TubesNode));
tube->xLeft = GRAPH_WIDTH;
tube->width = tubeWidth;
tube->xRight = tube->xLeft + tube->width;
tube->gapHeight = tubeGap;
getRandomGap(tube);
node->tube = tube;
node->next = NULL;
if (tubes.head == NULL) {
tubes.head = node;
}
else {
TubesNode* current = tubes.head;
while (current->next != NULL) {
current = current->next;
}
current->next = node;
}
tubes.num++;
}
void initRandom() {
srand((unsigned int)time(NULL));
}
void getRandomGap(Tube* ptube) {
int upper = rand() % (GRAPH_HEIGHT - base.getheight() - tubeGap);
int lower = upper + tubeGap;
ptube->gap[0] = upper;
ptube->gap[1] = lower;
}
void loadTestImages() {
loadimage(&base, _T("images\\base.png"));
Resize(&base, GRAPH_WIDTH, base.getheight());
loadimage(&back, _T("images\\background-day.png"));
loadimage(&TubeDown, _T("images\\pipe-green.png"));
rotateimage(&TubeUp, &TubeDown, PI);
}
void moveGround() {
int baseHeight = base.getheight();
int y = GRAPH_HEIGHT - baseHeight;
currentBase -= baseMoveStep;
if (currentBase + GRAPH_WIDTH <= 0) {
currentBase = 0;
}
}
void drawTubes(Tube* tube) {
IMAGE upTemp, downTemp;
rotateimage(&upTemp, &TubeDown, 0);
rotateimage(&downTemp, &TubeDown, 0);
int x = tube->xLeft;
int hUp = tube->gap[0];
int hDown = tube->gap[1];
Resize(&upTemp, upTemp.getwidth(), hUp);
rotateimage(&upTemp, &upTemp, PI);
Resize(&downTemp, downTemp.getwidth(), GRAPH_HEIGHT - base.getheight() - hDown);
putimage(x, 0, &upTemp);
putimage(x, hDown, &downTemp);
}
*/