I’m making pong using the Turtle library and want to make my code more tidy and efficient but I don’t know how [closed]

I have 4 different balls for the game. Starting with one ball visible and in use. When the total score gets to multiples of 5, I want to add a new ball into play.

So far I have:

      
import turtle

wn = turtle.Screen()
wn.title('pong by smelly bum')
wn.bgcolor('black')
wn.setup(width = 800, height = 600)
wn.tracer(0)  # stops window from updating so it needs to be done manually. this speeds up the game


# score
score_a = 0
score_b = 0
total_score = 0


"""
Ball
"""
ball1 = turtle.Turtle()
ball1.speed(0)
ball1.shape('square')
ball1.color('white')
ball1.penup()  # stops a line being drawn where the paddle has been
ball1.goto(0, 0)  # start psotion of the ball

ball1.dx = -0.1  # every time ball moves, it moves by this many pixels
ball1.dy = 0.1

"""
Ball 2
"""
ball2 = turtle.Turtle()
ball2.speed(0)
ball2.shape('square')
ball2.color('blue')
ball2.penup()  # stops a line being drawn where the paddle has been
ball2.goto(0, 0)  # start psotion of the ball

ball2.dx = 0  # every time ball moves, it moves by this many pixels
ball2.dy = 0.12
ball2.hideturtle()

"""
Ball 3
"""
ball3 = turtle.Turtle()
ball3.speed(0)
ball3.shape('square')
ball3.color('red')
ball3.penup()  # stops a line being drawn where the paddle has been
ball3.goto(0, 0)  # start psotion of the ball

ball3.dx = 0  # every time ball moves, it moves by this many pixels
ball3.dy = 0.1
ball3.hideturtle()

"""
Ball 4
"""
ball4 = turtle.Turtle()
ball4.speed(0)
ball4.shape('square')
ball4.color('grey')
ball4.penup()  # stops a line being drawn where the paddle has been
ball4.goto(0, 0)  # start psotion of the ball

ball4.dx = 0  # every time ball moves, it moves by this many pixels
ball4.dy = 0.09
ball4.hideturtle()

balls = [ball1, ball2, ball3, ball4]
ball_speed_x = [ball1.dx, ball2.dx, ball3.dx, ball4.dx]  # list of ball speeds that can be iterated through
ball_speed_y = [ball1.dy, ball2.dy, ball3.dy, ball4.dy]



while True:
    wn.update()

    for ball in balls:
        ball.setx(ball.xcor() + ball.dx)  # moves ball by dx
        ball.sety(ball.ycor() + ball.dy)  # moves ball by dy
        
        # border checking the ball
        if ball.xcor() < -390:
            ball.goto(0, 0)  # resets ball to centre
            ball.dx *= -1  # reverses direction of the ball
            ball.dy *= -1
            score_b += 1
            total_score += 1
            
        if ball.xcor() > 390:
            ball.goto(0, 0)
            ball.dx *= -1  # reverses direction of the ball
            ball.dy *= 1
            score_a += 1
            total_score += 1
           
        if ball.ycor() > 290:
            ball.sety(290)  # stops ball at border
            ball.dy *= -1 # reverses direction of the ball

        if ball.ycor() < -290:
            ball.sety(-290)
            ball.dy *= -1
        
    
        # make a list of balls and list of ball speeds so that i can index them in a loop to add balls during the game
        # make a variable that is true when total score is a desired value then false as soon as a ball is added.
             
        # want to find a way to put this in a more efficient way
        # maybe define a showturtle function and use a loop of ints
        if total_score == 5:
            ball2.showturtle()
            ball2.dx = 0.12
        
        if total_score == 10:
            ball3.showturtle()
            ball3.dx = 0.08
        
        if total_score == 15:
            ball4.showturtle()
            ball4.dx = 0.15

I want the addition of balls as the total score increases to be more automatic. so if i wanted to add ball5, I wouldnt have to add another if statement for total_score == 20, instead i could just define ball5 at the start of the code and it would work without additional code per new ball. I was thinking using a for loop, but dont know how i would implement properly.

furthermore is there a simple way to use loops for the creation of balls instead of repeating the same code to create them?

sorry if the code isnt quite minimal, ive left the paddle and the score display out.

New contributor

Merkinweaver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

1. Use elif to avoid checking multiple mutually-exclusive conditions

This program would be much more efficient with elif, the keyword for else if – so that once the first if statement runs, the second and third conditions won’t be checked (since all three conditions are mutually exclusive). So

if total_score == 5: 
    ball2.showturtle() 
    ball2.dx = 0.12
elif total_score == 10:
    ball3.showturtle()
    ball3.dx = 0.08
elif total_score == 15:
    ball4.showturtle()
    ball4.dx = 0.15

With this syntax, if score is 5, the if score == 5 portion of the code will run – and then the interpreter will skip completely to the bottom of this section.

2. Use case-switch for more elegant checking of many cases

If you have a long chain of if-else statements, it is more elegant to use a case-switch structure, such as:

match total_score:
    case 5:
        ball2.showturtle() 
        ball2.dx = 0.12
    case 10:
        ball3.showturtle()
        ball3.dx = 0.08
    case 15:
        ball4.showturtle()
        ball4.dx = 0.15
    case _:
        pass

3. Use a class to easily perform the same methods on an object

Finally, if you’ll need to do this to a lot of balls, you should probably write a class Ball, and create each ball as an instance of that class. updateBall could be a method of that class in which showturtle() and ball.dx are used. Best of luck!

New contributor

AmericanJael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật