void game_exit(void) { char choose; printf("\t\t\t:::::::::::::::::::\n"); printf("\t\t\t:: You are dead! ::\n"); printf("\t\t\t:::::::::::::::::::\n"); delay_time(1); while(1) { printf("\n\t\tYou wanna play other time? (y/n):"); scanf("%c", &choose); fflush(stdin); switch(choose) { case 'y': remove_garbage(); player_data(); game_start(); break; case 'n': printf("\n\t\tThanks for play the game!\n"); delay_time(1); clean_console(); exit(1); break; default: printf("\n\t\tPlease input a valid option! (y/n)\n"); break; } } }
static void playback_start (char *file) { char buffer[1024]; sprintf (buffer, "recordings/%s", file); playback_free (); if (record_load (buffer)) { playback_stop (); gamestate = GAME_AMODE; return; } /* if (playback_dump ()) { playback_stop (); gamestate = GAME_AMODE; return; } playback_dump2 (buffer); */ frame_counter = 0; rec_buffer_counter = 0; record_state = REC_PLAYING; input_mask = 0; fprintf (stdout, "Starting playback\n"); gamestate = GAME_RUNNING; game_start (); }
int main() { //start(); game_start(); end(); return 0; }
int main (int argc, char *argv[]) { game_t *game = game_create (); game_start (game); setup_board_every_gem_different (game); int **old_board = copy_board (game); int m = game->n_rows; int n = game->n_cols; replace_gem (game, 2, 2, 999999); replace_gem (game, 3, 2, 1999999); replace_gem (game, 4, 2, 999999); int i; for (i = 0; i < 50 * 2; ++i) { game_loop (game); } for (i = 0; i < m; ++i) { fail_if (game->board[3][i] == old_board[3][i]); } for (i = 0; i < n; ++i) { if (i == 3) continue; fail_if (game->board[i][1] != old_board[i][0]); fail_if (game->board[i][0] == old_board[i][0]); } setup_board_every_gem_different (game); old_board = copy_board (game); replace_gem (game, 2, 2, 999999); replace_gem (game, 2, 3, 1999999); replace_gem (game, 2, 4, 999999); for (i = 0; i < 50 * 2; ++i) { game_loop (game); } for (i = 0; i < m; ++i) { fail_if (game->board[2][i] == old_board[2][i]); } for (i = 0; i < n; ++i) { if (i == 2) continue; fail_if (game->board[i][1] != old_board[i][0]); fail_if (game->board[i][0] == old_board[i][0]); } game_destroy (game); return 0; }
void game_main() { game_start(); while(game_not_lost) { //Write data to SRAM to be used in OLED later } game_end(); }
static void window_load(Window *window) { Layer *window_layer = window_get_root_layer(window); GRect bounds = layer_get_bounds(window_layer); layer = layer_create(bounds); layer_add_child(window_layer, layer); game_init(); game_start(layer); }
void game_main(void) { game_introMessage(); while(!joy_read_right_button()) { _delay_us(10); } game_start(); while(!game_lost) { _delay_us(10); } oled_printf("Game lost!"); game_end(); }
/** * Enter game mode. If first time called the required game objects are * initialized. */ void game_enter(void) { clock_thaw(); /* prevent from reinitialisation after pause mode */ if (!game_initialized) { clock_init(); game_init(); game_initialized = 1; } game_start(); }
int game_init() { srand(get_ms()); screen = set_videomode(); init_res(); game_start(); /* Should not reach here! */ assert(0); return 0; }
//int main(int argc, char *argv[], char *envp[]) int main(int argc, char *argv[]) { puts("Starting morrigan."); unsigned short port = 0; if (1 < argc) { port = (unsigned short) atoi(argv[1]); if (!port) { port = PORT; } printf("Using port: %d.\n", port); } check(SIG_ERR != signal(SIGINT, __stop), "Failed to set signal handler.", ""); check(SIG_ERR != signal(SIGTERM, __stop), "Failed to set signal handler.", ""); srand((unsigned) (time(NULL) ^ _getpid())); l = landscape_load("land.dat", 32, 1.0); check(l, "Failed to load landscape.", ""); check(net_start(port), "Failed to start network interface.", ""); check(server_start(), "Failed to start server.", ""); check(game_start(l, server_get_clients()), "Failed to start game.", ""); do { printf(">"); input = bgets(fgetc, stdin, '\n'); if (NULL == input || 0 == strcmp("exit\n", bdata(input))) { break; } bdestroy(input); input = NULL; } while(true); __stop(0); return EXIT_SUCCESS; error: fprintf(stderr, "Error exit.\n"); __stop(0); return EXIT_FAILURE; }
bool app_run_editor_launch(char *err_str) { // launch directly into map server.state=gs_running; net_setup.mode=net_mode_none; if (!game_start(FALSE,skill_medium,0,0,err_str)) return(FALSE); if (!map_start(FALSE,TRUE,err_str)) { game_end(); return(FALSE); } return(TRUE); }
void select_single_click_handler(ClickRecognizerRef recognizer, void *context) { if (game_waiting_for_reaction && game_state == STATE_COUNTDOWN_GO) { game_player_reacted = true; app_timer_reschedule(game_timer, 1); } if (!game_waiting_for_reaction && game_state <= STATE_COUNTDOWN_GO && game_state > STATE_RESTING) { game_state = STATE_COUNTDOWN_GO; game_waiting_for_reaction = true; game_player_reacted = false; app_timer_reschedule(game_timer, 1); } if (game_state == STATE_SPLASH) { game_start(); } }
int main(void) { setup(); sei(); // Enable interrupts power_up_melody(); while(1) { display_mode(); if (input_button_pressed()) { _delay_ms(100); input_clear_button_pressed(); game_start(DEFAULT_STARTING_LENGTH); } input_clear_button_pressed(); } }
void main() { disable_interrupts(); DISPLAY_OFF; SPRITES_8x8; game_start(); SHOW_BKG; SHOW_SPRITES; DISPLAY_ON; enable_interrupts(); while(1) { wait_vbl_done(); joypad_state = joypad(); gameplay(joypad_state); } }
void GW_GameEngine_VTech::do_game_start(int mode) { data_hideall(true); score_=0; data().position_get((mode==MODE_GAMEA?PS_GAMEA:PS_GAMEB))->show(); score_update(); misses_=0; canmove_=true; gameover_=false; miss_update(); level_update(mode); game_start(mode); data_starttimer(TMR_GAMESTART); }
static PRESULT sudoku_newgame_callback(POBJECT_HEAD obj, VEVENT event, UINT32 param1, UINT32 param2) { PRESULT ret = PROC_PASS; UINT8 unact; switch (event) { case EVN_UNKNOWN_ACTION: unact = (VACTION)(param1>>16); if (unact == VACT_ENTER) { OSD_SetAttr((POBJECT_HEAD)obj, C_ATTR_INACTIVE); OSD_ChangeFocus((POBJECT_HEAD)&game_sudoku_con, 6, \ C_UPDATE_FOCUS | C_DRAW_SIGN_EVN_FLG); game_start(); } break; } return ret; }
gboolean start_timeout(gpointer data) { GameNetworkPlayerManager * manager; GameNetworkPlayer * game; manager = GAME_NETWORK_PLAYER_MANAGER(data); UiMain * ui_main = ui_main_get_instance(); PRIVATE(manager)->playing = TRUE; g_print("start_timeout\n"); monkey_canvas_clear( PRIVATE(manager)->canvas); monkey_canvas_paint( PRIVATE(manager)->canvas); game = game_network_player_new( PRIVATE(manager)->window, PRIVATE(manager)->canvas, PRIVATE(manager)->monkey, PRIVATE(manager)->handler, PRIVATE(manager)->client_id, PRIVATE(manager)->current_score); ui_main_set_game(ui_main,GAME( game)); monkey_canvas_paint( PRIVATE(manager)->canvas); game_start( GAME(game) ); game_network_player_set_start_time(game,PRIVATE(manager)->start_time); g_signal_connect( G_OBJECT(game), "state-changed", G_CALLBACK(state_changed),manager); PRIVATE(manager)->current_game = game; return FALSE; }
int main( int argc, char *argv[] ) { int ch; int ret; int dir_pre; int cnt = 0; pthread_t thrd; init_curses(); box( stdscr, ACS_VLINE, ACS_HLINE ); get_size(); game_start(); /* srand( (int)time(NULL) ); dir = rand() % 4 + 1; snake_len = 5; snake_head.x = rand() % width + 2; snake_head.y = rand() % length + 2; */ ret = pthread_create( &thrd, NULL, snake_move, NULL ); if ( ret != 0 ) { printw( "thrd create err" ); endwin(); exit(-1); } quit_flag = 0; while (1) { ch = getch(); switch (ch) { case KEY_UP: dir = 1; break; case KEY_DOWN: dir = 2; break; case KEY_RIGHT: dir = 3; break; case KEY_LEFT: dir = 4; break; case 27: /* ESC */ quit_flag = 1; break; case ' ': if ( !cnt ) { dir_pre = dir; dir = 0; } else { dir = dir_pre; } break; default: break; } if ( quit_flag ) { break; } } pthread_cancel(thrd); endwin(); return 0; }
bool game_file_load(char *file_name,char *err_str) { char *c,path[1024],fname[256]; file_save_header head; // load and expand strcpy(fname,file_name); c=strrchr(fname,'.'); if (c!=NULL) *c=0x0; // remove any extensions file_paths_documents(&setup.file_path_setup,path,"Saved Games",fname,"sav"); if (!game_file_expand_load(path,err_str)) return(FALSE); game_file_pos=0; progress_initialize("Loading"); // if game isn't running, then start if (!server.game_open) { if (!game_start(skill_medium,NULL,err_str)) { free(game_file_data); return(FALSE); } } // get header game_file_get_chunk(&head); // check version if (strcmp(head.version,dim3_version)!=0) { sprintf(err_str,"This saved game file is from a different version of dim3"); free(game_file_data); return(FALSE); } // reload map progress_draw(10); if ((!server.map_open) || (strcmp(head.map_name,map.info.name)!=0)) { // need to load a map? if (server.map_open) map_end(); strcpy(map.info.name,head.map_name); map.info.player_start_name[0]=0x0; map.info.player_start_type[0]=0x0; map.info.in_load=TRUE; if (!map_start(TRUE,err_str)) { free(game_file_data); return(FALSE); } } // timing game_time_set(head.tick); // view and server objects progress_draw(20); game_file_get_chunk(&view.time); game_file_get_chunk(&view.fps); game_file_get_chunk(&camera); game_file_get_chunk(&server.time); game_file_get_chunk(&server.player_obj_uid); game_file_get_chunk(&server.skill); game_file_get_chunk(&server.uid); game_file_get_chunk(&server.count); progress_draw(30); free(server.objs); free(server.weapons); free(server.proj_setups); server.objs=(obj_type*)game_file_replace_chunk(); server.weapons=(weapon_type*)game_file_replace_chunk(); server.proj_setups=(proj_setup_type*)game_file_replace_chunk(); if ((server.objs==NULL) || (server.weapons==NULL) || (server.proj_setups==NULL)) { free(game_file_data); return(FALSE); } progress_draw(40); game_file_get_chunk(server.projs); game_file_get_chunk(server.effects); game_file_get_chunk(server.decals); progress_draw(50); game_file_get_chunk(hud.bitmaps); game_file_get_chunk(hud.texts); game_file_get_chunk(hud.bars); game_file_get_chunk(&hud.radar); // map changes progress_draw(60); game_file_get_chunk(&map.ambient); game_file_get_chunk(&map.rain); game_file_get_chunk(&map.background); game_file_get_chunk(&map.sky); game_file_get_chunk(&map.fog); progress_draw(70); map_group_dispose_unit_list(&map); // need to destroy and rebuild unit lists game_file_get_chunk(map.groups); map_group_create_unit_list(&map); game_file_get_chunk(map.movements); group_moves_synch_with_load(); // script objects progress_draw(80); game_file_get_chunk(&js.script_current_uid); game_file_get_chunk(&js.count); game_file_get_chunk(&js.time); game_file_get_chunk(js.timers); game_file_get_chunk(js.globals); // reset model UIDs progress_draw(90); models_reset_uid(); // send scripts load event // to restore globals progress_draw(95); script_state_load(); // free game data progress_draw(100); free(game_file_data); // finished progress_shutdown(); // fix some necessary functions map.rain.reset=TRUE; fade_screen_cancel(); // return to old game time game_time_reset(); return(TRUE); }
int main(){ //variaveis que tem alguma coisa a ver com o jogados, como exp recebida e level int game_status = 0, option = 0, school = 0; //--------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------- //Variaveis internas relacionadas as bibliotecas usadas e erros do jogo em si. int status_allegro = 0, tecla = 0; //A variavel teclado tem 5 estados /* 0 - Neutro 1 - Tecla para cima 2 - Tecla para Baixo 3 - Tecla para Direita 4 - Tecla para Esquerda */ //iniciando o Allegro e todos as suas bibliotecas e funções usadas. al_init(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); al_install_keyboard(); //Iniciando o jogo. display = al_create_display(SCREEN_W, SCREEN_H); background = al_load_bitmap("img/background.jpg"); start_menu_img = al_load_bitmap("img/start_screen.jpg"); font_text = al_load_font("font/font.ttf", 50, 0); timer = al_create_timer(0.1); tcont = al_create_timer(1.0); sections_event = al_create_event_queue(); al_set_window_title(display, "M.I.P. - Matemática, Português e Inglês"); al_start_timer(timer); al_start_timer(tcont); //Qualquer valor que seja colocado apos a imagem, ira interferir na sua posição em relação a tela. fadeout(7, SCREEN_W, SCREEN_H, display); al_draw_bitmap(background, 0,0,0); al_flip_display(); //Verificando erros no allegro e suas bibliotecas. status_allegro = check_allegro(background, start_menu_img, sections_event, display, font_text, timer, tcont); if(status_allegro == -1){ close_game(background, start_menu_img, sections_event, display, timer, tcont); return 0; } al_register_event_source(sections_event, al_get_keyboard_event_source()); al_register_event_source(sections_event, al_get_display_event_source(display)); //--------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------- //menu_background(); fadeout(1, SCREEN_W, SCREEN_H, display); al_draw_bitmap(start_menu_img, 0,0,0); printf("Menu Principal\n"); //capture_event(); al_flip_display(); al_draw_textf(font_text, al_map_rgb(0, 255, 255), SCREEN_H / 4, SCREEN_W / 4, ALLEGRO_ALIGN_LEFT, "Novo Jogo"); al_draw_textf(font_text, al_map_rgb(0, 255, 255), SCREEN_H / (3.7), SCREEN_W / 3, ALLEGRO_ALIGN_LEFT, "Continuar"); al_draw_textf(font_text, al_map_rgb(0, 255, 255), SCREEN_H / (3.4), SCREEN_W / (2.5), ALLEGRO_ALIGN_LEFT, "Sair do jogo"); al_flip_display(); option = capture_event_queue(SCREEN_H, SCREEN_W, keyboard, display, sections_event, tecla, font_text); if(option == -1){ close_game(background, start_menu_img, sections_event, display, timer, tcont); return 0; } switch(option){ case 0: game_start(); printf("game_start\n"); break; case 1: game_load(); printf("game_load\n"); break; case 2: printf("Finalizando Jogo\n"); close_game(background, start_menu_img, sections_event, display, timer, tcont); return 0; break; } //--------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------- //Apos a escolha entre continuar ou começar novamente o usuário estará na tela de seleção de escolas printf("Selecione a escola\n"); scanf("%d", &school); switch(school){ case 0: open_math(); break; case 1: open_ing(); break; case 2: open_port(); break; } //--------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------- return 0; }
int main (int argc, char *argv[]) { game_t *game = game_create (); game_start (game); int **old_board = NULL; old_board = setup_board (game); game->board_cursor_x = 2; game->board_cursor_y = 4; game->board[3][4] = 999999; press_key_and_loop (game, SDLK_SPACE); fail_if (!game->cursor_locked); release_key_and_loop (game, SDLK_SPACE); press_key_and_loop (game, SDLK_RIGHT); release_key_and_loop (game, SDLK_RIGHT); wait (game, 50 * 2); fail_if (!success (old_board, game, 3, 4, 2, 4)); old_board = setup_board (game); game->board_cursor_x = 3; game->board_cursor_y = 4; game->board[3][4] = 999999; press_key_and_loop (game, SDLK_SPACE); fail_if (!game->cursor_locked); release_key_and_loop (game, SDLK_SPACE); press_key_and_loop (game, SDLK_LEFT); release_key_and_loop (game, SDLK_LEFT); wait (game, 50 * 2); fail_if (!success (old_board, game, 2, 4, 3, 4)); old_board = setup_board (game); game->board_cursor_x = 2; game->board_cursor_y = 4; game->board[2][5] = 999999; press_key_and_loop (game, SDLK_SPACE); fail_if (!game->cursor_locked); release_key_and_loop (game, SDLK_SPACE); press_key_and_loop (game, SDLK_DOWN); release_key_and_loop (game, SDLK_DOWN); wait (game, 50 * 2); fail_if (!success (old_board, game, 2, 5, 2, 4)); old_board = setup_board (game); game->board_cursor_x = 2; game->board_cursor_y = 5; game->board[2][5] = 999999; press_key_and_loop (game, SDLK_SPACE); fail_if (!game->cursor_locked); release_key_and_loop (game, SDLK_SPACE); press_key_and_loop (game, SDLK_UP); release_key_and_loop (game, SDLK_UP); wait (game, 50 * 2); fail_if (!success (old_board, game, 2, 4, 2, 5)); game_destroy (game); return 0; }
bool app_run_dedicated_host(char *err_str) { char str[256]; // launch directly into hosting // setup hosting flags and IPs host_game_setup(); net_host_game_setup(); net_setup.mode=net_mode_host; net_setup.client.latency=0; net_setup.client.host_addr.ip=0; net_setup.client.host_addr.port=0; net_create_project_hash(); // setup map map.info.name[0]=0x0; strcpy(map.info.host_name,setup.network.map_list.maps[net_setup.host.current_map_idx].name); // start game if (!game_start(FALSE,skill_medium,0,0,err_str)) { net_host_game_end(); return(FALSE); } // add any multiplayer bots if (!game_multiplayer_bots_create(err_str)) { game_end(); net_host_game_end(); return(FALSE); } // start the map if (!map_start(FALSE,TRUE,err_str)) { game_end(); net_host_game_end(); return(FALSE); } // start hosting if (!net_host_game_start(err_str)) { map_end(); game_end(); net_host_game_end(); return(FALSE); } // dedicated hosting, no local // player to add, only add // multiplayer bots to host net_host_join_multiplayer_bots(); // game is running sprintf(str,"Running on %s...",net_setup.host.ip_resolve); console_add(str); server.next_state=gs_running; return(TRUE); }
void menu_update(t_time t) { menu_render(); if (menu_estado==MENU_ST_MENU) { if (t<menu_pilgrim_tt) { float alfa=(menu_pilgrim_tt-t)/menu_pilgrim_tt; menu_pilgrim_x=alfa*menu_pilgrim_x0+(1-alfa)*menu_pilgrim_xf; menu_pilgrim_y=alfa*menu_pilgrim_y0+(1-alfa)*menu_pilgrim_yf; } else { menu_pilgrim_x=menu_pilgrim_xf; menu_pilgrim_y=menu_pilgrim_yf; } if (t<menu_title_tt) { float alfa=(menu_title_tt-t)/menu_title_tt; menu_title_x=alfa*menu_title_x0+(1-alfa)*menu_title_xf; menu_title_y=alfa*menu_title_y0+(1-alfa)*menu_title_yf; } else { menu_title_x=menu_title_xf; menu_title_y=menu_title_yf; } } else if (menu_estado==MENU_ST_CREDITS) { if (t<menu_credits_tt) { float alfa=(menu_credits_tt-t)/menu_credits_tt; menu_credits_x=alfa*menu_credits_x0+(1-alfa)*menu_credits_xf; menu_credits_y=alfa*menu_credits_y0+(1-alfa)*menu_credits_yf; } else { menu_credits_x=menu_credits_xf; menu_credits_y=menu_credits_yf; } } else if (menu_estado==MENU_ST_HOF) { if (t<menu_hof_tt) { float alfa=(menu_hof_tt-t)/menu_hof_tt; menu_hof_x=alfa*menu_hof_x0+(1-alfa)*menu_hof_xf; menu_hof_y=alfa*menu_hof_y0+(1-alfa)*menu_hof_yf; } else { menu_hof_x=menu_hof_xf; menu_hof_y=menu_hof_yf; } } if (menu_estado==MENU_ST_MENU) { if ( game_hit_pressed ) { if (persistence_total_played_games<MAX_GAMES_AUTO_SHOW_HELP) { menu_help(1); } else { game_start(); } } else if (game_stop_pressed) { menu_help(0); } else if (game_esquivar_pressed) { menu_hof(); } else if (game_salto_pressed) { menu_credits(); } else if (game_other_pressed) { audio_switch_deactivate(); } } else if (menu_estado==MENU_ST_CREDITS || menu_estado==MENU_ST_HOF) { if ( game_other_pressed ) { if (menu_estado==MENU_ST_HOF) { audio_stop_music(); audio_start_music(AUDIO_MUSIC_MENU); } menu_init(); } if ( game_other2_pressed ) { if (menu_estado==MENU_ST_HOF) { // if (gamecenter_enter_hof_or_achievments==0) { // game_enter_gamecenter_hof=1; // gamecenter_enter_hof_or_achievments=1; // } else { // game_enter_gamecenter_achievments=1; // gamecenter_enter_hof_or_achievments=0; // } game_enter_gamecenter_hof=1; } } if (game_stop_pressed) { if (menu_estado==MENU_ST_HOF) { game_enter_gamecenter_achievments=1; } } #if PILGRIM_INCLUDE_WEBLINK if (menu_estado==MENU_ST_CREDITS && game_other2_pressed) { game_jump_to_weblink=1; } #endif } else if (menu_estado==MENU_ST_RESULTS) { if ( game_other_pressed ) { if (game_highscore_pos!=-1) { menu_enter_hof(); } else { menu_init(); } } } else if (menu_estado==MENU_ST_HELP || menu_estado==MENU_ST_HELPSTART) { if (game_other_pressed) { menu_help_page+=1; if (menu_help_page==3) { if (menu_estado==MENU_ST_HELPSTART) { game_start(); } else { menu_init(); } } } if (game_other2_pressed) { menu_help_page-=1; if (menu_help_page==-1) { menu_init(); } } } else if (menu_estado==MENU_ST_ENTERHOF) { if (game_highscore_name_entered) { game_highscore_name_entered=0; //highscore_print(); //printf("nuevo record de %s\n",game_highscore_name); highscore_insert_name_pos(game_highscore_name, game_last_score, game_highscore_pos); //highscore_print(); menu_hof(); } } }
int main (int argc, char *argv[]) { game_t *game = game_create (); game_start (game); int n = game->n_rows; int m = game->n_cols; int i; int j; int k; setup_board_every_gem_different (game); int **old_board = copy_board (game); game->n_gem_types = 1000000; replace_gem (game, 0, n-1, 999999); replace_gem (game, 1, n-1, 999999); replace_gem (game, 2, n-1, 999999); replace_gem (game, m-1, n-1, 999999); replace_gem (game, m-2, n-1, 999999); replace_gem (game, m-3, n-1, 999999); bool old_ones_dropped_1 = false; bool old_ones_dropped_2 = false; game_loop_n (game, 100); old_ones_dropped_1 = true; for (k = 0; k < 3; ++k) { for (j = 1; j < n; ++j) { if (game->board[k][j] != old_board[k][j-1]) { old_ones_dropped_1 = false; } } } old_ones_dropped_2 = true; for (k = m-3; k < m; ++k) { for (j = 1; j < n; ++j) { if (game->board[k][j] != old_board[k][j-1]) { old_ones_dropped_1 = false; } } } fail_if (!old_ones_dropped_1 || !old_ones_dropped_2); bool new_gems_1 = false; bool new_gems_2 = false; if (game->board[0][0] != old_board[0][0] || game->board[1][0] != old_board[1][0] || game->board[2][0] != old_board[2][0]) { new_gems_1 = true; } if (game->board[m-3][0] != old_board[m-3][0] || game->board[m-2][0] != old_board[m-2][0] || game->board[m-1][0] != old_board[m-1][0]) { new_gems_2 = true; } fail_if (!new_gems_1 || !new_gems_2); game_destroy (game); return 0; }
int main (void) { uint8_t game_ticks; uint8_t game_over_ticks; state_t state = STATE_INIT; flasher_t flashers[SPACEY_PIX_TYPE_NUM]; uint8_t flasher_state[SPACEY_PIX_TYPE_NUM]; flasher_obj_t flashers_info[SPACEY_PIX_TYPE_NUM]; uint8_t display[TINYGL_WIDTH * TINYGL_HEIGHT]; uint8_t i; uint8_t j; game_data_t data; char message[44]; ir_serial_ret_t ret; uint8_t ir_data; system_init (); /* The first time EEPROM is read all the bytes are 0xFF so set to sensible defaults. */ eeprom_read (0, &data, sizeof (data)); if (data.level == 0xff) { data.level = 0; data.games = 0; } for (i = 0; i < ARRAY_SIZE (flashers); i++) { flashers[i] = flasher_init (&flashers_info[i]); flasher_state[i] = 0; } for (i = 0; i < ARRAY_SIZE (display); i++) display[i] = 0; /* Set up flash patterns for different pixel types. */ flasher_pattern_set (flashers[SPACEY_PIX_GUN], &flasher_patterns[FLASH_MODE_GUN]); flasher_pattern_set (flashers[SPACEY_PIX_SHELL], &flasher_patterns[FLASH_MODE_SHELL]); flasher_pattern_set (flashers[SPACEY_PIX_ALIEN], &flasher_patterns[FLASH_MODE_ALIEN]); tinygl_init (LOOP_RATE); tinygl_font_set (&font3x5_1); tinygl_text_mode_set (TINYGL_TEXT_MODE_SCROLL); tinygl_text_dir_set (TINYGL_TEXT_DIR_ROTATE); tinygl_text_speed_set (10); spacey_init (GAME_UPDATE_RATE, TINYGL_WIDTH, TINYGL_HEIGHT, display_handler, display); spacey_event_handler_set (game_event, 0); game_ticks = 0; game_over_ticks = 0; pacer_init (LOOP_RATE); while (1) { pacer_wait (); if (state == STATE_PLAYING) { uint8_t *src; /* Update flasher states. NB, the first flasher is always off. */ for (i = 1; i < ARRAY_SIZE (flashers); i++) flasher_state[i] = flasher_update (flashers[i]); /* Update display. */ src = display; for (j = 0; j < TINYGL_HEIGHT; j++) for (i = 0; i < TINYGL_WIDTH; i++) { tinygl_point_t point = {i, j}; tinygl_draw_point (point, flasher_state[*src++]); } } /* Advance messages and refresh display. */ tinygl_update (); game_ticks++; if (game_ticks >= LOOP_RATE / GAME_UPDATE_RATE) { game_ticks = 0; switch (state) { case STATE_PLAYING: if (! spacey_update ()) { game_over_display (message); game_over_ticks = 0; state = STATE_OVER; } break; case STATE_INIT: tinygl_text ("SPACEY READY V" VERSION " BY MPH "); state = STATE_READY; break; case STATE_OVER: game_over_ticks ++; if (game_over_ticks >= GAME_UPDATE_RATE * GAME_OVER_PERIOD) state = STATE_READY; /* Fall through. */ case STATE_READY: case STATE_MENU_LEVEL: default: break; case STATE_START: /* Turn that bloody blimey space invader off... */ game_start (&data); state = STATE_PLAYING; break; } } ret = ir_serial_receive (&ir_data); if (ret == IR_SERIAL_OK) { if (ir_data == 1) { switch (state) { case STATE_READY: state = STATE_MENU_LEVEL; game_level_display (data.level, message); break; case STATE_MENU_LEVEL: state = STATE_INIT; break; case STATE_PLAYING: spacey_gun_move_right (); break; default: break; } } if (ir_data == 2) { switch (state) { case STATE_READY: state = STATE_START; break; case STATE_PLAYING: spacey_gun_fire (); break; case STATE_MENU_LEVEL: data.level++; if (data.level > GAME_LEVEL_MAX) data.level = 0; game_level_display (data.level, message); break; default: break; } } } } }
static void on_player_vs_ai_click(gui_control* target, gui_control* window, gui_size x, gui_size y) { game_set_players(PLAYER_HUMAN, PLAYER_AI); game_start(window); }
static void window_appear(Window *window) { game_start(); }
//---------------------------------------------------------------------------------------------------------------------- // process_commands //---------------------------------------------------------------------------------------------------------------------- static void process_commands(voglperf_data_t &data) { static const char *s_commands[] = { "game start [steamid | filename]: Start game.", "game stop: Send SIGTERM signal to game.", "game set (steamid | filename): Set gameid to launch.", "game args: set game arguments.", "logfile start [seconds]: Start capturing frame time data to filename.", "logfile stop: Stop capturing frame time data.", "status: Print status and options.", "quit: Quit voglperfrun.", }; for (size_t i = 0; i < data.commands.size(); i++) { bool handled = false; std::string ws_reply; std::vector<std::string> args; std::string &command = data.commands[i]; printf("> %s\n", command.c_str()); string_split(args, command, " "); bool on = (args[1] == "on" || args[1] == "1"); bool off = (args[1] == "off" || args[1] == "0"); if (on && (data.run_data.pid != (uint64_t)-1) && (args[0] == "logfile")) { // Special case someone typing "logfile on" while the game is running. // Turn it into a "logfile start" command. args[1] = "start"; } else if (!args[1].size() || on || off) { unsigned int flags_orig = data.flags; for (size_t j = 0; j < sizeof(g_options) / sizeof(g_options[0]); j++) { if (args[0] == g_options[j].name) { if (on) data.flags |= g_options[j].flag; else if(off) data.flags &= ~g_options[j].flag; ws_reply += string_format("%s: %s\n", g_options[j].name, (data.flags & g_options[j].flag) ? "On" : "Off"); // This is a launch option and the game is already running - warn them. if (on && g_options[j].launch_setting && (data.run_data.pid != (uint64_t)-1)) ws_reply += " Option used with next game launch...\n"; handled = true; } } if (data.run_data.pid != (uint64_t)-1) { // If the verbose or fpsshow args have changed, send msg. if ((flags_orig ^ data.flags) & (F_VERBOSE | F_FPSSHOW)) { mbuf_options_t mbuf; mbuf.mtype = MSGTYPE_OPTIONS; mbuf.fpsshow = !!(data.flags & F_FPSSHOW); mbuf.verbose = !!(data.flags & F_VERBOSE); int ret = msgsnd(data.msqid, &mbuf, sizeof(mbuf) - sizeof(mbuf.mtype), IPC_NOWAIT); if (ret == -1) { ws_reply += string_format("ERROR: msgsnd failed: %s\n", strerror(errno)); } } } } if (handled) { // Handled with g_options above... } else if (args[0] == "status") { ws_reply += get_vogl_status_str(data); handled = true; } else if (args[0] == "help") { ws_reply += "Commands:\n"; for (size_t j = 0; j < sizeof(s_commands) / sizeof(s_commands[0]); j++) ws_reply += string_format(" %s\n", s_commands[j]); for (size_t j = 0; j < sizeof(g_options) / sizeof(g_options[0]); j++) ws_reply += string_format(" %s [on | off]: %s\n", g_options[j].name, g_options[i].desc); handled = true; } else if (args[0] == "quit" || args[0] == "q" || args[0] == "exit") { data.flags |= F_QUIT; ws_reply += "Quitting...\n"; handled = true; } else if (args[0] == "game") { if (args[1] == "args") { size_t pos = command.find("args") + 5; data.game_args = command.substr(pos); handled = true; } else if ((args[1] == "set") && args[2].size()) { data.gameid = args[2]; ws_reply += "Gameid set to '" + data.gameid + "'"; handled = true; } else if (args[1] == "start") { if (args[2].size()) data.gameid = args[2]; game_start(data); handled = true; } else if (args[1] == "stop") { game_stop(data); handled = true; } } else if (args[0] == "logfile") { if (data.run_data.pid == (uint64_t)-1) { ws_reply += "ERROR: Game not running.\n"; handled = true; } else if (args[1] == "start") { mbuf_logfile_start_t mbuf; std::string logfile = get_logfile_name(data.run_data.game_name); mbuf.mtype = MSGTYPE_LOGFILE_START; strncpy(mbuf.logfile, logfile.c_str(), sizeof(mbuf.logfile)); mbuf.logfile[sizeof(mbuf.logfile) - 1] = 0; mbuf.time = (uint64_t)atoi(args[2].c_str()); int ret = msgsnd(data.msqid, &mbuf, sizeof(mbuf) - sizeof(mbuf.mtype), IPC_NOWAIT); if (ret == -1) { ws_reply += string_format("ERROR: msgsnd failed: %s\n", strerror(errno)); } handled = true; } else if (args[1] == "stop") { mbuf_logfile_stop_t mbuf; mbuf.mtype = MSGTYPE_LOGFILE_STOP; mbuf.logfile[0] = 0; int ret = msgsnd(data.msqid, &mbuf, sizeof(mbuf) - sizeof(mbuf.mtype), IPC_NOWAIT); if (ret == -1) { ws_reply += string_format("ERROR: msgsnd failed: %s\n", strerror(errno)); } handled = true; } } if (!handled) { ws_reply += string_format("ERROR: Unknown command '%s'.\n", command.c_str()); } if (ws_reply.size()) { webby_ws_write_buffer(NULL, ws_reply.c_str(), ws_reply.size()); } } data.commands.clear(); }
gboolean play_game(gpointer data) { int i; if (playing == FALSE) { //如果游戏还没开始,初始化游戏并开始 init_game(); set_label(); playing = TRUE; return TRUE; } snake_run(); switch (snake.direction) { case UP: snake.body[0].y = snake.body[0].y - 1; //判断是否有向自己身体走的情况,如果有就改变方向 //头部[0]和身体[2]的坐标(x或y)相同,就需要向反方向改变 if (snake.body[0].y == snake.body[2].y) { snake.direction = DOWN; snake.body[0].y = snake.body[0].y + 2; } break; case DOWN: snake.body[0].y = snake.body[0].y + 1; if (snake.body[0].y == snake.body[2].y) { snake.direction = UP; snake.body[0].y = snake.body[0].y - 2; } break; case LEFT: snake.body[0].x = snake.body[0].x - 1; if (snake.body[0].x == snake.body[2].x) { snake.direction = RIGHT; snake.body[0].x = snake.body[0].x + 2; } break; case RIGHT: snake.body[0].x = snake.body[0].x + 1; if (snake.body[0].x == snake.body[2].x) { snake.direction = LEFT; snake.body[0].x = snake.body[0].x - 2; } break; default: break; } //判断蛇的头部是否碰到了自己的身体,如果碰到了就去死! for (i = 4; i <= snake.length; i++) if (snake.body[0].x == snake.body[i].x && snake.body[0].y == snake.body[i].y) snake.live = FALSE; //判断蛇是否碰到边界,如果碰到了就去死! if (snake.body[0].x < 0 || snake.body[0].y < 0 || snake.body[0].x > 40 || snake.body[0].y > 40) { snake.live = FALSE; } //判断蛇是否死了 if (she.live == FALSE) { live = live - 1; if (live == 0) { //没命就GAME OVER speed = 200; level = 1; foods = 20; live = 3; game_stop(); gtk_label_set_text(GTK_LABEL(label), " - Game Over -"); return FALSE; } //还有命就继续 set_label(); init_game(); } //判断蛇是否吃到了蛋,如果吃到了就变长! if (snake.body[0].x == food.x && snake.body[0].y == food.y) { snake.length = snake.length + 1; snake.body[snake.length].x = snake.body[snake.length - 1].x; snake.body[snake.length].y = snake.body[snake.length - 1].y; //重新生成一个蛋 food.x = my_rand(0, 20); food.y = food.x; foods = foods - 1; //减少一个蛋 if (foods == 0) { //如果吃完全部的蛋,进入下一局,速度加快 level = level + 1; foods = 20; if (speed > 30) speed = speed - 20; game_stop(); game_start(); } set_label(); } draw(); return TRUE; }
int main (int argc, char *argv[]) { game_t *game = game_create (game); game_start (game); // Swapping to or from outside of the board is illegal fail_if (session_legal_move (game, 0, 0, -1, 0)); fail_if (session_legal_move (game, 0, 0, 0, -1)); fail_if (session_legal_move (game, game->n_cols - 1, 0, game->n_cols, 0)); fail_if (session_legal_move (game, game->n_cols - 1, 0, game->n_cols - 1, -1)); fail_if (session_legal_move (game, 0, game->n_rows - 1, 0, game->n_rows)); fail_if (session_legal_move (game, 0, game->n_rows - 1, -1, game->n_rows - 1)); fail_if (session_legal_move (game, game->n_cols - 1, game->n_rows - 1, game->n_cols - 1, game->n_rows)); fail_if (session_legal_move (game, game->n_cols - 1, game->n_rows - 1, game->n_cols, game->n_rows - 1)); // Swapping non-adjacent locations is illegal fail_if (session_legal_move (game, 0, 0, 1, 1)); fail_if (session_legal_move (game, 1, 1, 0, 0)); fail_if (session_legal_move (game, 3, 2, 0, 1)); fail_if (session_legal_move (game, 5, 4, 2, 2)); // Swap and neither location is part of row or col of 3 or more game->n_gem_types = 1000000; setup_board_every_gem_different (game); // Since every location is different, there can be no legal moves int i, j; for (i = 0; i < game->n_cols; ++i) { for (j = 0; j < game->n_rows; ++j) { fail_if (session_legal_move (game, i, j, i - 1, j)); fail_if (session_legal_move (game, i, j, i, j - 1)); fail_if (session_legal_move (game, i, j, i + 1, j)); fail_if (session_legal_move (game, i, j, i, j + 1)); } } // Swap into a row of three // On bottom setup_board_every_gem_different (game); game->board[2][2] = 999999; game->board[2][3] = 999999; game->board[2][4] = 0; game->board[3][4] = 999999; fail_if (!session_legal_move (game, 2, 4, 3, 4)); fail_if (!session_legal_move (game, 3, 4, 2, 4)); // In middle setup_board_every_gem_different (game); game->board[2][2] = 999999; game->board[2][3] = 0; game->board[2][4] = 1999999; game->board[3][3] = 999999; fail_if (!session_legal_move (game, 2, 3, 3, 3)); fail_if (!session_legal_move (game, 3, 3, 2, 3)); // On top setup_board_every_gem_different (game); game->board[2][2] = 0; game->board[2][3] = 999999; game->board[2][4] = 2999999; game->board[3][2] = 999999; fail_if (!session_legal_move (game, 2, 2, 3, 2)); fail_if (!session_legal_move (game, 3, 2, 2, 2)); // Swap into a col of three // On right setup_board_every_gem_different (game); game->board[2][2] = 999999; game->board[3][2] = 3999999; game->board[4][2] = 0; game->board[4][3] = 999999; fail_if (!session_legal_move (game, 4, 2, 4, 3)); fail_if (!session_legal_move (game, 4, 3, 4, 2)); // In middle setup_board_every_gem_different (game); game->board[2][2] = 1999999; game->board[3][2] = 0; game->board[4][2] = 999999; game->board[3][3] = 999999; fail_if (!session_legal_move (game, 3, 2, 3, 3)); fail_if (!session_legal_move (game, 3, 3, 3, 2)); // On left setup_board_every_gem_different (game); game->board[2][2] = 0; game->board[3][2] = 999999; game->board[4][2] = 999999; game->board[2][3] = 2999999; fail_if (!session_legal_move (game, 2, 2, 2, 3)); fail_if (!session_legal_move (game, 2, 3, 2, 2)); // Swapping a special1 with a special1 setup_board_every_gem_different (game); game->board[2][2] = 1999999; game->board[2][3] = 1999999; fail_if (!session_legal_move (game, 2, 2, 2, 3)); fail_if (!session_legal_move (game, 2, 3, 2, 2)); setup_board_every_gem_different (game); game->board[2][2] = 1999999; game->board[3][2] = 1999999; fail_if (!session_legal_move (game, 2, 2, 3, 2)); fail_if (!session_legal_move (game, 3, 2, 2, 2)); // Swapping a special1 with a special2 setup_board_every_gem_different (game); game->board[2][2] = 1999999; game->board[2][3] = 2999999; fail_if (!session_legal_move (game, 2, 2, 2, 3)); fail_if (!session_legal_move (game, 2, 3, 2, 2)); setup_board_every_gem_different (game); game->board[2][2] = 1999999; game->board[3][2] = 2999999; fail_if (!session_legal_move (game, 2, 2, 3, 2)); fail_if (!session_legal_move (game, 3, 2, 2, 2)); // Swapping a special2 with a special2 setup_board_every_gem_different (game); game->board[2][2] = 2999999; game->board[2][3] = 2999999; fail_if (!session_legal_move (game, 2, 2, 2, 3)); fail_if (!session_legal_move (game, 2, 3, 2, 2)); setup_board_every_gem_different (game); game->board[2][2] = 2999999; game->board[3][2] = 2999999; fail_if (!session_legal_move (game, 2, 2, 3, 2)); fail_if (!session_legal_move (game, 3, 2, 2, 2)); // Swapping a special1 with a special3 setup_board_every_gem_different (game); game->board[2][2] = 1999999; game->board[2][3] = 3999999; fail_if (!session_legal_move (game, 2, 2, 2, 3)); fail_if (!session_legal_move (game, 2, 3, 2, 2)); setup_board_every_gem_different (game); game->board[2][2] = 1999999; game->board[3][2] = 3999999; fail_if (!session_legal_move (game, 2, 2, 3, 2)); fail_if (!session_legal_move (game, 3, 2, 2, 2)); // Swapping a special2 with a special3 setup_board_every_gem_different (game); game->board[2][2] = 2999999; game->board[2][3] = 3999999; fail_if (!session_legal_move (game, 2, 2, 2, 3)); fail_if (!session_legal_move (game, 2, 3, 2, 2)); setup_board_every_gem_different (game); game->board[2][2] = 2999999; game->board[3][2] = 3999999; fail_if (!session_legal_move (game, 2, 2, 3, 2)); fail_if (!session_legal_move (game, 3, 2, 2, 2)); // Swapping a special3 with a special3 setup_board_every_gem_different (game); game->board[2][2] = 3999999; game->board[2][3] = 3999999; fail_if (!session_legal_move (game, 2, 2, 2, 3)); fail_if (!session_legal_move (game, 2, 3, 2, 2)); setup_board_every_gem_different (game); game->board[2][2] = 3999999; game->board[3][2] = 3999999; fail_if (!session_legal_move (game, 2, 2, 3, 2)); fail_if (!session_legal_move (game, 3, 2, 2, 2)); game_destroy (game); return 0; }