/* * CSE142, Spring 2000 * Homework 4 * Idyllic landscape animation * */ #include "gp142.h" #include #include #include /* * is included so the current time can be accessed * to initialize the random number generator. If you don't * use random numbers (and you don't have to) you can either * ignore this #include or delete it. */ /* Logical constants */ #define TRUE 1 #define FALSE 0 /* * For many of the GP142 functions, a line width of * zero means to fill the shape being drawn. The * constant FILL is used in function calls when a solid * shape is desired. */ #define FILL 0 /* Menu buttons: */ /* Top-left coordinates of button palette */ #define MENU_TOP (GP142_YMAX - 5) #define MENU_LEFT (-GP142_XMAX + 5) /* Height and width of buttons in pixels */ #define BUTTON_HEIGHT 25 #define BUTTON_WIDTH 100 /* Number of buttons */ #define NUM_BUTTONS 2 /* Possible locations of mouse clicks: */ /* Mouse click in some button */ #define BUTTON_CLICK 1 /* Mouse click elsewhere */ #define OTHER_CLICK 0 /*number of lines of text in intro*/ #define MAX_LINES 20 /*number of colors for monkey explosion*/ #define COLORS 6 /* Coordinates of objects in the drawing */ /* y-coordinate of the top of the ground in the landscape */ #define HORIZON (- 2 * (GP142_YMAX / 10)) /* Other constants pertaining to objects in the scene */ #define MOON_RADIUS 20 #define MOON_FULLNESS 2.5 #define MAX_STAR_RADIUS 4 #define MAX_BUG_SIZE 10 /*Monkey size jazz*/ #define MONKEY_WIDTH 10 #define MONKEY_HEIGHT 12 #define MONKEY_HEAD_RADIUS 10 #define MONKEY_MOUTH_RADIUS 3 #define MONKEY_EAR_RADIUS 3 #define MONKEY_LEG_HEIGHT 20 #define MONKEY_LEG_WIDTH 5 #define MONKEY_EYE_RADIUS 1 #define MAX_MONKEY 15 /*how many monkeys drop*/ /***************************************************************************** FUNCTION PROTOTYPES *****************************************************************************/ /* Initialize random number generator. [Ignore or */ /* remove if your program doesn't use random numbers.] */ void initialize_random_numbers(); /* print the text */ void the_text(int intro_text_x[], int intro_text_y[], int intro_text_size[]); /*cool scrolling action on the text */ void scroll_intro_text (int intro_text_x[], int intro_text_y[], int intro_text_size[], int moon_x, int moon_y, int star_x, int star_y, int star_radius, int star_color, int star2_x, int star2_y, int star2_radius, int star2_color, int flower_x, int flower_y, int flower_radius, int flower_color, int petal_radius, int petal_color, int boat_x, int boat_y, int boat_len, int boat_mast, int bug_x, int bug_y, int bug_size, int liberty_x, int liberty_y, int multi_monkey_x[], int multi_monkey_y[], int multi_monkey_arm[]); /*Initialize the text*/ void init_text(int intro_text_x[], int intro_text_y[], int intro_text_size[]); /* Initializes the moon parameters */ void init_moon(int *moon_x, int *moon_y); /* Initializes the monkey parameters */ void init_monkey(int multi_monkey_x[], int multi_monkey_y[], int multi_monkey_ground[], int multi_monkey_arm[],int periodic_counter[]); /* Draw a single boat */ void draw_boat(int boat_x, int boat_y, int boat_len, int boat_mast); /* Draw a single bug (buttefly) */ void draw_bug(int bug_x, int bug_y, int bug_size); /* Draw a single monkey at given point*/ void draw_monkey(int monkey_x, int monkey_y, int monkey_arm); /* Change flower field colors */ void change_colors(int *petal_color, int *flower_color); /* Draw the background, including the moon at coordinates moon_x and moon_y, the stars */ /* the tree, the flower(s), the boat, and the bug(s) */ void draw_landscape(int moon_x, int moon_y, int star_x, int star_y, int star_radius, int star_color, int star2_x, int star2_y, int star2_radius, int star2_color, int flower_x, int flower_y, int flower_radius, int flower_color, int petal_radius, int petal_color, int boat_x, int boat_y, int boat_len, int boat_mast, int bug_x, int bug_y, int bug_size, int liberty_x, int liberty_y, int multi_monkey_x[], int multi_monkey_y[], int multi_monkey_arm[]); /* Draw Statue of Liberty*/ void draw_liberty(int liberty_x, int liberty_y); /*draw the splash when the monkey splashes down*/ void draw_splash(int lower_x, int lower_y, int right_x, int right_y, int left_x, int left_y); /* Make stars blink */ void blink_star(int *star_radius, int *star_color); /* Move bug(s) */ void move_bug(int *bug_x, int *bug_y, int *bug_size); /* Update moon coordinates moon_x, moon_y for one tick */ void move_moon(int *moon_x, int *moon_y); /* Update monkey coordinates fast (splashing into lake) */ int move_monkey_fast( int monkey_y); /*move monkey arms*/ int move_monkey_arm(int monkey_arm); /* Update monkey coordinates */ int move_monkey( int monkey_y); /*draw explosion on monkey*/ void draw_explode( int monkey_x, int monkey_y, int explode_colors[]); /* 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, depending on the requested event */ void handle_button(int x, int y, int *petal_color, int *flower_color, int multi_monkey_x[], int multi_monkey_y[], int multi_monkey_ground[], int multi_monkey_arm[], int periodic_counter[]); /* Display error message if internal error is detected. */ void something_is_wrong (void); /***************************************************************************** MAIN *****************************************************************************/ /* * Main program contents: * - Major program variables that persist throughout execution * - Event loop that processes GP142 events until the user quits, * and updates variables and redraws the window as needed. */ 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 scrolling text at the beggining */ int intro_text_x[MAX_LINES]; int intro_text_y[MAX_LINES]; int intro_text_size[MAX_LINES]; /* Information about the moon and stars: */ int moon_x, moon_y; /* screen x and y coordinates */ int star_x, star_y; /* screen x and y coordinates */ int star_radius, star_color; /* radius and color of a star */ int star2_x, star2_y; /* screen x and y coordinates */ int star2_radius, star2_color; /* radius and color of another star */ /* Information about the flowers */ int flower_x, flower_y; /* screen x and y coordinates */ int flower_radius, petal_radius; /* flower and petal radii */ int flower_color, petal_color; /* flower and petal colors */ /* Information about the tree */ /* Fill in here! */ /* 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 */ /* Information about the bug(s) */ int bug_x, bug_y, bug_size; /* screen x and y coordinates and size of the bug */ /* Information about the Statue of Liberty */ int liberty_x, liberty_y; /* screen x and y coordinates of statue*/ /* Information about the monkey(s) */ int monkey_arm; /* screen x and y coordinates of monkey*/ int multi_monkey_x[MAX_MONKEY]; /* for multiple monkeys */ int multi_monkey_y[MAX_MONKEY]; int multi_monkey_arm[MAX_MONKEY]; /* keeps track of the arm movement factor*/ int multi_monkey_ground[MAX_MONKEY];/* ground point for monkey */ int splash_monkey; /* index for splash function */ int index; /* counter for monkey for loops*/ int periodic_counter_1; /* counts periodic events, monkey splashdown 1*/ int periodic_counter[MAX_MONKEY]; /*counter for explosion draw*/ /*information for the explosion of the monkeys*/ int explode_color[COLORS]; int explosion_colors[COLORS] = {RED, YELLOW, ORANGE, PINK, MAGENTA, PURPLE}; /* Initialize random number generator */ initialize_random_numbers(); init_text(intro_text_x, intro_text_y, intro_text_size); /* 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 of the stars */ star_x = GP142_XMAX - 200; star_y = GP142_YMAX - 90; star_radius = 3; star_color = WHITE; star2_x = -GP142_XMAX + 150; star2_y = GP142_YMAX - 80; star2_radius = 2; star2_color = YELLOW; /* set initial flower parameters */ flower_x = GP142_XMAX / 3; flower_y = -GP142_YMAX + 40; flower_radius = 2; flower_color = YELLOW; petal_radius = 8; petal_color = RED; /* set initial conditions for the boat */ boat_x = GP142_XMAX / 3 - 20; boat_y = HORIZON - 25; boat_len = 25; boat_mast = 40; /* set initial conditions for the buttefly */ bug_x = flower_x + 20; bug_y = flower_y + 30; bug_size = MAX_BUG_SIZE/2; /* set initial conditions for the statue */ liberty_x = -20; liberty_y = -75; /* set initial conditions for the monkey */ init_monkey(multi_monkey_x, multi_monkey_y, multi_monkey_ground, multi_monkey_arm, periodic_counter); monkey_arm = 4; /* set periodic counters to zero */ periodic_counter_1 = 0; /* Draw initial landscape, buttons, and traffic */ draw_landscape(moon_x,moon_y, star_x, star_y, star_radius, star_color, star2_x, star2_y, star2_radius, star2_color, flower_x, flower_y, flower_radius, flower_color, petal_radius, petal_color, boat_x, boat_y, boat_len, boat_mast, bug_x, bug_y, bug_size, liberty_x, liberty_y, multi_monkey_x, multi_monkey_y, multi_monkey_arm); draw_buttons(); scroll_intro_text (intro_text_x, intro_text_y, intro_text_size, moon_x, moon_y, star_x, star_y, star_radius, star_color, star2_x, star2_y, star2_radius, star2_color, flower_x, flower_y, flower_radius, flower_color, petal_radius, petal_color, boat_x, boat_y, boat_len, boat_mast, bug_x, bug_y, bug_size, liberty_x, liberty_y, multi_monkey_x, multi_monkey_y, multi_monkey_arm); /* * Main event loop: * ---- ----- ----- * Wait for the next user action, decode it, and call an * appropriate function to handle it. Repeat until the * user selects quit. */ quit = FALSE; while (!quit) { /* get next event */ event = GP142_await_event(&mouse_x, &mouse_y, &key_pressed); /* perform appropriate action depending on kind of event */ switch (event) { case GP142_QUIT: /* Quit selected */ quit = TRUE; break; case GP142_KBD: /* Key pressed. Not used now. If the program should */ /* react to keyboard events, insert something appropriate */ /* here. Note that scanf CANNOT be used with GP142. */ break; case GP142_MOUSE: /* Mouse click. Determine if in a menu button */ /* or elsewhere and process accordingly. */ click_kind = classify_mouse_click(mouse_x, mouse_y); if (click_kind == BUTTON_CLICK) { handle_button(mouse_x, mouse_y, &petal_color, &flower_color, multi_monkey_x, multi_monkey_y, multi_monkey_ground, multi_monkey_arm, periodic_counter); } else { for(index = 0; index < MAX_MONKEY; index++) { if(mouse_x >= (multi_monkey_x[index] - MONKEY_WIDTH) && mouse_x <= (multi_monkey_x[index] + MONKEY_WIDTH) && mouse_y >= (multi_monkey_y[index] - MONKEY_HEIGHT) && mouse_y <= (multi_monkey_y[index] + MONKEY_HEIGHT)) { periodic_counter[index] = -100; } } } break; case GP142_PERIODIC: /* Timer interval event. Update everything that needs the change */ /* as time advances. Animation should go here! */ /*count periodic events*/ periodic_counter_1 ++; /*movement and timed changes*/ move_moon(&moon_x, &moon_y); blink_star(&star_radius, &star_color); blink_star(&star2_radius, &star2_color); move_bug(&bug_x, &bug_y, &bug_size); for(index = 0; index < MAX_MONKEY; index++) { periodic_counter[index]++; } for(index=0; index multi_monkey_ground[index]) { multi_monkey_y[index] = move_monkey( multi_monkey_y[index]); multi_monkey_arm[index] = move_monkey_arm(multi_monkey_arm[index]); } if(multi_monkey_y[index] <= (multi_monkey_ground[index] +1) && multi_monkey_y[index] >= (multi_monkey_ground[index] -1) && multi_monkey_ground[index] <= HORIZON && multi_monkey_ground[index] >= HORIZON - 10 && multi_monkey_x[index] <= 100 && multi_monkey_x[index] > -GP142_XMAX) { multi_monkey_y[index] = move_monkey_fast( multi_monkey_y[index]); periodic_counter_1 = -10; splash_monkey = index; } if(periodic_counter[index] > -90 && periodic_counter[index] < 0) { multi_monkey_y[index] = move_monkey_fast(multi_monkey_y[index]); } } for(index=0; index= -10 && periodic_counter_1 < 0) { draw_splash(multi_monkey_x[splash_monkey], -100, multi_monkey_x[splash_monkey] - 75, 0, multi_monkey_x[splash_monkey] + 75, 0); } for(index = 0; index < MAX_MONKEY; index++) { if(periodic_counter[index] >= -100 && periodic_counter[index] <= -90) { draw_explode( multi_monkey_x[index], multi_monkey_y[index], explode_color); } } } /* end event loop */ /* Termination: shut down graphics window and exit */ GP142_close(); return 0; } /* end main */ /***************************************************************************** INITIALIZATION PROCEDURES *****************************************************************************/ /*Initialize the text*/ void init_text(int intro_text_x[], int intro_text_y[], int intro_text_size[]){ int index; intro_text_y[0] = -GP142_YMAX; intro_text_x[0] = -GP142_XMAX; intro_text_size[0] = 75; for(index=1; index HORIZON) { multi_monkey_ground[index] = HORIZON; } multi_monkey_arm[index] = 0; } } /***************************************************************************** BACKGROUND LANDSCAPE *****************************************************************************/ /* Change flower field colors */ void change_colors(int *petal_color, int *flower_color) { *petal_color = (*petal_color + 1) % MAX_COLORS; *flower_color = (*flower_color + 1) % MAX_COLORS; } /* Draw a single bug (butterfly) of a given size at given screen x and y coordinates */ void draw_bug(int bug_x, int bug_y, int bug_size) { GP142_triangleXY(MAGENTA, bug_x, bug_y, bug_x - bug_size, bug_y + bug_size, bug_x - bug_size, bug_y - bug_size, FILL); GP142_triangleXY(MAGENTA, bug_x, bug_y, bug_x + bug_size, bug_y + bug_size, bug_x + bug_size, bug_y - bug_size, FILL); GP142_lineXY(BLACK, bug_x, bug_y, bug_x - 3, bug_y + 7, 2); GP142_lineXY(BLACK, bug_x, bug_y, bug_x + 3, bug_y + 7, 2); } /* Draw the monkey and parachute at given x and y screen coords */ void draw_monkey(int monkey_x, int monkey_y, int monkey_arm){ GP142_rectangleXY(BROWN, monkey_x-MONKEY_WIDTH, monkey_y+MONKEY_HEIGHT, monkey_x+MONKEY_WIDTH, monkey_y-MONKEY_HEIGHT, FILL); GP142_circleXY(BROWN, monkey_x, monkey_y+MONKEY_HEIGHT+MONKEY_HEAD_RADIUS, MONKEY_HEAD_RADIUS); GP142_circleXY(OLIVE, monkey_x, monkey_y+MONKEY_HEIGHT+(MONKEY_HEAD_RADIUS/2), MONKEY_MOUTH_RADIUS); GP142_circleXY(BROWN, monkey_x+(MONKEY_HEAD_RADIUS), monkey_y+MONKEY_HEIGHT+ (int)(1.2*MONKEY_HEAD_RADIUS), MONKEY_EAR_RADIUS); GP142_circleXY(BROWN, monkey_x-(MONKEY_HEAD_RADIUS), monkey_y+MONKEY_HEIGHT+ (int)(1.2*MONKEY_HEAD_RADIUS), MONKEY_EAR_RADIUS); GP142_circleXY(BLACK, monkey_x+(int)(.5*MONKEY_WIDTH), monkey_y+MONKEY_HEIGHT+ (int)(1.2*MONKEY_HEAD_RADIUS), MONKEY_EYE_RADIUS); GP142_circleXY(BLACK, monkey_x-(int)(.5*MONKEY_WIDTH), monkey_y+MONKEY_HEIGHT+ (int)(1.2*MONKEY_HEAD_RADIUS), MONKEY_EYE_RADIUS); GP142_rectangleXY(BROWN, monkey_x+MONKEY_WIDTH, monkey_y-(int)(0.8*MONKEY_HEIGHT), monkey_x+MONKEY_WIDTH+MONKEY_LEG_WIDTH, monkey_y- (int)(0.8*MONKEY_HEIGHT)-MONKEY_LEG_HEIGHT, FILL); GP142_rectangleXY(BROWN, monkey_x-MONKEY_WIDTH, monkey_y-(int)(0.8*MONKEY_HEIGHT), monkey_x-MONKEY_WIDTH-MONKEY_LEG_WIDTH, monkey_y- (int)(0.6*MONKEY_HEIGHT)-MONKEY_LEG_HEIGHT, FILL); GP142_triangleXY(BROWN, monkey_x-MONKEY_WIDTH, monkey_y+(int)(.8*MONKEY_HEIGHT), monkey_x-MONKEY_WIDTH, monkey_y+(int)(.4*MONKEY_HEIGHT), monkey_x-MONKEY_WIDTH-MONKEY_LEG_HEIGHT, monkey_y+monkey_arm, FILL); GP142_triangleXY(BROWN, monkey_x+MONKEY_WIDTH, monkey_y+(int)(.8*MONKEY_HEIGHT), monkey_x+MONKEY_WIDTH, monkey_y+(int)(.4*MONKEY_HEIGHT), monkey_x+MONKEY_WIDTH+MONKEY_LEG_HEIGHT, monkey_y+monkey_arm, FILL); } /* Draw the Statue of Liberty in the drink */ void draw_liberty(int liberty_x, int liberty_y){ GP142_triangleXY(MED_GRAY, liberty_x-12, liberty_y, liberty_x-22, liberty_y, liberty_x-26, liberty_y+25, FILL); GP142_triangleXY(BLACK, liberty_x-25, liberty_y+20, liberty_x-28, liberty_y+33, liberty_x-22, liberty_y+33, FILL); GP142_rectangleXY(MED_GRAY, liberty_x-28, liberty_y+23, liberty_x-22, liberty_y+28, FILL); GP142_triangleXY(ORANGE, liberty_x-26, liberty_y+33, liberty_x-24, liberty_y+33, liberty_x-25, liberty_y+42, FILL); GP142_triangleXY(RED, liberty_x-28, liberty_y+33, liberty_x-26, liberty_y+38, liberty_x-25, liberty_y+33, FILL); GP142_triangleXY(RED, liberty_x-22, liberty_y+33, liberty_x-23, liberty_y+38, liberty_x-24, liberty_y+33, FILL); GP142_circleXY(LT_GRAY, liberty_x+5, liberty_y, 8); GP142_rectangleXY(BLUE, liberty_x-5, liberty_y, liberty_x+15, liberty_y-8, FILL); GP142_triangleXY(MED_GRAY, liberty_x, liberty_y, liberty_x-5, liberty_y, liberty_x-3, liberty_y+7, FILL); GP142_triangleXY(MED_GRAY, liberty_x, liberty_y, liberty_x+5, liberty_y, liberty_x+3, liberty_y+7, FILL); GP142_triangleXY(MED_GRAY, liberty_x+5, liberty_y, liberty_x+10, liberty_y, liberty_x+8, liberty_y+7, FILL); GP142_triangleXY(MED_GRAY, liberty_x+10, liberty_y, liberty_x+15, liberty_y, liberty_x+13, liberty_y+7, FILL); } void draw_splash(int lower_x, int lower_y, int right_x, int right_y, int left_x, int left_y){ GP142_triangleXY(BLUE, lower_x, lower_y, right_x, right_y, left_x, left_y, FILL); } /* Draw a boat at horizontal and vertical positions x and y and of the given length */ /* and mast height */ void draw_boat(int boat_x, int boat_y, int boat_len, int boat_mast) { GP142_rectangleXY(BLACK, boat_x - boat_len, boat_y, boat_x + boat_len, boat_y - 4, FILL); GP142_triangleXY(CYAN, boat_x, boat_y, boat_x, boat_y + boat_mast, boat_x + boat_mast, boat_y + 2 * boat_mast / 3, FILL); } /* Draw the background, including the moon at coordinates moon_x and moon_y and stars */ void draw_landscape(int moon_x, int moon_y, int star_x, int star_y, int star_radius, int star_color, int star2_x, int star2_y, int star2_radius, int star2_color, int flower_x, int flower_y, int flower_radius, int flower_color, int petal_radius, int petal_color, int boat_x, int boat_y, int boat_len, int boat_mast, int bug_x, int bug_y, int bug_size, int liberty_x, int liberty_y, int multi_monkey_x[], int multi_monkey_y[], int multi_monkey_arm[]) { int text_offset = -GP142_XMAX + 15; /* horizontal offset of text */ int text_size = 12; /* size of text font */ int index; /*counting index*/ /* Draw the sky */ GP142_rectangleXY(NAVY_BLUE, -GP142_XMAX, -GP142_YMAX, GP142_XMAX, GP142_YMAX, FILL); /* Draw the moon */ GP142_circleXY(WHITE, moon_x, moon_y, MOON_RADIUS); GP142_ovalXY(NAVY_BLUE, moon_x, moon_y - MOON_RADIUS, moon_x + (int)(MOON_FULLNESS * MOON_RADIUS), moon_y + MOON_RADIUS, FILL); /* Draw stars */ GP142_triangleXY(star_color, star_x, star_y + star_radius, star_x + (int)(star_radius * 0.87), star_y - (int) (star_radius / 2.0), star_x - (int)(star_radius * 0.87), star_y - (int) (star_radius / 2.0), FILL); GP142_triangleXY(star_color, star_x, star_y - star_radius, star_x - (int)(star_radius * 0.87), star_y + (int) (star_radius / 2.0), star_x + (int)(star_radius * 0.87), star_y + (int) (star_radius / 2.0), FILL); GP142_triangleXY(star2_color, star2_x, star2_y + star2_radius, star2_x + (int)(star2_radius * 0.87), star2_y - (int) (star2_radius / 2.0), star2_x - (int)(star2_radius * 0.87), star2_y - (int) (star2_radius / 2.0), FILL); GP142_triangleXY(star2_color, star2_x, star2_y - star2_radius, star2_x - (int)(star2_radius * 0.87), star2_y + (int) (star2_radius / 2.0), star2_x + (int)(star2_radius * 0.87), star2_y + (int) (star2_radius / 2.0), FILL); /* Draw mountains */ GP142_triangleXY(LT_GRAY, -GP142_XMAX, HORIZON, GP142_XMAX, HORIZON, -GP142_XMAX / 8, (5 * GP142_YMAX)/6, FILL); GP142_triangleXY(MED_GRAY, -GP142_XMAX, HORIZON, GP142_XMAX, HORIZON, -GP142_XMAX / 3, (3 * GP142_YMAX)/4, FILL); GP142_triangleXY(CHALK, -GP142_XMAX, HORIZON, GP142_XMAX, HORIZON, GP142_XMAX / 4, (2 * GP142_YMAX)/3, FILL); GP142_triangleXY(OLIVE, -( 5 * GP142_XMAX/4), HORIZON, (GP142_XMAX)/3 , HORIZON, -(3 * GP142_XMAX)/4, (2 * GP142_YMAX)/3 , FILL); GP142_triangleXY(FOREST_GREEN, -GP142_XMAX / 3 , HORIZON, GP142_XMAX, HORIZON, GP142_XMAX, (2 * GP142_YMAX)/3 , FILL); /* Draw meadow */ GP142_rectangleXY(SEA_GREEN, -GP142_XMAX, -GP142_YMAX, GP142_XMAX, HORIZON, FILL); /* Draw lake */ GP142_ovalXY(BLUE, -GP142_XMAX - 100, HORIZON, GP142_XMAX / 3, HORIZON - 50, FILL); /* Draw flower */ GP142_circleXY(petal_color, flower_x, flower_y, petal_radius); GP142_circleXY(flower_color, flower_x, flower_y, flower_radius); GP142_lineXY(FOREST_GREEN, flower_x, flower_y - petal_radius, flower_x, flower_y - petal_radius - 15, 2); GP142_lineXY(FOREST_GREEN, flower_x, flower_y - petal_radius - 10, flower_x + 5, flower_y - petal_radius - 3, 2); /* Draw tree */ GP142_rectangleXY(BROWN, -GP142_XMAX + 50, -GP142_YMAX + 50, -GP142_XMAX + 60, -GP142_YMAX + 100, FILL); GP142_circleXY(FOREST_GREEN, -GP142_XMAX + 55, -GP142_YMAX + 110, 30); /* Draw boat */ draw_boat(boat_x, boat_y, boat_len, boat_mast); /* Draw statue */ draw_liberty(liberty_x, liberty_y); /* Draw buttefly */ draw_bug(bug_x, bug_y, bug_size); /* Draw Monkeys */ for(index=0; 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 multi_monkey_x[], int multi_monkey_y[], int multi_monkey_ground[], int multi_monkey_arm[], int periodic_counter[]) { 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: /* initialize monkeys*/ init_monkey( multi_monkey_x, multi_monkey_y, multi_monkey_ground, multi_monkey_arm, periodic_counter); 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) { *star_radius = rand() % MAX_STAR_RADIUS + 1; *star_color = rand() % MAX_COLORS; } /* Update moon coordinates moon_x, moon_y by one tick */ void move_moon(int *moon_x, int *moon_y) { /* Move the moon to the left. If it reaches the edge, move it */ /* to the other side and down a bit. */ (*moon_x) --; if (*moon_x < -GP142_XMAX -30) { *moon_x = - *moon_x; (*moon_y) --; } } /* Make the bug(s) move around and flutter */ void move_bug(int *bug_x, int *bug_y, int *bug_size) { *bug_x = *bug_x + rand() % 3 - 1; *bug_y = *bug_y + rand() % 3 - 1; *bug_size = MAX_BUG_SIZE/2 + rand() % MAX_BUG_SIZE/2; draw_bug(*bug_x, *bug_y, *bug_size); } /* Make the monkey's arms move around and flutter */ int move_monkey_arm(int monkey_arm) { monkey_arm = monkey_arm + rand() % 3 - 1 ; if(monkey_arm > 4 ) { monkey_arm = 3; } if(monkey_arm < -4) { monkey_arm = -3; } return monkey_arm; } /* Update monkey coordinates */ int move_monkey( int monkey_y) { (monkey_y) = monkey_y - 2; if (monkey_y< -GP142_YMAX -30) { monkey_y = - monkey_y; } return monkey_y; } /* Update monkey coordinates */ int move_monkey_fast( int monkey_y) { (monkey_y) = monkey_y - 3000; if (monkey_y< -GP142_YMAX - 100) { monkey_y = -GP142_YMAX - 100; } return monkey_y; } /*explode the monkey*/ void draw_explode( int monkey_x, int monkey_y, int explode_color[]) { GP142_circleXY(explode_color[0], monkey_x, monkey_y, 8 + rand()%10); GP142_circleXY(explode_color[1], monkey_x, monkey_y, rand()%8); GP142_triangleXY(explode_color[2], monkey_x, monkey_y, monkey_x + rand()%6, monkey_y + rand()%6, monkey_x + 4 + rand()%10, monkey_y + 4 + rand()%10, FILL); GP142_triangleXY(explode_color[3], monkey_x, monkey_y, monkey_x - rand()%10, monkey_y - rand()%15, monkey_x - rand()% 20, monkey_y - rand()%18, FILL); GP142_lineXY(explode_color[4], monkey_x, monkey_y, monkey_x + rand()%18, monkey_y - rand()%18, 3); GP142_lineXY(explode_color[5], monkey_x, monkey_y, monkey_x - rand()%18, monkey_y + rand()%18, 3); } /***************************************************************************** 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!"); } /* Display the scrolling intro text */ void scroll_intro_text (int intro_text_x[], int intro_text_y[], int intro_text_size[], int moon_x, int moon_y, int star_x, int star_y, int star_radius, int star_color, int star2_x, int star2_y, int star2_radius, int star2_color, int flower_x, int flower_y, int flower_radius, int flower_color, int petal_radius, int petal_color, int boat_x, int boat_y, int boat_len, int boat_mast, int bug_x, int bug_y, int bug_size, int liberty_x, int liberty_y, int multi_monkey_x[], int multi_monkey_y[], int multi_monkey_arm[]) { int scroll; int index; int count; int done; int dummy_var; dummy_var = 0; done = FALSE; count = 0; while(!done) { count++; scroll = GP142_await_event(&dummy_var, &dummy_var, &dummy_var); switch(scroll) { case GP142_PERIODIC: for(index=0; index < MAX_LINES; index++) { intro_text_y[index] = intro_text_y[index] + 15; intro_text_x[index] = intro_text_x[index] + 1; if(intro_text_y[index] >= -GP142_YMAX + 25 && intro_text_size[index] != 1) { intro_text_size[index] = intro_text_size[index] - 2; } draw_landscape(moon_x, moon_y, star_x, star_y, star_radius, star_color, star2_x, star2_y, star2_radius, star2_color, flower_x, flower_y, flower_radius, flower_color, petal_radius, petal_color, boat_x, boat_y, boat_len, boat_mast, bug_x, bug_y, bug_size, liberty_x, liberty_y, multi_monkey_x, multi_monkey_y, multi_monkey_arm); } break; } the_text(intro_text_x, intro_text_y, intro_text_size); if(count == 75) { done = TRUE; } } } void the_text(int intro_text_x[], int intro_text_y[], int intro_text_size[]){ GP142_printfXY(YELLOW, intro_text_x[0], intro_text_y[0], intro_text_size[0], "In the near future, in a plain"); GP142_printfXY(YELLOW, intro_text_x[1], intro_text_y[1], intro_text_size[1], "not so far away..."); GP142_printfXY(YELLOW, intro_text_x[2], intro_text_y[2], intro_text_size[2], " "); GP142_printfXY(YELLOW, intro_text_x[3], intro_text_y[3], intro_text_size[3], "It is a dark time for the humans."); GP142_printfXY(YELLOW, intro_text_x[4], intro_text_y[4], intro_text_size[4], "Although the ape city has been destroyed,"); GP142_printfXY(YELLOW, intro_text_x[5], intro_text_y[5], intro_text_size[5], "the apes have driven the humans int hiding."); GP142_printfXY(YELLOW, intro_text_x[6], intro_text_y[6], intro_text_size[6], " "); GP142_printfXY(YELLOW, intro_text_x[7], intro_text_y[7], intro_text_size[7], "Evading the dreaded Imperial Ape Fleet,"); GP142_printfXY(YELLOW, intro_text_x[8], intro_text_y[8], intro_text_size[8], "a group of freedom fighters led by"); GP142_printfXY(YELLOW, intro_text_x[9], intro_text_y[9], intro_text_size[9], "Lucas Walker has established a base in"); GP142_printfXY(YELLOW, intro_text_x[10], intro_text_y[10], intro_text_size[10], "the meadow Hoth."); GP142_printfXY(YELLOW, intro_text_x[11], intro_text_y[11], intro_text_size[11], " "); GP142_printfXY(YELLOW, intro_text_x[12], intro_text_y[12], intro_text_size[12], "The evil Ape Lord Cornelius, obssesed with"); GP142_printfXY(YELLOW, intro_text_x[13], intro_text_y[13], intro_text_size[13], "finding young Walker has dispatched"); GP142_printfXY(YELLOW, intro_text_x[14], intro_text_y[14], intro_text_size[14], "thousands of apes into the far reaches"); GP142_printfXY(YELLOW, intro_text_x[15], intro_text_y[15], intro_text_size[15], "of space."); GP142_printfXY(YELLOW, intro_text_x[16], intro_text_y[16], intro_text_size[16], " "); GP142_printfXY(YELLOW, intro_text_x[17], intro_text_y[17], intro_text_size[17], " "); GP142_printfXY(YELLOW, intro_text_x[18], intro_text_y[18], intro_text_size[18], "SHOOT THE MONKEYS!!!"); GP142_printfXY(YELLOW, intro_text_x[19], intro_text_y[19], intro_text_size[19], "STOP THE PLANET OF THE APES!!"); }