/* * CSE142, Spring 2000 * Homework 4 * Idyllic landscape animation STARTER PROGRAM * * */ #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 /* 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 /* number of rockets */ #define NUM_ROCKETS 2 /* number of trees */ #define NUM_TREES 15 /* number of raccoons */ #define NUM_RACCOONS 5 /***************************************************************************** FUNCTION PROTOTYPES *****************************************************************************/ /* Initialize random number generator. [Ignore or */ /* remove if your program doesn't use random numbers.] */ 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); /* Draw a single bug (buttefly) */ void draw_bug(int bug_x, int bug_y, int bug_size); /* 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); /* 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); /* 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 *callBullpen, int *goDuckGo, int *quit, int *goRaccoons, int raccoonX [], int raccoonY [] ); /* Display error message if internal error is detected. */ void something_is_wrong (void); /* draw Jose Mesa */ void drawMesa ( mesaX, mesaY ) { int foot1, foot2; /* moves feet to walk */ /* draw his head */ GP142_circleXY ( BROWN, 0 + mesaX, 0 + mesaY, 20 ); GP142_rectangleXY ( BROWN, -5 + mesaX, -24 + mesaY, 5 + mesaX, -17 + mesaY, 0 ); GP142_rectangleXY ( NAVY_BLUE, -20 + mesaX, 0 + mesaY, 20 + mesaX, 17 + mesaY, 0 ); GP142_rectangleXY ( NAVY_BLUE, -15 + mesaX, 17 + mesaY, 15 + mesaX, 20 + mesaY, 0 ); GP142_triangleXY ( NAVY_BLUE, -20 + mesaX, 17 + mesaY, -15 + mesaX, 17 + mesaY, -15 + mesaX, 20 + mesaY, 0 ); GP142_triangleXY ( NAVY_BLUE, 20 + mesaX, 17 + mesaY, 15 + mesaX, 17 + mesaY, 15 + mesaX, 20 + mesaY, 0 ); GP142_triangleXY ( BLACK, -20 + mesaX, -5 + mesaY, 20 + mesaX, -5 + mesaY, 0 + mesaX, -7 + mesaY, 0 ); GP142_rectangleXY ( BLACK, -20 + mesaX, -5 + mesaY, 20 + mesaX, 0 + mesaY, 0 ); /* draw his body */ GP142_rectangleXY ( NAVY_BLUE, -30 + mesaX, -65 + mesaY, 30 + mesaX, -34 + mesaY, 0 ); GP142_triangleXY ( NAVY_BLUE, -30 + mesaX, -34 + mesaY, -20 + mesaX, -34 + mesaY, -20 + mesaX, -24 + mesaY, 0 ); GP142_triangleXY ( NAVY_BLUE, 30 + mesaX, -34 + mesaY, 20 + mesaX, -34 + mesaY, 20 + mesaX, -24 + mesaY, 0 ); GP142_rectangleXY ( BLACK, -30 + mesaX, -85 + mesaY, -20 + mesaX, -65 + mesaY, 0 ); GP142_circleXY ( BROWN, 25 + mesaX, -70 + mesaY, 6 ); GP142_rectangleXY ( LT_GRAY, -20 + mesaX, -70 + mesaY, 20 + mesaX, -24 + mesaY, 0 ); GP142_printfXY ( BLACK, -12 + mesaX, -36 + mesaY, 10, "MESA" ); GP142_printfXY ( BLACK, -17 + mesaX, -70 + mesaY, 35, "49" ); /* draw his legs */ foot1 = rand() %4; foot2 = rand() %4; GP142_rectangleXY ( MED_GRAY, -20 + mesaX, -80 + mesaY, 20 + mesaX, -70 + mesaY, 0 ); GP142_triangleXY ( MED_GRAY, -20 + mesaX, -80 + mesaY, 0 + mesaX, -80 + mesaY, -10 + mesaX, -120 + mesaY + foot1, 0 ); GP142_triangleXY ( MED_GRAY, 20 + mesaX, -80 + mesaY, 0 + mesaX, -80 + mesaY, 10 + mesaX, -120 + mesaY + foot2, 0 ); GP142_rectangleXY ( BLACK, -15 + mesaX, -120 + mesaY + foot1, -5 + mesaX, -110 + mesaY + foot1, 0 ); GP142_rectangleXY ( BLACK, 5 + mesaX, -120 + mesaY + foot2, 15 + mesaX, -110 + mesaY + foot2, 0 ); } /* set Mesa's initial position */ void initMesa ( int *mesaX, int *mesaY ) { *mesaX = GP142_XMAX + 35; *mesaY = 0; } /* move Jose Mesa across the screen */ void moveMesa ( int *mesaX, int *mesaY, int *callBullpen ) { if ( *mesaX > 0 ) *mesaX = *mesaX - 5; else { if ( *mesaY < GP142_YMAX + 100 ) *mesaY = *mesaY + 60; else { *callBullpen = FALSE; initMesa ( mesaX, mesaY ); } } } /* draw the rockets */ void drawRockets ( int rocket [] ) { int i; /* number of element in the array */ int length; /* length of flame */ for ( i = 0; i < NUM_ROCKETS; i++ ) { length = rand() %10; GP142_rectangleXY ( MED_GRAY, rocket [i] - 20, GP142_YMAX - 70, rocket [i] + 20, GP142_YMAX - 60, 0 ); GP142_triangleXY ( RED, rocket [i], GP142_YMAX - 110 - length, rocket [i] - 18, GP142_YMAX - 70, rocket [i] + 18, GP142_YMAX - 70, 0 ); GP142_triangleXY ( YELLOW, rocket [i], GP142_YMAX - 100 - length, rocket [i] - 8, GP142_YMAX - 70, rocket [i] + 8, GP142_YMAX - 70, 0 ); } } /* draw the spaceship */ void drawSpaceship ( int rocket [] ) { GP142_rectangleXY ( MED_GRAY, -100, GP142_YMAX - 60, 100, GP142_YMAX, 0 ); GP142_triangleXY ( MED_GRAY, -130, GP142_YMAX - 60, -100, GP142_YMAX - 60, -100, GP142_YMAX - 10, 0 ); GP142_triangleXY ( MED_GRAY, 130, GP142_YMAX - 60, 100, GP142_YMAX - 60, 100, GP142_YMAX - 10, 0 ); GP142_rectangleXY ( YELLOW, -30, GP142_YMAX - 30, 30, GP142_YMAX, 0 ); GP142_rectangleXY ( YELLOW, -85, GP142_YMAX - 30, -45, GP142_YMAX, 0 ); GP142_rectangleXY ( YELLOW, 85, GP142_YMAX - 30, 45, GP142_YMAX, 0 ); drawRockets ( rocket ); } /* draw tractor beam */ void drawTractorBeam ( void ) { GP142_rectangleXY ( PINK, -50, -120, 50, GP142_YMAX - 60, 0 ); } /* make tractor beam translucent by adding random circles */ void drawSpecs ( void ) { int i; /* a counter for the loop */ for ( i = 0; i < 300; i++ ) { GP142_circleXY ( RED, rand() %100 - 50, rand() %310 - 120, 1 ); } } /* draw the feet */ void drawDuckFeet ( int duckX, int duckY ) { GP142_triangleXY ( ORANGE, -3 + duckX, -120 + duckY, 2 + duckX, -120 + rand() %4 + duckY, -3 + duckX, -118 + duckY, 0 ); GP142_rectangleXY ( ORANGE, -3 + duckX, -120 + duckY, -5 + duckX, -113 + duckY, 0 ); } /* draw the duck */ void drawDuck ( int duckX, int duckY ) { if ( duckY <= 15 ) { drawDuckFeet ( duckX, duckY ); } /* draw the head */ GP142_rectangleXY ( FOREST_GREEN, 7 + duckX, -106 + duckY, 4 + duckX, -96 + duckY, 0 ); GP142_triangleXY ( YELLOW, 7 + duckX, -99 + duckY, 20 + duckX, -98 + duckY, 7 + duckX, -93 + duckY, 0 ); GP142_circleXY ( FOREST_GREEN, 7 + duckX, -95 + duckY, 5 ); GP142_circleXY ( BLACK, 9 + duckX, -93 + duckY, 1 ); /* draw the body */ GP142_ovalXY ( WHITE, 7 + duckX, -113 + duckY, -13 + duckX, -100 + duckY, 0 ); GP142_triangleXY ( WHITE, -3 + duckX, -113 + duckY, -3 + duckX, -100 + duckY, -30 + duckX, -100 + duckY, 0 ); GP142_triangleXY ( BLACK, 4 + duckX, -105 + duckY, -11 + duckX, -105 + duckY, -2 + duckX, -111 + duckY, 0 ); } /* move the raccoons */ void moveRaccoons ( int raccoonX [], int raccoonY [] ) { int i; /* counter for the loop */ for ( i = 0; i < NUM_RACCOONS; i++ ) { if ( raccoonX [i] < 0 ) { raccoonX [i] = raccoonX [i] + 5; } else { if ( raccoonY [i] < GP142_YMAX + 100 ) { raccoonY [i] = raccoonY [i] + 20; } } } } /* set initial position of raccoons */ void initRaccoons ( int raccoonX [], int raccoonY [] ) { int i; /* counter for the loop */ raccoonX [0] = -GP142_XMAX - 20; raccoonY [0] = 0; for ( i=1; i < NUM_RACCOONS; i ++ ) { raccoonX [i] = raccoonX [i - 1] - 30; raccoonY [i] = 0; } } /* draw a raccoon */ void drawRaccoon ( int raccoonX [], int raccoonY [] ) { int i; /* counter for the loop */ int foot1, foot2; /* moves raccoon feet */ foot1 = rand() %3; foot2 = rand() %3; for ( i=0; i < NUM_RACCOONS; i ++ ) { /* draw the tail */ GP142_ovalXY ( BLACK, -25 + raccoonX [i], -115 + raccoonY [i], 0 + raccoonX [i], -102 + raccoonY [i], 0 ); /* draw the body */ GP142_rectangleXY ( MED_GRAY, -7 + raccoonX [i], -120 + raccoonY [i] + foot1, -3 + raccoonX [i], -115 + raccoonY [i], 0 ); GP142_rectangleXY ( MED_GRAY, 7 + raccoonX [i], -120 + raccoonY [i] + foot2, 3 + raccoonX [i], -115 + raccoonY [i], 0 ); GP142_rectangleXY ( MED_GRAY, -7 + raccoonX [i], -115 + raccoonY [i], 7 + raccoonX [i], -95 + raccoonY [i], 0 ); GP142_triangleXY ( MED_GRAY, -7 + raccoonX [i], -95 + raccoonY [i], -7 + raccoonX [i], -103 + raccoonY [i], -13 + raccoonX [i], -108 + raccoonY [i], 0 ); GP142_triangleXY ( MED_GRAY, 7 + raccoonX [i], -95 + raccoonY [i], 7 + raccoonX [i], -103 + raccoonY [i], 13 + raccoonX [i], -108 + raccoonY [i], 0 ); /* draw the head */ GP142_triangleXY ( MED_GRAY, -8 + raccoonX [i], -87 + raccoonY [i], -3 + raccoonX [i], -82 + raccoonY [i], -10 + raccoonX [i], -78 + raccoonY [i], 0 ); GP142_triangleXY ( MED_GRAY, 8 + raccoonX [i], -87 + raccoonY [i], 3 + raccoonX [i], -82 + raccoonY [i], 10 + raccoonX [i], -78 + raccoonY [i], 0 ); GP142_circleXY ( LT_GRAY, 0 + raccoonX [i], -90 + raccoonY [i], 9); GP142_circleXY ( BLACK, -3 + raccoonX [i], -90 + raccoonY [i], 2 ); GP142_circleXY ( BLACK, 3 + raccoonX [i], -90 + raccoonY [i], 2 ); GP142_circleXY ( LT_GRAY, -3 + raccoonX [i], -90 + raccoonY [i], 1 ); GP142_circleXY ( LT_GRAY, 3 + raccoonX [i], -90 + raccoonY [i], 1 ); GP142_triangleXY ( BLACK, 0 + raccoonX [i], -95 + raccoonY [i], -1 + raccoonX [i], -97 + raccoonY [i], 1 + raccoonX [i], -97 + raccoonY [i], 0 ); } } /* print message to user */ void printMessage ( void ) { GP142_printfXY ( YELLOW, -200, -200, 20, "Use the buttons to select what\nyou would like to abduct." ); } /* move the duck */ void moveDuck ( int *duckX, int *duckY, int *goDuckGo ) { if ( *duckX > -200 && *duckX <= -150 && *duckY > 0 ) { *duckX = *duckX + 2; *duckY = *duckY - 1; } else { if ( *duckX < 0 ) { *duckX = *duckX + 2; } else { if ( *duckY < GP142_YMAX + 100 ) *duckY = *duckY + 20; else *goDuckGo = FALSE; } } } /* draws the trees */ void drawTrees ( int tree [], int color [], int height [] ) { int i; /* counter for loop */ /* draw trees with random shades of green */ for ( i = 0; i < NUM_TREES; i++ ) { GP142_rectangleXY ( BROWN, tree [i] - 2, HORIZON, tree [i] + 2, HORIZON + 8, 0 ); GP142_triangleXY ( color [i], tree [i] - 6, HORIZON + 8, tree [i] + 6, HORIZON + 8, tree [i], HORIZON + 40 + height [i], 0 ); } } /***************************************************************************** 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 */ /* 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 Jose Mesa */ int mesaX, mesaY; /* Mesa's position */ int callBullpen; /* variable for moving Mesa */ /* information about the duck */ int duckX, duckY; /* duck's position */ int goDuckGo; /* variable for moving duck */ /* information about the spaceship */ int rocket [NUM_ROCKETS]; /* position of rockets */ /* position of trees */ int tree [NUM_TREES]; /* holds x position of trees */ int color [NUM_TREES]; /* holds color of trees */ int height [NUM_TREES]; /* change in height of trees */ int count; /* counter for loops */ /* position of raccoons */ int raccoonX [NUM_RACCOONS]; /* moves x value of raccoons */ int raccoonY [NUM_RACCOONS]; /* moves y value of raccoons */ int goRaccoons; /* variable for moving the raccoons */ /* 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 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 position of rockets */ rocket [0] = -85; rocket [1] = 85; /* set position and color of trees */ for ( count = 0; count < NUM_TREES; count++ ) { tree [count] = rand() %200 + 100; color [count] = rand() %3 + 19; height [count] = rand() %6 - 3; } /* set position of raccoons */ initRaccoons ( raccoonX, raccoonY ); goRaccoons = FALSE; /* 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); drawTractorBeam (); drawSpaceship ( rocket ); drawTrees ( tree, color, height ); draw_buttons(); drawRaccoon ( raccoonX, raccoonY ); /* set Mesa's initial position */ callBullpen = FALSE; initMesa ( &mesaX, &mesaY ); /* set duck's initial position */ goDuckGo = FALSE; duckX = -200; duckY = 25; drawDuck ( duckX, duckY ); /* tell the user what to do */ printMessage (); /* * 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, &callBullpen, &goDuckGo, &quit, &goRaccoons, raccoonX, raccoonY ); } else { /* insert code here (and additional else-if statements) */ /* to react to mouse clicks elsewhere on the screen (if needed) */ } 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_bug(&bug_x, &bug_y, &bug_size); if ( callBullpen == TRUE ) { moveMesa ( &mesaX, &mesaY, &callBullpen ); } if ( goDuckGo == TRUE ) { moveDuck ( &duckX, &duckY, &goDuckGo ); } if ( goRaccoons == TRUE ) { moveRaccoons ( raccoonX, raccoonY ); } 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, boat_x, boat_y, boat_len, boat_mast, bug_x, bug_y, bug_size); drawTractorBeam (); drawTrees ( tree, color, height ); drawMesa ( mesaX, mesaY ); drawDuck ( duckX, duckY ); drawRaccoon ( raccoonX, raccoonY ); drawSpaceship ( rocket ); draw_buttons(); drawSpecs (); printMessage (); } /* 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 other objects and their parameters here - tree, bug, etc. */ /* ... */ /***************************************************************************** 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 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 text_offset = -GP142_XMAX + 15; /* horizontal offset of text */ int text_size = 12; /* size of text font */ /* 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); /* Draw mountains */ GP142_triangleXY(LT_GRAY, -GP142_XMAX, HORIZON, GP142_XMAX, HORIZON, -GP142_XMAX / 8, (5 * GP142_YMAX)/6, FILL); GP142_triangleXY(PEACH, -GP142_XMAX, HORIZON, GP142_XMAX, HORIZON, -GP142_XMAX / 3, (3 * GP142_YMAX)/4, FILL); GP142_triangleXY(DUSTY_PLUM, -GP142_XMAX, HORIZON, GP142_XMAX, HORIZON, GP142_XMAX / 4, (2 * GP142_YMAX)/3, FILL); GP142_triangleXY(PURPLE, -( 5 * GP142_XMAX/4), HORIZON, (GP142_XMAX)/3 , HORIZON, -(3 * GP142_XMAX)/4, (2 * GP142_YMAX)/3 , FILL); GP142_triangleXY(MED_GRAY, -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 buttefly */ draw_bug(bug_x, bug_y, 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, "Duck"); i -= BUTTON_HEIGHT; GP142_printfXY(WHITE, text_offset, i, font_size, "Raccoons"); i -= BUTTON_HEIGHT; GP142_printfXY(WHITE, text_offset, i, font_size, "Jose Mesa"); i -= BUTTON_HEIGHT; GP142_printfXY(WHITE, text_offset, i, font_size, "Yourself"); } /* 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; 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 *callBullpen, int *goDuckGo, int *quit, int *goRaccoons, int raccoonX [], int raccoonY [] ) { 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: /* move the duck */ *goDuckGo = TRUE; break; case 1: /* move the raccoons */ *goRaccoons = TRUE; initRaccoons ( raccoonX, raccoonY ); break; case 2: /* draw Mesa and move him across the screen */ *callBullpen = TRUE; break; case 3: /* quit the program */ *quit = TRUE; 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); } /***************************************************************************** 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!"); }