Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<ncurses.h>
- #include<string>
- #include<stdlib.h>
- #include<math.h>
- // only for debugging
- #include<iostream>
- #define pi 3.141
- using namespace std;
- class Particle
- {
- public:
- float x, y, max_life, life_left;
- float direction;
- Particle()
- {
- this->x = 0;
- this->y = 0;
- this->max_life = 0;
- this->direction = 0;
- life_left = 0;
- }
- Particle(float x, float y, float direction, int max_life)
- {
- this->x = x;
- this->y = y;
- this->max_life = max_life;
- this->direction = direction;
- life_left = max_life;
- }
- void move()
- {
- x+=cos(direction);
- y+=sin(direction);
- }
- };
- // function prototypes
- void update_particles(Particle * first, int list_size, int max_x, int max_y, float max_age)
- {
- for(int i = 0; i < list_size; i++)
- {
- // inside screen still? move if so else reset
- if(first->x <0 || first->y <0 || first->x > max_x || first->y > max_y)
- {
- first->x = rand()%max_x;
- first->y = max_y;
- first->direction = pi * 1.5;
- first->max_life= rand()%30 + 1;
- first->life_left = first->max_life;
- }
- else
- {
- first->move();
- }
- // shave a life point off
- if(first->life_left > 0)
- {
- first->life_left -= 1;
- }
- else
- {
- first->x = rand()%max_x;
- first->y = max_y;
- first->direction = pi * 1.5;
- first->max_life= rand()%30 + 1;
- first->life_left = first->max_life;
- }
- first++;
- }
- }
- void draw_particles(Particle * first, int list_size,string dither_pattern)
- {
- for(int i = 0; i < list_size; i++)
- {
- float percent_dead = float(first->life_left) / float(first->max_life);
- int dither = percent_dead * 6.0;
- int color = dither + 1;
- attrset(COLOR_PAIR(color));
- mvaddch(first->y,first->x,dither_pattern[dither]);
- first++;
- }
- }
- int main()
- {
- // set up constants
- // set the screen up disable echo of keys
- initscr();
- noecho();
- nodelay(stdscr, TRUE);
- curs_set(0);
- start_color();
- // colors for sparks
- init_pair(7, COLOR_WHITE, COLOR_WHITE);
- init_pair(6, COLOR_WHITE, COLOR_YELLOW);
- init_pair(5, COLOR_YELLOW, COLOR_YELLOW);
- init_pair(4, COLOR_YELLOW, COLOR_RED);
- init_pair(3, COLOR_RED, COLOR_RED);
- init_pair(2, COLOR_RED, COLOR_BLACK);
- init_pair(1, COLOR_BLACK, COLOR_BLACK);
- // array of dither patterns for particle color gradient
- char half_shade = ACS_BOARD;
- string dither_pattern =" # # # ";
- // screen size
- int SCREEN_WIDTH = 80;
- int SCREEN_HEIGHT = 24;
- getmaxyx(stdscr,SCREEN_HEIGHT, SCREEN_WIDTH);
- // initial particles
- const int NUM_PARTICLES = 20;
- srand((unsigned)time(0));
- Particle Part_array[NUM_PARTICLES];
- // dummy up some particles
- for(int i = 0; i < NUM_PARTICLES; i++)
- {
- Part_array[i].x = rand()%SCREEN_WIDTH;
- Part_array[i].y = SCREEN_HEIGHT;
- Part_array[i].direction = pi*1.5;
- Part_array[i].max_life = rand()%30;
- }
- while(getch() != 'q')
- {
- update_particles(Part_array, NUM_PARTICLES, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_HEIGHT);
- draw_particles(Part_array, NUM_PARTICLES,dither_pattern);
- refresh();
- napms(30);
- }
- getch();
- endwin();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment