Skip to content

Commit 7cacaa9

Browse files
authored
Create Pong_Game.py
Created a basic pong game
1 parent f3dabfe commit 7cacaa9

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

Pong_Game.py

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

0 commit comments

Comments
 (0)