-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.txt
195 lines (187 loc) · 5.12 KB
/
game.txt
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
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include "raylib.h"
#define MAX_BULLET 10
#define MAX_ENEMY 10
typedef struct circlePosition
{
signed int x;
signed int y;
}POSITION;
typedef struct bullet
{
signed int x;
signed int y;
bool active;
signed int speedY;
}BULLET;
typedef struct enemy
{
signed int x;
signed int y;
bool active;
signed int speedX;
}ENEMY;
bool gameState = false;
unsigned int counter = 0;
signed int radius = 5, speed = -10;
void updatePosition(POSITION* position);
void bulletUpdate(BULLET* bullet, POSITION position);
void updateEnemy(ENEMY* enemies);
void fillEnmySpeed(ENEMY* enemies);
void collision(ENEMY* enemies, BULLET* bullet);
int main(void)
{
POSITION position;
srand(time(NULL));
ENEMY enemies[MAX_ENEMY] = {0};
BULLET bullet[MAX_BULLET] = {0};
position.y = 760;
position.x = 400;
InitWindow(800, 800, "BASIC GAME");
SetTargetFPS(60);
fillEnmySpeed(enemies);
while(!WindowShouldClose())
{
if(!gameState)
{
if(IsKeyPressed(KEY_ENTER))
gameState = true;
BeginDrawing();
ClearBackground(WHITE);
DrawText("SHOOTING GAME", 250, 250, 20, BLACK);
DrawText("PRESS ENTER TO START", 250, 300, 20, BLACK);
DrawText("BUILD BY : ", 200, 600, 20, BLACK);
DrawText("DALI FETHI ABDELLATIF", 300, 650, 20, BLACK);
EndDrawing();
}
else
{
if(counter == 10)
{
counter = 0;
gameState = false;
}
else
{
updatePosition(&position);
bulletUpdate(bullet, position);
updateEnemy(enemies);
collision(enemies, bullet);
BeginDrawing();
ClearBackground(WHITE);
DrawRectangle(position.x, position.y, 40, 40, BLACK);
for(unsigned int i = 0; i < MAX_ENEMY; ++i)
{
if(enemies[i].active)
DrawCircle(enemies[i].x, enemies[i].y, radius, RED);
}
for(unsigned int i = 0; i < MAX_BULLET; ++i)
{
if(bullet[i].active)
DrawCircle(bullet[i].x, bullet[i].y, radius, GREEN);
}
EndDrawing();
}
}
}
CloseWindow();
return 0;
}
void fillEnmySpeed(ENEMY* enemies)
{
for(unsigned int i = 0; i < MAX_ENEMY; ++i)
enemies[i].speedX = rand()%5 + 1;
}
void updateEnemy(ENEMY* enemies)
{
bool allEnmiesInactive = true;
for(unsigned int i = 0; i < MAX_ENEMY; ++i)
{
if(enemies[i].active)
{
allEnmiesInactive = false;
break;
}
}
if(allEnmiesInactive)
{
for(unsigned int i = 0; i < MAX_ENEMY; ++i)
{
if(!enemies[i].active)
{
enemies[i].active = true;
enemies[i].x = rand()%700;
enemies[i].y = rand()%300;
}
}
}
for(unsigned int i = 0; i < MAX_ENEMY; ++i)
{
if(enemies[i].active)
{
enemies[i].x += enemies[i].speedX;
if(enemies[i].x < 0 || enemies[i].x > 800)
enemies[i].speedX *= -1;
}
}
}
void bulletUpdate(BULLET* bullet, POSITION position)
{
if(IsKeyPressed(KEY_SPACE))
{
for(unsigned int i = 0; i < MAX_BULLET; ++i)
{
if(!bullet[i].active)
{
bullet[i].active = true;
bullet[i].x = position.x;
bullet[i].y = position.y;
bullet[i].speedY = speed;
break;
}
}
}
for(unsigned int i = 0; i < MAX_BULLET; ++i)
{
if(bullet[i].active)
{
bullet[i].y += bullet[i].speedY;
if(bullet[i].y < 0)
bullet[i].active = false;
}
}
}
void updatePosition(POSITION* position)
{
if(IsKeyDown(KEY_LEFT))
position->x -= 3;
else if(IsKeyDown(KEY_RIGHT))
position->x += 3;
}
void collision(ENEMY* enemies, BULLET* bullet)
{
float distance = 0.0f;
for(unsigned int i = 0; i < MAX_BULLET; ++i)
{
if(bullet[i].active)
{
for(unsigned int j = 0; j < MAX_ENEMY; ++j)
{
if(enemies[j].active)
{
distance = sqrt(pow(bullet[i].x - enemies[j].x, 2) + pow(bullet[i].y - enemies[j].y, 2));
if(distance <= (radius*2))
{
bullet[i].active = false;
enemies[j].active = false;
++counter;
break;
}
}
}
}
}
}