-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
373 lines (332 loc) · 10.2 KB
/
main.cpp
File metadata and controls
373 lines (332 loc) · 10.2 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* Author: Brendon Kofink
* Johann Rajadurai
* Aaron Sierra
* David Day
* Lucy Ray
* Assignment Title: Ball Game
* Assignment Description: user can launch balls to hit objects.
* Due Date: 12/08/2021
* Date Created: 10/19/2021
* Date Last Modified: 12/08/2021
*/
/*
* Data Abstraction:
* Create the game, set an initial game state,
* then initialize various counters.
* Input:
* Get the user's input using game.Key() to determine the next state.
* Process:
* Depending on the state, render text, balls, obstacles, and score.
* Move them based on their state.
* Output:
* Use game.Update() to draw everything to the screen.
* Assumptions:
* The user is on a computer with a keyboard.
* The user is on a computer with a monitor.
* The user has all requirements and dlls installed.
* Please let this work on other machines.
*/
#include <iostream>
#include "GameControl.h"
/*
* description: Sets the dots to point through an angle.
* return: void
* precondition: The angle is in radians.
* postcondition: The dots' destination are set to point through the angle.
*/
void setPointer(GameControl &game, double angle);
/*
* description: Creates rainbow text that changes based on time.
* return: void
* precondition: game is a valid GameControl object.
* postcondition: The rainbow text is created.
*/
void RainbowText(GameControl &game, string text, vec2 pos, int size, int time);
int main(int argc, char **argv)
{
// Scores and counters
int score = 0;
int prevScore = 0;
int count = 0;
double gameTime = 0;
// Move is true the frame the ball falls off screen
bool move = false;
// Angle of the pointer
double angle = 3 * PI / 2;
// For the '-10' text
double lastShot = 0;
string scoreMod = "";
enum gameState
{
TITLE,
PLAY,
LOSE,
};
gameState state = TITLE;
// Initialize the game
GameControl game;
game.InitSound("Drop.wav");
game.InitSound("GoodDesign.wav");
// Initialize the pointer
game.Spawn(GO(TOP_CENTER, Shape(10, 5, RED)), 3);
game.Spawn(GO(TOP_CENTER, Shape(10, 10, RED)), 3);
setPointer(game, angle);
srand(time(0));
// Game Loop
while (!game.getQuit())
{
// Title Screen
while (state == TITLE && !game.getQuit())
{
// display title screen
RainbowText(game, "Ball Game!", vec2(80, 160), 8, gameTime);
game.Text("Arrow keys to aim", vec2(140, 260), 3, 1, false, NOTWHITE, true);
game.Text("Space to launch ball", vec2(120, 220), 3, 1, false, NOTWHITE, true);
game.Text("Hit space to start", vec2(140, 500), 3, 1, false, NOTWHITE, true);
if (game.KeyDown(' '))
{
state = PLAY; //if space is hit, game is in PLAY state
}
move = true;
game.Update();
gameTime += game.DeltaTime();
}
angle = PI / 2;
setPointer(game, angle);
// Main Game
while (state == PLAY && !game.getQuit())
{
// Check for collisions
game.layerCollide(1, 2);
// Check for input.
// Left and Right arrow move the pointer.
if (game.Key(LEFT_ARROW))
{
angle += 0.1;
setPointer(game, angle);
// Clamp the angle
if (angle > PI)
{
angle = PI;
}
}
else if (game.Key(RIGHT_ARROW))
{
angle -= 0.1;
setPointer(game, angle);
// Clamp the angle
if (angle < 0)
{
angle = 0;
}
}
// Space launches the ball.
else if (game.KeyDown(' '))
{
// Count tracks the number of balls launched.
// col takes that number and uses it to spin around the color wheel.
Color col = Color::HSV((count * 20) % 361, 70, 100);
count += 1;
// Create the ball. Make it physics based and set its velocity.
GO &ball = game.Spawn(GO(TOP_CENTER, Shape(10, 15, col)), 1);
ball.setMoveMethod(MoveMethod::PHYSICS);
ball.ApplyForce(vec2::Angle(angle) * 3);
// You lose points for each ball you launch.
// Scaled by your score.
score -= score / 15 + 1;
scoreMod = to_string(-(score / 15 + 1));
lastShot = gameTime;
}
// Clamp Score
if (score < 0)
{
score = 0;
}
// Go through each ball
vector<GO> &ballLayer = game.GetLayer(1);
int ballCount = ballLayer.size();
for (int i = 0; i < ballLayer.size(); i++)
{
GO &ball = ballLayer.at(i);
// If the ball is off screen, apply a force to away from the wall.
if (ball.getCenter().x > SCREEN_WIDTH - 20)
{
ball.ApplyForce(LEFT);
}
else if (ball.getCenter().x < 20)
{
ball.ApplyForce(RIGHT);
}
if (ball.getCenter().y < 0)
{
ball.ApplyForce(DOWN);
}
// If it is below the screen, remove it.
if (ball.getCenter().y > SCREEN_HEIGHT + 20)
{
game.Delete(ball, 1);
ballCount -= 1;
// If there are no more balls left on the screen,
// flag the obstacles to move.
if (ballCount <= 0)
{
move = true;
}
}
}
// Go through each obstacle
vector<GO> &obstacleLayer = game.GetLayer(2);
for (int i = 0; i < obstacleLayer.size(); i++)
{
GO &obstacle = obstacleLayer.at(i);
// If it collided, delete it. Play a sound too.
if (obstacle.isColliding())
{
GO &deleted = game.Spawn(obstacle, 0);
deleted.setPhysics(NOCOLLIDE);
game.Delete(obstacle, 2);
scoreMod = "+10";
lastShot = gameTime;
game.Sound("Drop.wav");
score += 10;
}
else if (move)
{
// If flagged to move, set its destination higher. Scaled by score.
obstacle.SetDest(vec2(obstacle.getCenter().x, obstacle.getCenter().y - 100 - score / 3));
}
else if (obstacleLayer.at(i).getCenter().y <= 0)
{
// If it got above the screen, you lose.
state = LOSE;
}
// Pentagons and Triangles rotate.
else if (obstacle.getShape().getSides() == 5)
{
obstacle.Rotate(.002);
}
else if (obstacle.getShape().getSides() == 3)
{
obstacle.Rotate(-.002);
}
}
// If move, spawn a new row of obstacles.
if (move)
{
for (int i = 0; i < rand() % 7 + 1; i++)
{
// Random position, size, sides, and color.
GO &obj = game.Spawn(
GO(vec2(rand() % SCREEN_WIDTH, SCREEN_HEIGHT + 50),
Shape(rand() % 6 + 3, rand() % 25 + 20,
Color::HSV(rand() % 361, 50, 100))),
2);
obj.SetDest(vec2(obj.getCenter().x, 700 - rand() % 50));
obj.Rotate(rand() % 100 / 50.0 * PI);
}
move = false;
}
vector<GO> &delLayer = game.GetLayer(0);
for (int i = 0; i < delLayer.size(); i++)
{
GO &del = delLayer.at(i);
if (del.getShape().getColor() == BLANK)
{
game.Delete(del, 0);
}
del.Scale(1.5);
del.SetColor(Color::Lerp(del.getShape().getColor(), BLANK, 0.05 * game.DeltaTime()));
}
// Display score
game.Text("Score:" + to_string(score) + " ", vec2(15, 25), 3, 0, true);
game.Text(scoreMod, vec2(120, 50), 3, 0, true, Color::Lerp(NOTWHITE, BLANK, (gameTime - lastShot) / 180));
game.Update();
gameTime += game.DeltaTime();
}
// In between main game and lose screen
// Cover up the scoreMod text with spaces
game.Text(" ", vec2(120, 50), 3, 3, true, BLANK);
// Set the Score text to delete.
// (Literally does the same thing as the above but different ways of doing it)
game.Text("Score:" + to_string(score), vec2(15, 25), 3, 3, true, NOTWHITE, true);
setPointer(game, 3 * PI / 2);
// Go through each obstacle, set them to physics based and apply random force
vector<GO> &obstacleLayer = game.GetLayer(2);
for (int i = 0; i < obstacleLayer.size(); i++)
{
obstacleLayer.at(i).setMoveMethod(MoveMethod::PHYSICS);
obstacleLayer.at(i).ApplyForce(UP * .75 + RIGHT * (rand() % 100 / 250.0 - .2));
}
// Delete all deleted objects
int size = game.GetLayer(0).size();
for (int i = 0; i < size; i++)
{
game.Delete(0, 0);
}
// Create an invisible object that moves from top left to center.
GO &mover = game.Spawn(GO(vec2(15, 25), Shape(10, 10, RED)), 1);
mover.setVisible(false);
if (score > prevScore)
{
mover.SetDest(vec2(100, 200));
game.Sound("GoodDesign.wav");
}
else
{
mover.SetDest(vec2(160, 200));
}
// Lose Screen
while (state == LOSE && !game.getQuit())
{
// Make score text follow the invisible object
game.Text("Score:" + to_string(score), mover.getCenter(), 3, 3, false, NOTWHITE, true);
// Highscore or Game Over Title
if (score > prevScore)
{
RainbowText(game, "New highscore!", vec2(100, 160), 5, gameTime);
}
else
{
game.Text("Game over", vec2(160, 160), 5, 1, false, NOTWHITE, true);
}
game.Text("Hit space to restart", vec2(110, 500), 3, 1, false, NOTWHITE, true);
// Check for restart
if (game.KeyDown(' '))
{
state = TITLE;
game.Delete(mover, 1);
if (score > prevScore)
{
prevScore = score;
}
score = 0;
}
game.Update();
gameTime += game.DeltaTime();
}
// Delete all obstacles
size = game.GetLayer(2).size();
for (int i = 0; i < size; i++)
{
game.Delete(2, 0);
}
}
return 0;
}
void setPointer(GameControl &game, double angle)
{
vector<GO> &pointerLayer = game.GetLayer(3);
vec2 dir = vec2::Angle(angle);
for (int i = 0; i < pointerLayer.size(); i++)
{
pointerLayer.at(i).SetDest(TOP_CENTER + dir * (i * 50 + 50));
}
}
void RainbowText(GameControl &game, string text, vec2 pos, int size, int time)
{
int length = text.length();
for (int i = 0; i < length; i++)
{
game.Text(text.substr(i, 1), pos + RIGHT * i * size * 6, size, 3, false, Color::HSV((time + i * 5) % 360, 100, 100), true);
}
}