Advertisement
Guest User

driver.c

a guest
Dec 16th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. // DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  2. // Version 2, December 2004
  3. //
  4. // Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Everyone is permitted to copy and distribute verbatim or modified
  7. // copies of this license document, and changing it is allowed as long
  8. // as the name is changed.
  9. //
  10. // DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  11. // TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  12. //
  13. // 0. You just DO WHAT THE FUCK YOU WANT TO.
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <ncurses.h>
  18. #include <unistd.h>
  19. #include <time.h>
  20.  
  21. #define RED 1
  22. #define BLUE 2
  23. #define YELLOW 3
  24.  
  25. #define LEFT 260
  26. #define RIGHT 261
  27.  
  28. void sleepMs(int ms) {
  29. usleep(ms * 1000);
  30. }
  31.  
  32. void readysteadygo() {
  33. printw(" _____ _ \n| __ |___ ___ _| |_ _ \n| -| -_| .'| . | | |\n|__|__|___|__,|___|_ |\n |___|");
  34. refresh();
  35. sleep(1);
  36. clear();
  37. printw(" _____ _ _ \n| __| |_ ___ ___ _| |_ _ \n|__ | _| -_| .'| . | | |\n|_____|_| |___|__,|___|_ |\n |___|");
  38. refresh();
  39. sleep(1);
  40. clear();
  41. printw(" _____ \n| __|___ \n| | | . |\n|_____|___|");
  42. refresh();
  43. sleep(1);
  44. clear();
  45. }
  46.  
  47. void tryagain(int score, int current_game) {
  48. int i;
  49.  
  50. attron(A_BOLD);
  51. mvprintw(16, 0, "You crashed :)\nScore : %d", score);
  52. refresh();
  53. attroff(A_BOLD);
  54. sleep(1);
  55.  
  56. if (current_game == 42)
  57. mvprintw(18, 0, "You played 42 times ! You geek.");
  58. else
  59. mvprintw(18, 0, "Game restarts in 5 seconds...");
  60. refresh();
  61.  
  62. while (getch() != ERR);
  63. nodelay(stdscr, FALSE);
  64. timeout(5000);
  65. getch();
  66. nodelay(stdscr, TRUE);
  67. }
  68.  
  69. void game() {
  70. int i, j, current_game;
  71.  
  72. for (current_game = 1 ; current_game <= 42 ; current_game++)
  73. {
  74. int player_x = 9, police_alive[3] = {TRUE, TRUE, TRUE}, left_border[15] = {5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}, range = 9, delay = 250, key, random, score = 0;
  75. int collision = FALSE;
  76.  
  77. while (collision == FALSE)
  78. {
  79. if (player_x <= left_border[7] || player_x >= left_border[7] + range + 1)
  80. collision = TRUE;
  81. else
  82. {
  83. clear();
  84. for (i = 14 ; i > 0 ; i--)
  85. left_border[i] = left_border[i - 1];
  86. random = rand() % 3 - 1;
  87. left_border[0] += random;
  88. if (left_border[0] < 0)
  89. left_border[0] = 0;
  90. else if (left_border[0] > 10)
  91. left_border[0] = 10;
  92.  
  93. for (i = 0 ; i < 15 ; i++)
  94. {
  95. for (j = 0 ; j < left_border[i] ; j++)
  96. printw(" ");
  97. attron(COLOR_PAIR(3));
  98. printw("^");
  99. for (j = 0 ; j < range ; j++)
  100. printw(" ");
  101. printw("^\n");
  102. attroff(COLOR_PAIR(YELLOW));
  103. }
  104.  
  105. attron(COLOR_PAIR(BLUE));
  106. attron(A_BOLD);
  107. for (i = 0 ; i < 3 ; i++)
  108. {
  109. if (police_alive[i] == TRUE)
  110. {
  111. random = rand() % 3 - 1;
  112. mvprintw(i + 9, player_x + random, "@");
  113. refresh();
  114. if (player_x + random <= left_border[7] || player_x + random >= left_border[7] + range + 1)
  115. police_alive[i] = FALSE;
  116. }
  117. }
  118. attroff(COLOR_PAIR(BLUE));
  119.  
  120. key = getch();
  121. switch (key)
  122. {
  123. case RIGHT:
  124. player_x++;
  125. break;
  126. case LEFT:
  127. player_x--;
  128. break;
  129. }
  130. attron(COLOR_PAIR(RED));
  131. mvprintw(7, player_x, "$");
  132. attroff(COLOR_PAIR(RED));
  133. attroff(A_BOLD);
  134. move(7, player_x);
  135. refresh();
  136. score += 100;
  137. switch (score)
  138. {
  139. case 2000:
  140. range = 7;
  141. break;
  142. case 4000:
  143. range = 5;
  144. delay = 200;
  145. break;
  146. case 5000:
  147. range = 4;
  148. delay = 150;
  149. break;
  150. case 10000:
  151. range = 3;
  152. delay = 100;
  153. break;
  154. case 100000:
  155. range = 1;
  156. break;
  157. }
  158. }
  159.  
  160. sleepMs(delay);
  161. }
  162. tryagain(score, current_game);
  163. }
  164. }
  165.  
  166. void initialization() {
  167. srand(time(NULL));
  168. initscr();
  169. keypad(stdscr, TRUE);
  170. nodelay(stdscr, TRUE);
  171. scrollok(stdscr, TRUE);
  172. noecho();
  173. if (has_colors() == FALSE)
  174. {
  175. endwin();
  176. printf("The terminal does not support color... :w\n");
  177. exit(1);
  178. }
  179. else
  180. {
  181. start_color();
  182. init_pair(1, COLOR_RED, COLOR_BLACK);
  183. init_pair(2, COLOR_BLUE, COLOR_BLACK);
  184. init_pair(3, COLOR_YELLOW, COLOR_BLACK);
  185. }
  186. }
  187.  
  188. int main() {
  189. int key, counter = 0;
  190. int x, y;
  191. initialization();
  192. readysteadygo();
  193. game();
  194. endwin();
  195. return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement