/* This is the .aud file for the "ski" demo. * * Source code: ambiences/ski/ski.c * .aud file: ambiences/ski/ski.aud * Documentation: ambiences/HowTo.html */ #include #include #include #include #include #include #include #include "vssClient.h" void PrintBlanks(); int RandAB(); int ReadKeyboard(); void main() { int left = 25; /* position of left edge, skier, right edge */ int skier = 40; int right = 55; int c; if (!BeginSoundServer()) { printf("Connection to sound server failed. Is vss running?\n"); exit(2); } AUDinit("ski.aud"); /* open the .aud file */ printf("a -- left\ns -- right\nq -- quit\n\n"); system("stty -echo raw"); /* set up the terminal */ sginap(400); for (;;) { sginap(15); /* Game speed */ /* Draw the game. */ PrintBlanks(left); putchar(']'); PrintBlanks(skier-left-1); putchar('S'); PrintBlanks(right-skier-1); putchar('['); putchar('\r'); putchar('\n'); /* Adjust the ski slope. */ if (RandAB(1, 8) == 1) { /* narrow the ski slope by one */ if (RandAB(1, 2) == 1) left++; else right--; /* if the slope got narrow enough, player wins! */ if (right - left <= 9) { /* send messages for "Win" */ AUDupdateSimple("Win", 0, NULL); printf("\n\n\nYou win!\n\n"); break; } } else { /* move the ski slope left or right */ int jog = RandAB(-1, 1); left += jog; right += jog; } /* Get position of skier. */ c = ReadKeyboard(); if (c == 'a') { skier--; /* send messages for "Turn Left" */ AUDupdateSimple("TurnL", 0, NULL); } else if (c == 's') { skier++; /* send messages for "Turn Right" */ AUDupdateSimple("TurnR", 0, NULL); } else if (c == 'q') break; /* keep everything less than 80 columns */ if (left < 0) left = 0; else if (left > 79) left = 79; if (right < 0) right = 0; else if (right > 79) right = 79; if (skier < 0) skier = 0; else if (skier > 79) skier = 79; /* did skier run into the edge of the slope? */ if (skier <= left || skier >= right) { /* send messages for "Crash" */ AUDupdateSimple("Crash", 0, NULL); printf("You lose!\n"); break; } } sginap(150); system("stty echo -raw"); AUDupdateSimple("Exit", 0, NULL); /* send messages for "Exit" */ sginap(400); /* wait for sound to finish */ /* Essential cleanup. */ EndSoundServer(); } /* print n blanks */ void PrintBlanks(int n) { const char blanks[81] = " "; if (n > 0 && n <= 80) printf(blanks + 80 - n); } /* return a number in the range a to b inclusive */ int RandAB(int a, int b) { return (rand() % (b-a+1)) + a; } /* stuff for polling the keyboard */ fd_set fds; struct timeval timeout = { 0, 0 }; int ReadKeyboard(void) { FD_CLR(0, &fds); FD_SET(0, &fds); if (select(1, &fds, NULL, NULL, &timeout) == 1) return getchar(); return 0; }