Ejemplo n.º 1
0
        ball.detect_bat(ai_bat.pos())
        if ball.detect_edge() == 1:
            # Player wins
            player_score.update()
            # Reposition ball with new heading
            ball.new_position()
            ball.setheading(choice([135, -135]))  # + randint(-10, 10))
        elif ball.detect_edge() == 2:
            # AI wins
            ai_score.update()
            # Reposition ball with new heading
            ball.new_position()
            ball.setheading(choice([-45, 45]))  # + randint(-10, 10))
        else:
            # If the bat direction has changed, then get a new random AI bat speed
            if ball.ycor() - ai_bat.ycor() > 10:
                new_ai_direction = 1
            elif ball.ycor() - ai_bat.ycor() < -10:
                new_ai_direction = -1
            else:
                new_ai_direction = 0
            if new_ai_direction != ai_direction and new_ai_direction != 0:
                ai_direction = new_ai_direction
                ai_speed = int(
                    uniform(C.AI_BAT_SPEED - 3, C.AI_BAT_SPEED + 3) *
                    C.INIT_SPEED_MULTIPLIER / ball.speed_multiplier)
            ai_bat.move(new_ai_direction * ai_speed)

# Close the screen once the game has ended and the screen is clicked
# This binds bye() method to mouse clicks on the Screen.
# Also enters s.mainloop() if “using_IDLE” in the configuration dictionary is False.
Ejemplo n.º 2
0
        bat1.reset()
        bat2.reset()
        score_right.increase_score()
        goal = True
        goal_right += 1
        rally = 0
        ball.decrease_speed()
        if goal_right > 9:
            score_right.game_over()
            game_is_on = False

    # Detect collision with bats

    if ball.distance(bat2) < 25 or ball.distance(
            bat2.xcor(),
            bat2.ycor() + 27) < 25 or ball.distance(bat2.xcor(),
                                                    bat2.ycor() - 27) < 25:
        ball.setheading(random.randint(125, 200))
        rally = check_for_rally(rally)

    if ball.distance(bat1) < 25 or ball.distance(
            bat1.xcor(),
            bat1.ycor() + 27) < 25 or ball.distance(bat1.xcor(),
                                                    bat1.ycor() - 27) < 25:
        ball.setheading(random.randint(-60, 60))
        rally = check_for_rally(rally)

    # Detect collision with floor
    if ball.ycor() < -360:
        # ball.setheading(random.randint(3, 60))
        ball.setheading(-ball.heading())