-1

When pressing escape to go back to main menu, the code terminated

or even whenever im trying to close the program its crashes and this is the error code:

  File "E:\pythonProject1\pong.py", line 217, in <module>
    main_menu()
  File "E:\pythonProject1\pong.py", line 42, in main_menu
    play()
  File "E:\pythonProject1\pong.py", line 161, in play
    for event in pygame.event.get():
pygame.error: video system not initialized

Process finished with exit code 1

Sorry for the long code but i dont really know what is the exact problem in my code so i sent it all:

import pygame, sys, random

pygame.init()

WIDTH, HEIGHT = 1280, 720

FONT = pygame.font.SysFont("David", int(WIDTH / 20))
FONTTEXT = pygame.font.SysFont("David", 14)
FONTOPTIONS = pygame.font.SysFont("David", int(WIDTH / 20))

SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong!")
CLOCK = pygame.time.Clock()
# PADDLES

player1 = pygame.Rect(WIDTH - 110, HEIGHT / 2 - 50, 10, 100)
player2 = pygame.Rect(110, HEIGHT / 2 - 50, 10, 100)
player1_score = 0
player2_score = 0
# BALL

ball = pygame.Rect(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20)
x_speed = 1
y_speed = 1
color_ball = 'white'
color_paddle1 = 'blue'
color_paddle2 = 'red'
final_score = 5


def checkcolorvalid(color):
    if color == 'red' or color == 'blue' or color == 'yellow' or color == 'green' or color == 'purple' or color == 'orange' or color == 'pink' or color == 'cyan' or color == 'gray' or color == 'brown':
        return True


def main_menu():
    while True:
        keys_pressed = pygame.key.get_pressed()
        SCREEN.fill('black')
        if keys_pressed[pygame.K_p]:
            play()
        if keys_pressed[pygame.K_o]:
            options()
        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()


def options():
    global color_ball
    global color_paddle1
    global color_paddle2
    while True:
        input_box = pygame.Rect(WIDTH / 2 - 100, HEIGHT / 2, 140, 32)
        textoptions = "OPTIONS"
        color_inactive = pygame.Color('white')
        color_active = pygame.Color('white')
        color = color_inactive
        active = False
        text = ''
        done = False
        amount = 0
        color_paddle1_done = False
        color_paddle2_done = False

        while not done:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    done = True
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if input_box.collidepoint(event.pos):
                        active = not active
                    else:
                        active = False

                    if active:
                        color = color_active
                    else:
                        color = color_inactive
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        main_menu()
                if not amount == 2:
                    if event.type == pygame.KEYDOWN:
                        if active:
                            if event.key == pygame.K_RETURN:
                                if checkcolorvalid(text):
                                    print(text)
                                    if not color_paddle1_done:
                                        color_paddle1 = text
                                        color_paddle1_done = True
                                    elif not color_paddle2_done:
                                        color_paddle2 = text
                                        color_paddle2_done = True

                                    amount += 1
                                else:
                                    if amount == 0:
                                        amount = 0
                                    else:
                                        amount -= 1
                                text = ''


                            elif event.key == pygame.K_BACKSPACE:
                                text = text[:-1]
                            else:
                                text += event.unicode

            SCREEN.fill((0, 0, 0))
            text_surface = FONTTEXT.render(text, True, color)
            textoptions_surface = FONTOPTIONS.render(textoptions, True, 'white')
            width = max(200, text_surface.get_width() + 10)
            input_box.w = width
            SCREEN.blit(text_surface, (input_box.x + 5, input_box.y + 5))
            SCREEN.blit(textoptions_surface, (WIDTH / 2 - 130, HEIGHT / 2 - 300))

            pygame.draw.rect(SCREEN, color, input_box, 2)

            pygame.display.update()
            CLOCK.tick(30)

main_menu()

the play() def is missing because i cant post it if you need it ask for it in the comments and i will reupload it.

SpooKy
  • 1
  • ``` def play(): while True: # leave game if keys_pressed[pygame.K_ESCAPE]: main_menu() # change to BACK TO MAIN MENU ``` – SpooKy Nov 30 '22 at 17:10
  • *"When pressing escape to go back to main menu,"* - no it does not. You call `main_menu` recursively. See [Pygame level/menu states](https://stackoverflow.com/questions/14700889/pygame-level-menu-states) how to implement something like this. – Rabbid76 Nov 30 '22 at 17:40

0 Answers0