def dotcollision(ball, dot): ax = ball.xcor() ay = ball.ycor() bx = dot.xcor() by = dot.ycor() distance = math.sqrt(((ax - bx)**2) + ((ay - by)**2)) if (distance + 10) <= (ball.r + dot.r): return True else: return False
def main(): score_a = 0 score_b = 0 wnd.keyboard_binding(wd, paddle_a_up, paddle_a_down, paddle_b_up, paddle_b_down) pen.write("Player A: {} Player B: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal")) #Main game loop while True: wd.update() ball.setx(ball.xcor() + ball.dx) ball.sety(ball.ycor() + ball.dy) if ball.ycor() > 290: ball.sety(290) ball.dy *= -1 if ball.ycor() < -290: ball.sety(-290) ball.dy *= -1 if ball.xcor() > 390: ball.goto(0, 0) ball.dx *= -1 score_a += 1 pen.clear() pen.write("Player A: {} Player B: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal")) if ball.xcor() < -390: ball.goto(0, 0) ball.dx *= -1 score_b += 1 pen.clear() pen.write("Player A: {} Player B: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal")) if (ball.xcor() > 340 and ball.xcor() < 350) and ( ball.ycor() < paddle_b.ycor() + 40 and ball.ycor() > paddle_b.ycor() - 40): ball.setx(340) ball.dx *= -1 if (ball.xcor() < -340 and ball.xcor() > -350) and ( ball.ycor() < paddle_a.ycor() + 40 and ball.ycor() > paddle_a.ycor() - 40): ball.setx(-340) ball.dx *= -1
my_screen.onkey(r_paddle.goUp, "Up") my_screen.onkey(r_paddle.goDown, "Down") my_screen.onkey(l_paddle.goUp, "w") my_screen.onkey(l_paddle.goDown, "s") score = ScoreBoard() is_game_on = True while is_game_on: my_screen.update() ball.go() if ball.ycor() > 280 or ball.ycor() < -260: ball.bounce_y() if ball.distance(r_paddle) < 50 and ball.xcor() < -320 or ball.distance( l_paddle) < 50 and ball.xcor() > 320: ball.bounce_x() fast += 1 ball.speed(fast) elif ball.xcor() < -380: score.l_ascore() ball.skrrt() score.update_score() elif ball.xcor() > 380: score.r_ascore() ball.skrrt() score.update_score()
des_point = force.desired_point ball.apply_force(des_point[0], des_point[1]) screen.listen() screen.onkey(pong1.up, "w") screen.onkey(pong1.down, "s") screen.onkey(pong2.up, "Up") screen.onkey(pong2.down, "Down") game_over = False while not game_over: screen.update() if (ball.xcor() < x1): p1_score.increase_score() ball.init_ball() elif (ball.xcor() > x2): p2_score.increase_score() ball.init_ball() if (ball.distance(pong1) <= 50 or ball.distance(pong2) <= 50): des_point = ball.bounce(MAGNITUDE, 180) if (ball.ycor() > y2 or ball.ycor() < y1): des_point = ball.bounce(MAGNITUDE, 360) ball.apply_force(des_point[0], des_point[1]) screen.exitonclick()
ball = ball.Ball(size) scoreboard = Scoreboard() game_is_on = True # move paddles background.listen() background.onkey(key='Up', fun=r_paddle.up) background.onkey(key='Down', fun=r_paddle.down) background.onkey(key='o', fun=l_paddle.up) background.onkey(key='q', fun=l_paddle.down) while game_is_on: time.sleep(0.05) ball.move() background.update() x = ball.xcor() y = ball.ycor() if y > 280 or y < -280: ball.bounce_wall() elif x > 350 and ball.distance(r_paddle) < 50 or x < -350 and ball.distance(l_paddle) < 50: ball.bounce_paddle() elif x > 350: ball.reset_position() scoreboard.l_point() elif x < -350: scoreboard.r_point() ball.reset_position() # detect collision with wall # detect collision with paddle