0

I made a pong game with turtle and add sound with winsound. Firstly, it worked but then I tried if one of the players reach score 3, a text to write on the screen. Then, although score_a and score_b are not 3, 'GAME END' is writing when A or B hits 1 point. I want if one of them hits 3 points this to happen, 'GAME END' to write on the screen. What do I need to do?

# import modules:
import turtle
import winsound

# main

window = turtle.Screen()
window.title('My Pong Game')
window.bgcolor('black')
window.setup(width=800, height=600)
# speed up the game
window.tracer(0)

# Score
score_a = 0
score_b = 0

# Paddle A
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape('square')
paddle_a.color('white')
# Adjust the shape
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
# Don't draw a line
paddle_a.penup()
# Position of the square
paddle_a.goto(-350, 0)

# Paddle B
paddle_b = turtle.Turtle()
paddle_b.speed(0)
paddle_b.shape('square')
paddle_b.color('white')
# Adjust the shape
paddle_b.shapesize(stretch_wid=5, stretch_len=1)
# Don't draw a line
paddle_b.penup()
# Position of the square
paddle_b.goto(350, 0)

# Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape('circle')
ball.color('white')
# Don't draw a line
ball.penup()
# Position of the square
ball.goto(0, 0)
# Move 2 pixel
ball.dx = 0.09
ball.dy = -0.09

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


# Key bind
window.listen()
window.onkeypress(paddle_a_up, 'w')
window.onkeypress(paddle_a_down, 's')
window.onkeypress(paddle_b_up, 'Up')
window.onkeypress(paddle_b_down, 'Down')

# Main game loop
while True:
    window.update()

    # Move the ball
    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)

    # Hit check
    if ball.ycor() > 290:
        ball.sety(290)
        ball.dy *= -1
        winsound.PlaySound('bounce.wav', winsound.SND_ASYNC)

    if ball.ycor() < -290:
        ball.sety(-290)
        ball.dy *= -1
        winsound.PlaySound('bounce.wav', winsound.SND_ASYNC)

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

    # Collisions
    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
        winsound.PlaySound('bounce.wav', winsound.SND_ASYNC)

    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
        winsound.PlaySound('bounce.wav', winsound.SND_ASYNC)

    # End game
    if score_a or score_b > 3:
        pen.clear()
        pen.write('GAME END', align='center', font=('Courier', 24, 'normal'))

  • 1
    Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – ChrisGPT was on strike Jan 05 '20 at 13:00

1 Answers1

0

The bug lies here:

if score_a or score_b > 3:
    pen.clear()
    pen.write('GAME END', align='center', font=('Courier', 24, 'normal'))

This checks could be written as: (score_a) or (score_b > 3). score_a is evaluated to True by Python if not zero. You want (score_a > 3) or (score_b > 3) however. So change it to:

if (score_a > 3) or (score_b > 3):
    pen.clear()
    pen.write('GAME END', align='center', font=('Courier', 24, 'normal'))

Note that the bug caused your program to display "END GAME" either if score_a > 0 or score_b > 3. It does not display "END GAME" if score_b == 1.

To illustrate this:

score_a = 0
score_b = 1
print(score_a or score_b > 3) # False

As noted by Chris, this answer is relevant.

Luatic
  • 8,513
  • 2
  • 13
  • 34