static void key(void) { uint8_t c; moveto(0,0); switch (c = getkey()) { case 'Q': if (notifier("quit")) exit(0); break; case 'R' & 31: redraw_all(); break; case 'R': if (notifier("restart")) start_level(); break; case 'U': undo(); break; case 'H': case 'J': case 'K': case 'L': move(c - 'H'); break; } /* TODO arrow key handling */ }
void start_game(void) { current_level = 0; global_snake.lives = 3; global_snake.score = 0; game_speed = start_speed; start_level(current_level); draw_score(); do_frame(1); }
static void play_level(void) { start_level(); while (map.done != map.blocks) key(); moveto(0, 0); strcpy(str, "Level "); strcpy(str + 6, _uitoa(level)); strcat(str + 6, " done in "); strcat(str + 6, _uitoa(moves)); strcat(str + 6, " moves."); write(1, str, strlen(str)); getkey(); }
int main(int argc, char *argv[]) { uint16_t n; struct passwd *pw; const char *path = "/usr/lib/sok/sok.levels"; if (argc > 2 && strcmp(argv[1], "-l") == 0) { level = atoi(argv[2]); argc -= 2; argv += 2; } if (argc == 2) path = argv[1]; if (argc > 2) { write(2, "sok [-l level] [path to levels].\n", 21); exit(1); } level_fd = open(path, O_RDONLY); if (level_fd == -1) { perror(path); exit(1); } if (level == 0) { level = 1; pw = getpwuid(getuid()); if (pw && chdir(pw->pw_dir) == 0) { conf_fd = open(".sokoban", O_RDWR | O_CREAT, 0600); if (conf_fd != -1) { if (read(conf_fd, &n, 2) == 2) level = n; } } } tty_init(); /* Level '0' is actually not a level but a title screen */ load_level(0); start_level(); getkey(); play_game(); }
static int initialize_and_run(bool launch_editor) { shared_editor_state_t *editor_state = new shared_editor_state_t; std::shared_ptr<warp::transition_i> start_level(new level_transition_t); std::shared_ptr<warp::transition_i> start_editor (new enter_editor_transition_t(editor_state)); std::shared_ptr<warp::transition_i> edit_region (new edit_region_transition_t(editor_state)); //std::shared_ptr<warp::transition_i> end_level(new gameover_transition_t); warp::statemanager_t states = { "level", "editor-region-sel", "editor-region", "editor-level" }; states.insert_transition(START_STATE, "level", start_level, false); states.insert_transition("level", "level", start_level, false); states.insert_transition(START_STATE, "editor-region-sel", start_editor, false); states.insert_transition("editor-region-sel", "editor-region", edit_region, false); //states.insert_transition("level", END_STATE, end_level, false); warp::game_config_t config; fill_default_config(&config); config.max_entites_count = 512; config.window_name = APP_NAME; config.first_state = launch_editor ? "editor-region-sel" : "level"; warp::game_t *game = new warp::game_t; warp_result_t init_result = game->initialize(config, states); if (WARP_FAILED(init_result)) { warp_result_log("Faile to initialize game", &init_result); warp_result_destory(&init_result); return 1; } int exit_code = game->run(); /* on iOS the game continues to operate after main has finished */ if (game->is_alive_after_main() == false) { delete game; } return exit_code; }
static int nxsnake_handle_event(GR_EVENT *event) { switch(event->type) { case GR_EVENT_TYPE_KEY_DOWN: /* Allow m to quit no matter where we are*/ if (event->keystroke.ch == 'm') { //GrClose(); fin = 1; pz_close_window(offscreen); pz_close_window(swindow); GrDestroyTimer(nxsnake_timer); //exit(0); } switch(game_state) { case SNAKE_START: if (event->keystroke.ch == 'd') { game_state = SNAKE_START; draw_string((char *) instructions, 1); show_buffer(); break; } case SNAKE_INSTRUCTIONS: case SNAKE_DONE: switch(event->keystroke.ch) { default: start_game(); break; } break; case SNAKE_PAUSED: if ((event->keystroke.ch == '\r') || (event->keystroke.ch == 'd')) { draw_score(); do_frame(1); game_state = SNAKE_PLAYING; } break; case SNAKE_NEXTLEVEL: if (current_level >= LEVELCOUNT) current_level = 0; start_level(current_level); draw_score(); do_frame(1); /* and show the first frame */ break; case SNAKE_PLAYING: if (event->keystroke.ch == 'd') { game_state = SNAKE_PAUSED; draw_string((char *) instructions, 1); show_buffer(); break; } if (event->keystroke.ch == '\r') { game_state = SNAKE_PAUSED; draw_string((char *) gamepaused, 2); show_buffer(); break; } if ((event->keystroke.ch == 'w') || (event->keystroke.ch == 'f')) { if (redirect_snake(event->keystroke)) do_snake_advance(); } if ((event->keystroke.ch == 'l') || (event->keystroke.ch == 'r')) { if(last_keystroke == event->keystroke.ch) { if (count_wheel > 7) { if (redirect_snake(event->keystroke)) do_snake_advance(); count_wheel = 0; } else count_wheel ++; } else count_wheel = 0; } last_keystroke = event->keystroke.ch; break; } break; case GR_EVENT_TYPE_EXPOSURE: show_buffer(); break; } return(1); }