Blogger page to publish content related to programming , AI , problem solving , interview questions , coding projects , programming languages ( python , C++ , C# , html, css , javascript , etc ) , frameworks and libraries.

ads 728x90

Friday, July 19, 2024

Creating a Ping-Pong Game in Python Using the Turtle library

 Creating a Ping-Pong Game in Python Using the Turtle library


This code is written in Python using the "turtle" library to create a ping-pong game. This tutorial covers all the basic parts of the code and explains how to set up a simple ping-pong game using the "turtle" library in Python.


import turtle


wind = turtle.Screen()

wind.bgcolor("black")

wind.title("ping pong game")

wind.setup(width=800, height=600)

wind.tracer(0)


#madrab1

madrab1 = turtle.Turtle()

madrab1.speed(0)

madrab1.shape("square")

madrab1.shapesize(stretch_wid=7, stretch_len=1)

madrab1.color("blue")

madrab1.penup()

madrab1.goto(-375, 0)


#madrab2

madrab2 = turtle.Turtle()

madrab2.speed(0)

madrab2.shape("square")

madrab2.shapesize(stretch_wid=5, stretch_len=1)

madrab2.color("red")

madrab2.penup()

madrab2.goto(375, 0)


#ball

ball = turtle.Turtle()

ball.speed(0)

ball.shape("circle")

ball.color("white")

ball.penup()

ball.goto(0, 0)

ball.dx = 0.5

ball.dy = 0.5


#score

score1 = 0

score2 = 0

score = turtle.Turtle()

score.speed(0)

score.color("white")

score.penup()

score.hideturtle()

score.goto(0, 260)

score.write("player 1: 0 player 2: 0" , align="center", font=("coureir" , 24, "normal"))

#Funtions

def madrab1_up():

  y = madrab1.ycor()

  y += 25

  madrab1.sety(y)

  

def madrab1_down():

   y = madrab1.ycor()

   y -= 25

   madrab1.sety(y)

def madrab2_up():

   y = madrab2.ycor()

   y += 25

   madrab2.sety(y)

def madrab2_down():

   y = madrab2.ycor()

   y -= 25

   madrab2.sety(y)


wind.listen()

wind.onkeypress(madrab1_up, "w")

wind.onkeypress(madrab1_down, "s")

wind.onkeypress(madrab2_up, "Up")

wind.onkeypress(madrab2_down, "Down")


while True:

  wind.update()

  #ball movement

  ball.setx(ball.xcor() + ball.dx)

  ball.sety(ball.ycor() + ball.dy)

  

  #border check

  if ball.ycor() > 290:

    ball.sety(290)

    ball.dy *= -1

    

  if ball.ycor() < -290:

    ball.sety(-290)

    ball.dy *= -1


  if ball.xcor() > 390:

   ball.goto(0, 0)

   ball.dx *= -1

   score1 += 1

   score.clear()

   score.write("player 1:{} player 2:{}".format(score1, 

   score2), align="center",font=("coureir" , 24, "normal"))

  

  if ball.xcor() < -390:

   ball.goto(0, 0)

   ball.dx *= -1

   score2 += 1

   score.clear()

   score.write("player 1:{} player 2:{}".format(score1, 

   score2), align="center",font=("coureir" , 24, "normal"))


  #

  if (ball.xcor() > 365 and ball.xcor() < 375) and (ball.ycor() < madrab2.ycor() + 40 and ball.ycor() > madrab2.ycor() -40):

    ball.setx(365)

    ball.dx *= -1

  if (ball.xcor() < -365 and ball.xcor() > -375) and (ball.ycor() < madrab1.ycor() + 60 and ball.ycor() > madrab1.ycor() -60):

     ball.setx(-365)

     ball.dx *= -1



Explanation of the code


 1. Window setting:

    - Create a game window using turtle.Screen() and set the background color to black, the title to "ping pong game", the window dimensions to 800 width and 600 height, and turn off automatic screen refresh using wind.tracer(0).


 2. Establishing Mudarib:

    - The first bat madrab1 is created as a square, with its size, position and blue color set.

    - The second racket madrab2 is created in the same way but in a red color and a different size.


 3. Create the ball:

    - The ball is created in the form of a circle, with its color white and its position in the middle of the screen, and its speed in the x- and y-directions (ball.dx and ball.dy).


 4. Set up the scoreboard:

    - The variables score1 and score2 are set to track scores, and a score object is created to display the score on the screen at the top of the window.


 5. Functions to move the rackets:

    - madrab1_up and madrab1_down to move the first bat up and down.

    - madrab2_up and madrab2_down to move the second paddle up and down.


 6. Listen to the buttons:

    - Pressing the buttons (w and s for the first paddle, and Up and Down for the second paddle) are associated with the appropriate functions to move the paddles.


 7. Main episode of the game:

    - while True loop to refresh the screen and move the ball.

    - Update the ball’s position by increasing its x- and y-coordinates.

    - Verify that the ball collides with the upper and lower borders of the window and reverses its direction.

    - Reset the ball position and update the score if it goes out of the side boundaries (points).

    - Check whether the ball collides with the rackets and changes its direction if it collides with them.


 

No comments:

Post a Comment

Leave a comment