// // CSE142, Summer 2000 // Homework 4 // "Hunt the Wumpus" Starter Code // #include "gp142display.h" #include "gp142.h" // For many of the GP142 functions, a line width of // zero means to fill the shape being drawn. The // constant FillWidth is used in function calls when a solid // shape is desired. const int FillWidth = 0; // Sizes of menu and menu items // Top-left coordinates of menu const int MenuTop = (GP142_YMAX - 5); const int MenuLeft = (-GP142_XMAX + 5); // Size of shapes drawn in menu and margin inside menu // Height and width of menu items const int MenuItemHeight = 30; const int MenuItemWidth = 70; const int MenuItemXMargin = 5; const int MenuItemYMargin = -20; // Number of menu items const int NumMenuItems = 2; // Possible kinds of mouse clicks enum MouseClickKind { MenuClick, // click inside the menu NewShapeClick // click outside the menu }; //**************************************************************************** // // BUTTONS AND MOUSE CLICKS // //**************************************************************************** // Draw the menu with shapes void draw_shape_menu(GP142Display &display) { // draw rectangles for each menu item for(int i = 0; i < NumMenuItems; i++) { display.drawRectangle(MenuLeft, MenuTop - i * MenuItemHeight, MenuLeft + MenuItemWidth, MenuTop - (i + 1) * MenuItemHeight, Black, 1); } // draw shapes for each menu item int offsetX = MenuLeft; int offsetY = MenuTop; display.write(offsetX+MenuItemXMargin, offsetY + MenuItemYMargin, "New Game", Black,12); offsetY = offsetY - MenuItemHeight + MenuItemYMargin; display.write(offsetX + MenuItemXMargin, offsetY, "Quit Game", Black,12); } // Classify mouse click at location mouse_x, mouse_y and return its kind MouseClickKind classify_mouse_click(int mouse_x, int mouse_y) { if ((mouse_x > MenuLeft) && (mouse_x < MenuLeft + MenuItemWidth) && (mouse_y < MenuTop) && (mouse_y > (MenuTop - NumMenuItems * MenuItemHeight))) return MenuClick; else return NewShapeClick; } // Process menu item click at location x, y void handle_menu_item(int x, int y) { // number of menu item (counting from top), converted from the y coordinate int menuItemNum = (MenuTop - y)/MenuItemHeight; switch(menuItemNum) { case 0: //Your code for starting a new game here.. break; case 1: //Your code for exiting the game here..Be sure to deallocate memory as needed. exit(0); break; default: break; } } void handleKeyPress(char key_pressed) { switch(key_pressed) { //Your code for handling keyboard interaction here default: break; } } //**************************************************************************** // // MAIN // //**************************************************************************** // * Main program contents: // * - Major program variables that persist throughout execution // * - Event loop that processes GP142 events, // * and updates variables and redraws the window as needed. until the user quits // int main() { // Event handling and menu items: bool quit = false; // = "user has selected quit" char key_pressed; // last keyboard character from key event int mouse_x, mouse_y; // x and y coordinates of latest mouse event MouseClickKind click_kind; // kind of mouse click (menu item or not) GP142Display display; // our GP142 window display.clear (White); display.setAnimation (Run); draw_shape_menu(display); // * 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) { // perform appropriate action depending on kind of event switch (display.getNextEvent(mouse_x, mouse_y, key_pressed)) { case Quit: // Quit selected quit = true; break; case Mouse: // Mouse click. Determine if in a menu menu item // or elsewhere and process accordingly. click_kind = classify_mouse_click(mouse_x, mouse_y); if (click_kind == MenuClick) { handle_menu_item(mouse_x, mouse_y); } else { // mouse click outside the menu // insert appropriate code here if needed } break; case Key: handleKeyPress(key_pressed); break; default: // all other events we do not need to process break; } // The code for redrawing the screen goes here...just the menu for now draw_shape_menu(display); } return 0; }