Samu
Samu

Reputation: 21

i´m trying to make a pong game, but the ball keeps changing speed, and I can´t find what´s wrong

I was following a tutorial so i really don´t know what is wrong with it. When i run it everything else seems fine but the ball keeps slowing down or speeding up. I´m still at the basics in python and programming in general so I would really appreciate any help.

I thought the problem could be in

ball.dx = 0.15
ball.dy = 0.15

but even if it is there I don´t know how to fix it

import turtle

wn = turtle.Screen()
wn.title("Pongpong :)")
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.15
ball.dy = 0.15



# 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)

    # Borda
    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 
    
    if ball.xcor() < -390:
        ball.goto(0, 0)  
        ball.dx *= -1  

    # Rebatendo
    if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 40 and  ball.ycor() > paddle_b.ycor() -40):
        ball.setx(340)
        ball.dx *= -1
    
    if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() > paddle_a.ycor()  -40 and  ball.ycor() < paddle_a.ycor() + 40):
        ball.setx(-340)
        ball.dx *= -1

Upvotes: 2

Views: 402

Answers (1)

Leroy
Leroy

Reputation: 72

Here's someone who had the same problem : Python Ping-Pong game, the ball speed randomly changes during paddle movement If I understand it well, the changing speed is caused by how the game loop is programmed : in this code, the game updates whenever it wants, and it's actually as fast as your computer is capable, and if there's something slowing down your computer, it will change the speed of the game refreshing. I usually use pygame top make games which has a clock function.

TL DR : use a clock to make your game updates regularly instead of as fast as it can.

Upvotes: 2

Related Questions