int main() { init_curses(); const char *exit_msg = ""; srandom(time(NULL)); struct game_t game = {0}; int last_turn = game.turns; get_highscore_filepath(&game); load_highscore(&game); place_tile(&game); place_tile(&game); while (1) { print_game(&game); if (lose_game(game)) { exit_msg = "lost"; goto lose; } last_turn = game.turns; switch (getch()) { case 'h': case KEY_LEFT: case 'a': move_left(&game); break; case 'j': case KEY_DOWN: case 's': move_down(&game); break; case 'k': case KEY_UP: case 'w': move_up(&game); break; case 'l': case KEY_RIGHT: case 'd': move_right(&game);break; case 'q': exit_msg = "quit"; goto end; } if (last_turn != game.turns) place_tile(&game); } lose: move(7, 0); printw("You lose! Press q to quit."); while (getch() != 'q'); end: endwin(); if(game.score > game.highscore) { game.highscore = game.score; save_highscore(&game); } printf("You %s after scoring %d points in %d turns, " "with largest tile %d. The local highscore is %d points.\n", exit_msg, game.score, game.turns, 1 << max_tile((tile_t *)game.board), game.highscore); free(game.highscorefile); return 0; }
// This is the task that deals with making everything interactive AsyncTask::DoneStatus World::roll(GenericAsyncTask* taskPtr) { // Standard technique for finding the amount of time since the last frame double dt = taskPtr->get_elapsed_time() - m_last; m_last = taskPtr->get_elapsed_time(); // If dt is large, then there has been a # hiccup that could cause the ball // to leave the field if this functions runs, so ignore the frame if(dt > 0.2) { return AsyncTask::DS_cont; } // The collision handler collects the collisions. We dispatch which function // to handle the collision based on the name of what was collided into for(int i = 0; i < m_cHandlerPtr->get_num_entries(); ++i) { PT(CollisionEntry) entryPtr = m_cHandlerPtr->get_entry(i); const string& name = entryPtr->get_into_node()->get_name(); if(name == "wall_collide") { wall_collide_handler(*entryPtr); } else if(name == "ground_collide") { ground_collide_handler(*entryPtr); } else if(name == "loseTrigger") { lose_game(*entryPtr); } } // Read the mouse position and tilt the maze accordingly PT(MouseWatcher) mouseWatcherPtr = DCAST(MouseWatcher, m_windowFrameworkPtr->get_mouse().node()); if(mouseWatcherPtr->has_mouse()) { // get the mouse position const LPoint2f& mpos = mouseWatcherPtr->get_mouse(); m_mazeNp.set_p(mpos.get_y() * -10); m_mazeNp.set_r(mpos.get_x() * 10); } // Finally, we move the ball // Update the velocity based on acceleration m_ballV += m_accelV * dt * ACCEL; // Clamp the velocity to the maximum speed if(m_ballV.length_squared() > MAX_SPEED_SQ) { m_ballV.normalize(); m_ballV *= MAX_SPEED; } // Update the position based on the velocity m_ballRootNp.set_pos(m_ballRootNp.get_pos() + (m_ballV * dt)); // This block of code rotates the ball. It uses something called a quaternion // to rotate the ball around an arbitrary axis. That axis perpendicular to // the balls rotation, and the amount has to do with the size of the ball // This is multiplied on the previous rotation to incrementally turn it. LRotationf prevRot(m_ballNp.get_quat()); LVector3f axis = UP.cross(m_ballV); LRotationf newRot(axis, 45.5 * dt * m_ballV.length()); m_ballNp.set_quat(prevRot * newRot); // Continue the task indefinitely return AsyncTask::DS_cont; }
int main() { init_curses(); const char *exit_msg = ""; srand(time(NULL)); struct game_t game = {0}; int last_turn = game.turns; place_tile(&game); place_tile(&game); while (1) { print_game(&game); if (lose_game(game)) { exit_msg = "lost"; goto lose; } last_turn = game.turns; switch (getch()) { case 'h': case KEY_LEFT: move_left(&game); break; case 'j': case KEY_DOWN: move_down(&game); break; case 'k': case KEY_UP: move_up(&game); break; case 'l': case KEY_RIGHT: move_right(&game); break; case 'q': exit_msg = "quit"; goto end; } if (last_turn != game.turns) place_tile(&game); } lose: move(7, 0); printw("You lose! Press q to quit."); while (getch() != 'q'); end: endwin(); printf("You %s after scoring %d points in %d turns, " "with largest tile %d\n", exit_msg, game.score, game.turns, 1 << max_tile((tile_t *)game.board)); return 0; }
int main() { init_curses(); const char *exit_msg = ""; struct game_t game = { 0 }; int last_turn = game.turns; place_tile(&game); place_tile(&game); gotoxy(0, 7); printf("Press ENTER to quit."); int cur_key; while (1) { print_game(&game); if (lose_game(game)) { exit_msg = "lost"; goto lose; } last_turn = game.turns; switch (cur_key = keyb_getkey(1)) { case 22: move_left(&game); break; case 38: move_down(&game); break; case 6: move_up(&game); break; case 54: move_right(&game); break; case 96: exit_msg = "quit"; goto end; } if (last_turn != game.turns) place_tile(&game); } lose: gotoxy(0, 7); printf("You lose! Press ENTER to quit."); while (keyb_getkey(1) != 96); end: gotoxy(0, 7); printf("You %s after scoring %d points in %d turns, " "with largest tile %d\n", exit_msg, game.score, game.turns, 1 << max_tile((tile_t *)game.board)); sys_sleep(3000); return 0; }