/* * CSE142, Spring 2000 * Homework 4 * Idyllic landscape animation NAINTARA AGARWAL, SECTION AH , JIWON KIM * * */ #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 #define RIGHT 1 #define LEFT 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 25 #define MOON_FULLNESS 1.5 #define MAX_STAR_RADIUS 4 #define MAX_BUG_SIZE 10 #define MAX_STARS 10 #define MAX_FLOWERS 30 #define TIME_PER_FRAME 20 #define BOAT_LEN 25 #define BOAT_MAST 20 /***************************************************************************** FUNCTION PROTOTYPES *****************************************************************************/ /* Initialize random number generator. [Ignore or */ /* remove if your program doesn't use random numbers.] */ void initialize_random_numbers(); void introduction_scr(); void enter_castle(int *quit); void draw_status(int status); void clr_screen(); void introduction(int *quit); /* 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 on_boat); void init_boat(int *boat_x, int *boat_y); void draw_ball(int t); /* Draw a single bug (buttefly) */ void draw_bug(int bug_x, int bug_y, int bug_size); /* Draw the princess*/ void draw_castle(); void draw_room(); void draw_man(int *man_x , int *man_y); /* Change flower field colors */ void change_colors(int *petal_color, int *flower_color); void move_man(int *on_boat, int boat_x, int boat_y, int *man_x, int *man_y); void draw_flower(int flower_x, int flower_y, int petal_color,int petal_radius, int flower_radius, 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 on_boat, 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 status, int boat_x, int boat_y,int bug_x, int bug_y, int bug_size, int man_x , int man_y); void hop_boat(int boat_x, int boat_y ); /* Initializes the man */ void init_man(int *man_x , int *man_y); void draw_lady(int man_x, int man_y); /* Make stars blink */ void draw_stars(int star_color, int star_x ,int star_y, int star_radius); 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); void move_boat(int *boat_x, int direction); /* Draw buttons */ void draw_buttons(); /* 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 *quit, int boat_x, int boat_y, int *on_boat, int *man_x, int *man_y); /* 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 the moon and stars: */ int moon_x, moon_y; /* screen x and y coordinates */ int star_x[MAX_STARS], star_y[MAX_STARS]; /* 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[MAX_FLOWERS], flower_y[MAX_FLOWERS]; /* screen x and y coordinates */ int flower_radius, petal_radius; /* flower and petal radii */ int flower_color, petal_color; /* flower and petal colors */ int i , k; int t = 0; int j = 0;/* the time */ int status = 0; int on_boat = FALSE; /* Information about the tree */ /* Fill in here! */ /* Information about the boat */ int boat_x, boat_y; /* screen x and y coordinates */ int direction; /* Information about the man */ int man_x , man_y; /* Information about the bug(s) */ int bug_x, bug_y, bug_size; /* screen x and y coordinates and size of the bug */ // int girl_x , girl_y, girl_size; /* 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); introduction(&quit); /* set initial location of the moon */ init_moon(&moon_x, &moon_y); /* set initial location of the stars */ star_x[0] = GP142_XMAX - 200; star_y[0] = GP142_YMAX - 90; for( k =0; k < MAX_STARS; k++){ star_x[k] = rand() % GP142_XMAX; if (star_x[k] % 2 == 0){ star_x[k] = -( star_x[k]); } star_y[k] = rand() % (GP142_XMAX - 90); } star_radius = 20; 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[0] = (GP142_XMAX / 2); flower_y[0] = (-GP142_YMAX + 40); for(i= 0; i < MAX_FLOWERS; i++){ flower_x[i] = (GP142_XMAX / 2) + (rand() % 10 * i); flower_y[i] = -(50 + rand() % 200); } flower_radius = 2; flower_color = YELLOW; petal_radius = 8; petal_color = RED; /* set initial conditions for the boat */ init_boat(&boat_x, &boat_y); /* set initial conditions for the buttefly */ bug_x = flower_x[2] + 20; bug_y = flower_y[2] + 30; bug_size = MAX_BUG_SIZE/2; /* set initial conditions for the man */ init_man(&man_x , &man_y); /* * 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. */ 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, */ if (key_pressed == '6' ) { if (man_x > GP142_XMAX - 30 ) { status = 1; draw_status(status); } else { man_x = man_x + 2; } } else { if(key_pressed == '4') { if (man_x < 105 && man_y > -80) { status = 3; draw_status(status); } else { if (man_x < -GP142_XMAX + 20) { status = 1; draw_status(status); } else man_x = man_x - 2; } } if(key_pressed == '8') { if(man_y > -80 && man_x < 105) { status = 3; draw_status(3); } else { if (man_y > HORIZON + 20) { status = 2; draw_status(status); } else man_y = man_y + 2; } } if(key_pressed == '2') { if (man_y < -GP142_YMAX + 60) { status = 1; draw_status(status); } else man_y = man_y - 2; } } 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, &quit, boat_x, boat_y ,&on_boat, &man_x, &man_y); } 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); if( boat_x < -GP142_XMAX) direction = RIGHT; else if (boat_x>80) direction = LEFT; move_boat(&boat_x, direction); move_man(&on_boat, boat_x, boat_y, &man_x, &man_y); blink_star(&star_radius, &star_color); blink_star(&star2_radius, &star2_color); move_bug(&bug_x, &bug_y, &bug_size); t++; break; default: break; } /*end switch */ /* Redraw everything */ //clr_screen(); draw_landscape(moon_x, moon_y, on_boat, 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,status, boat_x, boat_y, bug_x, bug_y, bug_size, man_x , man_y); draw_ball(t); draw_buttons(); } /* 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. */ /* Initialize the boat parameters */ void init_boat(int *boat_x, int *boat_y) { *boat_x = GP142_XMAX / 3 - 20; *boat_y = HORIZON - 25; } void init_man(int *man_x , int *man_y) { *man_x = (- GP142_XMAX / 2) + 28; *man_y = -GP142_YMAX + 80; } /***************************************************************************** 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 on_boat) { 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); if (on_boat == TRUE){ boat_x = boat_x - 28; boat_y = boat_y + 24; draw_man(&boat_x, &boat_y); } } /* 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 on_boat, 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 status, int boat_x, int boat_y, int bug_x, int bug_y, int bug_size, int man_x, int man_y){ int i , n; 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 and the princess moving */ GP142_circleXY(WHITE, moon_x, moon_y, MOON_RADIUS); GP142_ovalXY(BLACK, moon_x-10, moon_y - MOON_RADIUS, moon_x + (int)(MOON_FULLNESS * MOON_RADIUS), moon_y + MOON_RADIUS, FILL); GP142_triangleXY(PINK, moon_x - 5 , moon_y-45, moon_x -20 + rand() % 5, moon_y-75, moon_x + 7 + rand() % 8, moon_y-75, FILL); GP142_rectangleXY(BLUE, moon_x - 12, moon_y - 38, moon_x + 2, moon_y-48, FILL); GP142_circleXY(GOLD, moon_x - 5,moon_y -35 , 5); GP142_lineXY(WHITE, moon_x, moon_y - 38, moon_x + 3, moon_y-25, FILL); GP142_lineXY(WHITE, moon_x - 10, moon_y -38 , moon_x - 13,moon_y -17, FILL); /* Draw stars */ for(n=0; n < MAX_STARS; n++){ draw_stars( star_color, star_x[n] ,star_y[n], star_radius); } 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(BROWN, -GP142_XMAX, HORIZON, GP142_XMAX, HORIZON, GP142_XMAX / 4, (2 * GP142_YMAX)/3, FILL); /* Draw castle */ draw_castle(); /* Draw meadow */ GP142_rectangleXY(SEA_GREEN, -GP142_XMAX, -GP142_YMAX, GP142_XMAX, HORIZON, FILL); /* Draw the mountains*/ 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); GP142_pixelXY (BLACK, GP142_XMAX - 300 , GP142_YMAX - 250); /* draw Flowers */ for ( i=0; i < MAX_FLOWERS ; i++){ draw_flower(flower_x[i], flower_y[i], petal_color, petal_radius, flower_radius, flower_color); } /* Draw lake */ GP142_ovalXY(BLUE, -GP142_XMAX - 100, HORIZON, GP142_XMAX / 3, HORIZON - 50, FILL); //draw_man(&man_x, &man_y); /* 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, on_boat); /* Draw the man */ if (on_boat == FALSE) { GP142_circleXY(GOLD, man_x + 13, man_y + 50 , 10); GP142_rectangleXY(BLACK, man_x, man_y +40 , man_x + 26, man_y - 25, FILL); GP142_rectangleXY(WHITE, man_x, man_y, man_x + 26, man_y - 25, FILL); GP142_lineXY(BLUE, man_x, man_y + 40, man_x - 10 , man_y + 8, 2); GP142_lineXY(BLUE, man_x + 26, man_y + 40, man_x + 36 , man_y + 8, 2); GP142_lineXY(BLACK, man_x + 13, man_y, man_x + 13, man_y - 25, FILL); } /* Draw buttefly */ draw_bug(bug_x, bug_y, bug_size); /* Draw the time bar */ GP142_rectangleXY(BLACK, -GP142_XMAX, -GP142_YMAX , GP142_XMAX, -GP142_YMAX + 40, FILL); draw_status(status); } /*end draw_landscape */ void draw_castle(){ GP142_rectangleXY(LT_GRAY, -GP142_XMAX + 250, GP142_YMAX -GP142_YMAX , GP142_XMAX - 250 , GP142_YMAX - 150, FILL); GP142_triangleXY (LT_GRAY, -GP142_XMAX + 275, GP142_YMAX - 150, -GP142_XMAX + 260, GP142_YMAX - 130, -GP142_XMAX + 290 , GP142_YMAX - 130, FILL); GP142_triangleXY (LT_GRAY, GP142_XMAX - 260, GP142_YMAX - 130, GP142_XMAX - 290 , GP142_YMAX - 130, GP142_XMAX -275, GP142_YMAX - 150, FILL); GP142_triangleXY (ICE_BLUE,-GP142_XMAX + 250, GP142_YMAX - 150, -GP142_XMAX + 275 , GP142_YMAX - 150, -GP142_XMAX + 262 , GP142_YMAX - 100, FILL); GP142_triangleXY (ICE_BLUE, GP142_XMAX - 250, GP142_YMAX - 150, GP142_XMAX - 275 , GP142_YMAX - 150, GP142_XMAX - 262 , GP142_YMAX - 100, FILL); GP142_triangleXY (ICE_BLUE,-GP142_XMAX + 275, GP142_YMAX - 150, GP142_XMAX - 275 , GP142_YMAX - 150, -GP142_XMAX + 300 , GP142_YMAX - 80, FILL); GP142_lineXY (DUSTY_PLUM,-GP142_XMAX + 275, GP142_YMAX - 150, -GP142_XMAX + 275, GP142_YMAX -GP142_YMAX , FILL); GP142_lineXY (DUSTY_PLUM,GP142_XMAX - 275, GP142_YMAX - 150, GP142_XMAX - 275, GP142_YMAX -GP142_YMAX , FILL); GP142_triangleXY (ICE_BLUE,-GP142_XMAX + 260, GP142_YMAX - 130, -GP142_XMAX + 290 , GP142_YMAX - 130, -GP142_XMAX + 273 , GP142_YMAX - 70, FILL); GP142_triangleXY (ICE_BLUE, GP142_XMAX - 260, GP142_YMAX - 130, GP142_XMAX - 290 , GP142_YMAX - 130, GP142_XMAX - 273 , GP142_YMAX - 70, FILL); GP142_lineXY (DUSTY_PLUM,-GP142_XMAX + 275, GP142_YMAX - 150, -GP142_XMAX + 262 , GP142_YMAX - 100 , FILL); GP142_lineXY (DUSTY_PLUM, GP142_XMAX - 275 , GP142_YMAX - 150, GP142_XMAX - 262 , GP142_YMAX - 100, FILL); GP142_lineXY (DUSTY_PLUM,-GP142_XMAX + 275, GP142_YMAX - 150, -GP142_XMAX + 300 , GP142_YMAX - 80 , FILL); GP142_lineXY (DUSTY_PLUM,GP142_XMAX - 275 , GP142_YMAX - 150, -GP142_XMAX + 300 , GP142_YMAX - 80, FILL); GP142_lineXY (DUSTY_PLUM,-GP142_XMAX + 262 , GP142_YMAX - 100,-GP142_XMAX + 262 , GP142_YMAX - 85, FILL); GP142_lineXY (DUSTY_PLUM,GP142_XMAX - 262 , GP142_YMAX - 100, GP142_XMAX - 262 , GP142_YMAX - 85, FILL); GP142_lineXY (DUSTY_PLUM,-GP142_XMAX + 300 , GP142_YMAX - 80, -GP142_XMAX + 300 , GP142_YMAX - 65, FILL); GP142_lineXY (DUSTY_PLUM,-GP142_XMAX + 273 , GP142_YMAX - 70, -GP142_XMAX + 273 , GP142_YMAX - 55, FILL); GP142_lineXY (DUSTY_PLUM, GP142_XMAX - 273 , GP142_YMAX - 70, GP142_XMAX - 273 , GP142_YMAX - 55, FILL); GP142_rectangleXY (DUSTY_PLUM, -GP142_XMAX + 283, GP142_YMAX - 250, GP142_XMAX - 283, GP142_YMAX- 180, FILL); GP142_lineXY (BLACK,GP142_XMAX - 300 , GP142_YMAX - 250, GP142_XMAX - 300 , GP142_YMAX - 180, FILL); } /* draws the instruction bar */ void draw_status(int status) { GP142_rectangleXY(BLACK, -300, -210, 300, -250, FILL); if (status == 0) GP142_printfXY(WHITE, -275, -235, 15,"Meet the Princess"); else { if (status == 1) GP142_printfXY(WHITE, -275, -240, 15, " U cannot go beyond this"); if (status == 2) GP142_printfXY(WHITE, -275, -240, 15, " U need more than just legs to climb the mountains"); if (status == 3) GP142_printfXY(WHITE, -275, -240, 15, " U need the boat to cross the lake"); } } /* draws the man */ void draw_man(int *man_x, int *man_y){ GP142_circleXY(GOLD, *man_x + 13, *man_y + 50 , 10); GP142_rectangleXY(BLACK, *man_x, *man_y +40 , *man_x + 26, *man_y - 25, FILL); GP142_rectangleXY(WHITE, *man_x, *man_y, *man_x + 26, *man_y - 25, FILL); GP142_lineXY(BLUE, *man_x, *man_y + 40, *man_x - 10 , *man_y + 8, 2); GP142_lineXY(BLUE, *man_x + 26, *man_y + 40, *man_x + 36 , *man_y + 8, 2); GP142_lineXY(BLACK, *man_x + 13, *man_y, *man_x + 13, *man_y - 25, FILL); } /***************************************************************************** BUTTONS AND MOUSE CLICKS *****************************************************************************/ /* Draws the buttons */ void draw_buttons() { 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(PINK, 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(PINK, text_offset, i, font_size, "Change Colors"); i -= BUTTON_HEIGHT; GP142_printfXY(PINK, text_offset, i, font_size, "Enter the Castle"); i -= BUTTON_HEIGHT; GP142_printfXY(PINK, text_offset, i, font_size, "Help"); i -= BUTTON_HEIGHT; GP142_printfXY(PINK, text_offset, i, font_size, "Hop in the Boat"); } /* 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 *quit, int boat_x, int boat_y,int *on_boat, int *man_x, int *man_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); break; case 1: enter_castle(quit); break; case 2: introduction(quit); break; case 3: *on_boat = TRUE; *man_x = boat_x; *man_y = boat_y; hop_boat(*man_x, *man_y); 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) --; } } void move_boat(int *boat_x ,int direction){ if (direction == LEFT) (*boat_x) --; else (*boat_x)++; } /* 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); } /* when the man enters the castle */ void enter_castle(int *quit){ int exit = FALSE; int mouse_x, mouse_y, event, num_buttons; char key_pressed ; int man_x, man_y, status= 0; num_buttons = 2; init_man(&man_x , &man_y); draw_room(); GP142_rectangleXY(BLACK, -GP142_XMAX, -GP142_YMAX , GP142_XMAX, -GP142_YMAX + 40, FILL); draw_buttons(num_buttons); draw_man(&man_x, &man_y ); draw_lady( man_x, man_y); draw_status(status); GP142_animate(ANI_RUN); *quit = FALSE; while (!(*quit) && !exit){ event = GP142_await_event(&mouse_x, &mouse_y, &key_pressed); switch(event){ case GP142_QUIT: *quit = TRUE; break; case GP142_KBD: if (key_pressed == '6') { if (man_x > GP142_XMAX - 30 ) { status = 1; draw_status(status); } else man_x = man_x + 2; } else { if(key_pressed == '4') { if (man_x < -GP142_XMAX + 20) { status = 1; draw_status(status); } else man_x = man_x - 2; } if(key_pressed == '8') { if (man_y > HORIZON + 20) { status = 1; draw_status(status); } else man_y = man_y + 2; } if(key_pressed == '2') { if (man_y < -GP142_YMAX + 60) { status = 1; draw_status(status); } else man_y = man_y - 2; } } break; case GP142_MOUSE: if ((( mouse_x >= -290) && (mouse_x <= - 230))&& ((mouse_y <= 170) && (mouse_y >=110))){ exit = TRUE; *quit = TRUE; } break; case GP142_PERIODIC: break; default: break; } draw_room(); draw_man(&man_x, &man_y); draw_lady(man_x, man_y); } } /* makes the man hop on the boat*/ void hop_boat(int boat_x, int boat_y){ int on_boat = TRUE; draw_boat( boat_x, boat_y ,on_boat); } /* makes the man move after he has hopped on the boat*/ void move_man(int *on_boat, int boat_x, int boat_y, int *man_x, int *man_y){ if (*on_boat == TRUE) { *man_x = boat_x - 28; *man_y = boat_y + 24 ; } } /* the introduction frame*/ void introduction(int* quit){ int mouse_x, mouse_y , event, exit; char key_pressed; exit = FALSE; *quit = FALSE; while (!(*quit) && !exit) { /* Get next event */ event = GP142_await_event(&mouse_x, &mouse_y, &key_pressed); switch (event) { case GP142_QUIT: *quit = TRUE; break; case GP142_MOUSE: exit = TRUE; break; default: introduction_scr(); break; } } } /***************************************************************************** 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!"); } void draw_flower(int flower_x, int flower_y, int petal_color,int petal_radius, int flower_radius, int flower_color){ 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); return; } void draw_stars(int star_color, int star_x ,int star_y, int star_radius){ 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); } void introduction_scr(){ int rad, i; rad = -GP142_YMAX + (rand() % 5); GP142_rectangleXY(BLACK, -GP142_XMAX, -GP142_YMAX, GP142_XMAX, GP142_YMAX, FILL); GP142_rectangleXY(SEA_GREEN, -300, 20 , 300, -250, FILL); GP142_rectangleXY(NAVY_BLUE, -300, 20, 300, 250 , FILL); draw_castle(); GP142_printfXY(PEACH, -270 , 200, 25, "Once upon a time there was an island where"); GP142_printfXY(PEACH, -270, 167, 25, "strange things happened. People were on the"); GP142_printfXY(PEACH, -270, 134,25, "moon and fishes played with balls. On this "); GP142_printfXY(PEACH, -270, 101,25, "island came a man and fell in love with the"); GP142_printfXY(PEACH, -270, 68,25, "princess. He wants to explore this island and meet"); GP142_printfXY(PEACH, -270, 35,25, "the princessHe can move to his right with '6', to "); GP142_printfXY(PURPLE, -270, 2,25, "his left with a '4', up with a '8' and down with a '2'"); GP142_printfXY(PURPLE, -270, -31,25, "HAVE FUN!"); GP142_printfXY(YELLOW, -270, -64,25, "CLICK TO BEGIN"); GP142_printfXY(WHITE, -125, -240, 22, "Click to enter"); for (i = 0 ; i < 16; i++) GP142_circleXY(ICE_BLUE, (-300 + (40 * i)) , rad , 40); } void draw_ball(int t){ int ball_x, ball_y; ball_x = -GP142_XMAX + 200; ball_y = -GP142_YMAX + 180; /* draw the ball*/ if (sin(t/3) > 0) GP142_circleXY(BLUE, (int)(100 * cos(t/3) + (ball_x)), (int)(100 * sin(t/3) + (ball_y)), 8); } void draw_room(){ GP142_rectangleXY(BLACK, -GP142_XMAX, -GP142_YMAX + 40, GP142_XMAX, GP142_YMAX, FILL); GP142_circleXY(GOLD,GP142_XMAX, GP142_YMAX , 200); GP142_circleXY(GOLD,-GP142_XMAX, GP142_YMAX , 200); GP142_lineXY(GOLD, 0, GP142_YMAX, 0, GP142_YMAX - 100, 2); GP142_circleXY(GOLD, 0, GP142_YMAX - 100 , 8); GP142_lineXY(GOLD, -20 , 80, -20, GP142_YMAX, 2); GP142_circleXY(GOLD, -20, 80, 8); GP142_lineXY(GOLD, 20, 115, 20, GP142_YMAX, 2); GP142_circleXY(GOLD, 20, 115,8); GP142_circleXY(RED, -260, 140, 30); GP142_printfXY(BLACK, -285, 131, 15, "QUIT"); } void draw_lady(int man_x, int man_y){ GP142_circleXY(GOLD, man_x + 88, man_y + 50 , 10); GP142_triangleXY(PINK, man_x + 88, man_y + 25, man_x + 68, man_y-25, man_x + 108, man_y - 25, FILL); GP142_rectangleXY(BLUE, man_x +73, man_y +40, man_x + 103, man_y + 10, FILL); GP142_lineXY(BLUE, man_x + 73,man_y + 40, man_x+63, man_y + 10, 2); GP142_lineXY(BLUE, man_x + 103, man_y + 40, man_x+113,man_y + 10, 2); }