Beispiel #1
0
    if game_ball.ycor() < -300:

        scores.losing_message()
        sleep(2)

        game_screen.reset()

        # a new game ball
        game_ball.create_ball()

        # rebuild new block
        game_blocks.build_blocks()

        # reset the scoreboard
        scores.user_score = 0
        scores.display_score()

        # reset the user paddle
        user_paddle.create_paddle((0, -200))

    else:
        # keep the ball moving so far the game is on
        game_ball.move()

    # end the game once all the blocks have been destroyed
    if scores.user_score == 52 and len(game_blocks.block_list) == 0:

        game_on = False
        scores.winning_message()

    for block in game_blocks.block_list:
Beispiel #2
0
def main():
    screen = Screen()
    screen.setup(width=800, height=600)
    screen.bgcolor('black')
    screen.title('Pong Game')
    screen.tracer(0)
    score_board = ScoreBoard()
    score_board.display_score()
    CenterLine()
    player1 = Paddle((350, 0))
    player2 = Paddle((-350, 0))
    ball = Ball()
    screen.onkey(player1.move_up, 'Up')
    screen.onkey(player1.move_down, 'Down')
    screen.onkey(player2.move_up, 'w')
    screen.onkey(player2.move_down, 's')
    screen.listen()

    def game_continue():
        if messagebox.askretrycancel("Game Over!!", "Wanna play again? "):
            screen.clear()
            main()

    running = True
    while running:
        time.sleep(0.05)
        screen.update()
        if score_board.check_win():
            score_board.final_result()
            ball.hideturtle()
            break
        if ball.ispoint_wait:
            if ball.point_wait < 50:
                ball.point_wait += 1
                continue
            else:
                ball.ispoint_wait = False
                ball.point_wait = 1
        screen.update()
        ball.move()
        # for not repeating collision if the distance is small
        if player1.is_collided:
            if player1.safe_count < 5:
                player1.safe_count += 1
            else:
                player1.is_collided = False
                player1.safe_count = 1
        elif player2.is_collided:
            if player2.safe_count < 5:
                player2.safe_count += 1
            else:
                player2.is_collided = False
                player2.safe_count = 1

        if ball.ycor() > 265 or ball.ycor() < -270:
            ball.wall_collision()

        if ball.xcor() < 370 and ball.distance(
                player1) < 45 and not player1.is_collided:
            ball.paddle_collision()
            ball.hit_count += 1
            ball.hit = True
            player1.is_collided = True
        elif ball.xcor() > -370 and ball.distance(
                player2) < 45 and not player2.is_collided:
            ball.paddle_collision()
            ball.hit_count += 1
            ball.hit = True
            player2.is_collided = True

        if ball.hit_count % 2 == 0 and ball.hit_count != 0 and ball.hit:
            if ball.bounce_speed < 0:
                ball.bounce_speed -= 1.5
            else:
                ball.bounce_speed += 1.5
            ball.bounce_x = ball.bounce_speed
            ball.hit = False

        if ball.xcor() > 380:
            score_board.clear()
            score_board.score1 += 1
            score_board.display_score()
            ball.ispoint_wait = True
            ball.ball_reset()

        elif ball.xcor() < -380:
            score_board.clear()
            score_board.score2 += 1
            score_board.display_score()
            ball.ispoint_wait = True
            ball.ball_reset()

    screen.update()
    time.sleep(1)
    game_continue()
    screen.bye()
    screen.exitonclick()