1+ import os
2+ import turtle as t
3+
4+ playerAscore = 0
5+ playerBscore = 0
6+
7+ # create a window and declare a variable called window and call the screen()
8+ window = t .Screen ()
9+ window .title ("The Pong Game" )
10+ window .bgcolor ("green" )
11+ window .setup (width = 800 , height = 600 )
12+ window .tracer (0 )
13+
14+ # Creating the left paddle
15+ leftpaddle = t .Turtle ()
16+ leftpaddle .speed (0 )
17+ leftpaddle .shape ("square" )
18+ leftpaddle .color ("white" )
19+ leftpaddle .shapesize (stretch_wid = 5 , stretch_len = 1 )
20+ leftpaddle .penup ()
21+ leftpaddle .goto (- 350 , 0 )
22+
23+ # Creating the right paddle
24+ rightpaddle = t .Turtle ()
25+ rightpaddle .speed (0 )
26+ rightpaddle .shape ("square" )
27+ rightpaddle .color ("white" )
28+ rightpaddle .shapesize (stretch_wid = 5 , stretch_len = 1 )
29+ rightpaddle .penup ()
30+ rightpaddle .goto (- 350 , 0 )
31+
32+ # Code for creating the ball
33+ ball = t .Turtle ()
34+ ball .speed (0 )
35+ ball .shape ("circle" )
36+ ball .color ("red" )
37+ ball .penup ()
38+ ball .goto (5 , 5 )
39+ ballxdirection = 0.2
40+ ballydirection = 0.2
41+
42+ # Code for creating pen for scorecard update
43+ pen = t .Turtle ()
44+ pen .speed (0 )
45+ pen .color ("Blue" )
46+ pen .penup ()
47+ pen .hideturtle ()
48+ pen .goto (0 , 260 )
49+ pen .write ("score" , align = "center" , font = ('Arial' , 24 , 'normal' ))
50+
51+
52+ # code for moving the leftpaddle
53+ def leftpaddleup ():
54+ y = leftpaddle .ycor ()
55+ y = y + 90
56+ leftpaddle .sety (y )
57+
58+
59+ def leftpaddledown ():
60+ y = leftpaddle .ycor ()
61+ y = y + 90
62+ leftpaddle .sety (y )
63+
64+
65+ # code for moving the rightpaddle
66+ def rightpaddleup ():
67+ y = rightpaddle .ycor ()
68+ y = y + 90
69+ rightpaddle .sety (y )
70+
71+
72+ def rightpaddledown ():
73+ y = rightpaddle .ycor ()
74+ y = y + 90
75+ rightpaddle .sety (y )
76+
77+
78+ # Assign keys to play
79+ window .listen ()
80+ window .onkeypress (leftpaddleup , 'w' )
81+ window .onkeypress (leftpaddledown , 's' )
82+ window .onkeypress (rightpaddleup , 'Up' )
83+ window .onkeypress (rightpaddledown , 'Down' )
84+
85+ while True :
86+ window .update ()
87+
88+ # moving the ball
89+ ball .setx (ball .xcor () + ballxdirection )
90+ ball .sety (ball .ycor () + ballxdirection )
91+
92+ # border set up
93+ if ball .ycor () > 290 :
94+ ball .sety (290 )
95+ ballydirection = ballydirection * - 1
96+ if ball .ycor () < - 290 :
97+ ball .sety (- 290 )
98+ ballydirection = ballydirection * - 1
99+
100+ if ball .xcor () <= 390 :
101+
102+
103+ ball .goto (0 , 0 )
104+ ball_dx = ball_dx * - 1
105+ player_a_score = player_a_score + 1
106+ pen .clear ()
107+ pen .write ("Player A: {} Player B: {} " .format (player_a_score , player_b_score ),
108+ align = "center" , font = ('Monaco' , 24 , "normal" ))
109+ os .system ("afplay wallhit.wav&" )
110+
111+ if (ball .xcor ()) < - 390 : # Left width paddle Border
112+ ball .goto (0 , 0 )
113+ ball_dx = ball_dx * - 1
114+ player_b_score = player_b_score + 1
115+ pen .clear ()
116+ pen .write ("Player A: {} Player B: {} " .format (player_a_score , player_b_score ),
117+ align = "center" , font = ('Monaco' , 24 , "normal" ))
118+ os .system ("afplay wallhit.wav&" )
119+
120+ # Handling the collisions with paddles.
121+
122+ if (ball .xcor () > 340 ) and (ball .xcor () < 350 ) and (
123+ ball .ycor () < rightpaddle .ycor () + 40 and ball .ycor () > rightpaddle .ycor () - 40 ):
124+ ball .setx (340 )
125+ ball_dx = ball_dx * - 1
126+ os .system ("afplay paddle.wav&" )
127+
128+ if (ball .xcor () < - 340 ) and (ball .xcor () > - 350 ) and (
129+ ball .ycor () < leftpaddle .ycor () + 40 and ball .ycor () > leftpaddle .ycor () - 40 ):
130+ ball .setx (- 340 )
131+ ball_dx = ball_dx * - 1
132+ os .system ("afplay paddle.wav&" )
0 commit comments