-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
55 lines (42 loc) · 1.32 KB
/
Copy pathmain.py
File metadata and controls
55 lines (42 loc) · 1.32 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
from turtle import Screen
from paddle import Paddle
from ball import Ball
import time
from scoreboard import Scoreboard
# SETUP SCREEN
screen = Screen()
screen.setup(width=800, height=800)
screen.bgcolor("black")
screen.title('Pong')
screen.tracer(0) # to remove the animation
# SET UP OBJECTS
r_paddle = Paddle((350, 0))
l_paddle = Paddle((-350, 0))
ball = Ball()
scoreboard = Scoreboard()
# PADDLE MOVEMENTS
screen.listen()
screen.onkey(key='m', fun=r_paddle.paddleup)
screen.onkey(key="Down", fun=r_paddle.paddledown)
screen.onkey(key='w', fun=l_paddle.paddleup)
screen.onkey(key="s", fun=l_paddle.paddledown)
game_on = True
while game_on:
time.sleep(0.1)
screen.update() # this updates the screen after creating the turtle to cremove the animation
ball.moveright()
# detect collision with up and down walls
if ball.ycor() > 330 or ball.ycor() < -350:
ball.wall_bounce()
# detect collision with paddle
if ball.distance(r_paddle) < 50 and ball.xcor() > 340 or ball.distance(l_paddle) <50 and ball.xcor() < -340:
ball.paddle_bounce()
# detect right paddle misses
if ball.xcor() > 380:
ball.reset_position()
scoreboard.l_point()
# detect Left paddle misses
if ball.xcor() < -380:
ball.reset_position()
scoreboard.r_point()
screen.exitonclick()