Skip to content

Commit a14841e

Browse files
authored
Merge pull request larymak#121 from FAHID-KHAN/ping_pong_game
Pong Game in python
2 parents 67c4c1d + c96861c commit a14841e

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

Pong Game in Python/PingPong.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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&")

Pong Game in Python/Readme1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
About System
2+
The project file contains python scripts (Pong.py). Talking about the gameplay, the user has to strike back the moving ping ball towards the opponent court. If he/she fails to touch the ball, then the opponent player wins that round. And the game will be over after the 20 continuous wins of the same team. The pc control of the game is also very simple. The left-side player has to use W and S keys to move or slide the stick, while the right-side player has to use up and down arrow keys. The gameplay design is so simple that the user won’t find it difficult to use and understand.

0 commit comments

Comments
 (0)