0

I'm having difficulty fixing this issue. What I'm trying to achieve in Pygame is have working buttons that call a new function, that will swap the screen. The first one works, where the play button is pressed it calls the new function to draw the new screen and characters, but when I press the quit button drawn onto the screen, instead of calling the first function and drawing the menu, pyjama closes and I get the title error. Here is the error exactly: for event in pygame.event.get(): pygame.error: video system not initialized

I have pygame.init() at the top also.

Thank you anyone who sees this

def startmenu():
    pygame.display.set_caption("Start Menu")
    # load images
    # bg image'
    startmenubg_img = pygame.image.load('/Users/shichek/Desktop/coursework/gameart/assetsmenu/titlescreen.png').convert_alpha()
    # load button images
    play_img = pygame.image.load('/Users/shichek/Desktop/coursework/gameart/assetsmenu/playbutton-removebg-preview.png').convert_alpha()
    stats_img = pygame.image.load('/Users/shichek/Desktop/coursework/gameart/assetsmenu/statbut-removebg-preview.png').convert_alpha()
    lib_img = pygame.image.load('/Users/shichek/Desktop/coursework/gameart/assetsmenu/librarybutton.png').convert_alpha()
    quit_img = pygame.image.load('/Users/shichek/Desktop/coursework/gameart/assetsmenu/quitbut-removebg-preview.png').convert_alpha()

    # creates each button object
    play_but = albutton.Button(800,50,play_img,1)
    stats_but = albutton.Button(1100,50,stats_img,1)
    lib_but = albutton.Button(800,240,lib_img,1)
    quit_but = albutton.Button(1100,240,quit_img,1)
    
    start = True
    while start:
        clock.tick(fps)
#     drawsscreen
        screen.blit(startmenubg_img,(0,0))
#     draws our buttons on the screen
        play_but.draw(screen)
        stats_but.draw(screen)
        lib_but.draw(screen)
        quit_but.draw(screen)

# check if buttons are clicked to change screen state
        if play_but.click(screen):
            lvl1()
            start = False
        if quit_but.click(screen):
            pygame.quit()
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                start = False
        
        pygame.display.update()


def lvl1():
    pygame.display.set_caption("game test")
#     load background image
    forestlevelbg_img = pygame.image.load("img/Background/polydurusland.png").convert_alpha()
#     create our objects of fighter, use resize function to get them correct
    knight = fighterclass.Fighter(250, 400, 'Knight', 30, 10, 3)
    knight.resize(0.5,0.5)
    bandit1 = fighterclass.Fighter(600, 480, 'Bandit', 20, 6, 1)
    bandit1.resize(0.8,0.8)
    bandit2 = fighterclass.Fighter(700, 480, 'Bandit', 20, 6, 1)
    bandit2.resize(0.8,0.8)
#    add our bad guys to an enemy list
    enemy_list = []
    enemy_list.append(bandit1)
    enemy_list.append(bandit2)
#     button to take us back to the menu
    menu_img = pygame.image.load('/Users/shichek/Desktop/coursework/gameart/assetsmenu/quitbut-removebg-preview.png').convert_alpha()
    menu_but = albutton.Button(1100,240,menu_img,1)
#     our animation loop    
    game = True
    while game:
        clock.tick(fps)
        screen.blit(forestlevelbg_img,(0,0))
#         button to take us back to the menu
        menu_but.draw(screen)
        if menu_but.click(screen):
            startmenu()
            game = False
#         draws us
        knight.draw()
#             iterates through the enemy list and draws each of them
        for i in enemy_list:
            i.draw()
#         must have end conditio 
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game = False
        pygame.display.update()
  • 1
    right now you have defined functions but how do you call them in your program? Remember to give a minimal reproducible example – Caridorc Jan 31 '23 at 23:36

0 Answers0