The issue is the game crashes everytime after the winner text is displayed (if either player reaches a score of 10 or greater) and fails to re-call the main() function when I stated that it should as you can see from my code below. So when it displays the winner text it should pause the game for 5 sec then break out of the while loop and recall main() but it crashes right after the winner text is displayed (see screenshot below). The logic for the winner text is near the top in my main() function.
Not sure what the root cause is since I called pygame.init() at the top of my program. Any help would be greatly appreciated! The full code is below... if someone can run it and let me know what the root cause of it crashing is that would be great. Thanks in advance!
import pygame
pygame.init()
WIDTH, HEIGHT = 750, 500
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("PONG")
WINNER_FONT = pygame.font.SysFont('comicsans', 100)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
class Paddle(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([10, 75])
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.paddle_speed = 4
self.points = 0
class Ball(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([10, 10])
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.speed = 2
self.dx = 1
self.dy = 1
paddle1 = Paddle()
paddle1.image.fill(BLUE)
paddle1.rect.x = 25
paddle1.rect.y = 225
paddle2 = Paddle()
paddle2.image.fill(RED)
paddle2.rect.x = 715
paddle2.rect.y = 225
ball = Ball()
ball.rect.x = 375
ball.rect.y = 250
all_sprites = pygame.sprite.Group()
all_sprites.add(paddle1, paddle2, ball)
def redraw():
WINDOW.fill(BLACK)
font = pygame.font.SysFont("Consolas", 35)
text = font.render("---[PONG]---", 1, YELLOW)
textRect = text.get_rect()
textRect.center = (WIDTH//2, 25)
WINDOW.blit(text, textRect)
#Player 1 score
p1_score = font.render(str([paddle1.points]), 1, WHITE)
p1Rect = p1_score.get_rect()
p1Rect.center = (50, 50)
WINDOW.blit(p1_score, p1Rect)
#Player 2 score
p2_score = font.render(str([paddle2.points]), 1, WHITE)
p2Rect = p2_score.get_rect()
p2Rect.center = (700, 50)
WINDOW.blit(p2_score, p2Rect)
all_sprites.draw(WINDOW)
pygame.display.update()
def draw_winner(text):
draw_text = WINNER_FONT.render(text, 1, WHITE)
WINDOW.blit(draw_text, (WIDTH//2 - draw_text.get_width()/2, HEIGHT//2 - draw_text.get_height()/2))
pygame.display.update()
pygame.time.delay(5000)
def main():
run = True
while run:
pygame.time.delay(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
winner_text = ""
if paddle1.points >= 10:
winner_text = "Blue Wins!"
if paddle2.points >= 10:
winner_text = "Red Wins!"
if winner_text != "":
draw_winner(winner_text)
break
# Paddle movement
key = pygame.key.get_pressed()
if key[pygame.K_w]:
paddle1.rect.y -= paddle1.paddle_speed
if key[pygame.K_s]:
paddle1.rect.y += paddle1.paddle_speed
if key[pygame.K_UP]:
paddle2.rect.y -= paddle2.paddle_speed
if key[pygame.K_DOWN]:
paddle2.rect.y += paddle2.paddle_speed
# Ensures paddles never move off the screen
if paddle1.rect.y < 0:
paddle1.rect.y = 0
if paddle1.rect.y > 425:
paddle1.rect.y = 425
if paddle2.rect.y < 0:
paddle2.rect.y = 0
if paddle2.rect.y > 425:
paddle2.rect.y = 425
# Ball movement
ball.rect.x += ball.speed * ball.dx
ball.rect.y += ball.speed * ball.dy
# Setting up collision detection with the walls by changing ball's direction
if ball.rect.y > 490:
ball.dy = -1
if ball.rect.x > 740:
ball.rect.x, ball.rect.y = 375, 250
paddle1.points += 1
if ball.rect.y < 10:
ball.dy = 1
if ball.rect.x < 10:
ball.rect.x, ball.rect.y = 375, 250
paddle2.points += 1
# Setting up collision detection with the paddles
if paddle1.rect.colliderect(ball.rect):
ball.dx = 1
if paddle2.rect.colliderect(ball.rect):
ball.dx = -1
redraw()
main()
if __name__ == "__main__":
main()