📄 tetris.c
字号:
return 1;}void tetris_clear(){ int i; if ( config.same_blocks_for_all && !config.expert && config.gametype >= 3 ) { next_blocks_size = 0; free( next_blocks ); } for ( i = 0; i < BOWL_COUNT; i++ ) { if ( bowls[i] ) bowl_delete( bowls[i] ); bowls[i] = 0; } /* shrapnells */ shrapnells_delete();}/*====================================================================Run a successfully initated game.====================================================================*/void tetris_run(){ SDL_Event event; int leave = 0; int i; int fps_delay = 0; int ms; int request_pause = 0; int game_over = 0; int bkgnd_level = 0; int bowl_count; /* number of active bowls */ char sshot_str[128]; int screenshot_id = 0; int gain_multiplayer_bonus = 0; int escape = 0; /* count number of bowls */ bowl_count = 0; for ( i = 0; i < BOWL_COUNT; i++ ) if ( bowls[i] ) bowl_count++; SDL_ShowCursor( 0 ); /* delay */ switch ( config.fps ) { case 1: fps_delay = 20; break; case 2: fps_delay = 10; break; case 3: fps_delay = 5; break; } /* main loop */ fade_screen( FADE_IN, FADE_DEF_TIME ); reset_timer(); event_reset(); while ( !leave && !term_game ) { if ( config.fps ) SDL_Delay( fps_delay ); if ( event_poll( &event ) ) { switch ( event.type ) { case SDL_QUIT: term_game = 1; break; case SDL_KEYDOWN: for ( i = 0; i < BOWL_COUNT; i++ ) if ( bowls[i] ) bowl_store_key( bowls[i], event.key.keysym.sym ); break; case SDL_KEYUP: switch ( event.key.keysym.sym ) { case SDLK_ESCAPE: case SDLK_RETURN: if ( game_over ) { leave = 1; break; } escape = 1; if ( confirm( large_font, "End Game? y/n", CONFIRM_YES_NO ) ) for ( i = 0; i < BOWL_COUNT; i++ ) if ( bowls[i] && !bowls[i]->game_over ) bowl_finish_game( bowls[i] ); break; case SDLK_p: request_pause = 1; break; case SDLK_f: /* switch fullscreen */ config.fullscreen = !config.fullscreen; set_video_mode( std_video_mode( config.fullscreen ) ); FULL_DEST( sdl.screen ); FULL_SOURCE( offscreen ); blit_surf(); refresh_screen( 0, 0, 0, 0 ); case SDLK_TAB: sprintf( sshot_str, "ss%i.bmp", screenshot_id++ ); SDL_SaveBMP( sdl.screen, sshot_str ); break; default: break; } break; default: break; } } ms = get_time(); for ( i = 0; i < BOWL_COUNT; i++ ) if ( bowls[i] ) bowl_hide( bowls[i] ); shrapnells_hide(); if ( request_pause && !game_over ) { for ( i = 0; i < BOWL_COUNT; i++ ) if ( bowls[i] ) bowl_toggle_pause( bowls[i] ); request_pause = 0; } for ( i = 0; i < BOWL_COUNT; i++ ) if ( bowls[i] ) bowl_update( bowls[i], ms, game_over ); /* check if any of the bowls entered a new level and change background if so */ if ( !config.keep_bkgnd ) { for ( i = 0; i < BOWL_COUNT; i++ ) if ( bowls[i] ) if ( bowls[i]->level > bkgnd_level ) { bkgnd_level = bowls[i]->level; /* recreate background */ tetris_recreate_bkgnd( -1 ); refresh_screen( 0, 0, 0, 0 ); reset_timer(); } } shrapnells_update( ms ); for ( i = 0; i < BOWL_COUNT; i++ ) if ( bowls[i] ) bowl_show( bowls[i] ); shrapnells_show(); refresh_rects(); /* check if game's over */ if ( !game_over ) { /* count number of finished bowls */ game_over = 0; for ( i = 0; i < BOWL_COUNT; i++ ) if ( bowls[i] && bowls[i]->game_over ) game_over++; if ( ( game_over == 1 && bowl_count == 1 ) || ( bowl_count > 1 && bowl_count - game_over <= 1 ) ) game_over = 1; else game_over = 0; if ( game_over && bowl_count > 1 && !escape ) gain_multiplayer_bonus = 1; } /* the last bowl in multiplayer gains additional 100,000 score */ if ( gain_multiplayer_bonus ) { gain_multiplayer_bonus = 0; for ( i = 0; i < BOWL_COUNT; i++ ) if ( bowls[i] && !bowls[i]->game_over ) counter_add( &bowls[i]->score, 50000 ); } } fade_screen( FADE_OUT, FADE_DEF_TIME ); /* highscore entries */ chart_clear_new_entries(); for ( i = 0; i < BOWL_COUNT; i++ ) if ( bowls[i] ) chart_add( chart_set_query( chart_map[config.gametype] ), bowls[i]->name, bowls[i]->level, counter_get( bowls[i]->score ) ); SDL_ShowCursor( 1 );}/*====================================================================Run a number of CPU games to get an average score gained so you'llsee if your analyze algorithm in cpu.c cpu_analyze_bowl() sucksor rocks!====================================================================*/extern int CPU_SCORE_HOLE;extern int CPU_SCORE_ALT;extern int CPU_SCORE_LINE;extern int CPU_SCORE_STEEP;extern int CPU_SCORE_ABYSS;extern int CPU_SCORE_BLOCK;extern int count_occ( int *array, int size, int min, int max ){ int i, count; count = 0; for ( i = 0; i < size; i++ ) if ( array[i] >= min && array[i] <= max ) count++; return count;}void tetris_make_stat(){ int i; int game_count = 50; double total = 0; int total_lines = 0; double scores[1024]; int lines[1024]; Bowl *bowl = 0; SDL_Event event; int leave = 0; FILE *file = 0; printf( "*****\n" ); bowl = bowl_create( 0, 0, -1, -1, blocks, qmark, "Demo", 0 ); /* reset counters */ total = 0; total_lines = 0; memset( lines, 0, sizeof( int ) * 1024 ); memset( scores, 0, sizeof( int ) * 1024 ); printf( "Computing: %3i %3i %3i %3i %3i %3i\n", CPU_SCORE_HOLE, CPU_SCORE_ALT, CPU_SCORE_LINE, CPU_SCORE_STEEP, CPU_SCORE_ABYSS, CPU_SCORE_BLOCK ); for ( i = 0; i < game_count; i++ ) { if ( SDL_PollEvent( &event ) && event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE ) leave = 1; bowl_quick_game( bowl, 1 ); lines[i] = bowl->lines; total_lines += lines[i]; scores[i] = bowl->score.value; total += scores[i]; printf( "%3i: %5i: %14.0f\n", i, lines[i], scores[i] ); if ( leave ) return; } if ( i != game_count ) game_count = i; if ( game_count <= 0 ) return; /* write to file */ file = fopen( "stats", "a+" ); fprintf( file, "SETTING: %3i %3i %3i %3i %3i %3i\n", CPU_SCORE_HOLE, CPU_SCORE_ALT, CPU_SCORE_LINE, CPU_SCORE_STEEP, CPU_SCORE_ABYSS, CPU_SCORE_BLOCK ); for (i = 0; i < game_count;i++) fprintf( file, "%3i: %5i: %14.0f\n", i, lines[i], scores[i] ); fprintf( file, "-----\n" ); fprintf( file, " 0-100: %4i 101-200: %4i 201-400: %4i\n", count_occ( lines, game_count, 0, 100 ), count_occ( lines, game_count, 101, 200 ), count_occ( lines, game_count, 201, 400 ) ); fprintf( file, "401-600: %4i 601-800: %4i rest: %4i\n", count_occ( lines, game_count, 401, 600 ), count_occ( lines, game_count, 601, 800 ), count_occ( lines, game_count, 801, 100000 ) ); fprintf( file, "-----\n" ); fprintf( file, "Avg.Lines: %i Avg.Score: %i\n", total_lines / game_count, (int)(total / game_count) ); fprintf( file, "\n" ); fclose( file ); bowl_delete( bowl );}#ifdef _1void tetris_make_stat(){ int i; int game_count = 10; double total = 0; int total_lines = 0; int scores[1024]; int lines[1024]; Bowl *bowl = 0; int ms = 4; SDL_Event event; int leave = 0; char str[256]; font->align = ALIGN_X_LEFT | ALIGN_Y_TOP; tetris_recreate_bkgnd( 0 ); DEST( sdl.screen, 300, 0, 320, 480 ); fill_surf( 0x0 ); refresh_screen( 0, 0, 0, 0 ); if ( config.visualize ) shrapnells_init(); for ( i = 0; i < game_count; i++ ) { bowl = bowl_create( 0, 0, -1, -1, blocks, qmark, "Demo", 0 ); if ( config.visualize ) bowl->blind = 0; else bowl->blind = 1; bowl->mute = 1; while ( !bowl->game_over && !leave ) { if ( SDL_PollEvent( &event ) && event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE ) leave = 1; bowl_update( bowl, ms, 0 ); if ( config.visualize ) refresh_rects(); } lines[i] = bowl->lines; scores[i] = counter_get_target( bowl->score ); sprintf( str, "%4i\n", lines[i] ); write_text( font, sdl.screen, 310 + ( i % 7 ) * 42, 10 + ( i / 7 ) * 16, str, OPAQUE ); refresh_screen( 0, 0, 0, 0 ); total += scores[i]; total_lines += bowl->lines; bowl_delete( bowl ); printf( "%3i: %12i\n", i, scores[i] ); /* DEBUG */ if ( leave ) break; } if ( config.visualize ) shrapnells_delete(); if ( i != game_count ) game_count = i; if ( game_count <= 0 ) return; sprintf( str, "Avg Score: %i Avg. Lines: %i\n", (int)(total / game_count), total_lines / game_count ); write_text( font, sdl.screen, 330, 428, str, OPAQUE ); sprintf( str, "0-200: %i 201-400:: %i\n", count_occ( lines, game_count, 0, 200 ), count_occ( lines, game_count, 201, 400 ) ); write_text( font, sdl.screen, 330, 444, str, OPAQUE ); sprintf( str, "401-600: %i >601: %i\n", count_occ( lines, game_count, 401, 600 ), count_occ( lines, game_count, 601, 10000 ) ); write_text( font, sdl.screen, 330, 460, str, OPAQUE ); refresh_screen( 0, 0, 0, 0 ); wait_for_click();}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -