/* * CSE142, Spring 2000 * Homework 4 * Idyllic landscape animation STARTER PROGRAM * NOEL PAUL * SECTION AB with Gary Look */ #include "gp142.h" #include #include #include /* Logical constants */ #define TRUE 1 #define FALSE 0 #define FILL 0 /* For use with GP142 draw funcs */ /* Symbolic constants. Change as needed... Except when I did change them I got warning messages which I couldn't figure out how to fix... */ /* Menu buttons: */ #define MENU_TOP (GP142_YMAX - 5) /* Top-left pt. of button palette */ #define MENU_LEFT (-GP142_XMAX + 5) #define BUTTON_HEIGHT 25 /* Height/width of buttons in pixels */ #define BUTTON_WIDTH 100 #define NUM_BUTTONS 1 /* Number of buttons */ /* Possible locations of mouse clicks: */ /* Mouse click in some button */ #define BUTTON_CLICK 1 /* Mouse click elsewhere */ #define OTHER_CLICK 0 /* Coordinates of objects in the drawing */ /* y-coordinate of the top of the sea in the seascape */ #define HORIZON 100 /* Other constants pertaining to objects in the scene */ #define MOON_RADIUS 40 #define MOON_FULLNESS 2.5 #define NUM_OF_STARS 12 #define MAX_STAR_RADIUS 2 #define NUM_OF_FISH 12 #define MAX_FISH_SIZE 8 #define FISH_FLIP 1 #define SUB_FLIP 1 /***************************************************************************** /***************************************************************************** FUNCTION PROTOTYPES *****************************************************************************/ /* Initialize random number generator*/ void initialize_random_numbers(); /* Initializes the moon parameters */ void init_moon(int *moon_x, int *moon_y); /* Draw a single boat */ void draw_boat(int boat_x, int boat_y, int boat_len, int boat_mast, int boat_flip); /* Draw fish using parallel arrays */ void draw_fish(int *fish_x, int *fish_y, int *fish_size, int *fish_radius, int *fish_color, int *tail_color, int *fish_flip, int num_of_fish, int detonate); /* Draw fish skeletons in parallel arrays*/ void draw_skeleton(int *fish_x, int *fish_y, int *fish_flip, int num_of_fish); /* Draw fish-explosion graphis*/ void draw_explode(int *fish_x, int *fish_y, int num_of_fish); /* Change submarine colors */ void change_colors(int *sub_color, int *window_color); /* Draw the background, including the moon at coordinates moon_x and moon_y, the stars */ /* the sea, the submarine, the boat, and the fish */ void draw_landscape(int moon_x, int moon_y, int *star_x, int *star_y, int *star_radius, int *star_color, int sub_x, int sub_y, int sub_color, int sub_flip, int window_radius, int window_color, int boat_x, int boat_y, int boat_len, int boat_mast, int boat_flip, int *fish_x, int *fish_y, int *fish_size, int *fish_radius, int *fish_color, int *tail_color, int *fish_flip, int num_of_fish, int detonate); /* Make stars blink */ void blink_star(int *star_radius, int *star_color); /* Move fish */ void move_fish(int *fish_x, int *fish_y, int *fish_size, int *fish_radius, int *fish_color, int *tail_color, int *fish_flip, int num_of_fish, int detonate); /* Make fish skeletons sink */ void move_skel(int *fish_x, int *fish_y, int *fish_flip, int num_of_fish); /* Update moon coordinates moon_x, moon_y for one tick */ void move_moon(int *moon_x, int *moon_y); /* Make boat sail back and forth*/ void move_boat(int *boat_x, int *boat_y, int *boat_flip); /* Make submarine move*/ void move_sub(int *sub_x, int *sub_y, int sub_flip); /* Draw buttons */ void draw_buttons(void); /* Return kind of mouse click (in a button, etc.) */ int classify_mouse_click(int mouse_x, int mouse_y); /* Process button click at location x, y. React appropriately*/ void handle_button(int x, int y, int *sub_color, int *window_color); /* Display error message if internal error is detected. */ void something_is_wrong (void); /***************************************************************************** /***************************************************************************** /***************************************************************************** MAIN *****************************************************************************/ int main(void) { /* Event handling and buttons: */ int quit; /* = "user has selected quit" */ int event; /* the most recent GP142 event */ char key_pressed; /* last keyboard character from key event */ int mouse_x, mouse_y; /* x and y coordinates of latest mouse event*/ int click_kind; /* kind of mouse click (button, etc.) */ /* Information about the moon and stars in arrays */ int moon_x, moon_y; /* screen x and y coordinates */ int star_x[] = {100, 160, 190, 250, 87, -165, /* screen x and y coordinates */ 69, 12, -35, -80, -101, -250}; int star_y[] = {220, 240, 160, 170, 164, 213, 178, 201, 190, 180, 172, 230}; int star_radius[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; /* radius of a star*/ int star_color[] = {LT_GRAY, LT_GRAY, LT_GRAY, LT_GRAY, /* color of a star */ LT_GRAY, LT_GRAY, LT_GRAY, LT_GRAY, LT_GRAY, LT_GRAY, LT_GRAY, LT_GRAY}; /* Information about the fish, initializing arrays */ int num_of_fish = NUM_OF_FISH; int s = MAX_FISH_SIZE/2; int fish_size[] = {s, s, s, s, s, s, s, s, s, s, s, s}; int fish_radius[] = {6, 9, 8, 8, 4, 4, 7, 6, 5, 6, 7, 6}; int c = CYAN; int fish_color[] = {c, c, c, c, c, c, c, c, c, c, c, c}; int t = YELLOW; int tail_color[] = {t, t, t, t, t, t, t, t, t, t, t, t}; int flip_flag[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int f = FISH_FLIP; int fish_flip[] = {f, f, f, f, f, f, f, f, f, f, f, f}; int fish_x[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int fish_y[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int fishi = 0; /* index for fish arrays in main */ int fish_color_flag = 0; /* color flag */ int detonate = 0; /* fish detonator flag */ /* Information about the submarine */ int sub_x, sub_y; /* screen x and y coordinates */ int window_radius; /* window radii */ int sub_color, window_color; /* body & window colors */ int sub_flip_flag = 0; /* directional orientation of sub flag */ int sub_flip = SUB_FLIP; /* directional orientation of sub flag */ /* Information about the boat */ int boat_x, boat_y; /* screen x and y coordinates */ int boat_len, boat_mast; /* length of the boat and height of the mast */ int boat_flip; /* directional orientation of boat */ /* Initialize random number generator */ initialize_random_numbers(); /* Initialize graphics package, clear the window, */ /* turn logging off, and start animation. */ GP142_open(); GP142_logging(LOG_OFF); GP142_clear(); GP142_animate(ANI_RUN); /* set initial location of the moon */ init_moon(&moon_x, &moon_y); /* set initial location and color of the fish */ for(fishi=0; fishi=num_of_fish && i>=0; i--) { if(fish_flip[i] != 1) { GP142_lineXY(CHALK, fish_x[i], fish_y[i], fish_x[i] + 8, fish_y[i], 1); GP142_lineXY(CHALK, fish_x[i], fish_y[i] + 8, fish_x[i], fish_y[i] - 8, 1); GP142_lineXY(CHALK, fish_x[i], fish_y[i] + 8, fish_x[i] - 4, fish_y[i] + 7, 1); GP142_lineXY(CHALK, fish_x[i] - 4, fish_y[i] + 7, fish_x[i] - 7, fish_y[i] + 5, 1); GP142_lineXY(CHALK, fish_x[i] - 7, fish_y[i] + 5, fish_x[i] - 8, fish_y[i], 1); GP142_lineXY(CHALK, fish_x[i] - 8, fish_y[i], fish_x[i] - 7, fish_y[i] - 5, 1); GP142_lineXY(CHALK, fish_x[i] - 7, fish_y[i] - 5, fish_x[i] - 4, fish_y[i] - 7, 1); GP142_lineXY(CHALK, fish_x[i] - 4, fish_y[i] - 7, fish_x[i], fish_y[i] - 8, 1); GP142_lineXY(CHALK, fish_x[i] + 3, fish_y[i] + 6, fish_x[i] + 3, fish_y[i] - 6, 1); GP142_lineXY(CHALK, fish_x[i] + 6, fish_y[i] + 3, fish_x[i] + 6, fish_y[i] - 3, 1); GP142_lineXY(CHALK, fish_x[i] - 2, fish_y[i] + 2, fish_x[i] - 5, fish_y[i] + 5, 1); GP142_lineXY(CHALK, fish_x[i] - 2, fish_y[i] + 5, fish_x[i] - 5, fish_y[i] + 2, 1); GP142_triangleXY(CHALK, fish_x[i] + 8, fish_y[i], fish_x[i] + 14, fish_y[i] + 6, fish_x[i] + 14, fish_y[i] - 6, 1); }else{ GP142_lineXY(CHALK, fish_x[i], fish_y[i], fish_x[i] - 8, fish_y[i], 1); GP142_lineXY(CHALK, fish_x[i], fish_y[i] + 8, fish_x[i], fish_y[i] - 8, 1); GP142_lineXY(CHALK, fish_x[i], fish_y[i] + 8, fish_x[i] + 4, fish_y[i] + 7, 1); GP142_lineXY(CHALK, fish_x[i] + 4, fish_y[i] + 7, fish_x[i] + 7, fish_y[i] + 5, 1); GP142_lineXY(CHALK, fish_x[i] + 7, fish_y[i] + 5, fish_x[i] + 8, fish_y[i], 1); GP142_lineXY(CHALK, fish_x[i] + 8, fish_y[i], fish_x[i] + 7, fish_y[i] - 5, 1); GP142_lineXY(CHALK, fish_x[i] + 7, fish_y[i] - 5, fish_x[i] + 4, fish_y[i] - 7, 1); GP142_lineXY(CHALK, fish_x[i] + 4, fish_y[i] - 7, fish_x[i], fish_y[i] - 8, 1); GP142_lineXY(CHALK, fish_x[i] - 3, fish_y[i] + 6, fish_x[i] - 3, fish_y[i] - 6, 1); GP142_lineXY(CHALK, fish_x[i] - 6, fish_y[i] + 3, fish_x[i] - 6, fish_y[i] - 3, 1); GP142_lineXY(CHALK, fish_x[i] + 2, fish_y[i] + 2, fish_x[i] + 5, fish_y[i] + 5, 1); GP142_lineXY(CHALK, fish_x[i] + 2, fish_y[i] + 5, fish_x[i] + 5, fish_y[i] + 2, 1); GP142_triangleXY(CHALK, fish_x[i] - 8, fish_y[i], fish_x[i] - 14, fish_y[i] + 6, fish_x[i] - 14, fish_y[i] - 6, 1); } } } /* Draw Fish Explosion using arrays and loops*/ void draw_explode(int *fish_x, int *fish_y, int num_of_fish) { int i = num_of_fish; if(i>=0) { GP142_lineXY(RED, fish_x[i] + 18, fish_y[i], fish_x[i] + 26, fish_y[i], 2); GP142_lineXY(RED, fish_x[i] + 9, fish_y[i] - 8, fish_x[i] + 14, fish_y[i] - 14, 2); GP142_lineXY(RED, fish_x[i], fish_y[i] - 12, fish_x[i], fish_y[i] - 20, 2); GP142_lineXY(RED, fish_x[i] - 6, fish_y[i] - 7, fish_x[i] - 12, fish_y[i] - 13, 2); GP142_lineXY(RED, fish_x[i] - 12, fish_y[i], fish_x[i] - 20, fish_y[i], 2); GP142_lineXY(RED, fish_x[i] - 6, fish_y[i] + 7, fish_x[i] - 12, fish_y[i] + 13, 2); GP142_lineXY(RED, fish_x[i], fish_y[i] + 12, fish_x[i], fish_y[i] + 20, 2); GP142_lineXY(RED, fish_x[i] +9, fish_y[i] + 8, fish_x[i] + 14, fish_y[i] + 14, 2); } } /* Draw a boat at horizontal and vertical positions x and y and of the given length */ /* and mast height, oriented according to the boat_flip flag */ void draw_boat(int boat_x, int boat_y, int boat_len, int boat_mast, int boat_flip) { if(boat_flip == 1) { GP142_rectangleXY(LT_GRAY, boat_x - boat_len, boat_y, boat_x + boat_len, boat_y - 4, FILL); GP142_triangleXY(ICE_BLUE, boat_x, boat_y, boat_x, boat_y + boat_mast, boat_x - boat_mast, boat_y + (boat_mast / 9), FILL); GP142_triangleXY(ICE_BLUE, boat_x + 7, boat_y + 2, boat_x, boat_y + boat_mast, boat_x + boat_len, boat_y, FILL); }else{ GP142_rectangleXY(LT_GRAY, boat_x - boat_len, boat_y, boat_x + boat_len, boat_y - 4, FILL); GP142_triangleXY(ICE_BLUE, boat_x, boat_y, boat_x, boat_y + boat_mast, boat_x + boat_mast - 10, boat_y + (boat_mast / 6), FILL); GP142_triangleXY(ICE_BLUE, boat_x -10, boat_y + 6, boat_x, boat_y + boat_mast, boat_x - boat_len, boat_y, FILL); } } /* Draw the background, including the moon and stars */ void draw_landscape(int moon_x, int moon_y, int*star_x, int*star_y, int*star_radius, int*star_color, int sub_x, int sub_y, int sub_color, int sub_flip, int window_radius, int window_color, int boat_x, int boat_y, int boat_len, int boat_mast, int boat_flip, int*fish_x, int*fish_y, int*fish_size, int*fish_radius, int*fish_color, int*tail_color, int*fish_flip, int num_of_fish, int detonate) { int text_offset = -GP142_XMAX + 15; /* horizontal offset of text */ int text_size = 12; /* size of text font */ int star_index = 0; /* index for stars */ /* Draw the sky */ GP142_rectangleXY(BLACK, -GP142_XMAX, -GP142_YMAX, GP142_XMAX, GP142_YMAX, FILL); /* Draw Instructions */ GP142_printfXY(CYAN, -GP142_XMAX + 200, GP142_YMAX - 15, 12, "Press Any Key To Kill Fish."); /* Draw stars */ for(star_index=0; star_index MENU_LEFT) && (mouse_x < MENU_LEFT + BUTTON_WIDTH) && (mouse_y < MENU_TOP) && (mouse_y > (MENU_TOP - NUM_BUTTONS * BUTTON_HEIGHT))) return BUTTON_CLICK; else return OTHER_CLICK; } /* Process button click at location x, y. React appropriately: */ /* i.e. change colors, tree tops, etc. if requested. */ void handle_button(int x, int y, int *petal_color, int *flower_color) { int buttonNum; /* number of selected button */ /* Complain if x coordinate couldn't be inside a button */ if (x < MENU_LEFT || x > MENU_LEFT + BUTTON_WIDTH) { something_is_wrong(); return; } /* Convert y coordinate to button number from top. */ buttonNum = (MENU_TOP - y)/BUTTON_HEIGHT; switch(buttonNum) { case 0: /* change flower field colors */ change_colors(petal_color, flower_color); break; case 1: break; case 2: break; default: /*Bad news if this is ever reached! */ GP142_animate(ANI_HALT); something_is_wrong(); break; } } /***************************************************************************** /***************************************************************************** /***************************************************************************** ANIMATED OBJECTS *****************************************************************************/ /* Make the stars blink */ void blink_star(int *star_radius, int *star_color) { int color = 0; /* Local color variable */ int i = 0; /* index for star arrays */ for(i=0; i= GP142_XMAX - 30) (*boat_flip = - *boat_flip); }else{ *boat_x = *boat_x -3; *boat_y = *boat_y; if(*boat_x <= -GP142_XMAX + 30) (*boat_flip = - *boat_flip); } } /* Animate submarine so it climbs, dives, turns, and wraps */ void move_sub(int *sub_x, int *sub_y, int sub_flip) { int a = 0; a = rand() % 100; if(sub_flip == 1){ if(a<50) { *sub_x = *sub_x + 1; *sub_y = *sub_y + 1; if(*sub_x > GP142_XMAX + 120) *sub_x = - *sub_x; if(*sub_y < -GP142_YMAX - 50) { *sub_y = - *sub_y; (*sub_x) --; } }else{ *sub_x = *sub_x + 1; *sub_y = *sub_y - 1; if(*sub_x > GP142_XMAX + 120) *sub_x = - *sub_x; if(*sub_y < -GP142_YMAX - 50) { *sub_y = - *sub_y; (*sub_x) --; } } }else{ if(a<50) { *sub_x = *sub_x - 1; *sub_y = *sub_y + 1; if(*sub_x < - GP142_XMAX + 120) *sub_x = - *sub_x; if(*sub_y < - GP142_YMAX - 50) { *sub_y = - *sub_y; (*sub_x) --; } }else{ *sub_x = *sub_x - 1; *sub_y = *sub_y - 1; if(*sub_x < - GP142_XMAX + 120) *sub_x = - *sub_x; if(*sub_y < - GP142_YMAX - 50) { *sub_y = - *sub_y; (*sub_x) --; } } } } /* Make Skeletons Sink */ void move_skel(int *fish_x, int *fish_y, int *fish_flip, int num_of_fish) { int i = 0; for(i=NUM_OF_FISH - 1; i>=num_of_fish && i>=0; i--) { if(fish_y[i] >= -235) { fish_y[i] = fish_y[i] - 3; draw_skeleton(fish_x, fish_y, fish_flip, num_of_fish); } } } /* Make the fish move directionally according to their orientation but randomly as well. Also make their tails flip */ void move_fish(int *fish_x, int *fish_y, int *fish_size, int *fish_radius, int *fish_color, int *tail_color, int *fish_flip, int num_of_fish, int detonate) { int a = 0; /* climb vs. dive flag */ int i = 0; /* index for fish arrays */ for(i=0; i GP142_XMAX + 30) { fish_x[i] = - fish_x[i]; (fish_y[i]) --; } if(fish_y[i] < -GP142_YMAX - 30) { fish_y[i] = - fish_y[i]; (fish_x[i]) ++ ; } }else{ if(a>90) { fish_x[i] = fish_x[i] + rand() % 3 - 1; fish_y[i] = fish_y[i] + rand() % 3 - 1; fish_size[i] = MAX_FISH_SIZE/2 + rand() % MAX_FISH_SIZE/2; draw_fish(fish_x, fish_y, fish_size, fish_radius, fish_color, tail_color, fish_flip, num_of_fish, detonate); if(fish_x[i] > GP142_XMAX + 30) { fish_x[i] = - fish_x[i]; (fish_y[i]) --; } if(fish_y[i] < -GP142_YMAX - 30) { fish_y[i] = - fish_y[i]; (fish_x[i]) ++ ; } }else{ fish_x[i] = fish_x[i] + 3; fish_y[i] = fish_y[i] - 3; fish_size[i] = MAX_FISH_SIZE/2 + rand() % MAX_FISH_SIZE/2; draw_fish(fish_x, fish_y, fish_size, fish_radius, fish_color, tail_color, fish_flip, num_of_fish, detonate); if(fish_x[i] > GP142_XMAX + 30) { fish_x[i] = - fish_x[i]; (fish_y[i]) --; } if(fish_y[i] < -GP142_YMAX - 30) { fish_y[i] = - fish_y[i]; (fish_x[i]) ++ ; } } } }else{ if(a<40) { fish_x[i] = fish_x[i] - 2; fish_y[i] = fish_y[i] + 2; fish_size[i] = MAX_FISH_SIZE/2 + rand() % MAX_FISH_SIZE/2; draw_fish(fish_x, fish_y, fish_size, fish_radius, fish_color, tail_color, fish_flip, num_of_fish, detonate); if(fish_x[i] < -GP142_XMAX -30) { fish_x[i] = - fish_x[i]; (fish_y[i]) --; } if(fish_y[i] < -GP142_YMAX - 30) { fish_y[i] = - fish_y[i]; (fish_x[i]) -- ; } }else{ if(a>90) { fish_x[i] = fish_x[i] + rand() % 3 - 1; fish_y[i] = fish_y[i] + rand() % 3 - 1; fish_size[i] = MAX_FISH_SIZE/2 + rand() % MAX_FISH_SIZE/2; draw_fish(fish_x, fish_y, fish_size, fish_radius, fish_color, tail_color, fish_flip, num_of_fish, detonate); if(fish_x[i] < -GP142_XMAX - 30) { fish_x[i] = - fish_x[i]; (fish_y[i]) --; } if(fish_y[i] < -GP142_YMAX - 30) { fish_y[i] = - fish_y[i]; (fish_x[i]) -- ; } }else{ fish_x[i] = fish_x[i] - 1; fish_y[i] = fish_y[i] - 1; fish_size[i] = MAX_FISH_SIZE/2 + rand() % MAX_FISH_SIZE/2; draw_fish(fish_x, fish_y, fish_size, fish_radius, fish_color, tail_color, fish_flip, num_of_fish, detonate); if(fish_x[i] < -GP142_XMAX - 30) { fish_x[i] = - fish_x[i]; (fish_y[i]) --; } if(fish_y[i] < -GP142_YMAX - 30) { fish_y[i] = - fish_y[i]; (fish_x[i]) -- ; } } } } } } /***************************************************************************** /***************************************************************************** /***************************************************************************** OTHER *****************************************************************************/ /* Initialize random number generator */ /* [Ignore or delete if your program */ /* doesn't user random numbers.] */ void initialize_random_numbers() { time_t t; /* current time */ srand (time(&t)); } /* Display error message if internal error is detected. */ void something_is_wrong (void) { GP142_printfXY (RED, 0, 0, 16, "Program error!"); }