-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
53 lines (42 loc) · 1.1 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
52
53
from turtle import Turtle, Screen
from paddle import Paddle
from ball import Ball
import time
from scoreboard import Scoreboard
s = Screen()
t = Turtle()
ball = Ball()
scoreboard = Scoreboard()
r_paddle = Paddle((350, 0))
l_paddle = Paddle((-350, 0))
s.setup(width=800, height=600)
s.bgcolor("black")
s.title("Pong")
s.tracer(0)
s.listen()
s.onkey(r_paddle.go_up, "Up")
s.onkey(r_paddle.go_down, "Down")
s.onkey(l_paddle.go_up, "w")
s.onkey(l_paddle.go_down, "s")
game_on = True
while game_on:
time.sleep(ball.move_speed)
s.update()
ball.move()
# Detect collision with the wall
if ball.ycor() > 280 or ball.ycor() < -280:
ball.bounce_y()
# Detect collision with the paddle
if ball.distance(r_paddle) < 50 and ball.xcor() > 320 or ball.distance(l_paddle) < 50 and ball.xcor() < -320:
ball.bounce_x()
# Detect R paddle misses
if ball.xcor() > 380:
ball.reset()
ball.bounce_x()
scoreboard.l_point()
# Detect L paddle misses
if ball.xcor() < -380:
ball.reset()
ball.bounce_x()
scoreboard.r_point()
s.exitonclick()