void StageState::handle_events(Game* game, sf::Event event) { if(event.type == sf::Event::KeyPressed) { switch(event.key.code) { case sf::Keyboard::Left: case sf::Keyboard::A: { move_selected(-1); }break; case sf::Keyboard::Right: case sf::Keyboard::D: { move_selected(1); }break; case sf::Keyboard::Return: { send_to_level(game); music.stop(); game->change_state(GameState::instance()); } break; case sf::Keyboard::Escape: { music.stop(); game->change_state(IntroState::instance()); }break; default: break; } } }
struct Game* options_menu(WINDOW* screen, struct Game* game, struct Ball* ball, struct Paddle* paddle, int max_x, int max_y) { clear(); refresh(); move_ball_xy(ball, ball->x, max_y / 2); char* opts_menu_l[8] = {"Difficulty", "Sound", "Paddle Sensitivity",\ "Screen Size", "Left Control", "Right Control",\ "Play to", "Save & Return"}; struct Menu* opts_menu = new_menu(max_x / 8, 8, 8, opts_menu_l, BLUE_ON_BLACK, GREEN_ON_BLACK); WINDOW* info_win = newwin(3, max_x, max_y - 3, 0); Timer* timer = sgl_timer_new(); char* tmp_str = malloc(sizeof(char) * MAX_STRING_LENGTH); int key; while (key != 'q'){ switch (key = getch()){ case KEY_DOWN: move_selected(opts_menu, DOWN); wclear(info_win); break; case KEY_UP: wclear(info_win); move_selected(opts_menu, UP); break; case KEY_LEFT: switch (opts_menu->selected){ case 0: //Difficulty if (game->difficulty == 0) game->difficulty = 2; else game->difficulty--; break; case 1: //Sound if (game->sound) game->sound = 0; else game->sound = 1; break; case 2: //Paddle sensitivity if (game->sensitivity == 1) game->sensitivity = 4;//DEHARDCODE THE 4 else game->sensitivity--; break; case 4: //Left control selection... game->p1_aictrl = (game->p1_aictrl ? 0 : 1); break; case 5: //Right control selection game->p2_aictrl = (game->p2_aictrl ? 0 : 1); break; case 6: //Change maximum score... game->max_score--; if (game->max_score < 1) game->max_score = MAX_SCORE; break; } break; case KEY_RIGHT: case KEY_ENTER: switch (opts_menu->selected){ case 0: //Difficulty if (game->difficulty == 2) game->difficulty = 0; else game->difficulty++; break; case 1: //Sound if (game->sound) game->sound = 0; else game->sound = 1; break; case 2: //Paddle sensitivity if (game->sensitivity == 4) game->sensitivity = 1; else game->sensitivity++; break; case 3: //Change Resolution change_resolution(screen, game); break; case 4: //Left control selection... if (game->p1_aictrl) game->p1_aictrl = 0; else game->p1_aictrl = 1; break; case 5: //Right control selection if (game->p2_aictrl) game->p2_aictrl = 0; else game->p2_aictrl = 1; break; case 6: //Change maximum score... game->max_score++; if (game->max_score > MAX_SCORE) game->max_score = 1; break; case 7: //Quit return game; break; } break; } //Move and bound ball if (sgl_timer_elapsed_milliseconds(timer) > 20){ sgl_timer_reset(timer); update_background(ball, paddle, max_x, max_y - 4); draw_background(screen, ball, paddle); box(info_win, 0, 0); } draw_strings(screen, 1, max_x / 2 - strlen(options_title[0]), options_title, 6); draw_menu(opts_menu); /************************************************************ * NOTE: The spaces in strings below are there for a purpose! * They erase double digits. eg. "1 " erases "10" */ snprintf(tmp_str, MAX_STRING_LENGTH, "%d ", game->difficulty + 1);//+1 so we don't get a difficulty of '0' mvwaddstr(screen, opts_menu->y, opts_menu->x + opts_menu->width + 1, tmp_str);//print difficulty mvwaddstr(screen, opts_menu->y + 1, opts_menu->x + opts_menu->width + 1, game->sound ? "On " : "Off");//print sound snprintf(tmp_str, MAX_STRING_LENGTH, "%d ", game->sensitivity); mvwaddstr(screen, opts_menu->y + 2, opts_menu->x + opts_menu->width + 1, tmp_str);//print sensitivity snprintf(tmp_str, MAX_STRING_LENGTH, "%dx%d", game->width, game->height); //Print the resolution mvwaddstr(screen, opts_menu->y + 3, opts_menu->x + opts_menu->width + 1, tmp_str); mvwaddstr(screen, opts_menu->y + 4, opts_menu->x + opts_menu->width + 1, game->p1_aictrl ? "Computer" : "Human "); mvwaddstr(screen, opts_menu->y + 5, opts_menu->x + opts_menu->width + 1, game->p2_aictrl ? "Computer" : "Human "); snprintf(tmp_str, MAX_STRING_LENGTH, "%d ", game->max_score); mvwaddstr(screen, opts_menu->y + 6, opts_menu->x + opts_menu->width + 1, tmp_str); //Write a helpful hint in the info_win switch (opts_menu->selected){ case 0: mvwaddstr(info_win, 1, 1, "Change the difficulty, higher numbers are more difficult"); break; case 1: mvwaddstr(info_win, 1, 1, "Toggle sound on or off"); break; case 2: mvwaddstr(info_win, 1, 1, "Adjust paddle sensitivity, higher numbers are less sensitive"); break; case 3: mvwaddstr(info_win, 1, 1, "Change the playing screen size"); break; case 4: mvwaddstr(info_win, 1, 1, "Toggle control between Computer and Human for the left paddle"); break; case 5: mvwaddstr(info_win, 1, 1, "Toggle control between Computer and Human for the right paddle"); break; case 6: mvwaddstr(info_win, 1, 1, "Change the maximum score needed to win"); break; case 7: mvwaddstr(info_win, 1, 1, "Save changes and go back to main menu"); break; } wrefresh(opts_menu->win); wrefresh(info_win); } clear(); refresh(); return game; }
int main_menu(WINDOW* screen) { int max_x, max_y; getmaxyx(screen, max_y, max_x); char* title_menu_l[4] = {"Play!", "Options", "Help", "Quit"}; struct Menu* title_menu = new_menu(max_x / 8, 8, 4, title_menu_l, BLUE_ON_BLACK, GREEN_ON_BLACK); //For the moving background... struct Ball* ball = make_ball(max_x / 2, max_y / 4, HARD_BALL_VX, HARD_BALL_VY); struct Paddle* paddle = make_paddle(0, max_y / 2, 5, 2); Timer* timer = sgl_timer_new(); //Default settings... int difficulty = EASY; int sound = SOUND_OFF; char* random_name = ai_names[randint(ai_names_c)]; struct Game* game = make_game(max_x, max_y, difficulty, sound, PADDLE_SENSITIVITY, random_name, getenv("USER"), 1, 0, DEFAULT_SCORE); int key; while (key != 'q'){ switch (key = getch()){ case KEY_DOWN: move_selected(title_menu, DOWN); break; case KEY_UP: move_selected(title_menu, UP); break; case KEY_RIGHT: case KEY_ENTER: switch (title_menu->selected){ case 0: //Play game play_game(game); break; case 1: //Options game = options_menu(screen, game, ball, paddle, max_x, max_y); clear(); break; case 2: //Help help_menu(screen, ball, paddle, max_x, max_y); break; case 3: //Quit return 0; break; } break; } //Render the background if (sgl_timer_elapsed_milliseconds(timer) > 20){ sgl_timer_reset(timer); update_background(ball, paddle, max_x, max_y); draw_background(screen, ball, paddle); refresh(); } draw_strings(screen, 1, max_x / 2 - strlen(game_title[0]), game_title, 6); draw_menu(title_menu); wrefresh(title_menu->win); } clear(); refresh(); return 0; }