void handle_game_over(void) { cancel_software_timer(foodTimerNum); cancel_software_timer(ratsTimerNum); splash_screen(); show_instruction(GAMEOVER); new_game(); }
void new_game(void) { char c = 0; cancel_software_timer(foodTimerNum); cancel_software_timer(ratsTimerNum); empty_display(); //wait for space while(c != ' ' && c != 'l' && c != 'L'){ if(input_available()) c = fgetc(stdin); if(c == 'M' || c == 'm'){ toggle_sound(); display_sound_status(); c = 0; } } init_display(); if(c == 'l' || c == 'L'){ if(load_state()) render_board(); else { /* Initialise internal representations. */ init_board(); init_score(); } } else { /* Initialise internal representations. */ init_board(); init_score(); } clear_terminal(); //Place scores update_score(); //place sound status display_sound_status(); show_instruction(PLAYING); /* Make food blink 5 times a second. We should call blink_food ** 10 times a second which is 100ms */ foodTimerNum = execute_function_periodically(BLINKRATE, blink_food); ratsTimerNum = execute_function_periodically(RATSPEED, move_rats); /* Debug * move_cursor(0, TITLEY-1); printf_P(PSTR("new_game %u"), get_score()); wait_for(1000); //*/ }
void pause_game() { /* ** status = 0, game is running ** status = 1, game is paused */ static uint8_t status = 0; char c = 0; if(status) { move_cursor(28, TITLEY); clear_to_end_of_line(); show_instruction(PLAYING); render_board(); foodTimerNum = execute_function_periodically(BLINKRATE, blink_food); ratsTimerNum = execute_function_periodically(RATSPEED, move_rats); status = 0; } else { show_instruction(PAUSE); cancel_software_timer(foodTimerNum); cancel_software_timer(ratsTimerNum); empty_display(); status = 1; while(c != 'p' && c != 'P'){ if(input_available()) c = fgetc(stdin); if(c == 's' || c == 'S'){ save_state(); //move_cursor(0, TITLEY); //clear_to_end_of_line(); move_cursor(28, TITLEY); printf_P(PSTR("State has been saved.")); c = 0; } } pause_game(); } }
/* * main -- Main program. */ int main(void) { uint8_t chars_into_escape_sequence = 0; int8_t moveStatus = 0; char c; /* Initialise our main clock */ init_timer(); /* Initialise serial I/O */ init_serial_stdio(19200, 0); /* Make the display_row() function be called every 2ms. ** (This function returns a timer number, but we ignore ** this since we'll never do anything with it.) */ execute_function_periodically(2, display_row); /* Register the time_increment() function to be called every 500ms. ** This function just sets a variable (timePassedFlag). */ mainTimerNum = execute_function_periodically(500, time_increment); //4209435 /* setup AVR to handle sounds*/ init_sound(); /* ** Turn on interrupts (needed for timer and serial input/output to work) */ sei(); /* ** Display splash screen */ splash_screen(); show_instruction(NEWGAME); /* ** Perform necessary initialisations for a new game. */ new_game(); /* ** Event loop - wait for a certain amount of time to pass or wait ** for a character to arrive from standard input. The time_passed_flag ** is set within the function time_increment() below - which is setup ** to be called periodically. */ for(;;) { if(timePassedFlag) { moveStatus = move_snake(); timePassedFlag = 0; } else if(input_available()) { /* Read the input from our terminal and handle it */ c = fgetc(stdin); if(chars_into_escape_sequence == 0 && c == '\x1b') { /* ** Received ESCAPE character - we're one character into ** an escape sequence */ chars_into_escape_sequence = 1; } else if(chars_into_escape_sequence == 1 && c == '[') { /* ** We're now two characters into an escape sequence */ chars_into_escape_sequence = 2; } else if (chars_into_escape_sequence == 2) { /* We're two characters into an escape sequence and ** have received another - see if it is as expected. */ if (c == 'C') { /* Cursor right key pressed - Set next direction to ** be moved to RIGHT */ set_snake_dirn(RIGHT); } if (c == 'D') { /* Cursor left key pressed - Set next direction to ** be moved to LEFT */ set_snake_dirn(LEFT); } if (c == 'A') { /* Cursor up key pressed - Set next direction to ** be moved to UP */ set_snake_dirn(UP); } if (c == 'B') { /* Cursor down key pressed - Set next direction to ** be moved to DOWN */ set_snake_dirn(DOWN); } /* else, unknown escape sequence */ /* We're no longer part way through an escape sequence */ chars_into_escape_sequence = 0; } else if(chars_into_escape_sequence != 0) { /* ** We started an escape sequence but didn't get a character ** we recognised - discard it and assume that we're not ** in an escape sequence. */ chars_into_escape_sequence = 0; } else if (c == ' ') { /* Space character received - move snake immediately */ moveStatus = move_snake(); } else { if(c == 'N' || c == 'n'){ show_instruction(NEWGAME); new_game(); } else if(c == 'P' || c == 'p'){ moveStatus = 0; pause_game(); } else if(c == 'M' || c == 'm'){ toggle_sound(); display_sound_status(); } } } switch(moveStatus){ case ATE_FOOD: if(sound_status()) play_sound(); moveStatus = MOVE_OK; break; } if(moveStatus < 0) { /* Move failed - game over */ handle_game_over(); moveStatus = 0; update_score(); } } }
int main(int argc, char *argv[]) { WINDOW *my_win, *score; WINDOW **car; game newGame, tmpGame; MEVENT event; int ch = 0, choosenCar = -1, soluce_move = 0; char message[1024]; bool quit = false; bool show_solution = false; gameStruct *resultSolv = NULL; //INIT setup(); //END INIT //Wait for good size wait_for_size(MINH, MINW); //Instruction show_instruction(MINH, MINW); while (!quit) { quit = false; show_solution = false; soluce_move = 0; strcpy(message, "Playing"); //Select game newGame = select_game(); //Check for level file if (handle_level(&tmpGame)) { delete_game(newGame); newGame = tmpGame; MAXCOL = game_width(newGame); MAXROW = game_height(newGame); MINH = MAXROW * SIZE + 2; MINW = MAXCOL * SIZE; } car = malloc(sizeof (WINDOW*) * game_nb_pieces(newGame)); //First draw draw_game(newGame, 0, 0, MAXROW, MAXCOL, &my_win, car, &score, choosenCar, message, gameOverRh); //Loop while the game is not finished while (!game_over(newGame)) { //Print on bottom of grid mvprintw(MAXROW * SIZE + 1, 0, "Please choose car :"); ch = getch(); if (ch == 's' && !show_solution) { show_solution = true; strcpy(message, "Solution"); resultSolv = solv(newGame, gameOverRh,true); if(!resultSolv){ strcpy(message, "No solution"); show_solution=false; } } if (show_solution) { newGame = play_solution(newGame, resultSolv, soluce_move++); } else { if (KEY_MOUSE == ch) { /* Mouse event. */ if (OK == getmouse(&event)) { choosenCar = get_car_with_mouse(event.y, event.x, car, game_nb_pieces(newGame)); } } else { if (ch == 'q') { quit = true; break; } play_input(newGame, ch, &choosenCar); } } wait_for_size(MINH, MINW); erase_game(newGame, my_win, car, score); draw_game(newGame, 0, 0, MAXROW, MAXCOL, &my_win, car, &score, choosenCar, message, gameOverRh); } if (!quit) { display_score(newGame); } for (int i = 0; i < game_nb_pieces(newGame); i++) { destroy_win(car[i]); } destroy_win(my_win); destroy_win(score); free(car); delete_game(newGame); if (resultSolv != NULL) { delete_game(resultSolv->current); free(resultSolv->move); free(resultSolv); resultSolv = NULL; } } endwin(); /* End curses mode */ return 0; }