-
Notifications
You must be signed in to change notification settings - Fork 0
/
asteroids.c
317 lines (293 loc) · 9.32 KB
/
asteroids.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
307
308
309
310
311
312
313
314
315
316
//Phil Stenger
//Ryan Hooley
#include <stdio.h>
#include "gfx3.h"
#include <math.h>
#include <stdlib.h>
#define PI 3.1415926535897
void drawship(double, double, double, int);
void flyship(void);
void asteroids(double [][5]);
void move_asteroids(double [][5], double [][5], int, int, int, int, double *, double *, double *, double *, int []);
void initialize(double [][5]);
void death(double [][5], double [][5], double *, double *, int []);
void shoot(double [][5], double, double, double);
void move_shots(double [][5]);
void destroy(double [][5], double [][5], int score[]);
void asteroids_level2(double [][5], int);
void asteroids_level3(double [][5], int);
int main()
{
int height, width;
height=700;
width=700;
srand(time(NULL));
gfx_open(width, height, "Asteroids");
flyship();
return 0;
}
void drawship(double x, double y, double theta, int go)
{
int i;
gfx_line(x, y, x+20*cos(theta+4*PI/3), y-20*sin(theta+4*PI/3)); //draws long 2 lines that make up ship
gfx_line(x, y, x+20*cos(theta+5*PI/3), y-20*sin(theta+5*PI/3));
gfx_line( x+16*cos(theta+4*PI/3), y-16*sin(theta+4*PI/3), x-16*cos(theta+5*10*PI/3), y-16*sin(theta+5*PI/3)); //draws back of ship
if(go>0){ //go will be greater than 1 if ship is being propulsed forward
gfx_color(255, 69, 0);
for(i=0; i<5; i++){
gfx_line( x+(17+i)*cos(theta+4*PI/3), y-(17+i)*sin(theta+4*PI/3), x-(17+i)*cos(theta+5*10*PI/3), y-(17+i)*sin(theta+5*PI/3));
}
}
gfx_color(255, 255, 255);
}
void flyship(void)
{
double x, y;
double theta;
char c;
int i, go, x1ship, x2ship, y1ship, y2ship;
int score[2];
score[0]=0;
score[1]=3;
char str[100];
go=0; //will tell drawship function if forward button is held down so flames can display
int iterate=0; //counts iterations of loop to determine when to move asteroids
x=350;
y=350; //initial conditions
theta=0;
//the 2d array roids will contain 32 elements, one for each potential asteroid
//and each element will have four numbers for the asteroids' properties
double astr[32][5];
double shots[32][5]; //2d array for the shots out of the ship and its properties
initialize(astr);
initialize(shots);
drawship(x, y, theta, go);
double speed=0;
while(1){
if(gfx_event_waiting()) {
c=gfx_wait();
if (score[1]==0 || c=='q') {
//this will run when lives are 0 and display the score
gfx_clear();
sprintf(str, "Score: %i Press q to exit game", score[0], score[1]);
gfx_text(250, 350, str);
c=gfx_wait();
if(c=='q'){
break;
}
}
if(c=='d'){
theta=theta-PI/20;
//this will rotate the ship
}
if(c=='a'){
theta=theta+PI/20;
}
if(c=='w'){
speed=speed+1; //moves ship forward
if (speed>5){
speed=5;
}
go=1; //will tell drawship function that ship is moving
}
if(c=='o') {
shoot(shots, theta, x, y);
}
if(x>700){
x=1;
} else if(x<0){
x=699;
} else if(y>700){ //this keeps the ship from flying off the screen
y=1;
} else if(y<0){
y=699;
}
}
gfx_clear();
usleep(10000);
x1ship= x+20*cos(theta+4*PI/3); //these variables create a sort of box around the ship, which is later used to check the ship's coordinates for collisions
y1ship= y;
x2ship= x+20*cos(theta+5*PI/3);
y2ship= y-20*sin(theta);
move_shots(shots);
destroy(astr, shots, score); //checks whether an asteroid should be destroyed
move_asteroids(astr, shots, x1ship, y1ship, x2ship, y2ship, &x, &y, &speed, &theta, score);
x=x+speed*cos(theta+PI/2); //iterates coordinates of ship based on changes to theta and speed
y=y-speed*sin(theta+PI/2);
drawship(x, y, theta, go);
if(go>=1){
go=go-1; //keeps go at 1 so it stops when stop pressing forward button
}
if(speed>.025){
speed=speed-.025;
}
iterate++;
sprintf(str, "Score: %d Lives: %d", score[0], score[1]);
gfx_text(20, 680, str);
}
}
void asteroids(double astr[][5])
{
int i;
for(i=0; i<=19; i=i+4){
if(astr[i][3]==0){
astr[0+i][0]=rand()%300 || 400+rand()%300; //x coordinate
astr[0+i][1]=rand()%550; //y coordinate
astr[0+i][2]=((double)rand()/(double)RAND_MAX)*(2*PI); //direction
astr[0+i][3]=40; //size
astr[0+i][4]=1; //speed
}
}
}
void move_asteroids(double astr[][5], double shots[][5], int x1ship, int y1ship, int x2ship, int y2ship, double *x, double *y, double *speed, double *theta, int score[])
{
int i;
double j;
asteroids(astr);
for(i=0; i<32; i++){
if(astr[i][3]!=0){
astr[i][0]=astr[i][0]+astr[i][4]*cos(astr[i][2]); //iterates asteroid coordinates
astr[i][1]=astr[i][1]-astr[i][4]*sin(astr[i][2]);
for(j=0; j<2*PI; j=j+PI/50){
//the line below checks whether ship has collided with an asteroid
if((astr[i][0]+astr[i][3]*cos(j))<=x1ship && (astr[i][1]-astr[i][3]*sin(j))<=y1ship && (astr[i][0]+astr[i][3]*cos(j+PI))>=x2ship && (astr[i][1]-astr[i][3]*sin(j+PI))>=y2ship){
*speed=0;
*theta=0; //resets theta and speed
death(astr, shots, x, y, score);
}
}
if(astr[i][0]>=740){
astr[i][0]=1;
} else if(astr[i][0]<=-40){
astr[i][0]=699;
} else if(astr[i][1]>=740){ //this keeps the ship from flying off the screen
astr[i][1]=1;
} else if(astr[i][1]<=-40){
astr[i][1]=699;
}
gfx_color(0, 170, 255);
gfx_circle(astr[i][0], astr[i][1], astr[i][3]);
gfx_color(255, 255, 255);
}
}
}
void initialize(double arrays[][5])
{
int i, j;
for(i=0; i<32; i++){
for(j=0; j<5; j++){
arrays[i][j]=0;
}
}
}
void death(double astr[][5], double shots[][5], double *x, double *y, int score[])
{
int i;
gfx_clear();
gfx_color(255, 0, 0);
for(i=0; i<10; i++){
gfx_flush();
gfx_color(255, 0, 0);
drawship(350, 350, 0, 0); //redraws ship and pauses to allow user time to recover
usleep(100000);
gfx_color(255, 255, 255);
drawship(350, 350, 0, 0);
usleep(100000);
}
gfx_flush();
*x=350;
*y=350;
score[1]=score[1]-1; //subtracts a life
initialize(astr); //resets astroids and shots
initialize(shots);
}
void shoot(double shots[][5], double theta, double x, double y)
{
// this will create a shot to destroy asteriods in the direction of the ship
int i, j;
for (i=0; i<30; i++) {
if (shots[i][3]==0) {
shots[i][0]=x; //X coordinate
shots[i][1]=y; //Y coordinate
shots[i][2]=theta; //direction
shots[i][3]=2; //radius
break;
}
}
}
void move_shots(double shots[][5])
{
int i, j;
for(i=0; i<30; i++){
if(shots[i][3]!=0){
shots[i][0]=shots[i][0]+6*cos(shots[i][2]+PI/2); //shots fired at angle ship is at
shots[i][1]=shots[i][1]-6*sin(shots[i][2]+PI/2);
if(shots[i][0]>=700){
shots[i][3]=0;
} else if(shots[i][0]<=0){
shots[i][3]=0;
} else if(shots[i][1]>=700){ //this kills shots when they leave the screen
shots[i][3]=0;
} else if(shots[i][1]<=0){
shots[i][3]=0;
}
gfx_color(0,255,0);
gfx_circle(shots[i][0], shots[i][1], shots[i][3]);
gfx_color(255,255, 255);
}
}
gfx_flush();
}
void destroy(double astr[][5], double shots[][5], int score[])
{
// This will break down the asteriods when they are hit with a shot
int i, j, k, l;
for (i=0; i<32; i++) {
for (j=0; j<30; j++) {
if(shots[j][3]!=0){ //keeps from running following statements with nonfired shots
//line below checks to see if shot overlaps with asteroid
if(astr[i][0]+astr[i][3]>=shots[j][0]-shots[j][3] && astr[i][0]-astr[i][3] <= shots[j][0]+shots[j][3] && astr[i][1]+astr[i][3]>=shots[j][1]-shots[i][3] && astr[i][1]- astr[i][3] <= shots[j][1]+shots[j][3]) {
if (astr[i][3]==40) {
shots[j][3]=0; //shot disappears
score[0]=score[0]+1; //score increases
asteroids_level2(astr, i);
} else if (astr[i][3]==20) {
score[0]=score[0]+2; //score increases
shots[j][3]=0; //shot disappears
asteroids_level3(astr, i);
} else if (astr[i][3]<=10) {
shots[i][3]=0; //shot disappears
score[0]=score[0]+3;
astr[i][3]=0; //asteroid radius set to zero so that it can go through asteroid()
astr[i][4]=1;
}
gfx_flush();
}
}
}
}
}
void asteroids_level2(double astr[][5], int p)
{
// splits a 40 r asteriod into 2 20 r
astr[p][2]=((double)rand()/(double)RAND_MAX)*(2*PI); //direction
astr[p][3]=20; //size
astr[p][4]=1.5; //speed changed
astr[p+1][2]=((double)rand()/(double)RAND_MAX)*(2*PI); //direction
astr[p+1][3]=20;
astr[p+1][0]=astr[p][0];
astr[p+1][1]=astr[p][1];
astr[p+1][4]=2;
}
void asteroids_level3(double astr[][5], int p)
{
// splits a 20 r asteriod into 2 10 r
astr[p][2]=((double)rand()/(double)RAND_MAX)*(2*PI); //direction
astr[p][3]=10; //size
astr[p][4]=2; //speed
astr[p+2][2]=((double)rand()/(double)RAND_MAX)*(2*PI); //direction
astr[p+2][3]=10;
astr[p+2][0]=astr[p][0];
astr[p+2][1]=astr[p][1];
astr[p+2][4]=3;
}