029
06.08.2003, 15:33 Uhr
0xdeadbeef
Gott (Operator)
|
ich habs mal ein bisschen auseinandergepfriemelt und mit sprechenderen Variablennamen versehen. So sieht das Programm in Normal-C aus:
C++: |
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/time.h> #include <signal.h>
#define LEFT (-1) #define RIGHT (1) #define FWD (0)
#define MAX_TRACK_INDEX (21) #define MAX_TREE_INDEX (4)
int track[] = {40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40}, width [] = { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8}, led [] = { 2, 3, 1, 0};
char delim [] = { '\\', '|', '/' }, *tree [] = {" |", " |", "%\\|/%", " %%%", ""}; int tree_ix = MAX_TREE_INDEX, tree_pos, delim_ix = 1, pos = 40, oldpos = 40, steer = FWD, lauf, speed = -1, gear = 0, sound_on = 0;
unsigned int score = 0;
void do_main (int sig) { int n;
pos += steer; printf ("\233;%uH\233L%c\233;%uH%c\233;%uH%s\23322;%uH@\23323;%uH \n", track[0] - width[0], delim[delim_ix], track[0] + width[0], delim[delim_ix], tree_pos, tree[tree_ix], pos, oldpos);
if (abs (pos - track[MAX_TRACK_INDEX]) >= width[MAX_TRACK_INDEX]) exit (0);
if (speed != gear*8) { struct itimerval t = { 0, 0, 0, 0 };
speed += (speed < gear*8) ? 1 : -1;
t.it_interval.tv_usec = t.it_value.tv_usec = 72000 / (speed / 8 + 1); setitimer (0, &t, 0);
if(sound_on) printf ("\e[10;%u]", speed + 24); }
if(sound_on) putchar (7);
score += (9 - width[MAX_TRACK_INDEX]) * (speed / 8 + 1); oldpos = pos;
for(lauf = MAX_TRACK_INDEX; lauf > 0; --lauf) { width[lauf] = width[lauf - 1]; track[lauf] = track[lauf - 1]; }
if((n = rand ()) % 256 == 0 && width[0] > 1) --width[0];
if (tree[tree_ix][0]) { ++tree_ix; } else if(!(n&0x1f00)) { while (abs ((tree_pos = rand () % 76) - track[0] + 2) - width[0] < 6); ++tree_pos; tree_ix = 0; }
if((n = rand () & 31) < 3) (delim_ix = n);
if(delim_ix == 0 && --track[0] - width[0] <= 0){ ++track[0]; ++delim_ix; } else if(delim_ix == 2 && ++track[0] + width[0] >= 80) { --track[0]; --delim_ix; }
signal (SIGALRM, do_main); }
void do_on_exit () { signal (SIGALRM, ((__sighandler_t) 1)); printf ("\e[0q\ecScore: %u\n", score); system ("stty echo -cbreak"); }
int main (int argc, char *argv[]) { int term; char c;
atexit (do_on_exit);
if((argc < 2 || *argv[1] != 113)) { term = *(int*) getenv("TERM"); if (term == (int) 0x756E696C || term == (int) 0x6C696E75) sound_on = 1; }
srand (getpid ()); system ("stty -echo cbreak");
printf("\e[%uq", led[gear]);
do_main (SIGALRM);
for(;;) switch (c = getchar ()) { case 'q': return 0; case '[': case 'b': case ',': steer = LEFT; continue; case ' ': case 'n': case '.': steer = FWD; continue; case ']': case 'm': case '/': steer = RIGHT; continue; case '1': case '2': case '3': case '4': gear = c - '1'; printf ("\e[%uq", led[gear]); continue; } }
|
-- Einfachheit ist Voraussetzung für Zuverlässigkeit. -- Edsger Wybe Dijkstra Dieser Post wurde am 06.08.2003 um 15:33 Uhr von 0xdeadbeef editiert. |