Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with my pong code
#1
Hi, I am following a tutorial on Youtube on how to make this pong game. I follow the tutorial code-for-code, and I am getting a different result than the instructor on Youtube, but my code and parameters are exactly the same as his. However, in the Youtube video his "ball" bounces from the center to the bottom right and bounces back. But my code only shows the ball starting from lower edge of the turtle screen and shoots off and never seeing it again. It doesn't even bounce back. I set ball.dx *=-1 to tell the ball to bounce back. Not sure what is wrong with my code or parameters, but it's exactly the same as his. any explanation or suggestion will be helpful and much appreciated!.

import turtle

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

# Paddle 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 = 0.1
ball.dy = -0.1


# 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")                                                                                          # this calls the function paddle_a_up()
wn.onkeypress(paddle_a_down, "s")
wn.onkeypress(paddle_b_up, "Up")                                                                                          # this calls the function paddle_a_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:  #the ball gone past the paddle and is off the screen
        ball.goto(0, 0)
        ball.dx *= -1

    if ball.xcor() > -390:  # the ball gone past the paddle and is off the screen
        ball.goto(0, 0)
        ball.dx *= -1
Reply
#2
Line 80
if ball.ycor() > -290:
should be
if ball.ycor() < -290:

Line 88
if ball.xcor() > -390:
should be
if ball.xcor() < -390:
Than999 likes this post
Reply
#3
Okay, thank you so much!
Reply
#4
(Sep-08-2021, 06:18 AM)Yoriz Wrote: Line 80
if ball.ycor() > -290:
should be
if ball.ycor() < -290:

Line 88
if ball.xcor() > -390:
should be
if ball.xcor() < -390:

Thanks. I am also facing the same situation as you posted
fall guys
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Game “Pong” I have problems with the code BenBach18 2 1,258 Jan-10-2021, 05:16 PM
Last Post: michael1789
  [PyGame] Creating Pong in Pygame Russ_CW 2 1,112 Oct-11-2020, 11:56 AM
Last Post: Russ_CW
  Trying to make a simple pong game. kevindadmun 1 2,326 Aug-05-2019, 06:39 AM
Last Post: kevindadmun
  Smiley Pong Help Jasmineleroy 6 2,213 May-22-2019, 11:36 AM
Last Post: metulburr
  [PyGame] How do I add more balls to basic Pong game? JUtah 2 2,107 Apr-18-2019, 08:40 PM
Last Post: JUtah
  [PyGame] Pong game key.event problem erickDarko 2 2,546 Dec-12-2018, 03:17 PM
Last Post: erickDarko

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020