-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunnerup.cpp
More file actions
246 lines (228 loc) · 8.18 KB
/
runnerup.cpp
File metadata and controls
246 lines (228 loc) · 8.18 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
#include "raylib.h"
struct AnimData
{
Rectangle rec;
Vector2 pos;
int frame;
float updateTime;
float runningTime;
};
//---------------------------------------------------------------------------------------------------------
bool isOnGround(AnimData data, int windowHeighht)
{
return data.pos.y >= windowHeighht - data.rec.height;
}
//---------------------------------------------------------------------------------------------------------
// Anim data for sarfy
AnimData updateAnimData(AnimData data, float deltaTime, int maxFrame)
{
// update running time
data.runningTime += deltaTime;
if (data.runningTime >= data.updateTime)
{
data.runningTime = 0.0;
// update animation frame
data.rec.x = data.frame * data.rec.width;
data.frame++;
if (data.frame > maxFrame)
{
data.frame = 0;
}
}
return data;
}
//------------------------------------------------------------------------------------------------------------
int main()
{
//------------------------------------------------------------------------------------------------------------
// window Dimension using Array
int windowDimensions[2];
windowDimensions[0] = 512;
windowDimensions[1] = 380;
// initialize Window
InitWindow(windowDimensions[0], windowDimensions[1], "RunnerUp");
// acceleartion due to Gravity(pixels/s)/s
const int gravity{1'000};
//-------------------------------------------------------------------------------------------------------------
// Nebula Variable
Texture2D nebula = LoadTexture("textures/12_nebula_spritesheet.png");
// size of nebula
const int sizeofNebulae{10};
// Array and Loop for Anim Data
AnimData nebulae[sizeofNebulae]{};
// for loop Nebula
for (int i = 0; i < sizeofNebulae; i++)
{
nebulae[i].rec.x = 0.0;
nebulae[i].rec.y = 0.0;
nebulae[i].rec.width = nebula.width / 8;
nebulae[i].rec.height = nebula.height / 8;
nebulae[i].pos.y = windowDimensions[1] - nebula.height / 8;
nebulae[i].frame = 0;
nebulae[i].runningTime = 0.0;
nebulae[i].updateTime = 0.0;
nebulae[i].pos.x = windowDimensions[0] + i * 300;
}
// finish line condition
float finishLine{nebulae[sizeofNebulae - 1].pos.x};
// nebula X Velocity (Pixels/seconds)
int nebVel{-200};
//------------------------------------------------------------------------------------------------------------
// scarfy Vaiables
Texture2D scarfy = LoadTexture("textures/scarfy.png");
AnimData scarfyData;
scarfyData.rec.width = scarfy.width / 6;
scarfyData.rec.height = scarfy.height;
scarfyData.rec.x = 0;
scarfyData.rec.y = 0;
scarfyData.pos.x = windowDimensions[0] / 2 - scarfyData.rec.width / 2;
scarfyData.pos.y = windowDimensions[1] - scarfyData.rec.height;
scarfyData.frame = 0;
scarfyData.updateTime = 1.0 / 12.0;
scarfyData.runningTime = 0.0;
//-----------------------------------------------------------------------------------------------------------------------------
// Is the Rectangle in Air
bool IsinAir{};
// JUMP VELOCITY pixels/sec
const int jumpVel{-600};
// jump velocity
int velocity{0};
//------------------------------------------------------------------------------------------------------------------------------
// Ground textures
Texture2D background = LoadTexture("textures/far-buildings.png");
float bgX{};
Texture2D midground = LoadTexture("textures/back-buildings.png");
float mgX{};
Texture2D foreground = LoadTexture("textures/foreground.png");
float fgX{};
bool collision{};
SetTargetFPS(60);
while (!WindowShouldClose())
{
// delta time (time since last frame)
const float dT{GetFrameTime()};
//------------------------------------------------------------------------------------------------------------
// Game Logic Begins
BeginDrawing();
ClearBackground(WHITE);
//------------------------------------------------------------------------------------------------------------
// scroll background
bgX -= 20 * dT;
if (bgX <= -background.width * 2)
{
bgX = 0.0;
}
// scroll midground
mgX -= 40 * dT;
if (mgX <= -midground.width * 2)
{
mgX = 0.0;
}
// scroll foreground
fgX -= 80 * dT;
if (fgX <= -foreground.width * 2)
{
fgX = 0.0;
}
// DRAW background
Vector2 bg1Pos{bgX, 0.0};
DrawTextureEx(background, bg1Pos, 0.0, 2.0, WHITE);
Vector2 bg2Pos{bgX + background.width * 2, 0.0};
DrawTextureEx(background, bg2Pos, 0.0, 2.0, WHITE);
// draw midground
Vector2 mg1Pos{mgX, 0.0};
DrawTextureEx(midground, mg1Pos, 0.0, 2.0, WHITE);
Vector2 mg2Pos{mgX + midground.width * 2, 0.0};
DrawTextureEx(midground, mg2Pos, 0.0, 2.0, WHITE);
// draw foreground
Vector2 fg1Pos{fgX, 0.0};
DrawTextureEx(foreground, fg1Pos, 0.0, 2.0, WHITE);
Vector2 fg2Pos{fgX + foreground.width * 2, 0.0};
DrawTextureEx(foreground, fg2Pos, 0.0, 2.0, WHITE);
//--------------------------------------------------------------------------------------------------------------
if (isOnGround(scarfyData, windowDimensions[1]))
{
velocity = 0;
IsinAir = false;
}
else
{
velocity += gravity * dT;
IsinAir = true;
}
// Jump Check
if (IsKeyPressed(KEY_SPACE) && !IsinAir)
{
velocity += jumpVel;
}
//-------------------------------------------------------------------------------------------------------------
for (int i = 0; i < sizeofNebulae; i++)
{
// update the position of each Nebula
nebulae[i].pos.x += nebVel * dT;
}
// update finish Line
finishLine += nebVel * dT;
//-----------------------------------------------------------------------------------------------------------
// update scarfy position
scarfyData.pos.y += velocity * dT;
// update scarfy's animation frame
if (!IsinAir)
{
scarfyData = updateAnimData(scarfyData, dT, 5);
}
//-----------------------------------------------------------------------------------------------------------
for (int i = 0; i < sizeofNebulae; i++)
{
nebulae[i] = updateAnimData(nebulae[i], dT, 7);
}
//------------------------------------------------------------------------------------------------------------
// collision condition
for (AnimData nebula : nebulae)
{
float pad{50};
Rectangle nebRec{
nebula.pos.x + pad,
nebula.pos.y + pad,
nebula.rec.width - 2 * pad,
nebula.rec.height - 2 * pad
};
Rectangle scarfyRec{
scarfyData.pos.x,
scarfyData.pos.y,
scarfyData.rec.width,
scarfyData.rec.height
};
if (CheckCollisionRecs(nebRec, scarfyRec))
{
collision = true;
}
}
if (collision)
{
DrawText("Game Over!", windowDimensions[0]/4,windowDimensions[1]/2, 40, RED);
}
else if(scarfyData.pos.x >= finishLine)
{
DrawText("You Win!", windowDimensions[0]/4,windowDimensions[1]/2, 40, RED);
}
else
{
// draw Nebula
for (int i = 0; i < sizeofNebulae; i++)
{
DrawTextureRec(nebula, nebulae[i].rec, nebulae[i].pos, WHITE);
}
// Draw Scarfy
DrawTextureRec(scarfy, scarfyData.rec, scarfyData.pos, WHITE);
}
// Game Logic Ends
EndDrawing();
}
UnloadTexture(scarfy);
UnloadTexture(nebula);
UnloadTexture(background);
UnloadTexture(midground);
UnloadTexture(foreground);
CloseWindow();
}