Exemple #1
0
def main():
    # Setup board and its components
    game_board = Screen()
    game_board.setup(width=800, height=600)
    game_board.bgcolor("black")
    game_board.tracer(0)
    game_board.listen()
    scoreboard = Scoreboard((0, game_board.window_height()/2-40))
    paddle_one = Paddle(((game_board.window_width()/2)-40, 25))
    paddle_two = Paddle((-(game_board.window_width()/2)+40, 25))
    ball = Ball()

    # Setup generic keys
    game_board.onkey(game_board.bye, "Escape")
    game_board.onkey(ball.increase_speed, "KP_Add")
    game_board.onkey(ball.decrease_speed, "KP_Subtract")
    # Setup player keys
    game_board.onkey(paddle_one.up, "Up")
    game_board.onkey(paddle_one.down, "Down")
    game_board.onkey(paddle_two.up, "q")
    game_board.onkey(paddle_two.down, "a")

    # Run the game
    game_is_on = True
    while game_is_on:
        game_board.update()
        time.sleep(ball.game_speed)
        ball.move()

        # bounce off top/bottom wall
        if ball.ycor() > (game_board.window_height()/2-20) or ball.ycor() < -game_board.window_height()/2+20:
            ball.change_direction()

        # hit paddle
        if (ball.xcor() > game_board.window_width()/2 - 70 and paddle_one.distance(ball) < 50) or \
                (ball.xcor() < -game_board.window_width()/2 + 70 and paddle_two.distance(ball) < 50):
            ball.change_direction(True)
            # Randomly increase the game speed a little
            if randint(0, 3) == 2:
                ball.game_speed *= 0.9

        # hit back wall and score point
        if ball.xcor() > game_board.window_width()/2 - 20:
            scoreboard.update_score(1)
            ball.reset_game()
        if ball.xcor() < -game_board.window_width()/2 + 20:
            scoreboard.update_score(0)
            ball.reset_game()

    game_board.exitonclick()
Exemple #2
0
screen.onkeypress(fun=paddle1.move_up, key="Up")
screen.onkeypress(fun=paddle1.move_down, key="Down")
screen.onkeypress(fun=paddle2.move_up, key="w")
screen.onkeypress(fun=paddle2.move_down, key="s")

game_on = True
while game_on:
    screen.update()
    scoreboard1.score(paddle1.score)
    scoreboard2.score(paddle2.score)
    time.sleep(0.1)
    ball.move()

    # Detect collision with paddle1
    if paddle1.distance(ball) < 50:
        print("paddle1")
        ball.bounce_hor()

    # Detect collision with paddle2
    if paddle2.distance(ball) < 50:
        print("paddle2")
        ball.bounce_hor()

    # Detect collision with wall
    if ball.ycor() > 280 or ball.ycor() < -280:
        ball.bounce_ver()

    # Detect collision with right side wall
    if ball.xcor() > 390:
        paddle1.goal()
Exemple #3
0
screen.onkey(key="Up", fun=right_paddle.go_up)
screen.onkey(key="Down", fun=right_paddle.go_down)

game_on = True
while game_on:
    screen.update()
    time.sleep(.1)
    game_ball.move()

    # collision with wall
    if game_ball.ycor() >= 280 or game_ball.ycor() <= -280:
        ball_heading = game_ball.heading()
        game_ball.setheading(-ball_heading)

    # bounce on paddle
    if left_paddle.distance(game_ball) <= 30 or right_paddle.distance(
            game_ball) <= 30:
        ball_heading = game_ball.heading()
        game_ball.setheading(180 - ball_heading)

    # if misse paddle
    if game_ball.xcor() > 400:
        game_ball.reset()
        left_score.write_score()
    elif game_ball.xcor() < -400:
        game_ball.reset()
        right_score.write_score()

    # check score
    score = left_score.get_score()
    if (score > 10):
Exemple #4
0
screen.listen()
screen.onkey(right_paddle.up, 'Up')
screen.onkey(right_paddle.down, 'Down')
screen.onkey(left_paddle.up, 'w')
screen.onkey(left_paddle.down, 's')

game_active = True
while game_active:
    time.sleep(ball.move_speed)
    ball.move()

    if ball.ycor() >= 280 or ball.ycor() <= -280:
        ball.bounce_and_reverse(x_dir=1, y_dir=-1)

    if ((ball.xcor() >= 330 and right_paddle.distance(ball) < 50) or
            (ball.xcor() <= -330 and left_paddle.distance(ball) < 50)):
        ball.bounce_and_reverse(x_dir=-1, y_dir=1, move_speedup=0.9)
    
    if ball.xcor() > 400:
        scoreboard.left_point()
        ball.reset_ball()

    if ball.xcor() < -400:
        scoreboard.right_point()
        ball.reset_ball()

    screen.update()

screen.exitonclick()
Exemple #5
0
screen.listen()
screen.onkey(playerOne.up, "Up")
screen.onkey(playerOne.down, "Down")

gameIsOn = True
AI_UP = True
AI_DOWN = False
BALL_UP = True
BALL_DOWN = False
BALL_RIGHT = False
BALL_LEFT = True

