Skip to content

Commit ee9c10d

Browse files
committed
initial commit
0 parents  commit ee9c10d

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Spaceship
2+
3+
Spaceship game in C, for Linux, based on @tsoj's, but with less code.
4+
5+
Use the arrows to move, try avoiding the asteroids.
6+
7+
## Compile
8+
9+
```sh
10+
gcc main.c -o main
11+
```
12+
13+
## Run
14+
15+
```sh
16+
./main
17+
```

main.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <sys/ioctl.h>
5+
#include <sys/time.h>
6+
#include <termios.h>
7+
#include <unistd.h>
8+
int t()
9+
{
10+
struct timeval t;
11+
gettimeofday(&t, 0);
12+
return t.tv_usec / 1000 + t.tv_sec * 1000;
13+
}
14+
15+
#define g(x, y, u, v, s) \
16+
for (int j = 0, X = x, Y = y; j < v && Y + j < h - X / w && Y >= 0 && X >= 0; ++j) \
17+
memcpy(&f[Y + j][X], &s[j * u], u)
18+
#define l(x, y, w, h, a, b, c, d) \
19+
!((x - a > c && x >= a) || (a - x > w && a >= x) || (y - b > d && y >= b) || (b - y > h && b >= y))
20+
21+
int main()
22+
{
23+
struct termios z, o;
24+
tcgetattr(0, &z);
25+
o = z;
26+
z.c_lflag &= ~ICANON & ~ECHO;
27+
z.c_cc[VMIN] = 0;
28+
tcsetattr(0, TCSANOW, &z);
29+
struct winsize v;
30+
ioctl(STDOUT_FILENO, TIOCGWINSZ, &v);
31+
int h = v.ws_row, w = v.ws_col, A = w * h / 100, l = t(), g = 1, i, c = 0, L;
32+
struct V
33+
{
34+
float x, y;
35+
} p = {w / 2, h / 2}, a[A], m[A];
36+
char u = 0, f[h + 1][w];
37+
while (1)
38+
{
39+
float d = (t() - l) * .001;
40+
read(0, &u, 1);
41+
l = t();
42+
c += 15;
43+
i = h * w - 1;
44+
K:
45+
L = c * d;
46+
L = abs(((i / w) - L) * (i % w) + h * w);
47+
(*f)[i] = L % 3 + L % 5 + L % 7 + L % 11 + L % 13 + L % 17 + L % 19 > 14 ? 32 : 46;
48+
if (i--)
49+
goto K;
50+
switch (u) // could be minified...
51+
{
52+
case 65:
53+
p.y -= d * 15;
54+
break;
55+
case 66:
56+
p.y += d * 15;
57+
break;
58+
case 67:
59+
p.x += d * 15;
60+
break;
61+
case 68:
62+
p.x -= d * 15;
63+
break;
64+
}
65+
p.x = p.x < 4 ? 4 : p.x >= w - 4 ? w - 4
66+
: p.x;
67+
p.y = p.y < 1 ? 1 : p.y >= h - 3 ? h - 3
68+
: p.y;
69+
i = A - 1;
70+
L:
71+
*(f[h]) = 0;
72+
struct V *e = &a[i], *z = &m[i];
73+
e->x += d * z->x;
74+
e->y += d * z->y;
75+
e->x < 0 - 3 || e->x >= w + 3 || e->y >= h + 2 || g ? e->y = -rand() % h * (1 + g),
76+
e->x = rand() % w,
77+
z->x = -8 + rand() % 15,
78+
z->y = 10 + rand() % 5 : 0;
79+
if (l(p.x, p.y, 4, 3, e->x, e->y, 3, 2))
80+
{
81+
tcsetattr(0, TCSADRAIN, &o);
82+
exit(0);
83+
};
84+
g(e->x, e->y, 3, 2, "OOOOOO");
85+
if (i--)
86+
goto L;
87+
g(p.x, p.y, 4, 3, " /\\ /__\\ VV ");
88+
puts(&f[0][4]);
89+
while (t() - l < 9)
90+
;
91+
g = 0;
92+
}
93+
}

0 commit comments

Comments
 (0)