-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
230 lines (182 loc) · 5.57 KB
/
Copy pathmain.cpp
File metadata and controls
230 lines (182 loc) · 5.57 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
/*
* This classic game uses http://www.libsigil.com/ (v0.9.0)
* and https://www.dafont.com/press-start-2p.font
* Compiles on Code::Blocks 17.12 with MinGW32
*/
#include "sl.h"
#include <stdlib.h> // Included for the rand() function
#include <stdio.h>
void DrawCar(int X, int Y);
void GetInput(int &X, int &Y, double &GameSpeed);
void DrawSingleBrick(int X, int Y);
void DrawLines(int Pos);
bool DrawEnemiesWithCD(bool Enemies[], int Pos, int PlayerX, int PlayerY);
void DrawLevel(int GameScore, int font);
void GameOver(int font);
void Intro(int font);
#define BrickSize 10
int main(int args, char *argv[])
{
// setup our window and a few resources we need
slWindow(640, 480, "Classic Brick Racing", false);
slSetBackColor(0.0, 0.0, 0.0); // black
int font = slLoadFont("ttf/PressStart2P.ttf");
int PlayerX = 270, PlayerY = 100, GameLevel = 1;
double GameSpeed = 120;
double dt; // Delta Time
double RoadScrollY = 1, EnemieScrollY = 1;
// array of enemies
bool Enemies[20];
Intro(font);
// Generate random enemies
// True = enemy to the left, false = enemy to the right
for(int i = 0; i < 19; i++){
if(rand()%2){
Enemies[i]=true;
}
else{
Enemies[i]=false;
}
}
// The window is open as long as it's not closed or the user presses escape
while(!slShouldClose() && !slGetKey(SL_KEY_ESCAPE))
{
dt = slGetDeltaTime(); // Get time since last slRender() call
// For smooth animation
RoadScrollY -= dt*GameSpeed;
EnemieScrollY -= dt*GameSpeed;
// If the road has scrolled more than -80 pixels reset to 0.
if(RoadScrollY < -80){
RoadScrollY = 0;
}
// Reset to next level
if(EnemieScrollY < -2800){
EnemieScrollY = 400;
GameSpeed += 20;
GameLevel += 1;
for(int i = 0; i < 19; i++){
if(rand()%2){
Enemies[i]=true;
}
else{
Enemies[i]=false;
}
}
}
GetInput(PlayerX, PlayerY, GameSpeed);
DrawCar(PlayerX, PlayerY);
DrawLines(RoadScrollY);
DrawLevel(GameLevel, font);
// Draw enemy card and check for collision detection
if(DrawEnemiesWithCD(Enemies, EnemieScrollY, PlayerX, PlayerY)){
slRender();
GameOver(font);
double Wait = slGetTime();
while( slGetTime() - Wait < 3.0){}
slClose();
return 1;
}
// draw everything
slRender();
}
slClose();// close the window and shutdown SIGIL
return 0;
}
void DrawCar(int X, int Y){
// Call DrawSingleBrick 12 times to draw a car
bool Car[4][3] = {{false, true, false},
{true, true, true},
{false, true, false},
{true, false, true}};
for(int CounterY = 0; CounterY <= 3; CounterY++){
for(int CounterX = 0; CounterX <= 2; CounterX++){
if(Car[CounterY][CounterX] == true){
DrawSingleBrick(X+(CounterX*BrickSize), Y-(CounterY*BrickSize));
}
}
}
return;
}
void DrawLines(int Pos){
// Draw the road
for(int Row = 520; Row >= 0; Row-=40){
DrawSingleBrick(213, Row+Pos);
DrawSingleBrick(426, Row+Pos);
DrawSingleBrick(213, Row-BrickSize+Pos);
DrawSingleBrick(426, Row-BrickSize+Pos);
}
};
void DrawSingleBrick(int X, int Y){
// draw the inner fill for a rectangle
slSetForeColor(0.5, 0.5, 0.0, 0.5);
slRectangleFill( X, Y, BrickSize, BrickSize);
// draw the outline for a rectangle
slSetForeColor(0.8, 0.8, 0.0, 0.8);
slRectangleOutline( X, Y, BrickSize, BrickSize);
return;
}
void GetInput(int &X, int &Y, double &GameSpeed){
if(slGetKey(SL_KEY_UP) && GameSpeed == 60)
{
GameSpeed+=60;
}
if(slGetKey(SL_KEY_DOWN) && GameSpeed == 120)
{
GameSpeed-=60;
}
if(slGetKey(SL_KEY_LEFT) && X > 270){
X-=80;
}
if(slGetKey(SL_KEY_RIGHT) && X < 350){
X+=80;
}
return;
}
// Draw other cars and check for collisions
bool DrawEnemiesWithCD(bool Enemies[], int Pos, int PlayerX, int PlayerY){
int EnemyX = 0; int EnemyY = 0;
for(int i = 0; i < 19; i++){
if(Enemies[i]==true){ // Draw car to the left
EnemyX = 270; EnemyY = Pos+400+(i*120);
DrawCar(EnemyX, EnemyY);
// Collision detection
if( PlayerX==EnemyX && EnemyY<140 && EnemyY>60 ){
return true;
}
}
if(Enemies[i]==false){ // Draw car to the right
EnemyX = 350; EnemyY = Pos+400+(i*120);
DrawCar(EnemyX, EnemyY);
// Collision detection
if( PlayerX==EnemyX && EnemyY<140 && EnemyY>60 ){
return true;
}
}
}
return false;
}
void GameOver(int font){
slSetFont(font, 18);
slText(200, 240, "[ Game Over ]");
slRender();
return;
}
void Intro(int font){
slSetForeColor(0.8, 0.8, 0.0, 0.8);
while(!slShouldClose() && !slGetKey(SL_KEY_ENTER)){
slSetFont(font, 18);
slText(150, 250, "Classic Brick Racing");
slSetFont(font, 14);
slText(240, 200, "Press Enter!");
slRender();
}
return;
}
void DrawLevel(int Level, int font){
slSetFont(font, 12);
char ScoreText[10];
sprintf(ScoreText, "%d", Level); // Convert int to char
slText(500, 240, "Level");
slText(510, 220, ScoreText);
return;
}