def main(canvas): curses.curs_set(False) canvas.border() canvas.nodelay(True) height, width = canvas.getmaxyx() coroutines = [ blink(canvas, row, column, symbol, random.randint(0, 3)) for row, column, symbol in stars_generator(height, width) ] start_row = height - 2 start_col = width / 2 coro_shot = fire(canvas, start_row, start_col) coroutines.append(coro_shot) rocket_frame_1 = load_frame_from_file( 'anim_frames/rocket/rocket_frame_1.txt') rocket_frame_2 = load_frame_from_file( 'anim_frames/rocket/rocket_frame_2.txt') rocket_frames = (rocket_frame_1, rocket_frame_2) start_rocket_row = height / 2 coro_rocket_anim = animate_frames(canvas, start_rocket_row, start_col, rocket_frames) coroutines.append(coro_rocket_anim) canvas.refresh() run_event_loop(coroutines)
async def run_spaceship(canvas, row, column): """ Анимация ракеты с поддержкой перемещения""" global year row_speed = column_speed = 0 MAX_Y, MAX_X = canvas.getmaxyx() while True: current_frame, current_row, current_column = spaceship_frame, row, column draw_frame(canvas, row, column, spaceship_frame) row_acceleration, column_acceleration, space_pressed = read_controls( canvas) row_speed, column_speed = update_speed(row_speed, column_speed, row_acceleration, column_acceleration) for obstacle in obstacles: if obstacle.has_collision(row, column): await show_gameover(canvas) return if (BORDER < row + row_speed < MAX_Y - FRAME_HEIGHT - BORDER): row += row_speed if (BORDER < column + column_speed < MAX_X - FRAME_WIDTH - BORDER): column += column_speed if space_pressed and year >= 2020: coroutines.append( fire(canvas, row, column + FRAME_WIDTH / 2, obstacles, obstacles_in_last_collisions)) await sleep(0.1) draw_frame(canvas, current_row, current_column, current_frame, True) draw_frame(canvas, row, column, spaceship_frame)
async def add_fire(self): while True: if self.is_shot and self.current_year > 2019: coroutine = fire(self.canvas, self.space_coords[1], self.space_coords[0] + 2, self.obstacles, self.destroyed_obstacle_ids) self.coroutines.append(coroutine) await sleep(1)
async def run_spaceship(canvas, coros, start_row, start_column, frame_container, level, start_year): height, width = canvas.getmaxyx() border_size = 1 frame_size_y, frame_size_x = get_frame_size(frame_container[0]) frame_pos_x = round(start_column) - round(frame_size_x / 2) frame_pos_y = round(start_row) - round(frame_size_y / 2) row_speed, column_speed = 0, 0 while True: direction_y, direction_x, spacebar = read_controls(canvas) current_year = start_year + level[0] if spacebar and current_year >= 2020: shot_pos_x = frame_pos_x + round(frame_size_x / 2) shot_pos_y = frame_pos_y - 0 shot_coro = fire(canvas, shot_pos_y, shot_pos_x) coros.append(shot_coro) row_speed, column_speed = update_speed(row_speed, column_speed, direction_y, direction_x) frame_pos_x += column_speed frame_pos_y += row_speed frame_x_max = frame_pos_x + frame_size_x frame_y_max = frame_pos_y + frame_size_y field_x_max = width - border_size field_y_max = height - border_size frame_pos_x = min(frame_x_max, field_x_max) - frame_size_x frame_pos_y = min(frame_y_max, field_y_max) - frame_size_y frame_pos_x = max(frame_pos_x, border_size) frame_pos_y = max(frame_pos_y, border_size) current_frame = frame_container[0] draw_frame(canvas, frame_pos_y, frame_pos_x, current_frame) await asyncio.sleep(0) draw_frame(canvas, frame_pos_y, frame_pos_x, current_frame, negative=True) for obstacle in obstacles_actual: if obstacle.has_collision(frame_pos_y, frame_pos_x): game_over_coro = show_gameover( canvas, height, width, load_frame_from_file('game_over/game_over.txt')) coros.append(game_over_coro) return
async def run_spaceship(canvas, coroutines, frames_container, border_size): main_window_height, main_window_width = canvas.getmaxyx() start_ship_row = main_window_height - border_size start_ship_column = main_window_width / 2 frame_size_y, frame_size_x = get_frame_size(frames_container[0]) frame_pos_x = round(start_ship_row) - round(frame_size_x / 2) frame_pos_y = round(start_ship_column) row_speed, column_speed = 0, 0 while True: direction_y, direction_x, spacepressed = read_controls(canvas) if spacepressed: shot_pos_x = frame_pos_x + round(frame_size_x / 2) shot_pos_y = frame_pos_y shot_coroutine = fire(canvas, shot_pos_y, shot_pos_x) coroutines.append(shot_coroutine) row_speed, column_speed = update_speed(row_speed, column_speed, direction_y, direction_x) frame_pos_x += column_speed frame_pos_y += row_speed frame_pos_x_max = frame_pos_x + frame_size_x frame_pos_y_max = frame_pos_y + frame_size_y game_field_x_max = main_window_width - border_size game_field_y_max = main_window_height - border_size frame_pos_x = min(frame_pos_x_max, game_field_x_max) - frame_size_x frame_pos_y = min(frame_pos_y_max, game_field_y_max) - frame_size_y frame_pos_x = max(frame_pos_x, border_size) frame_pos_y = max(frame_pos_y, border_size) frame = frames_container[0] draw_frame(canvas, frame_pos_y, frame_pos_x, frame) await asyncio.sleep(0) draw_frame(canvas, frame_pos_y, frame_pos_x, frame, negative=True) gameover_frame = load_frame_from_file(GAME_OVER_FRAME_PATH) for obstacle in obstacles: if obstacle.has_collision(frame_pos_y, frame_pos_x): gameover_coroutine = show_gameover(canvas, gameover_frame) coroutines.append(gameover_coroutine) return
async def run_spaceship(canvas, start_row, start_column, frames, obstacles, obstacles_in_last_collisions): global current_spaceship_frame row, column = int(start_row), int(start_column) row_speed, column_speed = 0, 0 rows, columns = canvas.getmaxyx() height, width = get_frame_size(frames[0]) max_row = rows - height - 1 max_column = columns - width - 1 while True: old_row, old_column = row, column rows_direction, columns_direction, space_pressed = read_controls( canvas) row_speed, column_speed = update_speed(row_speed, column_speed, rows_direction, columns_direction) row += row_speed column += column_speed if row < 1 or row > max_row: row = old_row if column < 1 or column > max_column: column = old_column for obstacle in obstacles: if obstacle.has_collision(row, column, *get_frame_size(frames[0])): obstacles_in_last_collisions.add(obstacle) coroutines.append(show_gameover(canvas, gameover_banner)) return if space_pressed and current_year >= PLASMA_GUN_YEAR: coroutines.append( fire(canvas, row, column + 2, obstacles=obstacles, obstacles_in_last_collisions=obstacles_in_last_collisions, coroutines=coroutines)) current_frame = current_spaceship_frame draw_frame(canvas, row, column, frames[current_frame]) await asyncio.sleep(0) draw_frame(canvas, row, column, frames[current_frame], negative=True)
async def run_spaceship(canvas, row, column, col_max, row_max): border = 1 frame_rows, frame_cols = curses_tools.get_frame_size(spaceship_frame) row_speed = column_speed = 0 while True: rows_direction, columns_direction, space = curses_tools.read_controls( canvas) row_speed, column_speed = update_speed(row_speed, column_speed, rows_direction, columns_direction) if row + row_speed + frame_rows >= row_max: row = row_max - frame_rows - border elif row + row_speed <= 0: row = border else: row += row_speed if column + column_speed + frame_cols >= col_max: column = col_max - frame_cols - border elif column + column_speed <= 0: column = border else: column += column_speed if space and globs.year >= 2020: globs.coroutines.append( fire_animation.fire(canvas, row, column + 2)) for obstacle in obstacles: if obstacle.has_collision(row, column): await explode(canvas, row, column) globs.coroutines.append( show_gameover(canvas, row, column, gameover_frame)) return curses_tools.draw_frame(canvas, row, column, spaceship_frame) old_frame = spaceship_frame await asyncio.sleep(0) curses_tools.draw_frame(canvas, row, column, old_frame, negative=True)
def make_fire(canvas, row, column): offset = 2 globals_vars.coroutines.append( fire(canvas, row, column + offset, rows_speed=-3))