-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathprogram.cpp
156 lines (136 loc) · 5.6 KB
/
program.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
#include "splashkit.h"
#include "include/types.h"
#include "include/global_constants.h"
#include "include/global_state.h"
#include "include/instance_creation.h"
#include "include/instance_state.h"
#include "include/rendering.h"
#include "include/resources.h"
GameData game_data;
InitialsEntryData initials_entry = {
{"-", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
};
int main() {
// Open the game window and toggle the border
open_window("DX Ball Game", 800, 600);
window_toggle_border("DX Ball Game");
// Load game resources
load_resources();
// Load high scores from JSON file
game_data.scores = json_from_file("scores.json");
for (int i = 0; i < 10; i++) {
game_data.score_rows[i] = json_read_object(game_data.scores, "row" + std::to_string(i));
}
bool game_closed = false;
// Main game loop
while (!game_closed) {
// Process user input events
process_events();
// Check if the escape key is pressed to close the game
if (key_down(ESCAPE_KEY)) {
game_closed = true;
}
// Update the exit timer and handle game over state
if (game_data.exit_timer > 0) {
game_data.exit_timer -= (1.0 / 60.0);
} else if (game_data.game_start && game_data.game_over) {
game_data.game_start = false;
game_data.exit_timer = TIME_ON_TITLE;
reset_game();
} else if (!game_data.game_start) {
game_closed = true;
}
// Show the title screen if the game hasn't started
if (!game_data.game_start) {
show_title_screen();
} else {
// Update the score multiplier timer
if (game_data.multiplier_timer > 0) {
game_data.multiplier_timer -= (1.0 / 60.0);
} else if (game_data.multiplier_timer < 0) {
game_data.multiplier_timer = 0;
} else {
game_data.score_multiplier = 1;
}
// Spawn new balls if the extra ball timer has expired and the game is not over
if (!game_data.ball_is_held) {
if (game_data.extra_ball_timer > 0) {
game_data.extra_ball_timer -= (1.0 / 60.0);
} else if (!game_data.game_over) {
int random_x = rnd(0 + BALL_RADIUS, 800 - BALL_RADIUS);
bool direction = static_cast<bool>(rnd(0, 2));
game_data.current_balls.push_back(create_ball(random_x, 0 + BALL_RADIUS, false, direction));
game_data.extra_ball_timer = NEW_BALL_DELAY;
}
}
// Start a new level if the next level flag is set
if (game_data.next_level) {
start_level();
}
// Update the paddle position based on user input
if (key_down(LEFT) && game_data.paddle_x > 10) {
game_data.paddle_x -= PADDLE_SPEED;
}
if (key_down(RIGHT) && game_data.paddle_x < screen_width() - PADDLE_LENGTH - 10) {
game_data.paddle_x += PADDLE_SPEED;
}
// Update ball positions and check for collisions
for (int i = 0; i < game_data.current_balls.size(); i++) {
check_ball_collision(i);
update_ball_location(i);
}
// Update power-up drop positions
for (int i = 0; i < game_data.current_power_ups.size(); i++) {
update_power_up_drops(i);
}
// Render the game graphics
draw_game();
// Check if the level is complete and advance to the next level
if (game_data.remaining_blocks == 0 && game_data.current_level <= 4) {
game_data.next_level = true;
game_data.current_level++;
}
// Handle game over state
if (game_data.game_over) {
end_game(game_data.game_won);
// Check if initials have been entered and handle user input
if (game_data.initials_entered) {
if (key_typed(START)) {
reset_game();
}
if (key_typed(P1_B2)) {
game_data.game_start = false;
game_data.exit_timer = TIME_ON_TITLE;
reset_game();
}
}
}
}
// debug cheats should be mapped to keys that are not usable with the arcade machine keyboard emulator
// Set exit timer to 1 second so timer can be tested without waiting
// TODO: These should not be available in any production build and should be at the very least gated behind a debug flag
if (key_typed(NUM_7_KEY)) {
game_data.exit_timer = 1;
}
if (key_typed(NUM_8_KEY)) {
game_data.game_won = false;
game_data.game_over = true;
game_data.exit_timer = TIME_ON_SCOREBOARD;
game_data.current_balls.clear();
game_data.current_power_ups.clear();
}
if (key_typed(NUM_9_KEY)) {
game_data.next_level = true;
game_data.current_level++;
}
if (key_typed(NUM_0_KEY)) {
game_data.multiplier_timer = MULTIPLIER_DURATION;
if (game_data.score_multiplier < 5) {
game_data.score_multiplier++;
}
}
// Refresh the screen at 60 frames per second
refresh_screen(60);
}
return 0;
}