while gameIsOn:
    screen.update()

    if playerOne.distance(ball) < 15:
        BALL_UP = True
        BALL_RIGHT = True
        BALL_DOWN = False
        BALL_LEFT = False
    elif playerTwo.distance(ball) < 15:
        BALL_DOWN = True
        BALL_LEFT = True
        BALL_UP = False
        BALL_RIGHT = False

    if ball.xcor() >= (screen.window_width() //
                       2) or ball.xcor() < (-1 * screen.window_width() // 2):
        ball.penup()
        ball.setpos(0, 0)
        randomNum = randrange(10)
Exemple #6
0

screen.listen()
screen.onkey(combine_two_functions_with_left_key, "Left")
screen.onkey(combine_two_functions_with_right_key, "Right")

while game_is_on:
    time.sleep(0.1)
    screen.update()
    ball.move()

    if ball.xcor() > 370 or ball.xcor() < -370:
        ball.bounce_x()

    if ball.ycor() > 380:
        ball.bounce_y()

    if ball.ycor() == -310 and paddle.distance(ball) <= 83:
        ball.paddle_bounce()

    for block in blocks:
        if block.distance(ball) < 30:
            ball.bounce_y()
            block.reset()
            block.goto(1000, 1000)

#     paddle bounce
#     press space to start
#     reset ball after > ycor() 400

screen.exitonclick()
Exemple #7
0
screen.onkey(paddle2.up, "w")
screen.onkey(paddle2.down, "s")

# main game loop
game_is_on = True
while game_is_on:
    screen.update()
    time.sleep(ball.move_speed)

    # detect collision with right paddle
    if ball.distance(paddle1) < 50 and ball.xcor() > RIGHT - 20:
        ball.setheading(180 - ball.heading())
        ball.move_speed *= 0.09

    # detect collision with left paddle
    if paddle2.distance(ball) < 50 and ball.xcor() < LEFT + 20:
        ball.setheading(180 - ball.heading())
        ball.move_speed *= 0.09

    # detect miss / score / game
    if ball.xcor() > RIGHT + 30:
        scoreboard.increase_score(2)
        ball.back_to_origin()
        screen.delay(5)

    if ball.xcor() < LEFT - 30:
        scoreboard.increase_score(1)
        ball.back_to_origin()
        screen.delay(5)

    ball.move()
Exemple #8
0
screen.listen()
screen.onkey(paddle.move_left, "a")
screen.onkey(paddle.move_right, "d")

# game
game = True
while game:
    ball.move()
    # wall clash
    if ball.xcor() > 290 or ball.xcor() < -290:
        ball.x_bounce()
    if ball.ycor() > 250:
        ball.y_bounce()

    # paddle clash
    if ball.ycor() < -180 and paddle.distance(ball) < 70:
        ball.y_bounce()
    elif ball.ycor() < -260:
        scoreboard.lost_live()
        paddle.restart()
        ball.restart()
        if scoreboard.lives == 0:
            scoreboard.game_over()
            game = False

    # clash block
    for n in blocks.all_blocks:
        if n.distance(ball.xcor() + 30, ball.ycor()) <= 15 or n.distance(ball.xcor() - 30, ball.ycor()) <= 15:
            scoreboard.add_score()
            ball.x_bounce()
            blocks.all_blocks.remove(n)
Exemple #9
0
screen.listen()
screen.onkey(player_right.move_up, "Up")
screen.onkey(player_right.move_down, "Down")
screen.onkey(player_left.move_up, "w")
screen.onkey(player_left.move_down, "s")

while True:
    time.sleep(ball.ball_speed)
    screen.update()

    ball.move()
    if ball.ycor() >= 280 or ball.ycor() <= -280:
        ball.bounce_y()

    if ball.xcor() > 332 and ball.xcor() < 360 and player_right.distance(
            ball) < 70 or ball.xcor() < -332 and ball.xcor(
            ) > -360 and player_left.distance(ball) < 70:
        ball.speed("fastest")
        ball.bounce_x()

    # Right Paddle misses the ball
    if ball.xcor() > 390:
        ball.refresh()
        score.left_score()

    # Left Paddle misses the ball
    if ball.xcor() < -390:
        ball.refresh()
        score.right_score()

screen.exitonclick()
Exemple #10
0
    tim.penup()
    tim.forward(20)
# screen.update()
# screen.tracer(1,10)

sleep_time = 0.05
is_game_on = True
while is_game_on:
    time.sleep(sleep_time)
    screen.update()
    ball.refresh()
    # check collision with left or right paddle
    if ball.ycor() > 280 or ball.ycor() < -280:
        ball.bounce()
    if 330 < ball.xcor() < 370 or -370 < ball.xcor() < -330:
        if l_paddle.distance(ball) <= 50:
            sleep_time *= 0.9
            ball.l_paddle_hit()
        elif r_paddle.distance(ball) <= 50:
            ball.r_paddle_hit()

    if ball.xcor() > 380:
        # print("Ball out of bound on right side")
        ball.reset_position()
        scoreboard.l_score_inc()
        sleep_time = 0.05

    if ball.xcor() < -380:
        # print("Ball out of bound on left side")
        ball.reset_position()
        scoreboard.r_score_inc()