-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
51 lines (41 loc) · 1.25 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from turtle import Turtle, Screen
from paddle import Paddle
from ball import Ball
from scoreboard import ScoreBoard
import time
screen = Screen()
screen.setup(800, 600)
screen.bgcolor("black")
screen.title("Mr. Pong")
screen.tracer(0)
r_paddle = Paddle((350, 0))
l_paddle = Paddle((-350, 0))
ball = Ball()
# ball.move()
scoreboard = ScoreBoard()
screen.listen()
screen.onkey(r_paddle.go_up, "Up")
screen.onkey(r_paddle.go_down, "Down")
screen.onkey(l_paddle.go_up, "w")
screen.onkey(l_paddle.go_down, "s")
# paddle.move_user()
game_is_on = True
while game_is_on:
screen.update()
ball.move()
time.sleep(ball.ball_speed)
# Detect collision with wall and bounce
if ball.ycor() >= 280 or ball.ycor() <= -280:
ball.bounce_y()
# Detect when ball misses the right paddle
if ball.xcor() >= 400:
ball.reset_position()
scoreboard.l_point()
# Detect when ball misses the left paddle
if ball.xcor() <= -400:
ball.reset_position()
scoreboard.r_point()
# Detect collision with both paddles
if (ball.distance(l_paddle) < 50 and ball.xcor() < -320) or (ball.distance(r_paddle) < 50 and ball.xcor() > 320):
ball.bounce_x()
screen.exitonclick()