-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
56 lines (44 loc) · 1.17 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
import time
from turtle import Screen
from paddle import Paddle
from ball import Ball
from scoreboard import Scoreboard
screen = Screen()
screen.bgcolor("black")
screen.setup(width=800, height=600)
screen.title("Pong")
screen.tracer(0)
r_paddle = Paddle(350)
l_paddle = Paddle(-350)
scoreboard = Scoreboard()
screen.listen()
screen.onkey(r_paddle.up, "Up")
screen.onkey(r_paddle.down, "Down")
screen.onkey(l_paddle.up, "w")
screen.onkey(l_paddle.down, "s")
game_on = True
dir = 1
ball = Ball(dir)
while game_on:
time.sleep(ball.move_speed)
ball.move()
screen.update()
# Detect collision with wall
if ball.ycor() > 280 or ball.ycor() < -280:
ball.turn_y()
# Detect collision with paddle
if ball.distance(r_paddle) < 50 and ball.xcor() > 320:
ball.turn_x()
if ball.distance(l_paddle) < 50 and ball.xcor() < -320:
ball.turn_x()
if ball.xcor() > 370:
ball.move_speed = 0.1
ball.goto(0, 0)
ball.turn_x()
scoreboard.increase_score_l()
if ball.xcor() < -370:
ball.move_speed = 0.1
ball.goto(0, 0)
ball.turn_x()
scoreboard.increase_score_r()
screen.exitonclick()