Guest User

Ryan ASCII_FIRE

a guest
Feb 27th, 2012
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include<ncurses.h>
  2. #include<string>
  3. #include<stdlib.h>
  4. #include<math.h>
  5. // only for debugging
  6. #include<iostream>
  7. #define pi 3.141
  8.  
  9. using namespace std;
  10.  
  11. class Particle
  12. {
  13.  public:
  14.   float x, y, max_life, life_left;
  15.   float direction;
  16.  
  17.   Particle()
  18.   {
  19.     this->x = 0;
  20.     this->y = 0;
  21.     this->max_life = 0;
  22.     this->direction = 0;
  23.     life_left = 0;
  24.   }
  25.  
  26.   Particle(float x, float y, float direction, int max_life)
  27.   {
  28.     this->x = x;
  29.     this->y = y;
  30.     this->max_life = max_life;
  31.     this->direction = direction;
  32.     life_left = max_life;
  33.   }
  34.  
  35.   void move()
  36.   {
  37.  
  38.     x+=cos(direction);
  39.     y+=sin(direction);
  40.   }
  41. };
  42.  
  43. // function prototypes
  44. void update_particles(Particle * first, int list_size, int max_x, int max_y, float max_age)
  45. {
  46.   for(int i = 0; i < list_size; i++)
  47.     {
  48.      
  49.       // inside screen still? move if so else reset
  50.       if(first->x <0 || first->y <0 || first->x > max_x || first->y > max_y)
  51.     {
  52.      first->x = rand()%max_x;
  53.      first->y = max_y;
  54.      first->direction = pi * 1.5;
  55.      first->max_life= rand()%30 + 1;
  56.      first->life_left = first->max_life;
  57.     }
  58.       else
  59.     {
  60.       first->move();
  61.     }
  62.      
  63.       // shave a life point off
  64.       if(first->life_left > 0)
  65.         {
  66.       first->life_left -= 1;
  67.         }
  68.       else
  69.     {
  70.       first->x = rand()%max_x;
  71.      first->y = max_y;
  72.      first->direction = pi * 1.5;
  73.      first->max_life= rand()%30 + 1;
  74.      first->life_left = first->max_life;
  75.     }
  76.       first++;
  77.     }
  78. }
  79.  
  80. void draw_particles(Particle * first, int list_size,string dither_pattern)
  81. {
  82.   for(int i = 0; i < list_size; i++)
  83.     {
  84.       float percent_dead = float(first->life_left) / float(first->max_life);
  85.       int dither = percent_dead * 6.0;
  86.       int color = dither + 1;
  87.       attrset(COLOR_PAIR(color));
  88.       mvaddch(first->y,first->x,dither_pattern[dither]);
  89.       first++;    
  90. }
  91. }
  92.  
  93. int main()
  94. {
  95.   // set up constants
  96.   // set the screen up disable echo of keys
  97.   initscr();
  98.   noecho();
  99.   nodelay(stdscr, TRUE);
  100.   curs_set(0);
  101.   start_color();
  102.  
  103. // colors for sparks
  104.   init_pair(7, COLOR_WHITE, COLOR_WHITE);
  105.   init_pair(6, COLOR_WHITE, COLOR_YELLOW);
  106.   init_pair(5, COLOR_YELLOW, COLOR_YELLOW);
  107.   init_pair(4, COLOR_YELLOW, COLOR_RED);
  108.   init_pair(3, COLOR_RED, COLOR_RED);
  109.   init_pair(2, COLOR_RED, COLOR_BLACK);
  110.   init_pair(1, COLOR_BLACK, COLOR_BLACK);
  111.   // array of dither patterns for particle color gradient
  112.   char half_shade = ACS_BOARD;
  113.   string dither_pattern =" # # # ";
  114.  
  115.   // screen size
  116.   int SCREEN_WIDTH = 80;
  117.   int SCREEN_HEIGHT = 24;
  118.   getmaxyx(stdscr,SCREEN_HEIGHT, SCREEN_WIDTH);
  119.  
  120.   // initial particles
  121.   const int NUM_PARTICLES = 20;
  122.   srand((unsigned)time(0));
  123.   Particle Part_array[NUM_PARTICLES];
  124.  
  125.   // dummy up some particles
  126.   for(int i = 0; i < NUM_PARTICLES; i++)
  127.     {
  128.       Part_array[i].x = rand()%SCREEN_WIDTH;
  129.       Part_array[i].y = SCREEN_HEIGHT;
  130.       Part_array[i].direction = pi*1.5;
  131.       Part_array[i].max_life = rand()%30;
  132.     }
  133.  
  134.   while(getch() != 'q')
  135.     {
  136.       update_particles(Part_array, NUM_PARTICLES, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_HEIGHT);
  137.       draw_particles(Part_array, NUM_PARTICLES,dither_pattern);
  138.       refresh();
  139.       napms(30);
  140.     }
  141.   getch();
  142.   endwin();
  143.   return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment