Exemplo n.º 1
0
 def update(self, tiles):
     if self.aware:
         self.xSpeed = self.speed * cos(angle(self, self.player))
         self.ySpeed = self.speed * sin(angle(self, self.player))
         if (tiles[floor((self.x + self.xSpeed) / TILE_WIDTH)][floor(
                 self.y / TILE_HEIGHT)]
                 or tiles[floor(self.x / TILE_WIDTH)][floor(
                     (self.y + self.ySpeed) / TILE_HEIGHT)]):
             if not tiles[floor((self.x + self.xSpeed) / TILE_WIDTH)][floor(
                     self.y / TILE_HEIGHT)]:
                 self.xSpeed = 0
                 self.ySpeed = self.speed * ifNeg(self.ySpeed)
             elif not tiles[floor(self.x / TILE_WIDTH)][floor(
                 (self.y + self.ySpeed) / TILE_HEIGHT)]:
                 self.xSpeed = self.speed * ifNeg(self.xSpeed)
                 self.ySpeed = 0
         else:
             self.xSpeed = 0
             self.ySpeed = 0
         self.x += self.xSpeed
         self.y += self.ySpeed
         if distance(self, self.player) >= 300:
             self.aware = False
         elif distance(self, self.player) <= 10:
             if self.player.timer < 0:
                 self.player.xSpeed = 5 * cos(angle(self, self.player))
                 self.player.ySpeed = 5 * sin(angle(self, self.player))
                 self.player.health -= 1
                 self.player.invincible = 16
             explosion.explode(self.player.x, self.player.y, self.group)
             self.group.remove(self)
     else:
         if distance(self, self.player) <= 225:
             self.aware = True
