Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python | Program to extract frames using OpenCV
Next article icon

Pong is one of the most famous arcade games, simulating table tennis. 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. Turtle is an inbuilt graphic module inPython. It uses a panel and pen to depict illustrations. 

Below are the steps used:

  • Step 1) Create two paddles A and B on the left and right side of the screen.
  • Step 2) Create a ball.
  • Step 3) Create an event to move the paddle vertically on pressing a certain key.
  • Step 4) Create the function to update the score after each player misses a collision.

Below is the program to create Paddle and Ball:

Python
# Import required libraryimportturtle# Create screensc=turtle.Screen()sc.title("Pong game")sc.bgcolor("white")sc.setup(width=1000,height=600)# Left paddleleft_pad=turtle.Turtle()left_pad.speed(0)left_pad.shape("square")left_pad.color("black")left_pad.shapesize(stretch_wid=6,stretch_len=2)left_pad.penup()left_pad.goto(-400,0)# Right paddleright_pad=turtle.Turtle()right_pad.speed(0)right_pad.shape("square")right_pad.color("black")right_pad.shapesize(stretch_wid=6,stretch_len=2)right_pad.penup()right_pad.goto(400,0)# Ball of circle shapehit_ball=turtle.Turtle()hit_ball.speed(40)hit_ball.shape("circle")hit_ball.color("blue")hit_ball.penup()hit_ball.goto(0,0)hit_ball.dx=5hit_ball.dy=-5

Output:

Below is the complete python program to create pong game using turtle library.

Python
importturtleimporttime# Create screensc=turtle.Screen()sc.title("Pong game")sc.bgcolor("white")sc.setup(width=1000,height=600)# Left paddleleft_pad=turtle.Turtle()left_pad.speed(0)left_pad.shape("square")left_pad.color("black")left_pad.shapesize(stretch_wid=6,stretch_len=2)left_pad.penup()left_pad.goto(-400,0)# Right paddleright_pad=turtle.Turtle()right_pad.speed(0)right_pad.shape("square")right_pad.color("black")right_pad.shapesize(stretch_wid=6,stretch_len=2)right_pad.penup()right_pad.goto(400,0)# Ball of circle shapehit_ball=turtle.Turtle()hit_ball.speed(4)# Adjusted speedhit_ball.shape("circle")hit_ball.color("blue")hit_ball.penup()hit_ball.goto(0,0)hit_ball.dx=5hit_ball.dy=-5# Initialize the scoreleft_player=0right_player=0# Displays the scoresketch=turtle.Turtle()sketch.speed(0)sketch.color("blue")sketch.penup()sketch.hideturtle()sketch.goto(0,260)sketch.write("Left_player : 0    Right_player: 0",align="center",font=("Courier",24,"normal"))# Functions to move paddlesdefpaddleaup():y=left_pad.ycor()ify<250:# Limit paddle movementy+=20left_pad.sety(y)defpaddleadown():y=left_pad.ycor()ify>-240:# Limit paddle movementy-=20left_pad.sety(y)defpaddlebup():y=right_pad.ycor()ify<250:# Limit paddle movementy+=20right_pad.sety(y)defpaddlebdown():y=right_pad.ycor()ify>-240:# Limit paddle movementy-=20right_pad.sety(y)# Keyboard bindingssc.listen()sc.onkeypress(paddleaup,"w")# Changed to 'w'sc.onkeypress(paddleadown,"s")# Changed to 's'sc.onkeypress(paddlebup,"Up")sc.onkeypress(paddlebdown,"Down")# Main game loopwhileTrue:sc.update()time.sleep(0.01)# Add delay to make game smootherhit_ball.setx(hit_ball.xcor()+hit_ball.dx)hit_ball.sety(hit_ball.ycor()+hit_ball.dy)# Checking bordersifhit_ball.ycor()>280:hit_ball.sety(280)hit_ball.dy*=-1ifhit_ball.ycor()<-280:hit_ball.sety(-280)hit_ball.dy*=-1ifhit_ball.xcor()>500:hit_ball.goto(0,0)hit_ball.dy*=-1left_player+=1sketch.clear()sketch.write("Left_player :{}    Right_player:{}".format(left_player,right_player),align="center",font=("Courier",24,"normal"))ifhit_ball.xcor()<-500:hit_ball.goto(0,0)hit_ball.dy*=-1right_player+=1sketch.clear()sketch.write("Left_player :{}    Right_player:{}".format(left_player,right_player),align="center",font=("Courier",24,"normal"))# Paddle ball collisionif(hit_ball.xcor()>360andhit_ball.xcor()<370)and \(hit_ball.ycor()<right_pad.ycor()+50andhit_ball.ycor()>right_pad.ycor()-50):hit_ball.setx(360)hit_ball.dx*=-1if(hit_ball.xcor()<-360andhit_ball.xcor()>-370)and \(hit_ball.ycor()<left_pad.ycor()+50andhit_ball.ycor()>left_pad.ycor()-50):hit_ball.setx(-360)hit_ball.dx*=-1

Output:


Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp