async def fire(canvas, start_row: int, start_column: int, rows_speed=-0.3, columns_speed=0) -> None: """Display animation of gun shot, direction and speed can be specified.""" row, column = start_row, start_column canvas_rows, canvas_columns = get_canvas_size(canvas) max_row, max_column = canvas_rows - 1, canvas_columns - 1 curses.beep() canvas.addstr(round(row), round(column), '*') await sleep(1) canvas.addstr(round(row), round(column), 'O') await sleep(1) canvas.addstr(round(row), round(column), ' ') symbol = '-' if columns_speed else '|' row += rows_speed column += columns_speed while 0 < row < max_row and 0 < column < max_column: for obstacle in obstacles: if obstacle.has_collision(row, column): obstacles_in_last_collisions.append(obstacle) coroutines.append(explode(canvas, row, column)) return canvas.addstr(round(row), round(column), symbol) await sleep(1) canvas.addstr(round(row), round(column), ' ') row += rows_speed column += columns_speed
async def show_year(canvas): """Show current year from global game_state.year on left bottom of canvas.""" canvas_rows_size, canvas_columns_size = get_canvas_size(canvas) while True: draw_frame(canvas, canvas_rows_size - 2, 0, f"Year: {str(game_state.year)}") canvas.syncup() await sleep(2)
async def game_over(canvas, frame: str) -> None: canvas_rows_size, canvas_columns_size = get_canvas_size(canvas) row_size, column_size = get_frame_size(frame) row = (canvas_rows_size - row_size) // 2 column = (canvas_columns_size - column_size) // 2 while True: draw_frame(canvas, row, column, frame) await sleep()
async def rocket(canvas, row: int, column: int, frames: List, speed_of_rocket=1, speed_animation_divider=1): canvas_rows_size, canvas_columns_size = get_canvas_size(canvas) animation_groups = [[frame] * speed_animation_divider for frame in frames] animation = chain(*animation_groups) frames_infinite_cycle = cycle(animation) current_frame = '' row_speed, column_speed = 0, 0 for frame in frames_infinite_cycle: object_row_size, object_column_size = get_object_size(frames) for obstacle in game_state.obstacles: if obstacle.has_collision(row, column, object_row_size, object_column_size): draw_frame(canvas, row, column, current_frame, negative=True) game_state.coroutines.append( game_over( canvas, read_animation_frames(settings.GAME_OVER_FRAME)[0])) game_state.coroutines.append(explode(canvas, row + 2, column)) return draw_frame(canvas, row, column, current_frame, negative=True) rocket_rows_direction, rocket_column_direction, space_pressed = read_controls( canvas) row_speed, column_speed = update_speed(row_speed, column_speed, rocket_rows_direction, rocket_column_direction) row += row_speed * speed_of_rocket column += column_speed * speed_of_rocket row = limit_axis_coord(row, object_row_size, canvas_rows_size) column = limit_axis_coord(column, object_column_size, canvas_columns_size) if space_pressed and game_state.year > settings.CANNON_APPEARS_YEAR: game_state.coroutines.append( fire(canvas, row - 1, column + object_column_size // 2)) draw_frame(canvas, row, column, frame) current_frame = frame canvas.refresh() await sleep(1)
def draw(canvas): curses.curs_set(0) canvas.nodelay(True) canvas.border() canvas_rows_size, canvas_columns_size = get_canvas_size(canvas) # added current year game_state.coroutines.append(show_year(canvas.derwin(10, 4))) # added stars border = settings.BORDER_WIDTH for n in range(settings.STARS_QUANTITY): game_state.coroutines.append( blink(canvas, row=random.randint(border, canvas_rows_size - border), column=random.randint(border, canvas_columns_size - border), symbol=random.choice(settings.STARS))) # added rocket game_state.coroutines.append( rocket(canvas, row=canvas_rows_size // 2, column=canvas_columns_size // 2, speed_of_rocket=settings.SPEED_OF_ROCKET, speed_animation_divider=settings.ROCKET_ANIMATION_SPEED_DIVIDER, frames=read_animation_frames( settings.ROCKET_ANIMATIONS_FRAMES))) game_state.coroutines.append( fill_orbit_with_garbage(canvas, canvas_columns_size)) game_state.coroutines.append(update_year(canvas, settings.UPDATE_YEAR_TICS)) while True: for coroutine in game_state.coroutines.copy(): try: coroutine.send(None) except StopIteration: game_state.coroutines.remove(coroutine) time.sleep(settings.TIC_TIMEOUT) canvas.refresh()
async def you_have_a_gun_info(canvas): canvas_rows_size, canvas_columns_size = get_canvas_size(canvas) draw_frame(canvas, canvas_rows_size - 3, 0, "You have a gun! Press SPACE to fire") await sleep(4) draw_frame(canvas, canvas_rows_size - 3, 0, "You have a gun! Press SPACE to fire", negative=True) await sleep(2) draw_frame(canvas, canvas_rows_size - 3, 0, "You have a gun! Press SPACE to fire") await sleep(4) draw_frame(canvas, canvas_rows_size - 3, 0, "You have a gun! Press SPACE to fire", negative=True) await sleep(2) draw_frame(canvas, canvas_rows_size - 3, 0, "You have a gun! Press SPACE to fire") await sleep(30) draw_frame(canvas, canvas_rows_size - 3, 0, "You have a gun! Press SPACE to fire", negative=True) while True: draw_frame(canvas, canvas_rows_size - 3, 0, "Press SPACE to fire") await sleep(2)