Skip to content

Commit 5d18f05

Browse files
authored
Add files via upload
1 parent 399b2ea commit 5d18f05

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

Diff for: python/ping pong game1.py

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import os
2+
import turtle
3+
4+
# First, we will create screen
5+
screen_1 = turtle.Screen()
6+
screen_1.title("Ping-Pong Game")
7+
screen_1.bgcolor("Yellow")
8+
screen_1.setup(width=1050, height=650)
9+
10+
# Left paddle
11+
left_paddle = turtle.Turtle()
12+
left_paddle.speed(0)
13+
left_paddle.shape("square")
14+
left_paddle.color("Red")
15+
left_paddle.shapesize(stretch_wid=6, stretch_len=2)
16+
left_paddle.penup()
17+
left_paddle.goto(-400, 0)
18+
19+
# Right paddle
20+
right_paddle = turtle.Turtle()
21+
right_paddle.speed(0)
22+
right_paddle.shape("square")
23+
right_paddle.color("Blue")
24+
right_paddle.shapesize(stretch_wid=6, stretch_len=2)
25+
right_paddle.penup()
26+
right_paddle.goto(400, 0)
27+
28+
# Ball of circle shape
29+
hit_ball = turtle.Turtle()
30+
hit_ball.speed(45)
31+
hit_ball.shape("circle")
32+
hit_ball.color("Black")
33+
hit_ball.penup()
34+
hit_ball.goto(0, 0)
35+
hit_ball.dx = 5
36+
hit_ball.dy = -5
37+
38+
# Now, we will initialize the score
39+
left_player = 0
40+
right_player = 0
41+
42+
# Displaying of the score
43+
sketch_1 = turtle.Turtle()
44+
sketch_1.speed(0)
45+
sketch_1.color("blue")
46+
sketch_1.penup()
47+
sketch_1.hideturtle()
48+
sketch_1.goto(0, 260)
49+
sketch_1.write("Left Player : 0 Right Player: 0",
50+
align="center", font=("Courier", 24, "normal"))
51+
52+
53+
# Implementing the functions for moving paddle vertically
54+
def paddle_L_up():
55+
y = left_paddle.ycor()
56+
y += 20
57+
left_paddle.sety(y)
58+
59+
60+
def paddle_L_down():
61+
y = left_paddle.ycor()
62+
y -= 20
63+
left_paddle.sety(y)
64+
65+
66+
def paddle_R_up():
67+
y = right_paddle.ycor()
68+
y += 20
69+
right_paddle.sety(y)
70+
71+
72+
def paddle_R_down():
73+
y = right_paddle.ycor()
74+
y -= 20
75+
right_paddle.sety(y)
76+
77+
78+
# Then, binding the keys for moving the paddles up and down.
79+
screen_1.listen()
80+
screen_1.onkeypress(paddle_L_up, "r")
81+
screen_1.onkeypress(paddle_L_down, "c")
82+
screen_1.onkeypress(paddle_R_up, "Up")
83+
screen_1.onkeypress(paddle_R_down, "Down")
84+
85+
while True:
86+
screen_1.update()
87+
88+
hit_ball.setx(hit_ball.xcor() + hit_ball.dx)
89+
hit_ball.sety(hit_ball.ycor() + hit_ball.dy)
90+
91+
# Check all the borders
92+
if hit_ball.ycor() > 280:
93+
hit_ball.sety(280)
94+
hit_ball.dy *= -1
95+
96+
if hit_ball.ycor() < -280:
97+
hit_ball.sety(-280)
98+
hit_ball.dy *= -1
99+
100+
if hit_ball.xcor() > 500:
101+
hit_ball.goto(0, 0)
102+
hit_ball.dy *= -1
103+
left_player += 1
104+
sketch_1.clear()
105+
sketch_1.write("Left_player : {} Right_player: {}".format(
106+
left_player, right_player), align="center",
107+
font=("Courier", 24, "normal"))
108+
109+
if hit_ball.xcor() < -500:
110+
hit_ball.goto(0, 0)
111+
hit_ball.dy *= -1
112+
right_player += 1
113+
sketch_1.clear()
114+
sketch_1.write("Left_player : {} Right_player: {}".format(
115+
left_player, right_player), align="center",
116+
font=("Courier", 24, "normal"))
117+
118+
# Collision of ball and paddles
119+
if (hit_ball.xcor() > 360 and
120+
hit_ball.xcor() < 370) and (hit_ball.ycor() < right_paddle.ycor() + 40 and
121+
hit_ball.ycor() > right_paddle.ycor() - 40):
122+
hit_ball.setx(360)
123+
hit_ball.dx *= -1
124+
125+
if (hit_ball.xcor() < -360 and
126+
hit_ball.xcor() > -370) and (hit_ball.ycor() < left_paddle.ycor() + 40 and
127+
hit_ball.ycor() > left_paddle.ycor() - 40):
128+
hit_ball.setx(-360)
129+
hit_ball.dx *= -1

0 commit comments

Comments
 (0)