-
Notifications
You must be signed in to change notification settings - Fork 5
/
asteroid.c
186 lines (166 loc) · 3.7 KB
/
asteroid.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define WIDTH 50
#define HEIGHT 20
typedef struct
{
int x;
int y;
} Point;
typedef struct
{
Point position;
} Asteroid;
typedef struct
{
Point position;
int score;
} Ship;
Asteroid new_asteroid()
{
Asteroid asteroid;
srand(time(NULL));
asteroid.position.x = rand() % WIDTH;
asteroid.position.y = 0;
return asteroid;
}
void move_asteroid(Asteroid *asteroid)
{
asteroid->position.y++;
}
Ship new_ship()
{
Ship ship;
ship.position.x = WIDTH / 2;
ship.position.y = HEIGHT - 1;
ship.score = 0;
return ship;
}
void move_ship_left(Ship *ship)
{
ship->position.x--;
if (ship->position.x < 0)
{
ship->position.x = 0;
}
}
void move_ship_right(Ship *ship)
{
ship->position.x++;
if (ship->position.x >= WIDTH)
{
ship->position.x = WIDTH - 1;
}
}
void increase_score(Ship *ship)
{
ship->score++;
}
bool check_collision(Ship ship, Asteroid asteroid)
{
if (ship.position.x == asteroid.position.x && ship.position.y == asteroid.position.y)
{
return true;
}
else
{
return false;
}
}
int main()
{
Asteroid asteroids[100];
Ship ship = new_ship();
int num_asteroids = 0;
bool game_over = false;
while (!game_over)
{
// Clear screen
system("clear");
// Display game board
printf("Score: %d\n", ship.score);
for (int i = 0; i < HEIGHT; i++)
{
for (int j = 0; j < WIDTH; j++)
{
bool asteroid_here = false;
Point current_point = {j, i};
for (int k = 0; k < num_asteroids; k++)
{
if (asteroids[k].position.x == j && asteroids[k].position.y == i)
{
asteroid_here = true;
break;
}
}
if (current_point.x == ship.position.x && current_point.y == ship.position.y)
{
printf("O");
}
else if (asteroid_here)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
// Move asteroids
for (int i = 0; i < num_asteroids; i++)
{
move_asteroid(&asteroids[i]);
if (asteroids[i].position.y == HEIGHT - 1)
{
for (int j = i; j < num_asteroids - 1; j++)
{
asteroids[j] = asteroids[j + 1];
}
num_asteroids--;
i--;
}
}
// Add new asteroid
if (rand() % 10 == 0)
{
asteroids[num_asteroids] = new_asteroid();
num_asteroids++;
}
// Move ship
char input;
scanf("%c", &input);
switch (input)
{
case 'a':
move_ship_left(&ship);
break;
case 'd':
move_ship_right(&ship);
break;
}
// Check collision
for (int i = 0; i < num_asteroids; i++)
{
if (check_collision(ship, asteroids[i]))
{
game_over = true;
break;
}
}
// Increase score
for (int i = 0; i < num_asteroids; i++)
{
if (asteroids[i].position.y == HEIGHT - 1)
{
increase_score(&ship);
}
}
}
// Game over
printf("Game over! Final score: %d\n", ship.score);
return 0;
}