-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
91 lines (74 loc) · 2.85 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from turtle import Screen
from paddle import Paddle
from ball import Ball
from score import ScoreBoard
import time
SCREEN_NAME = "PONG GAME"
WIDTH = 800
HEIGHT = 400
BACKCOLOR = "black"
PADDLE_MARGIN = 20
COLLISION_MARGIN_WALL = 20
COLLISION_MARGIN_PADDLE = 50
BALL_PADDLE_MIDDLE_DISTANCE_MARGIN = 20
SCOREBOARD_POSITION_LEFT = (-200, 100)
SCOREBOARD_POSITION_RIGHT = (200, 100)
WINNER_BOARD_POSITION = (0, 100)
LEFT_PADDLE_COLOR = "red"
RIGHT_PADDLE_COLOR = "green"
WINNING_SCORE = 10
def play_game():
screen = Screen()
screen.setup(WIDTH, HEIGHT)
screen.title(SCREEN_NAME)
screen.bgcolor(BACKCOLOR)
screen.tracer(0)
left_paddle = Paddle("left", LEFT_PADDLE_COLOR)
right_paddle = Paddle("right", RIGHT_PADDLE_COLOR)
ball = Ball()
left_score = ScoreBoard(SCOREBOARD_POSITION_LEFT[0], SCOREBOARD_POSITION_LEFT[1])
right_score = ScoreBoard(SCOREBOARD_POSITION_RIGHT[0], SCOREBOARD_POSITION_RIGHT[1])
winner_scoreboard = ScoreBoard(WINNER_BOARD_POSITION[0], WINNER_BOARD_POSITION[1], False)
screen.listen()
screen.onkey(left_paddle.up, "a")
screen.onkey(left_paddle.down, "z")
screen.onkey(right_paddle.up, "Up")
screen.onkey(right_paddle.down, "Down")
game_on = True
while game_on:
time.sleep(ball.moving_speed)
screen.update()
ball.move()
# detect collision with walls
if abs(ball.ycor()) >= ball.y_boundary - COLLISION_MARGIN_WALL:
ball.bounce_off_wall()
# detect collision with paddles
if ball.distance(right_paddle) <= COLLISION_MARGIN_PADDLE \
and abs(ball.xcor()) >= abs(right_paddle.xcor()) - BALL_PADDLE_MIDDLE_DISTANCE_MARGIN:
ball.bounce_off_paddle("left")
ball.increase_speed()
if ball.distance(left_paddle) <= COLLISION_MARGIN_PADDLE \
and abs(ball.xcor()) >= abs(left_paddle.xcor()) - BALL_PADDLE_MIDDLE_DISTANCE_MARGIN:
ball.bounce_off_paddle("right")
ball.increase_speed()
# detect a miss
if abs(ball.xcor()) >= ball.x_boundary:
if ball.xcor() < 0:
ball.reset_ball("right")
right_score.update_score()
else:
ball.reset_ball("left")
left_score.update_score()
# check for winner
if right_score.score >= WINNING_SCORE:
winner_scoreboard.declare_winner(RIGHT_PADDLE_COLOR)
game_on = False
if left_score.score >= WINNING_SCORE:
winner_scoreboard.declare_winner(LEFT_PADDLE_COLOR)
game_on = False
play_again = screen.textinput("Play Again", "Do you want to play again ? (Y or N) : ")
if play_again.upper() == "Y":
screen.clear()
play_game()
screen.exitonclick()
play_game()