dos33fsprogs/gr-sim/hgr/fireworks.c

164 lines
3.2 KiB
C
Raw Normal View History

2018-09-04 03:44:53 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
2018-09-04 03:59:51 +00:00
#include <math.h>
2018-09-04 03:44:53 +00:00
#include "gr-sim.h"
2018-09-05 02:34:52 +00:00
// Based on BASIC program posted by FozzTexx, originally written in 1987
2018-09-04 03:44:53 +00:00
//140 REM MS is max steps, CS is current step, X/Y/X1/Y1/X2/Y2 is rocket position
//150 REM CL is Apple II hi-res color group
2018-09-05 02:34:52 +00:00
const int ysize=160,xsize=280,margin=24;
int color_group;
int max_steps;
double x_even_older,x_old=0,y_even_older,y_old=0,cs,peak;
2018-09-04 03:44:53 +00:00
double xpos,ypos,x_velocity,y_velocity;
2018-09-04 03:59:51 +00:00
double i,n;
2018-09-04 03:44:53 +00:00
void routine_370(void) {
2018-09-05 02:34:52 +00:00
hplot(xpos+x_old+n,ypos+y_old+n); // NE
hplot(xpos+x_old-n,ypos+y_old-n); // SW
hplot(xpos+x_old+n,ypos+y_old-n); // SE
hplot(xpos+x_old-n,ypos+y_old+n); // NW
2018-09-04 03:44:53 +00:00
2018-09-05 02:34:52 +00:00
hplot(xpos+x_old,ypos+y_old+(n*1.5)); // N
hplot(xpos+x_old+(n*1.5),ypos+y_old); // E
2018-09-04 03:44:53 +00:00
2018-09-05 02:34:52 +00:00
hplot(xpos+x_old,ypos+y_old-(n*1.5)); // S
hplot(xpos+x_old-(n*1.5),ypos+y_old); // W
2018-09-04 03:44:53 +00:00
}
int main(int argc, char **argv) {
int ch;
grsim_init();
home();
hgr();
2018-09-05 02:34:52 +00:00
soft_switch(MIXCLR); // Full screen
2018-09-04 03:44:53 +00:00
label_180:
2018-09-05 02:34:52 +00:00
color_group=random()%2; // HGR color group (PG or BO)
x_velocity=(random()%3)+1; // x velocity = 1..3
y_velocity=-((random()%5)+3); // y velocity = -3..-7
max_steps=(random()%25)+40; // 40..64
2018-09-04 03:44:53 +00:00
2018-09-05 02:34:52 +00:00
xpos=(random()%(xsize-margin*2))+margin;
// margin .. xsize-margin
ypos=ysize; // start at ground
peak=ypos; // peak starts at ground?
2018-09-04 03:44:53 +00:00
/* Aim towards center of screen */
if (xpos>xsize/2) x_velocity=-x_velocity;
2018-09-05 02:34:52 +00:00
/* Draw rocket */
for(cs=1;cs<=max_steps;cs++) {
y_even_older=y_old;
y_old=ypos;
x_even_older=x_old;
x_old=xpos;
2018-09-04 03:44:53 +00:00
2018-09-05 02:34:52 +00:00
/* Move rocket */
2018-09-04 03:44:53 +00:00
xpos=xpos+x_velocity;
ypos=ypos+y_velocity;
2018-09-05 02:34:52 +00:00
/* adjust Y velocity, slow it down */
y_velocity=y_velocity+0.125;
/* if we went higher, adjust peak */
if (ypos<peak) peak=ypos;
2018-09-04 03:44:53 +00:00
/* check if out of bounds and stop moving */
2018-09-05 02:34:52 +00:00
if (xpos<=margin) {
cs=max_steps; // too far left
2018-09-04 03:44:53 +00:00
}
2018-09-05 02:34:52 +00:00
if (xpos>=(xsize-margin)) {
cs=max_steps; // too far right
2018-09-04 03:44:53 +00:00
}
2018-09-05 02:34:52 +00:00
if (ypos<=margin) {
cs=max_steps; // too far up
2018-09-04 03:44:53 +00:00
}
2018-09-05 02:34:52 +00:00
// if falling downward
2018-09-04 03:44:53 +00:00
if (y_velocity>0) {
2018-09-05 02:34:52 +00:00
// if too close to ground, explode
if (ypos>=ysize-margin) {
cs=max_steps;
}
// if fallen a bit past peak, explode
if (ypos>ysize-(ysize-peak)/2) {
cs=max_steps;
2018-09-04 03:44:53 +00:00
}
}
2018-09-05 02:34:52 +00:00
// if not done, draw rocket
if (cs<max_steps) {
hcolor_equals(color_group*4+3);
hplot(x_old,y_old);
2018-09-04 03:44:53 +00:00
hplot_to(xpos,ypos);
}
2018-09-05 02:34:52 +00:00
// erase with proper color black
hcolor_equals(color_group*4);
hplot(x_even_older,y_even_older);
hplot_to(x_old,y_old);
2018-09-04 03:44:53 +00:00
grsim_update();
ch=grsim_input();
if (ch=='q') exit(0);
usleep(100000);
}
label_290:
2018-09-05 02:34:52 +00:00
/* Draw explosion near x_old, y_old */
x_old=floor(x_old);
y_old=floor(y_old);
xpos=(random()%20)-10; // x +/- 10
ypos=(random()%20)-10; // y +/- 10
hcolor_equals(color_group*4+3); // draw white (with fringes)
hplot(xpos+x_old,ypos+y_old); // draw at center of explosion
/* Spread the explosion */
2018-09-04 03:44:53 +00:00
for(i=1;i<=9;i++) {
2018-09-05 02:34:52 +00:00
/* Draw spreading dots in white */
2018-09-04 03:44:53 +00:00
if (i<9) {
n=i;
2018-09-05 02:34:52 +00:00
hcolor_equals(color_group*4+3);
2018-09-04 03:44:53 +00:00
routine_370();
}
2018-09-05 02:34:52 +00:00
/* erase old */
2018-09-04 03:44:53 +00:00
n=i-1;
2018-09-05 02:34:52 +00:00
hcolor_equals(color_group*4);
2018-09-04 03:44:53 +00:00
routine_370();
grsim_update();
ch=grsim_input();
if (ch=='q') break;
usleep(100000);
}
2018-09-05 02:34:52 +00:00
/* randomly draw more explosions */
2018-09-04 03:44:53 +00:00
if (random()%2) goto label_290;
goto label_180;
return 0;
}