An assertation error that keeps coming back

Question:

I was doing some testing on the chatgpt bot and I wanted to know if it could write a simple game. So I let it write the famous Pong game in python. I’ve come really far but now I am stuck on this one error. It says the following:

Traceback (most recent call last):
  File "C:UsersFrodiPycharmProjectspythonProjectmain.py", line 36, in <module>
    left_score_anim = PygAnimation([(font.render(str(i), 1, (255, 255, 255)), first_frame_duration if i==0 else 0.5*i) for i in range(left_score, left_score+10)])
  File "C:UsersFrodiPycharmProjectspythonProjectvenvlibsite-packagespyganim__init__.py", line 168, in __init__
    assert frame[1] > 0, 'Frame %s duration must be greater than zero.' % (i)
AssertionError: Frame 0 duration must be greater than zero.

Here is the full script:

# Import the necessary libraries
import pygame
import random
from pyganim import PygAnimation

# Initialize the Pygame library
pygame.init()

# Set the window size and caption
size = (700, 500)
caption = "Pong"

# Create the window
screen = pygame.display.set_mode(size)
pygame.display.set_caption(caption)

# Create the ball
ball = pygame.Rect(size[0]//2, size[1]//2, 10, 10)

# Set the ball's initial velocity
ball_velocity = [5, 5]

# Create the paddles
left_paddle = pygame.Rect(10, size[1]//2, 10, 50)
right_paddle = pygame.Rect(size[0]-20, size[1]//2, 10, 50)

# Create the scores
left_score = 0
right_score = 0

# Create the font
font = pygame.font.Font(None, 30)

# Create the animations for the scores
first_frame_duration = 0.5
left_score_anim = PygAnimation([(font.render(str(i), 1, (255, 255, 255)), first_frame_duration if i==0 else 0.5*i) for i in range(left_score, left_score+10)])
left_score_anim.play()
first_frame_duration = 0.5
right_score_anim = PygAnimation([(font.render(str(i), 1, (255, 255, 255)), first_frame_duration if i==0 else 0.5*i) for i in range(right_score, right_score+10)])
right_score_anim.play()

# Create the game loop
running = True
while running:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Move the ball
    ball.x += ball_velocity[0]
    ball.y += ball_velocity[1]

    # Check for collisions with the walls
    if ball.top <= 0 or ball.bottom >= size[1]:
        ball_velocity[1] = -ball_velocity[1]
    if ball.left <= 0:  # Left wall
        right_score += 1
        right_score_anim = PygAnimation(
            [(font.render(str(i), 1, (255, 255, 255)), first_frame_duration if i==0 else 0.5*i) for i in range(right_score - 1, right_score + 10)])
        right_score_anim.play()
        ball.x = size[0] // 2
        ball.y = size[1] // 2
    elif ball.right >= size[0]:  # Right wall
        left_score += 1
        left_score_anim = PygAnimation(
            [(font.render(str(i), 1, (255, 255, 255)), first_frame_duration if i==0 else 0.5*i)
             for i in range(left_score - 1, left_score + 10)])
        left_score_anim.play()
        ball.x = size[0] // 2
        ball.y = size[1] // 2

    # Check for collisions with the paddles
    if ball.colliderect(left_paddle) or ball.colliderect(right_paddle):
        ball_velocity[0] = -ball_velocity[0]

    # Clear the screen
    screen.fill((0, 0, 0))

    # Draw the objects
    pygame.draw.rect(screen, (255, 255, 255), ball)
    pygame.draw.rect(screen, (255, 255, 255), left_paddle)
    pygame.draw.rect(screen, (255, 255, 255), right_paddle)

    # Draw the scores
    left_score_anim.blit(screen, (size[0] // 4, 20))
    right_score_anim.blit(screen, (size[0] * 3 // 4, 20))

    # Update the screen
    pygame.display.flip()
    pygame.time.wait(10)

    # Close the window and quit the program
    pygame.quit()

I’ve tried changing the value of the multplier of i. And added a variable that defines the first frame duration. After that it keeps on giving the same error everytime I run the code.

I am very new to coding, and I know this is a very big project to begin with but I am just so curious to learn and find the bugs in the code. Can you please help me out?

Asked By: Wargamer707

||

Answers:

I tried to implement your code as much as possible.
The error message is indicating that the duration of the first frame in the animation for the left score is zero, which is not allowed.
I thought the PygAnimation class, from the pyganim library, requires that the duration of each frame in the animation be greater than zero.
-> from 36 lines in main.py <-
The issue is caused by this line:

first_frame_duration = 0.5
left_score_anim = PygAnimation([(font.render(str(i), 1, (255, 255, 255)), first_frame_duration if i==0 else 0.5*i) for i in range(left_score, left_score+10)])
left_score_anim.play()

So, the problem is that when the first score is zero, the duration of the first frame is zero as well.

To fix the problem, you can change the first_frame_duration to any non-zero value or you can update the if condition to check if i is greater than zero, instead of checking if i is equal to zero.

Fix it.

left_score_anim = PygAnimation([(font.render(str(i), 1, (255, 255, 255)), 0.5 if i > 0 else 1) for i in range(left_score, left_score+10)])

This will ensure that the first frame has a duration of 1 instead of zero, which will fix the issue and your code should work as expected.

Answered By: Min
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.