45_fire.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Originally written by Matthew Simpson in Python: https://gist.github.com/msimpson/1096950
  2. #include <ncurses.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. static int *b = NULL;
  6. static int width;
  7. static int height;
  8. static int size;
  9. static void sizechanged()
  10. {
  11. getmaxyx(stdscr, height, width);
  12. size = width * height;
  13. b = (int *)realloc(b, (size + width + 1) * sizeof(int));
  14. memset(b, 0, (size + width + 1) * sizeof(int));
  15. clear();
  16. }
  17. int main()
  18. {
  19. initscr();
  20. const char *charz[] = {" ", ".", ":", "^", "*", "x", "s", "S", "#", "$"};
  21. curs_set(0);
  22. start_color();
  23. init_pair(1, COLOR_BLACK, COLOR_BLACK);
  24. init_pair(2, COLOR_RED, COLOR_BLACK);
  25. init_pair(3, COLOR_YELLOW, COLOR_BLACK);
  26. init_pair(4, COLOR_BLUE, COLOR_BLACK);
  27. sizechanged();
  28. while (1)
  29. {
  30. for (int i = 0; i < width / 9; i++)
  31. b[(rand() % width) + width * (height - 1)] = 65;
  32. for (int i = 0; i < size; i++)
  33. {
  34. b[i] = (b[i] + b[i + 1] + b[i + width] + b[i + width + 1]) / 4;
  35. int color = ((b[i] > 15) ? 4 : ((b[i] > 9) ? 3 : ((b[i] > 4) ? 2 : 1)));
  36. if (i < size - 1)
  37. {
  38. attrset(COLOR_PAIR(color) | A_BOLD);
  39. move(i / width, i % width);
  40. addstr(charz[((b[i] > 9) ? 9 : b[i])]);
  41. }
  42. }
  43. refresh();
  44. timeout(30);
  45. int ch = getch();
  46. if ((ch != -1) && (ch != KEY_RESIZE))
  47. break;
  48. if (ch == KEY_RESIZE)
  49. sizechanged();
  50. }
  51. endwin();
  52. return 0;
  53. }