-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
52 lines (41 loc) · 1.19 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
import time
from turtle import Screen
from ball import Ball
from paddle import Paddle
from scoreboard import ScoreBoard
screen = Screen()
r_paddle = Paddle((350, 0))
l_paddle = Paddle((-350, 0))
ball = Ball()
scoreboard = ScoreBoard()
# Create background
screen.setup(width=800, height=600)
screen.bgcolor("black")
screen.title("Pong Game")
screen.tracer(0)
# Listen to the keys
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")
game_is_on = True
while game_is_on:
time.sleep(ball.ball_speed)
screen.update()
ball.move()
# Detect collision with wall and bounce
if ball.ycor() > 280 or ball.ycor() < -270:
ball.bounce_y()
# Detect collision with peddle
if ball.distance(r_paddle) < 60 and ball.xcor() > 320 or ball.distance(l_paddle) < 60 and ball.xcor() < -320:
ball.bounce_x()
# Detect if r peddle misses
if ball.xcor() > 340:
ball.reset_position()
scoreboard.l_point()
# Detect if l peddle misses
if ball.xcor() < -340:
ball.reset_position()
scoreboard.r_point()
screen.exitonclick()