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.
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!
AmericanJael is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2