Skip to content

Commit 16c2107

Browse files
Add files via upload
1 parent 05ef8d2 commit 16c2107

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

Diff for: PyGamesScripts/Ping-Pong/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ping-Pong
2+
### This repo contains program for a two player ping pong game made using turtle.
3+
4+
#### Each player controls a paddle in the game by dragging it vertically across the screen’s left or right side. Players use their paddles to strike back and forth on the ball.
5+
![Sample-output](https://user-images.githubusercontent.com/75221153/112730913-4442f700-8f5a-11eb-881e-295664b4cd51.jpeg)

Diff for: PyGamesScripts/Ping-Pong/ping-pong.py

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# pong
2+
import os
3+
import turtle
4+
5+
wn = turtle.Screen()
6+
wn.title("Ping-Pong by PRAGYA KHANNA")
7+
wn.bgcolor("purple")
8+
wn.setup(width=800, height=600)
9+
wn.tracer(0) # stops the window from updating
10+
# game runs faster
11+
12+
# prompt user for names
13+
player1 = turtle.textinput("Personal Detail", "Player 1")
14+
player2 = turtle.textinput("Personal Detail", "Player 2")
15+
16+
# turtle.write("hello")
17+
18+
# Score
19+
score_a = 0
20+
score_b = 0
21+
22+
# Paddle A
23+
paddle_a = turtle.Turtle()
24+
paddle_a.speed(0)
25+
paddle_a.shape("square")
26+
paddle_a.color("white")
27+
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
28+
paddle_a.penup()
29+
paddle_a.goto(-350, 0)
30+
31+
# Paddle B
32+
paddle_b = turtle.Turtle()
33+
paddle_b.speed(0)
34+
paddle_b.shape("square")
35+
paddle_b.color("white")
36+
paddle_b.shapesize(stretch_wid=5, stretch_len=1)
37+
paddle_b.penup()
38+
paddle_b.goto(350, 0)
39+
40+
# Ball
41+
ball = turtle.Turtle()
42+
ball.speed(0)
43+
ball.shape("circle")
44+
ball.color("yellow")
45+
ball.penup()
46+
# ball.goto(0, 0)
47+
ball.dx = 0.5
48+
ball.dy = 0.5
49+
50+
# Pen
51+
pen = turtle.Turtle()
52+
pen.speed(0)
53+
pen.shape("square")
54+
pen.color("white")
55+
pen.penup()
56+
pen.hideturtle()
57+
pen.goto(0, 260)
58+
# pen.write("Player A: 0 Player B: 0", align="center", font=("Courier", 24, "normal"))
59+
pen.write("{}: 0 {}: 0".format(player1, player2), align="center", font=("Courier", 24, "normal"))
60+
61+
62+
# Function
63+
def paddle_a_up():
64+
y = paddle_a.ycor()
65+
y += 20
66+
paddle_a.sety(y)
67+
68+
69+
def paddle_a_down():
70+
y = paddle_a.ycor()
71+
y -= 20
72+
paddle_a.sety(y)
73+
74+
75+
def paddle_b_up():
76+
y = paddle_b.ycor()
77+
y += 20
78+
paddle_b.sety(y)
79+
80+
81+
def paddle_b_down():
82+
y = paddle_b.ycor()
83+
y -= 20
84+
paddle_b.sety(y)
85+
86+
87+
# Keyboard bindings
88+
wn.listen() # listen for keyboard input
89+
wn.onkeypress(paddle_a_up, "w") # up when w is pressed
90+
wn.onkeypress(paddle_a_down, "s")
91+
wn.onkeypress(paddle_b_up, "Up")
92+
wn.onkeypress(paddle_b_down, "Down")
93+
94+
# Main game loop
95+
while True:
96+
wn.update() # every time the loop runs it updates the screen
97+
98+
# Move the ball
99+
ball.setx(ball.xcor() + ball.dx)
100+
ball.sety(ball.ycor() + ball.dy)
101+
102+
# Border checking
103+
104+
# Top and bottom
105+
if ball.ycor() > 290:
106+
ball.sety(290)
107+
ball.dy *= -1
108+
os.system("afplay bounce.wav&")
109+
110+
elif ball.ycor() < -290:
111+
ball.sety(-290)
112+
ball.dy *= -1
113+
os.system("afplay bounce.wav&")
114+
115+
# Left and right
116+
if ball.xcor() > 350:
117+
score_a += 1
118+
pen.clear()
119+
pen.write("{}: {} {}: {}".format(player1, score_a, player2, score_b), align="center",
120+
font=("Courier", 24, "normal"))
121+
ball.goto(0, 0)
122+
ball.dx *= -1
123+
124+
elif ball.xcor() < -350:
125+
score_b += 1
126+
pen.clear()
127+
pen.write("{}: {} {}: {}".format(player1, score_a, player2, score_b), align="center",
128+
font=("Courier", 24, "normal"))
129+
ball.goto(0, 0)
130+
ball.dx *= -1
131+
# Paddle and ball collisions
132+
if ball.xcor() < -340 and paddle_a.ycor() + 50 > ball.ycor() > paddle_a.ycor() - 50:
133+
ball.dx *= -1
134+
os.system("afplay bounce.wav&")
135+
136+
elif ball.xcor() > 340 and paddle_b.ycor() + 50 > ball.ycor() > paddle_b.ycor() - 50:
137+
ball.dx *= -1
138+
os.system("afplay bounce.wav&")

0 commit comments

Comments
 (0)