20% Beneficiary to @siz-official of this amazing content.
Hello Guys,
Most of us have overhear about the famous pong game and many of us love playing it. Today lets we grasp how to code this classic game using the python programming language!
Before start coding we first need to install the turtle module. The turtle module is a library of Python Language that allows the user to make pictures and shapes by providing them with a virtual canvas.
Step#1
If have not installed the library, Install the library using pip in command prompt or ShellC:\Users\Admin>pip install turtle
Step#2
First we need to open source where we can write code VScode,Pycharm,Shell etc. Pycharm supports extra features for python I'll prefere you to use Pycharm for python. import turtle as t
playerAscore=0
playerBscore=0
Step#3:
#create a window and declare a variable called window and call the screen() window=t.Screen()
window.title("The Pong Game")
window.bgcolor("green")
window.setup(width=800,height=600)
window.tracer(0)
Step#4
We're starting to create the left paddle. leftpaddle=t.Turtle()
leftpaddle.speed(0)
leftpaddle.shape("square")
leftpaddle.color("white")
leftpaddle.shapesize(stretch_wid=5,stretch_len=1)
leftpaddle.penup()
leftpaddle.goto(-350,0)
Step#5
Now we're creating the right paddle. rightpaddle=t.Turtle()
rightpaddle.speed(0)
rightpaddle.shape("square")
rightpaddle.color("white")
rightpaddle.shapesize(stretch_wid=5,stretch_len=1)
rightpaddle.penup()
rightpaddle.goto(-350,0)
Step#6
Code for creating the ball ball=t.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("red")
ball.penup()
ball.goto(5,5)
ballxdirection=0.2
ballydirection=0.2
Step#7
Code for creating pen for scorecard update. pen=t.Turtle()
pen.speed(0)
pen.color("Blue")
pen.penup()
pen.hideturtle()
pen.goto(0,260)
pen.write("score",align="center",font=('Arial',24,'normal'))
Step #8
Code for moving left and right paddle.def leftpaddleup():
y=leftpaddle.ycor()
y=y+90
leftpaddle.sety(y)
def leftpaddledown():
y=leftpaddle.ycor()
y=y+90
leftpaddle.sety(y)
#moving the rightpaddle
def rightpaddleup():
y=rightpaddle.ycor()
y=y+90
rightpaddle.sety(y)
def rightpaddledown():
y=rightpaddle.ycor()
y=y+90
rightpaddle.sety(y)
Step#9
Here we are working for output, Assigning keys for output.window.listen()
window.onkeypress(leftpaddleup,'w')
window.onkeypress(leftpaddledown,'s')
window.onkeypress(rightpaddleup,'Up')
window.onkeypress(rightpaddledown,'Down')
while True:
window.update()
#moving the ball
ball.setx(ball.xcor()+ballxdirection)
ball.sety(ball.ycor()+ballxdirection)
#border set up
if ball.ycor()>290:
ball.sety(290)
ballydirection=ballydirection*-1
if ball.ycor()<-290:
ball.sety(-290)
ballydirection=ballydirection*-1
if ball.xcor() > 390:
ball.goto(0,0)
ball_dx = ball_dx * -1
player_a_score = player_a_score + 1
pen.clear()
pen.write("Player A: {} Player B: {} ".format(player_a_score,player_b_score),align="center",font=('Monaco',24,"normal"))
os.system("afplay wallhit.wav&")
if(ball.xcor()) < -390: # Left width paddle Border
ball.goto(0,0)
ball_dx = ball_dx * -1
player_b_score = player_b_score + 1
pen.clear()
pen.write("Player A: {} Player B: {} ".format(player_a_score,player_b_score),align="center",font=('Monaco',24,"normal"))
os.system("afplay wallhit.wav&")
Step#10
Here we're working for collision provision, It might possible that paddles collide so code for collision of paddles. if(ball.xcor() > 340) and (ball.xcor() < 350) and (ball.ycor() < paddle_right.ycor() + 40 and ball.ycor() > paddle_right.ycor() - 40):
ball.setx(340)
ball_dx = ball_dx * -1
os.system("afplay paddle.wav&")
if(ball.xcor() < -340) and (ball.xcor() > -350) and (ball.ycor() < paddle_left.ycor() + 40 and ball.ycor() > paddle_left.ycor() - 40):
ball.setx(-340)
ball_dx = ball_dx * -1
os.system("afplay paddle.wav&")
Output
Conclusion
It was a great effort hope you guys like please comment if you guys have any problem. ThankYouNote
Please take care of indentation when using this code.Special Thanks:
@siz-official
@cryptokraze
@suboohi
@vvarishayy
@steemcurator07
@steemcurator01
You can delegate your SP to @siz-official and take benefit of our Delegation Rewards Offer of getting up to 200% Curation Rewards
You can also use these quick delegation links. Just click the amount of SP you want to delegate and it will be done in process of 1 minute.
50 100 200 300 400 500 1000
2000 3000 4000 5000 10000
@cryptokraze | @vvarishayy | @suboohi | @ashkhan | @faran-nabeel
Good one Post dear friend you make a very good post thanks for sharing a good information with us my best wishes for you.
Regards, Faran Nabeel
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Congratulations your post has been selected in the best 5 post of the day. Keep Steeming :)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thankyou @vvarishayy :)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit