-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
60 lines (50 loc) · 1.56 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
import time
from turtle import Turtle, Screen
import random
from paddle import Paddle
from ball import Ball
from scoreboard import Scoreboard
game_over = False
winning_score = 5
screen = Screen()
screen_width = 800
screen_height =600
screen.setup(width = screen_width, height= screen_height)
screen.bgcolor('black')
screen.title("PONG")
screen.tracer(0)
paddle_r =Paddle((350,0))
paddle_l =Paddle((-350,0))
scoreboard_r = Scoreboard((200,200))
scoreboard_l = Scoreboard((-200,200))
ball = Ball()
screen.onkeypress(key="Up", fun=paddle_r.paddle_up)
screen.onkeypress(key="Down", fun=paddle_r.paddle_down)
screen.onkeypress(key="w", fun=paddle_l.paddle_up)
screen.onkeypress(key="s", fun=paddle_l.paddle_down)
screen.listen()
while(not game_over):
time.sleep(ball.sleep_speed)
screen.update()
ball.move()
#detect ball collision with top & abottom walls
if (abs(ball.ycor()) > ((screen_height-40)/2)):
ball.y_collision()
#detect ball collision with paddles
paddle_r.paddle_hit(ball)
paddle_l.paddle_hit(ball)
# detect the ball traveling past the paddle
if(abs(ball.xcor()) > ((screen_width-20)/2) ):
# increment score
if ball.xcor()>0:
scoreboard_l.add_score()
game_over = scoreboard_l.is_winner(winning_score)
if ball.xcor()<0:
scoreboard_r.add_score()
game_over = scoreboard_r.is_winner(winning_score)
# reset the ball
ball.reset()
ball.sleep_speed = 0.09
# update initial heading
ball.x_collision()
screen.exitonclick()