Exemplo n.º 2
0
async def fly_garbage(canvas, column, garbage_frame, speed=0.5):
    """Animate garbage, flying from top to bottom. Сolumn position will stay same, as specified on start."""
    rows_number, columns_number = canvas.getmaxyx()

    column = max(column, 0)
    column = min(column, columns_number)
    row = 1
    height, width = curses_tools.get_frame_size(garbage_frame)
    obstacle = Obstacle(row, column, rows_size=height, columns_size=width)
    _, bounding_column, bounding_frame = obstacle.dump_bounding_box()

    state.obstacles.append(obstacle)

    while obstacle.row < rows_number:
        # curses_tools.draw_frame(canvas, obstacle.row - 1, bounding_column, bounding_frame)
        curses_tools.draw_frame(canvas, obstacle.row, column, garbage_frame)
        await asyncio.sleep(0)
        curses_tools.draw_frame(canvas,
                                obstacle.row,
                                column,
                                garbage_frame,
                                negative=True)
        # curses_tools.draw_frame(canvas, obstacle.row - 1, bounding_column, bounding_frame, negative=True)
        obstacle.row += speed

        if obstacle in state.obstacles_in_last_collisions.copy():
            state.obstacles_in_last_collisions.remove(obstacle)
            state.obstacles.remove(obstacle)

            state.coroutines.append(
                explode(canvas, obstacle.row + height // 2,
                        column + width // 2))

            return
Exemplo n.º 3
0
async def run_spaceship(canvas, row, column):
    """ controls location of spaceship and draws it on the screen"""
    row_speed = column_speed = 0
    global spaceship_frame
    global coroutines
    global year
    spaceship_collision = False
    while not spaceship_collision:
        maxx, maxy = canvas.getmaxyx()
        rows_direction, columns_direction, space_pressed = read_controls(
            canvas)
        row_speed, column_speed = update_speed(row_speed, column_speed,
                                               rows_direction,
                                               columns_direction)
        frame_rows, frame_columns = get_frame_size(spaceship_frame)
        row, column = speed_reduction_near_border(row, column, frame_rows,
                                                  frame_columns, maxx, maxy,
                                                  row_speed, column_speed)

        draw_frame(canvas, row, column, spaceship_frame)
        last_frame = spaceship_frame

        frame_rows, frame_columns = get_frame_size(spaceship_frame)
        await sleep(1)
        if space_pressed and (year >= 2020):
            coroutines.append(fire(canvas, row, column + 2))

        draw_frame(canvas, row, column, last_frame, negative=True)

        if if_collision_append_collision_list(row, column, frame_rows,
                                              frame_columns):
            spaceship_collision = True
    coroutines.append(
        explode(canvas, row + frame_rows // 2, column + frame_columns // 2))
    coroutines.append(show_gameover(canvas))
Exemplo n.º 4
0
async def fly_garbage(canvas, column, garbage_frame, speed=0.5):
    """Animate garbage, flying from top to bottom.
    Сolumn position will stay same, as specified on start."""
    global obstacles
    global obstacles_in_last_collision
    global coroutines

    rows_number, columns_number = canvas.getmaxyx()

    column = max(column, 0)
    column = min(column, columns_number - 1)
    row = 0
    row_size, column_size = get_frame_size(garbage_frame)
    obstacle = Obstacle(row, column, row_size, column_size)
    obstacles.append(obstacle)

    while row < rows_number:
        if obstacle in obstacles_in_last_collision:
            coroutines.append(
                explode(canvas, row + row_size // 2,
                        column + column_size // 2))
            obstacles_in_last_collision.remove(obstacle)
            break
        draw_frame(canvas, row, column, garbage_frame)
        obstacle.row = row
        obstacle.column = column
        await asyncio.sleep(0)
        draw_frame(canvas, row, column, garbage_frame, negative=True)
        row += speed
    obstacles.remove(obstacle)
Exemplo n.º 5
0
async def fly_garbage(
    state,
    obstacle,
    canvas,
    column,
    garbage_frame,
    row=0,
    speed=0.3,
):
    """Animate garbage, flying from top to bottom. Сolumn position will stay same, as specified on start."""
    rows_number, columns_number = canvas.getmaxyx()

    column = max(column, 0)
    column = min(column, columns_number - 1)
    try:
        while row < rows_number:
            obstacle.row = row
            draw_frame(canvas, row, column, garbage_frame)

            await sleep(1)
            draw_frame(canvas, row, column, garbage_frame, negative=True)
            row += speed

            for collision in state["collisions"]:
                if obstacle.has_collision(
                        collision.row,
                        collision.column,
                ):
                    state["routines"].append(explode(canvas, row, column))
                    state["collisions"].remove(collision)
                    return
    finally:
        state["obstacles"].remove(obstacle)
Exemplo n.º 6
0
async def fly_garbage(canvas, column, garbage_frame, speed=0.5):
    """Animate garbage, flying from top to bottom. Сolumn position will stay same, as specified on start."""

    rows_number, columns_number = canvas.getmaxyx()

    column = max(column, 0)
    column = min(column, columns_number - 1)

    # Get frame row size and run it higher so
    # that it appears gradually instead suddenly
    row = -utils.get_frame_size(garbage_frame)[0] + BORDER_WIDTH + 1

    row_size, column_size = utils.get_frame_size(garbage_frame)
    obstacle = Obstacle(row, column, row_size, column_size)
    obstacles.append(obstacle)

    while obstacle.row < rows_number - BORDER_WIDTH * 2 - DERIVED_WINDOW_HEIGHT:
        if obstacle in obstacles_in_last_collisions:
            obstacles_in_last_collisions.remove(obstacle)
            coroutines.append(
                explode(canvas, obstacle.row + row_size // 2,
                        obstacle.column + column_size // 2))
            break
        utils.draw_frame(canvas, obstacle.row, obstacle.column, garbage_frame)
        await asyncio.sleep(0)
        utils.draw_frame(canvas,
                         obstacle.row,
                         obstacle.column,
                         garbage_frame,
                         negative=True)
        obstacle.row += speed
    obstacles.remove(obstacle)
Exemplo n.º 7
0
async def explode_collided_garbage(canvas, obstacles_to_explode):
    while True:
        for obstacle in obstacles_to_explode:
            coroutines.append(
                explode(canvas, obstacle.row + obstacle.rows_size / 2,
                        obstacle.column + obstacle.columns_size / 2))
        obstacles_to_explode.clear()
        await sleep(1)
Exemplo n.º 8
0
async def run_spaceship(canvas, row, column):
    # find shift to draw text the way, when (row, column) is the center
    init_row_shift_x2, init_col_shift_x2 = get_frame_size(
        state.spaceship_frame)
    max_row, max_col = canvas.getmaxyx()

    # fix position to center spaceship
    row = limit_coordinate(row - init_row_shift_x2 // 2, max_row)
    column = limit_coordinate(column - init_col_shift_x2 // 2, max_col)

    row_speed, column_speed = 0, 0

    prev_frame = None
    while True:
        if prev_frame is not None:
            draw_frame(canvas, row, column, prev_frame, negative=True)

        frame_rows, frame_cols = get_frame_size(state.spaceship_frame)

        for obstacle in state.obstacles:
            if obstacle.has_collision(row, column, frame_rows, frame_cols):
                # game over
                state.coroutines.append(
                    explode(canvas, row + frame_rows / 2,
                            column + frame_cols / 2))
                await sleep(4)  # wait till the end of explosion
                state.coroutines.append(show_gameover(canvas))
                return

        row_shift, col_shift, space_pressed = read_controls(canvas)
        row_speed, column_speed = update_speed(row_speed, column_speed,
                                               row_shift, col_shift)

        row = limit_coordinate(row + row_speed, max_row - frame_rows)
        column = limit_coordinate(column + column_speed, max_col - frame_cols)

        prev_frame = state.spaceship_frame
        draw_frame(canvas, row, column, state.spaceship_frame)

        if space_pressed:  # cannon shot
            state.coroutines.append(fire(canvas, row,
                                         column + frame_cols // 2))

        await sleep()
Exemplo n.º 9
0
async def fire(canvas,
               start_row,
               start_column,
               obstacles,
               obstacles_in_last_collisions,
               coroutines,
               rows_speed=-1,
               columns_speed=0):
    """Display fire of gun shot. Direction and speed can be specified."""

    row, column = start_row, start_column

    canvas.addstr(round(row), round(column), '*')
    await asyncio.sleep(0)

    canvas.addstr(round(row), round(column), 'O')
    await asyncio.sleep(0)
    canvas.addstr(round(row), round(column), ' ')

    row += rows_speed
    column += columns_speed

    symbol = '-' if columns_speed else '|'

    rows, columns = canvas.getmaxyx()
    max_row, max_column = rows - 1, columns - 1

    curses.beep()

    while 0 < row < max_row and 0 < column < max_column:
        for obstacle in obstacles:
            if obstacle.has_collision(row, column):
                obstacles_in_last_collisions.add(obstacle)
                coroutines.append(
                    explode(canvas, obstacle.row + obstacle.rows_size / 2,
                            obstacle.column + obstacle.columns_size / 2))
                return
        canvas.addstr(round(row), round(column), symbol)
        await asyncio.sleep(0)
        canvas.addstr(round(row), round(column), ' ')
        row += rows_speed
        column += columns_speed
Exemplo n.º 10
0
async def draw_rocket(
    state,
    canvas,
    start_row,
    start_column,
    border,
    negative=True,
    speed_boost=1,
):
    row, column = (start_row, start_column)
    row_speed = column_speed = 0
    frames = get_rocket_frames_iter()
    rows, columns = get_frame_size(next(frames))
    rocket = Obstacle(row, column, rows, columns)

    for frame in frames:
        rocket.row = row
        rocket.column = column
        for obstacle in state["obstacles"]:
            if rocket.has_collision(
                obstacle.row,
                obstacle.column,
                obstacle.rows_size,
                obstacle.columns_size,
            ):
                state["routines"].append(explode(canvas, row, column))
                state["routines"].append(end(canvas))
                return
        draw_frame(canvas, row, column, frame)
        await sleep()
        draw_frame(canvas, row, column, frame, negative=True)

        row_delta, column_delta, space = read_controls(canvas)
        if space and state["year"] >= 2020:
            state["routines"].append(fire(state, canvas, row, column + 2))

        frame_rows, frame_columns = get_frame_size(frame)

        row_speed, column_speed = update_speed(
            row_speed,
            column_speed,
            row_delta,
            column_delta,
        )

        if row_delta == -1:
            row = max(border["top"], row + row_speed)
        elif row_delta == 1:
            row = min(border["bottom"] - frame_rows, row + row_speed)
        elif row_delta == 0:
            row = min(border["bottom"] - frame_rows, row + row_speed)
            row = max(border["top"], row)

        if column_delta == 1:
            column = min(
                border["right"] - frame_columns,
                column + column_speed,
            )
        elif column_delta == -1:
            column = max(border["left"], column + column_speed)
        elif column_delta == 0:
            column = max(border["left"], column + column_speed)
            column = min(
                border["right"] - frame_columns,
                column,
            )