-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (50 loc) · 1.35 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
54
55
56
57
58
59
60
61
62
63
64
from turtle import Screen, Turtle
from paddle import Paddle
from ball import Ball
from scoreboard import Scoreboard
import time
screen = Screen()
screen.setup(width=800, height=600)
screen.bgcolor("black")
screen.title("My Classic Pong Game!")
screen.tracer(0)
net = Turtle()
net.hideturtle()
net.color("white")
net.penup()
net.goto(0, -300)
net.setheading(90)
for _ in range(60):
net.pendown()
net.forward(10)
net.penup()
net.forward(10)
r_paddle = Paddle((350, 0))
l_paddle = Paddle((-350, 0))
ball = Ball()
scoreboard = Scoreboard()
screen.listen()
screen.onkeypress(r_paddle.go_up, "Up")
screen.onkeypress(r_paddle.go_down, "Down")
screen.onkeypress(l_paddle.go_up, "w")
screen.onkeypress(l_paddle.go_down, "s")
is_game_on = True
while is_game_on:
time.sleep(ball.move_speed)
screen.update()
ball.move()
# Detect collision with wall.
if ball.ycor() > 280 or ball.ycor() < -280:
ball.bounce_y()
# Detect collision with 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 miss.
if ball.xcor() > 400:
ball.reset_position()
scoreboard.l_point()
# Detect L paddle miss.
if ball.xcor() < -400:
ball.reset_position()
scoreboard.r_point()
screen.exitonclick()