Skip to content

Commit cf7c30d

Browse files
authored
Optimize (#1)
1 parent 7ed92ce commit cf7c30d

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Spaceship
22

3-
Spaceship game in 100 lines of C, for Linux and Windows, based on @tsoj's.
3+
Spaceship game in 111 lines of C, for Linux and Windows, based on @tsoj's.
44

55
Use the arrows to move, try avoiding the asteroids.
66

main.c

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ void getSize(int *x, int *y) {
1717
*x = csbi.srWindow.Right - csbi.srWindow.Left + 1;
1818
*y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
1919
}
20+
const char *spaceship[] = {
21+
" /\\ / \\ vv ",
22+
NULL,
23+
NULL,
24+
" />< > \\>",
25+
NULL,
26+
"<\\ < ></ ",
27+
NULL,
28+
NULL,
29+
" ^^ \\ / \\/ ",
30+
};
2031
#else
2132
#include <time.h>
2233
#include <stdlib.h>
@@ -47,10 +58,15 @@ void getSize(int *x, int *y) {
4758
*x = w.ws_col;
4859
*y = w.ws_row;
4960
}
61+
const char spaceship[][13] = {
62+
" /\\ / \\ vv ",
63+
" ^^ \\ / \\/ ",
64+
"<\\ < ></ ",
65+
" />< > \\>",
66+
};
5067
#endif
51-
#define g(x, y, u, v, s) for (int j = 0, X = x, Y = y; j < v && Y + j < h - X / w && Y >= 0 && X >= 0; ++j) memcpy(&f[Y + j][X], s + j * u, u)
52-
#define l(x, y, w, h, a, b, c, d) !((x - a > c && x >= a) || (a - x > w && a >= x) || (y - b > d && y >= b) || (b - y > h && b >= y))
53-
#define f(x, y, z) p.y += z * d; strcpy(b, x); break
68+
#define g(x, y, u, v, s) for (int j = 0, X = x, Y = y; j < v && Y + j < h - X / w && Y >= 0 && X >= 0; ++j) memcpy(&f[Y + j][X], &s[j * u], u)
69+
#define l(x, y, w, h, a, b, c, d) !((x - a > c && x >= a) || (a - x > w && a >= x) || (y - b > d && y >= b) || (b - y > h && b >= y))
5470
int main() {
5571
SetupConsole();
5672
printf("\x1b[?25l"); // hide cursor
@@ -59,43 +75,37 @@ int main() {
5975
int A = w * h / 100, l = GetTickCount(), g = 1, start = l;
6076
struct V {
6177
float x, y;
62-
} p = {w / 2, h / 2}, a[A], m[A];
63-
char u = 0, f[h + 1][w], b[13] = " /\\ / \\ vv ";
78+
} a[A], m[A];
79+
float p[2] = {w / 2, h};
80+
char u = 0, f[h + 1][w];
6481
for (ever) {
6582
float d = (GetTickCount() - l) * .001;
6683
Read(u); // read input
84+
if (u != UP && u != DOWN && u != LEFT && u != RIGHT) u = UP;
6785
l = GetTickCount();
6886
memset(f, ' ', h * w); // clear the screen
69-
switch (u) { // could be minified...
70-
case UP:
71-
f(" /\\ / \\ vv ", y, -20);
72-
case DOWN:
73-
f(" ^^ \\ / \\/ ", y, 20);
74-
case RIGHT:
75-
f("<\\ < ></ ", x, 40);
76-
case LEFT:
77-
f(" />< > \\>", x, -40);
78-
}
79-
p.x = p.x < 4 ? 4 : p.x >= w - 4 ? w - 4 : p.x; // make sure the player is in the screen (horizontally)
80-
p.y = p.y < 2 ? 2 : p.y >= h - 3 ? h - 3 : p.y; // make sure the player is in the screen (vertically)
87+
p[u == UP || u == DOWN] -= 2 * (u == UP || u == LEFT) - 1;
88+
p[0] = p[0] < 0 ? 0 : p[0] >= w - 7 ? w - 7 : p[0]; // make sure the player is in the screen (horizontally)
89+
p[1] = p[1] < 8 ? 8 : p[1] / 2 >= h - 3 ? (h - 3) * 2 : p[1]; // make sure the player is in the screen (vertically)
8190
for (int i = A - 1; i--;) {
8291
*f[h] = 0;
8392
struct V *e = a + i, *z = m + i;
8493
e->x += d * z->x; // move the asteroid in the x direction
8594
e->y += d * z->y; // move the asteroid in the y direction
8695
e->x < 0 - 3 || e->x >= w + 3 || e->y >= h + 2 || g ? e->y = -rand() % h * (1 + g), e->x = rand() % w, z->x = -8 + rand() % 15, z->y = 10 + rand() % 5 : 0;
87-
if (l(p.x, p.y, 4, 3, e->x, e->y, 3, 2)) { // if player hit an asteroid
88-
tcsetattr(0, TCSADRAIN, &o); // restore the terminal
89-
exit(0); // exit the program
96+
if (l(p[0], p[1] / 2, 4, 3, e->x, e->y, 3, 2)) { // if player hit an asteroid
97+
tcsetattr(0, TCSADRAIN, &o); // restore the terminal
98+
printf("\x1b[?25h"); // show cursor
99+
exit(0); // exit the program
90100
};
91101
g(e->x, e->y, 3, 2, "OOOOOO"); // draw the asteroid
92102
}
93-
g(p.x, p.y, 4, 3, b); // draw the player
103+
g(p[0], p[1] / 2, 4, 3, spaceship[u - UP]); // draw the player
94104
for (int i = -2, j = 1e7, k, K = 0; i <= 2; i++, j /= 10, k = (l - start) / j % 10, K = k | K) f[1][w / 2 + i] = K ? '0' + k : ' '; // draw the score
95105
getSize(&W, &H); // get the new screen size
96106
if (W != w || H < h) exit(printf("\033[0;31m\nPlease don't resize the screen, it will break the program\033[0m\n")); // make sure the screen is not resized besides getting higher
97-
else printf("\033[0;4H%s", f + 4); // print the screen
98-
while (GetTickCount() - l < 10); // wait for a bit
107+
else printf("\033[0;4H%s", f + 4); // print the screen
108+
while (GetTickCount() - l < 20); // wait for a bit
99109
g = 0;
100110
}
101-
}
111+
}

0 commit comments

Comments
 (0)