def test_create_grid(self): grid = create_grid(n_rows=2, n_columns=2) self.assertEqual(grid, [[0, 0], [0, 0]]) grid[0][0] = 1 self.assertEqual(grid, [[1, 0], [0, 0]]) grid = create_grid(n_rows=3, n_columns=7) self.assertEqual( grid, [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]] )
def run_game(): pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Raindrops") rain_drop = Group() bg_color = (230, 230, 230) raindrops = Group() while True: gf.check_events() if ai_settings.new_grid: gf.create_grid(ai_settings, screen, rain_drop) gf.rain_update(ai_settings, rain_drop) gf.update_screen(ai_settings, screen, rain_drop)
# Why 40? # Ok, comes from eruptions in Ecuador in the variable data, but it has 42 rows, not 40 # And is i called somewhere in the functions below? for i in range(40): counter0 = time.perf_counter() # Returns annual probability - only for VEI 4? probabilities = f.get_prob(data, startYear, stopYear, thresholdYear, timeStepLength, mode) # Creates one file per volcano per VEI that contains a 3-column grid fileList = f.create_vei_files(outputFolder, refVolcano, data, refVEI, refZone) # grid, minLat, minLon = f.create_grid(inputFileFolder, fileList, cellSize, outputFolder, probThreshold) # eruptions = f.get_stoch_eruptions(data, probabilities, startYear, stopYear, thresholdYear, refZone, mode) f.add_eruptions_to_grid(outputFolder, fileList, eruptions, grid, probThreshold, minLat, minLon, cellSize) carbonGrid, surfaceGrid, logC = f.get_carbon_grid( grid, startYear, stopYear, surfaceC, outputFolder, cellSize, carbonReduction) count = f.save_results(outputFolder, carbonGrid, surfaceGrid, logC, eruptions) counter1 = time.perf_counter() print(str(count) + ": " + str(counter1 - counter0) + " secondes")
def main(settings, level, fall_speed, first_advance_lines): # music settings.music = random.choice(sl.music) song_end = pygame.USEREVENT + 1 pygame.mixer.music.set_endevent(song_end) if settings.play_music: pygame.mixer.music.load(settings.music) pygame.mixer.music.play() settings.music_is_playing = True locked_positions = {} # choose random background settings.background = random.choice(sl.backgrounds) # prepare pieces and play_area change_piece = False change_piece_counter = 0 run = True current_piece = fun.get_piece(settings) next_piece = fun.get_piece(settings) move_piece_x = 0 move_piece_y = 0 move_counter_x = 0 move_counter_y = 0 fall_time = 0 level_meter = 0 lines = 0 score = 0 single = 0 double = 0 triple = 0 quad = 0 pentris = 0 hexis = 0 drop_points = 0 cleared_rows_count = 0 clock = pygame.time.Clock() start_the_time = True first_advance_lines_reached = False while run: if start_the_time: start_time = time.time() start_the_time = False grid = fun.create_grid(settings, locked_positions) # ghost_grid = grid.copy() ghost_piece = copy.deepcopy(current_piece) # generate oll centre oll_centre = copy.deepcopy(current_piece) oll_centre.shape = psac.OLL_O clock.tick(sl.fps) fall_time += 1 if (lines >= first_advance_lines) & (level_meter >= 10): sl.levelup_sound.play() if level < 8: fall_speed -= 5 elif level == 8: fall_speed -= 2 elif level in [9, 12, 15, 18, 28]: fall_speed -= 1 settings.background = random.choice(sl.backgrounds) level += 1 # only subtract first_advance_lines away once if first_advance_lines_reached: level_meter -= 10 else: level_meter -= first_advance_lines first_advance_lines_reached = True # drop piece by 1 if fall_time >= fall_speed: fall_time = 0 current_piece.y += 1 if not (fun.valid_space(current_piece, grid, settings)) and current_piece.y > 0: current_piece.y -= 1 change_piece_counter += 1 if change_piece_counter == 1: change_piece = True change_piece_counter = 0 for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.VIDEORESIZE: if not settings.fullscreen: settings.surface = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE) fun.update_settings(settings) # song ended, pick a random new song and play it if event.type == song_end: settings.music = random.choice(sl.music) pygame.mixer.music.load(settings.music) pygame.mixer.music.play() if event.type == pygame.KEYDOWN: # Quit if event.key == pygame.K_ESCAPE: pygame.mixer.music.stop() sl.cancel_sound.play() settings.background = sl.initial_background run = False if event.key == pygame.K_F11: fun.toggle_fullscreen(settings) fun.update_settings(settings) if event.key == cont.start: sl.pause_sound.play() pause_menu(settings) if event.key == pygame.K_m: fun.mute_music(settings) # Move left if event.key == cont.move_left: current_piece.x -= 1 if not (fun.valid_space(current_piece, grid, settings)): current_piece.x += 1 move_piece_x = -1 move_counter_x = 0 # Move right if event.key == cont.move_right: current_piece.x += 1 if not (fun.valid_space(current_piece, grid, settings)): current_piece.x -= 1 move_piece_x = 1 move_counter_x = 0 # soft drop if event.key == cont.soft_drop: current_piece.y += 1 if not (fun.valid_space(current_piece, grid, settings)): current_piece.y -= 1 move_piece_y = 1 move_counter_y = 0 # hard drop if event.key == cont.hard_drop: sl.harddrop_sound.play() while not change_piece: current_piece.y += 1 drop_points += 1 if not (fun.valid_space(current_piece, grid, settings)) and current_piece.y > 0: current_piece.y -= 1 change_piece = True # rotate clockwise if event.key == cont.rotate_cw: sl.rotate_sound.play() current_piece.rotation += 1 if not (fun.valid_space(current_piece, grid, settings)): current_piece.rotation -= 1 # rotate anti-clockwise if event.key == cont.rotate_acw: sl.rotate_sound.play() current_piece.rotation -= 1 if not (fun.valid_space(current_piece, grid, settings)): current_piece.rotation += 1 # Stop moving piece when key up if event.type == pygame.KEYUP: if (event.key == cont.move_left) or (event.key == cont.move_right): move_piece_x = 0 move_counter_x = 0 if event.key == cont.soft_drop: move_piece_y = 0 move_counter_y = 0 # if left or right is pressed and held down if move_piece_x != 0: move_counter_x += 1 if move_piece_y == 1: move_counter_y += 1 if move_counter_x == 8: current_piece.x += move_piece_x if not (fun.valid_space(current_piece, grid, settings)): current_piece.x -= move_piece_x move_counter_x = 7 if move_counter_y == 8: current_piece.y += move_piece_y if not (fun.valid_space(current_piece, grid, settings)): current_piece.y -= move_piece_y move_counter_y = 7 piece_pos = fun.convert_piece_orientation(current_piece) # move ghost piece to bottom ghost_thing = True while ghost_thing: ghost_piece.y += 1 if not (fun.valid_space(ghost_piece, grid, settings)) and ghost_piece.y > 0: ghost_piece.y -= 1 ghost_thing = False ghost_piece_pos = fun.convert_piece_orientation(ghost_piece) oll_centre_pos = fun.convert_piece_orientation(oll_centre) # colour stuff in for i in range(len(piece_pos)): x, y = piece_pos[i] # need to prevent pieces from appearing from bottom of screen if y > -1: grid[y][x] = current_piece.colour # update time elapsed_time = time.time() - start_time if settings.mode == '40 Lines': if fun.check_win(lines): fun.draw_window(settings, grid, ghost_piece, ghost_piece_pos, oll_centre_pos, score, fun.max_score(settings), level, lines, single, double, triple, quad, pentris, elapsed_time) fun.draw_next_piece(next_piece, settings) sl.lock_sound.stop() pygame.mixer.music.stop() font = pygame.font.Font(settings.text_font, 60) fun.draw_text_middle(settings, 'YOU WIN', font, 80, (255, 255, 255), (0, 0)) pygame.display.update() sl.youwin_sound.play() pygame.time.delay(4000) fun.update_scores(settings, elapsed_time) settings.background = sl.initial_background break if fun.check_lost(piece_pos, locked_positions): fun.draw_window(settings, grid, ghost_piece, ghost_piece_pos, oll_centre_pos, score, fun.max_score(settings), level, lines, single, double, triple, quad, pentris, elapsed_time) fun.draw_next_piece(next_piece, settings) sl.lock_sound.stop() pygame.mixer.music.stop() font = pygame.font.Font(settings.text_font, 60) fun.draw_text_middle(settings, 'GAME OVER', font, 80, (255, 255, 255), (0, 0)) pygame.display.update() sl.gameover_sound.play() pygame.time.delay(6000) fun.update_scores(settings, score) settings.background = sl.initial_background break if change_piece: # update locked positions (but don't draw it yet) for pos in piece_pos: p = (pos[0], pos[1]) locked_positions[p] = current_piece.colour current_piece = next_piece ghost_piece = copy.deepcopy(current_piece) # next piece. if same as current piece, generate another one (can be the same) next_piece = fun.get_piece(settings) if next_piece.shape == current_piece.shape: next_piece = fun.get_piece(settings) change_piece = False cleared_rows_count = fun.clear_rows(grid, locked_positions, settings) # scoring if cleared_rows_count == 1: sl.single_sound.play() score += (level + 1) * 40 single += 1 elif cleared_rows_count == 2: sl.double_sound.play() score += (level + 1) * 100 double += 1 elif cleared_rows_count == 3: sl.triple_sound.play() score += (level + 1) * 300 triple += 1 elif cleared_rows_count == 4: sl.quad_sound.play() score += (level + 1) * 1200 quad += 1 elif cleared_rows_count == 5: score += (level + 1) * 6000 pentris += 1 else: sl.lock_sound.play() score += drop_points drop_points = 0 level_meter += cleared_rows_count lines += cleared_rows_count # basic entry delay if settings.delay: time.sleep(0.25) fun.draw_window(settings, grid, ghost_piece, ghost_piece_pos, oll_centre_pos, score, fun.max_score(settings), level, lines, single, double, triple, quad, pentris, elapsed_time) fun.draw_next_piece(next_piece, settings) pygame.display.update() # basic line clear delay if settings.delay: if cleared_rows_count > 0: time.sleep(0.3) cleared_rows_count = 0
import functions as f # Test everything test_grid = f.create_grid(3, 3) f.print_grid(test_grid) instructions = open('test-input-3x3', 'r') for line in instructions: operation = f.parse_op(line) test_grid = f.op_step(operation, test_grid) f.print_grid(test_grid) print(f.get_n_on(test_grid)) # Real input grid = f.create_grid(1000, 1000) instructions = open('input', 'r') for line in instructions: operation = f.parse_op(line) grid = f.op_step(operation, grid) f.get_n_on(grid)