-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainCode.c
306 lines (292 loc) · 10.2 KB
/
mainCode.c
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
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include "raylib.h"
#define FRAME 60
#define SIZE_WINDOW 400
#define SQUARE_H 30
#define SQUARE_W 30
#define SPEED 6
#define ENEMY_SIZE 6
#define COLOR GREEN
#define BULLET_SIZE 6
#define MAX 10
signed int SQUARE_X = SIZE_WINDOW/2;
signed int SQUARE_Y = SIZE_WINDOW - SQUARE_H;
bool colledPlayerWithEnemy = false;
Sound bulletShootSound, collisionSound, gameStart;
struct gameScore
{
unsigned int score;
}score = {0};
typedef struct enemy
{
Vector2 position;
Vector2 size;
signed int speed;
bool active;
}ENEMY;
typedef struct bullet
{
Vector2 position;
Vector2 size;
bool active;
signed int speedY;
}BULLET;
void playerUpdate(void);
void exitWindow(void);
void enemyUpdate(ENEMY* listOfEnemies);
void bulletUpdate(BULLET* listOfBullet);
void collision(BULLET* listOfBullet, ENEMY* listOfEnemy);
char* convertIntToString(void);
void clearEnemyBullet(BULLET* listOfBullet, ENEMY* listofEnemy);
int main(void)
{
srand(time(NULL));
Vector2 buttonPosition_1 = {162.5, 175};
Vector2 buttonSize_1 = {75, 50};
Vector2 buttonPosition_2 = {162.5, 235};
Vector2 buttonSize_2 = {75, 50};
Rectangle Button_1 = {buttonPosition_1.x, buttonPosition_1.y, buttonSize_1.x, buttonSize_1.y};
Rectangle Button_2 = {buttonPosition_2.x, buttonPosition_2.y, buttonSize_2.x, buttonSize_2.y};
Color defaultColor_1 = GRAY, defaultColor_2 = GRAY;
Color pressedColor = BLACK;
Color mouseOnButtonColor = DARKBROWN;
bool buttonPressed_1 = false, buttonPressed_2 = false;
ENEMY listOfEnemies[ENEMY_SIZE];
BULLET listOfBullet[BULLET_SIZE];
InitWindow(SIZE_WINDOW, SIZE_WINDOW, "BASIC GAME");
SetTargetFPS(FRAME);
InitAudioDevice();
bulletShootSound = LoadSound("gun-gunshot-02.wav");
collisionSound = LoadSound("you-lose-game-sound-230514.mp3");
gameStart = LoadSound("df start snd.wav");
while(!WindowShouldClose())
{
if(!buttonPressed_1)
{
if(!IsSoundPlaying(gameStart))
PlaySound(gameStart);
Vector2 mousePosition = GetMousePosition();
bool mouseCollideButton_1 = CheckCollisionPointRec(mousePosition, Button_1);
bool mouseCollideButton_2 = CheckCollisionPointRec(mousePosition, Button_2);
if(mouseCollideButton_1)
{
defaultColor_1 = (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) ? pressedColor : mouseOnButtonColor;
if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
buttonPressed_1 = true;
}
else if(mouseCollideButton_2)
{
defaultColor_2 = (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) ? pressedColor : mouseOnButtonColor;
if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
buttonPressed_2 = true;
}
else
{
defaultColor_1 = GRAY;
defaultColor_2 = GRAY;
}
BeginDrawing();
ClearBackground(WHITE);
DrawText("SKY DEFENDER GAME", 120, 140, 15, BLACK);
DrawRectangle(Button_1.x, Button_1.y, Button_1.width, Button_1.height, defaultColor_1);
DrawRectangle(Button_2.x, Button_2.y, Button_2.width, Button_2.height, defaultColor_2);
DrawText("EXIT", Button_2.x+13, Button_2.y+10, 15, WHITE);
DrawText("START", Button_1.x+10, Button_1.y+10, 15, WHITE);
DrawText("CREATED BY : DALI FETHI ABDELLATIF", 50, 350, 15, BLACK);
if(buttonPressed_2)
exitWindow();
EndDrawing();
}
else
{
StopSound(gameStart);
if(!colledPlayerWithEnemy)
{
playerUpdate();
bulletUpdate(listOfBullet);
enemyUpdate(listOfEnemies);
collision(listOfBullet, listOfEnemies);
BeginDrawing();
ClearBackground(WHITE);
DrawRectangle(SQUARE_X, SQUARE_Y, SQUARE_W, SQUARE_W, BLACK);
for(unsigned int i = 0; i < ENEMY_SIZE; ++i)
{
if(listOfEnemies[i].active)
DrawRectangle(listOfEnemies[i].position.x, listOfEnemies[i].position.y, listOfEnemies[i].size.x, listOfEnemies[i].size.y, COLOR); }
for(unsigned int i = 0; i < BULLET_SIZE; ++i)
{
if(listOfBullet[i].active)
DrawRectangle(listOfBullet[i].position.x, listOfBullet[i].position.y, listOfBullet[i].size.x, listOfBullet[i].size.y, BLUE);
}
EndDrawing();
}
else
{
char* scoreStr = convertIntToString();
if(IsKeyPressed(KEY_ENTER))
{
colledPlayerWithEnemy = false;
buttonPressed_1 = false;
buttonPressed_2 = false;
score.score = 0;
clearEnemyBullet(listOfBullet, listOfEnemies);
free(scoreStr);
}
BeginDrawing();
ClearBackground(WHITE);
DrawText("END GAME", 100, 100, 15, BLACK);
DrawText("YOUR SCORE : ", 100, 150, 15, BLACK);
DrawText(scoreStr, 230, 150, 18, BLACK);
DrawText("PRESS ENTER", 100, 200, 15, BLACK);
EndDrawing();
}
}
}
CloseWindow();
return 0;
}
void clearEnemyBullet(BULLET* listOfBullet, ENEMY* listofEnemy)
{
for(unsigned int i = 0; i < BULLET_SIZE; ++i)
listOfBullet[i].active = false;
for(unsigned int i = 0; i < BULLET_SIZE; ++i)
listofEnemy[i].active = false;
}
char* convertIntToString(void)
{
signed int TOP = -1;
unsigned int i = 0, number = score.score;
unsigned char stack[MAX];
char* str = calloc(MAX, sizeof(char));
while(i < MAX)
str[i++] = '\0';
if(number == 0)
str[0] = 0x30;
else
{
i = 0;
while(number != 0)
{
stack[++TOP] = (number % 10) + 0x30;
number /= 10;
}
while(TOP != -1)
str[i++] = stack[TOP--];
}
return str;
}
void collision(BULLET* listOfBullet, ENEMY* listOfEnemy) // COLLISION FUNCTION BETWEEN BULLET AND ENEMY
{
for(unsigned int i = 0; i < BULLET_SIZE; ++i)
{
if(listOfBullet[i].active)
{
for(unsigned int j = 0; j < ENEMY_SIZE; ++j)
{
if(listOfEnemy[i].active)
{
if (listOfBullet[i].position.x + listOfBullet[i].size.x < listOfEnemy[j].position.x || listOfBullet[i].position.x > listOfEnemy[j].position.x + listOfEnemy[j].size.x ||
listOfBullet[i].position.y + listOfBullet[i].size.y < listOfEnemy[j].position.y || listOfBullet[i].position.y > listOfEnemy[j].position.y + listOfEnemy[j].size.y);
else
{
listOfBullet[i].active = false;
listOfEnemy[j].active = false;
score.score += 10;
break;
}
}
}
}
}
for(unsigned int i = 0; i < ENEMY_SIZE; ++i)
{
if(listOfEnemy[i].active)
{
if (listOfEnemy[i].position.x + listOfEnemy[i].size.x < SQUARE_X || listOfEnemy[i].position.x > SQUARE_X + SQUARE_W ||
listOfEnemy[i].position.y + listOfEnemy[i].size.y < SQUARE_Y || listOfEnemy[i].position.y > SQUARE_Y + SQUARE_H);
else
{
listOfEnemy[i].active = false;
colledPlayerWithEnemy = true;
PlaySound(collisionSound);
break;
}
}
}
}
void bulletUpdate(BULLET* listOfBullet) // FUNCTION TO UPDATE BULLET
{
if(IsKeyPressed(KEY_SPACE))
{
PlaySound(bulletShootSound);
for(unsigned int i = 0; i < BULLET_SIZE; ++i)
{
if(!(listOfBullet[i].active))
{
listOfBullet[i].active = true;
listOfBullet[i].position.x = SQUARE_X;
listOfBullet[i].position.y = SQUARE_Y;
listOfBullet[i].size.x = 3;
listOfBullet[i].size.y = 8;
listOfBullet[i].speedY = -4;
break;
}
}
}
for(unsigned int i = 0; i < BULLET_SIZE; ++i)
{
if(listOfBullet[i].active)
{
listOfBullet[i].position.y += listOfBullet[i].speedY;
if(listOfBullet[i].position.y < 0)
listOfBullet[i].active = false;
}
}
}
void enemyUpdate(ENEMY* listOfEnemies)
{
for(unsigned int i = 0; i < ENEMY_SIZE; ++i)
{
if(!(listOfEnemies[i].active))
{
listOfEnemies[i].active = true;
listOfEnemies[i].size.x = SQUARE_H;
listOfEnemies[i].size.y = SQUARE_W;
listOfEnemies[i].position.x = rand() % (SIZE_WINDOW - SQUARE_W);
listOfEnemies[i].position.y = rand()%50;
listOfEnemies[i].speed = SPEED;
break;
}
}
for(unsigned int i = 0; i < ENEMY_SIZE; ++i)
{
if(listOfEnemies[i].active)
{
listOfEnemies[i].position.y += listOfEnemies[i].speed;
if(listOfEnemies[i].position.y >= SIZE_WINDOW)
listOfEnemies[i].active = false;
}
}
}
void playerUpdate(void)
{
if(IsKeyDown(KEY_LEFT))
{
SQUARE_X -= SPEED;
if(SQUARE_X <= 0)
SQUARE_X = SIZE_WINDOW;
}
else if(IsKeyDown(KEY_RIGHT))
{
SQUARE_X += SPEED;
if(SQUARE_X >= SIZE_WINDOW)
SQUARE_X = 0;
}
}
void exitWindow(void)
{
CloseWindow();
}