-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
64 lines (52 loc) · 1.67 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
import turtle
from turtle import Screen
from os import path
from time import sleep
from paddle import Paddle
from ball import Ball
from scoreboard import Scoreboard
# creating game screen and initial setup
screen = Screen()
screen.setup(width=800, height=600)
screen.bgcolor("black")
screen.title("Pong Game created by YJ-928")
screen.tracer(0)
# creating paddle obj from Paddle class
paddle = Paddle()
# creating ball obj from Ball class
ball = Ball()
# creating scoreboard obj from Scoreboard class
scoreboard = Scoreboard()
# listen to input user key_press
# onkeypress() to continously move paddle up or down
# while the key is pressed
screen.listen()
screen.onkeypress(paddle.lhs_move_up, "w")
screen.onkeypress(paddle.lhs_move_down, "s")
screen.onkeypress(paddle.rhs_move_up, "Up")
screen.onkeypress(paddle.rhs_move_down, "Down")
# loop condition
game_is_on = True
# loop to continously update/refresh the screen
while game_is_on:
screen.update()
sleep(ball.move_speed)
ball.move()
# detect collision with wall
if ball.ycor() > 280 or ball.ycor() < -280:
ball.bounce_y_axis()
# detect collision with the paddles
if ball.distance(paddle.rhs_paddle) < 50 and ball.xcor() > 320 or ball.distance(paddle.lhs_paddle) < 50 and ball.xcor() < -320:
ball.bounce_x_axis()
# detect when ball goes beyond RHS Paddle
# then LHS player scores a point
if ball.xcor() > 400:
ball.reset()
scoreboard.lhs_point()
# detect when ball goes beyond LHS Paddle
# then RHS player scores a point
if ball.xcor() < -400:
ball.reset()
scoreboard.rhs_point()
# to exit only when clicked on screen
screen.exitonclick()