Python Game Tutorial: Pong

Python Game Tutorial: Pong

FavoriteLoadingAdd to favorites

A Pong clone game tutorial for beginners using Python 3.

⭐️Course contents ⭐️
⌨️ (00:00) Part I: Introduction and the basic window setup
Code:
⌨️ (03:32) Part II: Add game objects to the screen (Paddles and Ball)
Code:
⌨️ (09:15) Part III: Moving the Paddles
Code:
⌨️ (15:06) Part IV: Moving the ball
Code:
⌨️ (24:10) Part V: Colliding with the Paddles
Code:
⌨️ (30:47) Part VI: Scoring
Code:
⌨️ (37:57) Part VII: Adding Sound
Code:

🔈 Sound file:
💻 Finished Code:

Tutorial from Christian Thompson. Check out his YouTube channel:

Learn to code for free and get a developer job:

Read hundreds of articles on programming:

And subscribe for new videos on technology every day:

source

By |2020-07-21T08:29:03+00:00July 21st, 2020|Python Video Tutorials|43 Comments

43 Comments

  1. bertie e July 21, 2020 at 8:29 am - Reply

    when i completed the Pen commands, and ran it, only a black screen appeared? Does anyone know why? Whenever I delete the Pen section it goes back to normal and i have made sure it is identical to his.

  2. swiss drone photos July 21, 2020 at 8:29 am - Reply

    Hello everyone, I hope someone can help me with my problem: if I execute the code the ball goes like a dancing ball from the top right to the bottom left. And I get the error: "afplay" couldn't get found (I don't know how the error really says in the English version)

  3. Databas3 July 21, 2020 at 8:29 am - Reply

    How do you make the window scaleable?

  4. Databas3 July 21, 2020 at 8:29 am - Reply

    What is the method to play a sound using os.system on windows?

  5. Platin Xpress July 21, 2020 at 8:29 am - Reply

    That helped me alot to understand more of python programming
    thank you very much

  6. AetriX 7 July 21, 2020 at 8:29 am - Reply

    Why does this never work for me…………?????

  7. amit karade July 21, 2020 at 8:29 am - Reply

    bro how to run it

  8. ROHAN ALFRED July 21, 2020 at 8:29 am - Reply

    are you sure ur code works? the paddles didn't show…

  9. DuttyBwoy July 21, 2020 at 8:29 am - Reply

    My onkeypress function doesn't work.. I been 2 weeks frustrated and stress Caz I get to do everything except the key control /key binding.. So I have a cool old school Atari based animation that I can't interact with… … Anyone???

  10. Mr Feynman July 21, 2020 at 8:29 am - Reply

    when i close the window the program shows 'Not Responding'

  11. Mikaila Angela Cadungog-Cadsawan July 21, 2020 at 8:29 am - Reply

    I can't run my program. Please help me. There is no terminal on my python

  12. ImGaming4YT July 21, 2020 at 8:29 am - Reply

    could you tell me what i did wrong plz

    import turtle

    wn = turtle.Screen()

    wn.title("Pong")

    wn.bgcolor("black")

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

    wn.tracer(0)

    # Score

    score_a = 0

    score_b = 0

    # Paddles A

    paddle_a = turtle.Turtle()

    paddle_a.speed(0)

    paddle_a.shape("square")

    paddle_a.color("white")

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

    paddle_a.penup()

    paddle_a.goto(-350, 0)

    # Paddle B

    paddle_b = turtle.Turtle()

    paddle_b.speed(0)

    paddle_b.shape("square")

    paddle_b.color("white")

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

    paddle_b.penup()

    paddle_b.goto(350, 0)

    # Ball

    ball = turtle.Turtle()

    ball.speed(0)

    ball.shape("square")

    ball.color("white")

    ball.penup()

    ball.goto(0, 0)

    ball.dx = .10 # speed of the ball

    ball.dy = -.10

    # Pen // turtle

    pen = turtle.Turtle()

    pen.speed(0)

    pen.color("white")

    pen.penup()

    pen.hideturtle()

    pen.goto(0,260)

    pen.write("Player A: 0 Player B: 0", align="center", font=("Courier", 24, "normal"))

    # Function

    def paddle_a_up():

    y = paddle_a.ycor()

    y += 20

    paddle_a.sety(y)

    def paddle_a_down():

    y = paddle_a.ycor()

    y -= 20

    paddle_a.sety(y)

    def paddle_b_up():

    y = paddle_b.ycor()

    y += 20

    paddle_b.sety(y)

    def paddle_b_down():

    y = paddle_b.ycor()

    y -= 20

    paddle_b.sety(y)

    # Keyboard binding

    wn.listen()

    wn.onkeypress(paddle_a_up, "w")

    wn.onkeypress(paddle_a_down, "s")

    wn.onkeypress(paddle_b_up, "Up")

    wn.onkeypress(paddle_b_down, "Down")

    # Main game loop

    while True:

    wn.update()

    # Move the ball

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

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

    # border checking

    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

    score_a += 1

    pen.clear()

    pen.write("Player A: {} Player B: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal"))

    if ball.xcor() < -390:

    ball.goto(0, 0)

    ball.dx *= -1

    score_b += 1

    pen.clear()

    pen.write("Player A: {} Player B: {}".format(score_a, score_b), align="center", font=("Courier", 24, "normal"))

    # Paddle and Ball collisons

    if (ball.xcor() > 340 and (ball.ycor() < 350) and paddle_b.ycor() + 40 and ball.ycor() > paddle_b.ycor() -40):

    ball.setx(340)

    ball.dx *= -1

    if (ball.xcor() < -340 and (ball.ycor() > -350) and paddle_a.ycor() + 40 and ball.ycor() > paddle_a.ycor() -40):

    ball.setx(-340)

    ball.dx *= -1

  13. Arjun Thakar July 21, 2020 at 8:29 am - Reply

    I am getting this error despite entering the same code shown on screen for the first 11 lines; so the turtle is imported and shows on screen but the background isn't black- with the error shown as; (win.bgcolour("black")
    AttributeError: '_Screen' object has no attribute 'bgcolour')

  14. WhiteTylerPerry July 21, 2020 at 8:29 am - Reply

    What IDE are you using? I'm so lost.

  15. rhys reynolds July 21, 2020 at 8:29 am - Reply

    How to I change my terminal from power shell to python?

  16. Anouar Aynan July 21, 2020 at 8:29 am - Reply

    Monika: script yes

  17. Deltacon777 1 July 21, 2020 at 8:29 am - Reply

    @TokyoEdTech I have a problem with wn.update()

  18. Alex JONES July 21, 2020 at 8:29 am - Reply

    my ball doesn't move?

  19. Adam Moustafa July 21, 2020 at 8:29 am - Reply

    My python doesn't say run pycharm does

  20. Techno Rax July 21, 2020 at 8:29 am - Reply

    I have added a timer too..(5 mins)

  21. Flobby July 21, 2020 at 8:29 am - Reply

    when i move my paddles the ball starts to move faster sometimes, how do i fix this and why is this?

  22. GreenBeanMan Z July 21, 2020 at 8:29 am - Reply

    This was great!!I had to figure out how to show the turtle window and stuff on windows because the way visual studios was set up. I didn’t add sounds because I didn’t want to mess anything up and I didn’t want to spend any more time. Great tutorial!!!

  23. Tanmay Beats July 21, 2020 at 8:29 am - Reply

    how to slow down the ball if its fast in our pc

  24. Royal Couch Potato July 21, 2020 at 8:29 am - Reply

    Hay man I need some help because I cant get this invalid syntax on the if ball.xcor () > 390: so that's what I typed

  25. RoboKing004 July 21, 2020 at 8:29 am - Reply

    I did the coding and it was fun and great! Only issue, it took me an hour and my file disappeared. Now I am sad and have to do it all again.

  26. Hiro K July 21, 2020 at 8:29 am - Reply

    So now can you do a tutorial video on how to create GTA5?

  27. Olzz33 July 21, 2020 at 8:29 am - Reply

    For me the ball is going way too fast.

  28. buddyruns14 July 21, 2020 at 8:29 am - Reply

    even copy /pasting your code into pyCharm gives me the black window only, the paddle doesn't show up. Any idea why?

  29. Daniel ndukwe July 21, 2020 at 8:29 am - Reply

    Please I'm confused.. How did you get import turtle?

  30. Water Sprout July 21, 2020 at 8:29 am - Reply

    why do my pong ball move so fast? its like x3 of the speed shown in ur video help :))

  31. Matthew Fidler July 21, 2020 at 8:29 am - Reply

    When I start the code after making it so it should bounce off the wall, the ball just keeps on going against the wall and doesn't change direction. Can anyone help pls?

  32. Alexander Lokling July 21, 2020 at 8:29 am - Reply

    wn.onkeypress(paddle_a_down, "s")

    NameError: name 'paddle_a_down' is not defined
    please help

  33. Mujtaba Hussain July 21, 2020 at 8:29 am - Reply

    which IDE should i use for this?

  34. Living With Hope July 21, 2020 at 8:29 am - Reply

    please tell me how you can donload python 3 i beg you please

  35. Chris Powell July 21, 2020 at 8:29 am - Reply

    Finished. I never thought I could code a game until I watched this video. Thank you, Christian!

  36. Slicx July 21, 2020 at 8:29 am - Reply

    1 question, like if anybody knows bout curses, we havta download it soo, so we have to download turtle

  37. baconhair7227 July 21, 2020 at 8:29 am - Reply

    sqaures doesent work for me 🙁

  38. Isha Pawar July 21, 2020 at 8:29 am - Reply

    I have made the ball speed 0 but ball is moving very fast

  39. Christopher Chastain July 21, 2020 at 8:29 am - Reply

    The ball goes way to fast on the screen how can I change the speed? Right now, my ball.speed(0) is this how can I slow it down?

  40. ELOC July 21, 2020 at 8:29 am - Reply

    print("Im currently at 7:25")

  41. Gnaaruag July 21, 2020 at 8:29 am - Reply

    The score does not seem to be visible
    Anything I could have done wrong

  42. ElectronicGeek July 21, 2020 at 8:29 am - Reply

    I am not able to get the paddle on my screen.Can you help me out?
    Here's the code
    import turtle

    wn = turtle.Screen()
    wn.title("Pong")
    wn.bgcolor("black")
    wn.setup(width=800, height=600)
    wn.tracer(0)

    score_a = 0
    score_b = 0

    paddle_a = turtle.Turtle()
    paddle_a.speed(0)
    paddle_a.shape("square")
    paddle_a.color("white")
    paddle_a.shapesize(stretch_wid=5,stretch_len=1)
    paddle_a.penup()
    paddle_a.goto(-350, 0)

  43. Alexander Luke July 21, 2020 at 8:29 am - Reply

    Make p***s painters

Leave A Comment

*