-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (40 loc) · 1.24 KB
/
main.py
File metadata and controls
59 lines (40 loc) · 1.24 KB
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
54
55
56
57
58
59
from paddle import Paddle
from turtle import Screen
from ball import Ball
from scoreboard import Scoreboard
import time
screen = Screen()
screen.bgcolor("black")
screen.setup(800, 600)
screen.title("Ping-Pong Game (Table Tennis)")
screen.listen()
screen.tracer(0)
r_paddle = Paddle((350,0))
l_paddle = Paddle((-350,0))
screen.onkey(r_paddle.move_up, "Up")
screen.onkey(r_paddle.move_down, "Down")
screen.onkey(l_paddle.move_up, "w")
screen.onkey(l_paddle.move_down, "s")
pong_ball = Ball()
scoreboard = Scoreboard()
game_is_on = True
while game_is_on:
time.sleep(pong_ball.move_speed)
screen.update()
pong_ball.move()
#detects if the ball detects the Y axis
if pong_ball.ycor() > 280 or pong_ball.ycor() < -280:
pong_ball.bounce_y()
# detects if the ball detects touches the paddle
if pong_ball.distance(r_paddle) < 50 and pong_ball.xcor() > 320 or pong_ball.distance(l_paddle) < 50 and pong_ball.xcor() < -320:
pong_ball.bounce_x()
# detects if the ball goes beyond the x axis
if pong_ball.xcor() > 380:
pong_ball.reset_position()
pong_ball.bounce_x()
scoreboard.l_point()
if pong_ball.xcor() < -380:
pong_ball.reset_position()
pong_ball.bounce_x()
scoreboard.r_point()
screen.exitonclick()