/* * CSE142, Spring 2000 * Homework 4 * Idyllic landscape animation STARTER PROGRAM David Meglasson Section BI * * */ #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 /* * Symbolic constants. Change as needed. */ /* 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 4 /* Possible locations of mouse clicks: */ /* Mouse click in some button */ #define BUTTON_CLICK 1 /* Mouse click elsewhere */ #define OTHER_CLICK 0 #define TOP_CLICK 2 /* 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 5 #define MAX_BUGS 8 #define MAX_PEOPLE 90 /***************************************************************************** FUNCTION PROTOTYPES *****************************************************************************/ /* Initialize random number generator. [Ignore or */ /* remove if your program doesn't use random numbers.] */ void initialize_random_numbers(); /* 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); /* Display error message if internal error is detected. */ void something_is_wrong (void); /***********************************************************************/ /* 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 tree_color, int boat_x, int boat_y, int boat_len, int boat_mast, int boat2_x, int boat2_y, int bug_size, int bug_x_coord[], int bug_y_coord[], int rabbit_x,int rabbit_y, int dead_rabbit_x, int dead_rabbit_y, int *rabbit_draw, int bear_x, int bear_y, int person_x_coord[],int person_y_coord[], int *night_time); /* 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 *tree_color, int *boatmove, int *boat_x, int *boat_y, int *boat2_x, int *boat2_y, int *show_boat1, int *show_boat2, int *rabbit_draw, int *bear_move, int *rabbit_x, int *rabbit_y, int *bear_x, int *bear_y); /***********************************************************************/ /* Sky Functions */ /***********************************************************************/ /* Initializes the moon parameters */ void init_moon(int *moon_x, int *moon_y); /* Update moon coordinates moon_x, moon_y for one tick */ void move_moon(int *moon_x, int *moon_y); /* Make stars blink */ void blink_star(int *star_radius, int *star_color); /***********************************************************************/ /* Plant Functions */ /***********************************************************************/ /* Change tree top field colors */ void change_tree_color(int *tree_color); /* Change flower field colors */ void change_colors(int *petal_color, int *flower_color); /***********************************************************************/ /* Bug Functions */ /***********************************************************************/ /* Draw a single bug (buttefly) */ void draw_bug(int bug_x_coord[],int bug_y_coord[], int bug_size); /* Move bug(s) */ void move_bug(int *bug_x, int *bug_y, int *bug_size); void move_swarm(int bug_x_coord[], int bug_y_coord[], int bug_size); /* Initialize bug coordinates */ void init_bugs(int bug_x_coord[], int bug_y_coord[], int flower_x, int flower_y); /***********************************************************************/ /* Boat Functions */ /***********************************************************************/ /* Draw a single boat */ void draw_boat(int boat_x, int boat_y, int boat_len, int boat_mast, int boat2_x, int boat2_y); /* Update boat coordinates boat_x, boat_y for one tick and initialize coordinates */ void init_boat(int *boat_x, int *boat_y, int *boat2_x, int *boat2_y); void move_boat(int *boat_x, int *boat_y, int *boat2_x, int *boat2_y, int *show_boat1, int *show_boat2); /* Move boats up and down along y axis */ void waving_boat(int *boat_y, int *boat2_y); /***********************************************************************/ /* Animal Functions */ /***********************************************************************/ /* Initialize Rabbit */ void init_rabbit(int *rabbit_x,int *rabbit_y); /* Draw and move rabbit */ void draw_rabbit(int rabbit_x, int rabbit_y); void move_rabbit(int *rabbit_x,int *rabbit_y, int *rabbithope, int *rabbit_draw); /* Draw dead rabbit */ void draw_dead_rabbit(int dead_rabbit_x,int dead_rabbit_y ); /* Initialize Bear */ void init_bear(int *bear_x, int *bear_y); void draw_bear(int bear_x, int bear_y); void move_bear(int *bear_x, int *bear_y, int *bear_move); /***********************************************************************/ /* People Functions */ /***********************************************************************/ void draw_person( int person_x_coord[], int person_y_coord[]); void init_person( int person_x_coord[], int person_y_coord[]); void init_color( int shirt_color[]); /***************************************************************************** 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 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 */ int tree_color; /* alter tree colors */ /* Information about the boats */ /* Black 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 */ /* Red Boat */ int boat2_x; int boat2_y; int boatmove = FALSE; int show_boat1 = FALSE; int show_boat2 = FALSE; /* Information about the bug(s) */ int bug_size; int bug_x_coord[MAX_BUGS]; int bug_y_coord[MAX_BUGS]; /* Information about the rabbit and bear */ int rabbit_x, rabbit_y; int rabbithope = FALSE; int rabbit_draw = TRUE; int dead_rabbit_x = -GP142_XMAX + 250; int dead_rabbit_y = -GP142_YMAX + 60; int bear_x, bear_y; int bear_move = FALSE; /* Information about people */ int person_x_coord[MAX_PEOPLE]; int person_y_coord[MAX_PEOPLE]; int shirt_color[MAX_COLORS]; /* Night to day */ int night_time = TRUE; // init_person(person_x_coord, person_y_coord); // init_color(shirt_color); /* 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); /* initial function locations of the moon, boat, rabbit, bear, people */ init_moon(&moon_x, &moon_y); init_boat(&boat_x, &boat_y, &boat2_x, &boat2_y); init_rabbit(&rabbit_x, &rabbit_y); init_bear(&bear_x, &bear_y); init_person(person_x_coord, person_y_coord); init_color(shirt_color); /* 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 color for tree */ tree_color = FOREST_GREEN; /* set initial conditions for the boat */ boat_len = 25; boat_mast = 40; /* set initial conditions for the buttefly */ init_bugs( bug_x_coord, bug_y_coord, flower_x, flower_y); bug_size = MAX_BUG_SIZE; /* 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, tree_color, boat_x, boat_y, boat_len, boat_mast, boat2_x, boat2_y, bug_size, bug_x_coord, bug_y_coord, rabbit_x, rabbit_y, dead_rabbit_x, dead_rabbit_y, &rabbit_draw, bear_x, bear_y, person_x_coord, person_y_coord, &night_time); draw_buttons(); /* * 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, &tree_color, &boatmove, &boat_x, &boat_y, &boat2_x, &boat2_y, &show_boat1, &show_boat2, &rabbit_draw, &bear_move, &rabbit_x, &rabbit_y, &bear_x, &bear_y); } if (click_kind == OTHER_CLICK) { /* Mouse click is somewhere else then menu buttons and below river, rabbit hopes */ rabbithope = TRUE; bear_move = TRUE; } else if ( click_kind == TOP_CLICK) { /* Mouse click above river to turn night to day */ night_time = FALSE; } break; case GP142_PERIODIC: /* Timer interval event. Update everything that needs the change */ /* as time advances. Animation should go here! */ move_moon(&moon_x, &moon_y); blink_star(&star_radius, &star_color); blink_star(&star2_radius, &star2_color); move_swarm(bug_x_coord, bug_y_coord, bug_size); waving_boat(&boat_y, &boat2_y); /* Use buttons to make boat stop or start, depending on value of */ /* of boatmove */ if (boatmove == TRUE) move_boat(&boat_x, &boat_y, &boat2_x, &boat2_y, &show_boat1, &show_boat2); if (rabbithope == TRUE) move_rabbit(&rabbit_x, &rabbit_y, &rabbithope, &rabbit_draw); if (bear_move == TRUE) move_bear(&bear_x, &bear_y, &bear_move); break; default: /* unknown event */ break; } /*end switch */ /* Redraw everything */ 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, tree_color, boat_x, boat_y, boat_len, boat_mast, boat2_x, boat2_y, bug_size, bug_x_coord, bug_y_coord, rabbit_x, rabbit_y, dead_rabbit_x, dead_rabbit_y, &rabbit_draw, bear_x, bear_y, person_x_coord, person_y_coord, &night_time); draw_buttons(); /* Display text for boats */ /* Display Red boat has won */ if (show_boat2 == TRUE && show_boat1 == FALSE) GP142_printfXY( WHITE, -GP142_XMAX + 60, HORIZON + 40, 12, " Red boat wins! "); /* Display Black boat has won */ else if (show_boat1 == TRUE && show_boat2 == FALSE) GP142_printfXY( WHITE, -GP142_XMAX + 60, HORIZON + 40, 12, " Black boat wins! "); } /* end event loop */ /* Termination: shut down graphics window and exit */ GP142_close(); return 0; } /* end main */ /***************************************************************************** INITIALIZATION PROCEDURES *****************************************************************************/ /* Initializes the moon parameters */ void init_moon(int *moon_x, int *moon_y) { *moon_x = GP142_XMAX - 50; *moon_y = GP142_YMAX - 35; } /* Initialize the boat parameters... */ void init_boat(int *boat_x, int *boat_y, int *boat2_x, int *boat2_y) { /* Black Boat */ *boat_x = GP142_XMAX - 200; *boat_y = HORIZON - 27; /* Red Boat */ *boat2_x = GP142_XMAX - 200; *boat2_y = HORIZON - 7; } /* Initialize bug parameters */ void init_rabbit(int *rabbit_x,int *rabbit_y) { *rabbit_x =-GP142_XMAX + 200; *rabbit_y = -GP142_YMAX + 60; } /* Initialize bugs */ void init_bugs( int bug_x_coord[], int bug_y_coord[], int flower_x, int flower_y) { int i = 0; for( i; i < MAX_BUGS; i++ ){ bug_x_coord[i] = flower_x + 20 + rand()%60; bug_y_coord[i] = flower_y + 30 + rand()%60; } } /* Initialize Bear Coordinates */ void init_bear( int *bear_x, int *bear_y ) { *bear_x = -GP142_XMAX + 350; *bear_y = -GP142_YMAX + 70; } /* Initialize person coordinates with people */ void init_person( int person_x_coord[], int person_y_coord[]) { int i; for( i = 0; i < MAX_PEOPLE; i++ ) { person_x_coord[i] = GP142_XMAX - 300 + rand()%280; person_y_coord[i] = GP142_YMAX - 275 + rand()%60; } } /* Initialize shirt color */ void init_color( int shirt_color[]) { int i; for ( i = 0; i < MAX_COLORS; i++ ) { shirt_color[i] = rand()%5; } } /***************************************************************************** BACKGROUND LANDSCAPE *****************************************************************************/ /* Change tree top colors */ void change_tree_color(int *tree_color) { *tree_color = (*tree_color + 3) % MAX_COLORS; } /* 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_coord[], int bug_y_coord[], int bug_size) { int i = 0; for ( i; i < MAX_BUGS; i++ ) { GP142_triangleXY(MAGENTA, bug_x_coord[i], bug_y_coord[i], bug_x_coord[i] - bug_size, bug_y_coord[i] + bug_size, bug_x_coord[i] - bug_size, bug_y_coord[i] - bug_size, FILL); GP142_triangleXY(MAGENTA, bug_x_coord[i], bug_y_coord[i], bug_x_coord[i] + bug_size, bug_y_coord[i] + bug_size, bug_x_coord[i] + bug_size, bug_y_coord[i] - bug_size, FILL); GP142_lineXY(BLACK, bug_x_coord[i], bug_y_coord[i], bug_x_coord[i] - 3, bug_y_coord[i] + 7, 2); GP142_lineXY(BLACK, bug_x_coord[i], bug_y_coord[i], bug_x_coord[i] + 3, bug_y_coord[i] + 7, 2); } } /* Draw rabbit relative to rabbit head*/ void draw_rabbit(int rabbit_x, int rabbit_y) { /* Head */ GP142_circleXY(MED_GRAY, rabbit_x, rabbit_y, 9); /* BODY */ GP142_ovalXY(MED_GRAY, rabbit_x - 10, rabbit_y - 10, rabbit_x + 10, rabbit_y - 10, 15); /* EARS */ GP142_ovalXY(MED_GRAY, rabbit_x - 3, rabbit_y, rabbit_x - 5, rabbit_y + 20, 4); GP142_ovalXY(MED_GRAY, rabbit_x + 3, rabbit_y, rabbit_x + 5, rabbit_y + 20, 4); /* FEET */ GP142_circleXY(WHITE, rabbit_x - 10, rabbit_y - 15, 3); GP142_circleXY(WHITE, rabbit_x + 10, rabbit_y - 15, 3); /* EYES AND NOSE*/ GP142_pixelXY(BLUE, rabbit_x - 3, rabbit_y + 3); GP142_pixelXY(BLUE, rabbit_x + 3, rabbit_y + 3); GP142_circleXY(RED, rabbit_x, rabbit_y, 2); /* WHISKERS */ GP142_lineXY(BLACK, rabbit_x - 2, rabbit_y, rabbit_x - 14, rabbit_y, 1); GP142_lineXY(BLACK, rabbit_x + 2, rabbit_y, rabbit_x + 14, rabbit_y, 1); GP142_lineXY(BLACK, rabbit_x - 2, rabbit_y, rabbit_x - 12, rabbit_y - 6, 1); GP142_lineXY(BLACK, rabbit_x + 2, rabbit_y, rabbit_x + 12, rabbit_y - 6, 1); } /* Draw remains of dead rabbit */ void draw_dead_rabbit(int dead_rabbit_x, int dead_rabbit_y ) { /* Blood Spot */ GP142_ovalXY(RED, dead_rabbit_x - 90, dead_rabbit_y - 25, dead_rabbit_x - 50, dead_rabbit_y, FILL); /* Body Parts */ GP142_rectangleXY( CHALK, dead_rabbit_x - 90, dead_rabbit_y - 16, dead_rabbit_x - 110, dead_rabbit_y - 20, FILL); GP142_circleXY( CHALK, dead_rabbit_x - 90, dead_rabbit_y - 18, 3); GP142_circleXY( CHALK, dead_rabbit_x - 110, dead_rabbit_y - 18, 3); // GP142_ovalXY(MED_GRAY, dead_rabbit_x - 95, dead_rabbit_y + 5, dead_rabbit_x - 97, dead_rabbit_y - 15, 4); /* Eyes */ GP142_circleXY( WHITE, dead_rabbit_x - 70, dead_rabbit_y - 16, 3); GP142_circleXY( BLUE, dead_rabbit_x - 70, dead_rabbit_y - 16, 1); } /* Draw Bear */ void draw_bear( int bear_x, int bear_y) { /* body */ GP142_rectangleXY(BROWN, bear_x + 5, bear_y - 20, bear_x + 45, bear_y + 12, FILL); GP142_triangleXY(BROWN, bear_x + 5, bear_y - 20, bear_x + 5, bear_y - 5, bear_x - 5, bear_y - 5, FILL); /* Head */ GP142_rectangleXY(BROWN, bear_x - 10 , bear_y - 5, bear_x + 10, bear_y + 15 , FILL ); /* Mouth */ GP142_rectangleXY(BROWN, bear_x - 5, bear_y - 3, bear_x - 10, bear_y, 10); /* Ear */ GP142_rectangleXY(BROWN, bear_x + 3, bear_y + 12, bear_x + 10, bear_y + 20, FILL); GP142_rectangleXY(WHITE, bear_x + 5, bear_y + 14, bear_x + 7, bear_y + 17, FILL); /* Legs */ GP142_rectangleXY(BROWN, bear_x + 4, bear_y - 30, bear_x + 15, bear_y -20, FILL); GP142_rectangleXY(BROWN, bear_x + 34, bear_y - 30, bear_x + 45, bear_y -20, FILL); /* Eyes */ GP142_rectangleXY(RED, bear_x - 5, bear_y + 9, bear_x - 3, bear_y + 12, FILL); /* Teeth */ GP142_lineXY(BLACK, bear_x - 15, bear_y - 3, bear_x - 5, bear_y - 3, 2); /* Nose */ GP142_rectangleXY( BLACK, bear_x - 15, bear_y + 3, bear_x - 13, bear_y + 5, FILL); } /* Draw a person */ void draw_person( int person_x_coord[], int person_y_coord[], int shirt_color[] ) { // int shirt_color = rand()%MAX_COLORS; int i = 0; for ( i; i < MAX_PEOPLE; i++ ) { /* Head, Body */ GP142_rectangleXY(shirt_color[i], person_x_coord[i], person_y_coord[i], person_x_coord[i] + 6, person_y_coord[i] + 7, FILL); GP142_circleXY( PINK, person_x_coord[i] + 3, person_y_coord[i] + 10, 3); /* Legs */ GP142_rectangleXY(NAVY_BLUE, person_x_coord[i], person_y_coord[i] - 5, person_x_coord[i] + 2, person_y_coord[i], FILL); GP142_rectangleXY(NAVY_BLUE, person_x_coord[i], person_y_coord[i] - 5, person_x_coord[i] + 6, person_y_coord[i], FILL); /* Arms */ GP142_lineXY(PURPLE, person_x_coord[i] + 1, person_y_coord[i] + 5, person_x_coord[i] - 4, person_y_coord[i] + 1, 2); GP142_lineXY(PURPLE, person_x_coord[i] + 6, person_y_coord[i] + 5, person_x_coord[i] + 11, person_y_coord[i] + 1, 2); } } /* 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, int boat2_x, int boat2_y) { /* red Boat */ GP142_rectangleXY(RED, boat2_x, boat2_y, boat2_x + boat_len * 2, boat2_y - 4, FILL); GP142_triangleXY(MAGENTA, boat2_x + 25, boat2_y, boat2_x + 25, boat2_y + boat_mast, boat2_x + 50, boat2_y + 2 * boat_mast /3, FILL); /* black Boat */ GP142_rectangleXY(BLACK, boat_x, boat_y, boat_x + boat_len * 2, boat_y - 4, FILL); GP142_triangleXY(CYAN, boat_x + 25, boat_y, boat_x + 25, boat_y + boat_mast, boat_x + 50, 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 tree_color, int boat_x, int boat_y, int boat_len, int boat_mast, int boat2_x, int boat2_y, int bug_size, int bug_x_coord[], int bug_y_coord[], int rabbit_x,int rabbit_y, int dead_rabbit_x, int dead_rabbit_y, int *rabbit_draw, int bear_x, int bear_y, int person_x_coord[],int person_y_coord[], int *night_time) { int text_offset = -GP142_XMAX + 15; /* horizontal offset of text */ int text_size = 12; /* size of text font */ /* Night ime functions */ if ( *night_time == TRUE ) { /* Draw the sky */ GP142_rectangleXY(BLACK, -GP142_XMAX, -GP142_YMAX, GP142_XMAX, GP142_YMAX, FILL); /* Draw the moon */ GP142_circleXY(WHITE, moon_x, moon_y, MOON_RADIUS); GP142_ovalXY(BLACK, 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); } else { /* Draw blue sky */ GP142_rectangleXY(BLUE, -GP142_XMAX, -GP142_YMAX, GP142_XMAX, GP142_YMAX, FILL); /* Draw Sun */ GP142_circleXY(YELLOW, moon_x, moon_y, MOON_RADIUS); } /* 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(BROWN, -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); /* Draw stadium */ GP142_rectangleXY(LT_GRAY, -GP142_XMAX + 275 , HORIZON, GP142_XMAX, HORIZON + 100, FILL); GP142_rectangleXY(MED_GRAY, -GP142_XMAX + 125 , HORIZON, GP142_XMAX, HORIZON + 20, FILL); GP142_lineXY(BLACK, -GP142_XMAX + 125, HORIZON + 20, GP142_XMAX, HORIZON + 20, 2); GP142_lineXY(BLACK, -GP142_XMAX + 275, HORIZON + 40, GP142_XMAX, HORIZON + 40, 2); GP142_lineXY(BLACK, -GP142_XMAX + 275, HORIZON + 60, GP142_XMAX, HORIZON + 60, 2); GP142_lineXY(BLACK, -GP142_XMAX + 275, HORIZON + 80, GP142_XMAX, HORIZON + 80, 2); GP142_lineXY(BLACK, -GP142_XMAX + 275, HORIZON + 100, GP142_XMAX, HORIZON + 100, 2); GP142_rectangleXY(LT_GRAY, -GP142_XMAX + 150, HORIZON + 22, -GP142_XMAX + 240, HORIZON + 110, FILL); GP142_rectangleXY(GOLD, -GP142_XMAX + 165, HORIZON + 70, -GP142_XMAX + 225, HORIZON + 90, FILL); /* Draw crowd */ draw_person( person_x_coord, person_y_coord ); /* Draw meadow */ GP142_rectangleXY(SEA_GREEN, -GP142_XMAX, -GP142_YMAX, GP142_XMAX, HORIZON, FILL); /* Draw River */ GP142_rectangleXY(BLUE, -GP142_XMAX, HORIZON, GP142_XMAX, HORIZON - 50, FILL); /* Draw Finish Line Behind boats*/ GP142_lineXY(NAVY_BLUE, -GP142_XMAX+ 100, HORIZON - 50, -GP142_XMAX + 100, HORIZON, 2); GP142_lineXY(BLACK, -GP142_XMAX + 100, HORIZON, -GP142_XMAX + 100, HORIZON + 20, 2); GP142_triangleXY(YELLOW, -GP142_XMAX + 100, HORIZON + 10, -GP142_XMAX + 100, HORIZON + 20, -GP142_XMAX + 90, HORIZON + 20, FILL); /* Draw bear */ draw_bear(bear_x, bear_y); /* 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 boat 1 and 2 */ draw_boat(boat_x, boat_y, boat_len, boat_mast, boat2_x, boat2_y); /* Finish line in front of boats */ GP142_lineXY(BLACK, -GP142_XMAX + 100, HORIZON - 50, -GP142_XMAX + 100, HORIZON - 30, 2); GP142_triangleXY(YELLOW, -GP142_XMAX + 100, HORIZON - 40, -GP142_XMAX + 100, HORIZON - 30, -GP142_XMAX + 90, HORIZON - 30, FILL); /* Draw tree */ GP142_rectangleXY(BROWN, -GP142_XMAX + 50, -GP142_YMAX + 50, -GP142_XMAX + 60, -GP142_YMAX + 100, FILL); GP142_circleXY(tree_color, -GP142_XMAX + 55, -GP142_YMAX + 110, 30); /* Draw 2nd tree */ GP142_rectangleXY(BROWN, -GP142_XMAX + 100, -GP142_YMAX + 35, -GP142_XMAX + 110, -GP142_YMAX + 85, FILL); GP142_triangleXY(tree_color + 2, -GP142_XMAX + 103, -GP142_YMAX + 130, -GP142_XMAX + 90, -GP142_YMAX + 60, -GP142_XMAX + 125, -GP142_YMAX + 60, FILL); /* Draw 3rd tree */ GP142_rectangleXY(BROWN, -GP142_XMAX + 300, -GP142_YMAX + 55, -GP142_XMAX + 310, -GP142_YMAX + 105, FILL); GP142_circleXY(tree_color, -GP142_XMAX + 304, -GP142_YMAX + 135, 35); GP142_triangleXY(BROWN, -GP142_XMAX + 275, -GP142_YMAX + 125, -GP142_XMAX + 300, -GP142_YMAX + 100, -GP142_XMAX + 310, -GP142_YMAX + 100, FILL); GP142_triangleXY(BROWN, -GP142_XMAX + 330, -GP142_YMAX + 125, -GP142_XMAX + 300, -GP142_YMAX + 100, -GP142_XMAX + 310, -GP142_YMAX + 100, FILL); GP142_triangleXY(BROWN, -GP142_XMAX + 304, -GP142_YMAX + 120, -GP142_XMAX + 300, -GP142_YMAX + 100, -GP142_XMAX + 310, -GP142_YMAX + 100, FILL); /* Draw rabbit alive or dead */ if ( *rabbit_draw == TRUE ) { draw_rabbit(rabbit_x, rabbit_y); } else if ( *rabbit_draw == FALSE ) draw_dead_rabbit( dead_rabbit_x, dead_rabbit_y ); /* Draw buttefly */ draw_bug(bug_x_coord, bug_y_coord, bug_size); } /*end draw_landscape */ /***************************************************************************** BUTTONS AND MOUSE CLICKS *****************************************************************************/ /* Draws the buttons */ void draw_buttons(void) { int i; int text_offset = MENU_LEFT+10; /* offset of button text in button */ int font_size = 12; /* size of button text */ /* draw rectangles for each button */ for(i = 0; i < NUM_BUTTONS; i++) { GP142_rectangleXY(WHITE, MENU_LEFT, MENU_TOP - i * BUTTON_HEIGHT, MENU_LEFT + BUTTON_WIDTH, MENU_TOP - (i + 1) * BUTTON_HEIGHT, 1); } /* display button text */ i = MENU_TOP - (BUTTON_HEIGHT/2) - (font_size/2); GP142_printfXY(WHITE, text_offset, i, font_size, "Alter Colors"); i -= BUTTON_HEIGHT; GP142_printfXY(WHITE, text_offset, i, font_size, "Revive Bunny"); i -= BUTTON_HEIGHT; GP142_printfXY(WHITE, text_offset, i, font_size, "Start Boat Race"); i -= BUTTON_HEIGHT; GP142_printfXY(WHITE, text_offset, i, font_size, "Stop Boat Race"); } /* Classify mouse click at (mouse_x,mouse_y) and return kind */ int classify_mouse_click(int mouse_x, int mouse_y) { if ((mouse_x > MENU_LEFT) && (mouse_x < MENU_LEFT + BUTTON_WIDTH) && (mouse_y < MENU_TOP) && (mouse_y > (MENU_TOP - NUM_BUTTONS * BUTTON_HEIGHT))) return BUTTON_CLICK; if ( mouse_y < HORIZON ) return OTHER_CLICK; else if (mouse_y >= HORIZON) // && // (mouse_x < MENU_LEFT) && // (mouse_x > MENU_LEFT + BUTTON_WIDTH)) return TOP_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 *tree_color, int* boatmove, int *boat_x, int *boat_y, int *boat2_x, int *boat2_y, int *show_boat1, int *show_boat2, int *rabbit_draw, int *bear_move, int *rabbit_x, int *rabbit_y, int *bear_x, int *bear_y) { 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); /* alter the types of tree tops */ change_tree_color(tree_color); break; case 1: /* Initialize live rabbit and bear */ *rabbit_draw = TRUE; *bear_move = FALSE; init_rabbit( rabbit_x, rabbit_y); init_bear( bear_x, bear_y); break; case 2: /* Start Boat Race */ *boatmove = TRUE; // move_boat(boat_x, boat_y, boat2_x, boat2_y); break; case 3: /* Stop boat from moving and Restart Game */ *boatmove = FALSE; init_boat(boat_x, boat_y, boat2_x, boat2_y); *show_boat1 = FALSE; *show_boat2 = FALSE; 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 one bug around and flutter */ void move_bug(int *bug_x_coord, int *bug_y_coord, int *bug_size) { *bug_x_coord = *bug_x_coord + rand() % 3 - 1; *bug_y_coord = *bug_y_coord + rand() % 3 - 1; *bug_size = MAX_BUG_SIZE/2 + rand() % MAX_BUG_SIZE/2; draw_bug(bug_x_coord, bug_y_coord, *bug_size); } /* Move all bugs move around, defined by MAX_BUGS */ void move_swarm(int bug_x_coord[], int bug_y_coord[], int bug_size) { int i = 0; for( i; i < MAX_BUGS; i++ ) { move_bug( &bug_x_coord[i], &bug_y_coord[i], &bug_size ); } } /* Update boat coordinates boat x and y by one tick */ void move_boat(int *boat_x, int *boat_y, int *boat2_x, int *boat2_y, int *show_boat1, int *show_boat2) { /* Move BLACK Boat */ *boat_x = *boat_x - rand()%10 - 3; *boat_y = *boat_y; if (*boat_x < -GP142_XMAX -30) { *boat_x = - *boat_x; (*boat_y) --; } /* Move RED boat */ *boat2_x = *boat2_x - rand()%10 - 3; *boat2_y = *boat2_y; if (*boat2_x < -GP142_XMAX -30) { *boat2_x = - *boat2_x; (*boat2_y) --; } /* Declare winning boat using finish line as ending point */ if ( *boat_x <= -GP142_XMAX + 90 ) *show_boat1 = TRUE; else if ( *boat2_x <= -GP142_XMAX + 90 ) *show_boat2 = TRUE; } /* Move boats up and down along y axis continiuosly */ void waving_boat(int *boat_y, int *boat2_y) { *boat_y = -85 + (int) ( 3 * sin(*boat_y)); *boat2_y = -59 + (int) ( 4 * sin(*boat2_y)) ; } /* Make the rabbit hope up and down, to the left */ void move_rabbit(int *rabbit_x, int *rabbit_y, int *rabbithope, int *rabbit_draw) { *rabbit_x = *rabbit_x - 2; if ( *rabbit_x <= -GP142_XMAX + 175 ) { *rabbit_x = *rabbit_x + 2; *rabbithope = FALSE; *rabbit_draw = FALSE; } *rabbit_y = *rabbit_y + 10; if ( *rabbit_y == -GP142_YMAX + 70) *rabbit_y = *rabbit_y - 20; } /* Make the bear move */ void move_bear(int *bear_x, int *bear_y, int *bear_move) { *bear_x = *bear_x -13; if ( *bear_x <= -GP142_XMAX + 175 ) { *bear_x = *bear_x + 13; *bear_move = FALSE; } *bear_y = *bear_y + 10; if ( *bear_y == -GP142_YMAX + 100) *bear_y = *bear_y - 20; } /***************************************************************************** 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!"); }