pygame.display.update() error: video system not initialized

我与影子孤独终老i 提交于 2021-02-05 10:46:11

问题


import pygame
from pygame.locals import*

def main():
   global FPSCLOCK, DISPLAYSURF, BASICFONT, PLAY_SURF, PLAY_RECT, NEW_SURF, NEW_RECT, SOLVE_SURF, SOLVE_RECT 


black = ( 0, 0, 0)
Aqua = ( 0, 255, 255)
Blue = ( 0, 0, 255)
Fuchsia = (255, 0, 255)
Gray = (128, 128, 128)
Green =( 0, 128, 0)
Lime =( 0, 255, 0)
Maroon= (128, 0, 0)
NavyBlue = ( 0, 0, 128)
Olive =(128, 128, 0)
Purple =(128, 0, 128)
Red= (255, 0, 0)
Silver =(192, 192, 192)
Teal =( 0, 128, 128)
White =(255, 255, 255)
Yellow =(255, 255, 0)
ButtonColor= black
textcolor= Red
BASICFONTSIZE = 20

pygame.init()

def makeText(text, color, bgcolor, top, left):
    textSurf = BASICFONT.render(text, True, color, bgcolor)
    textRect = textSurf.get_rect()
    textRect.topleft = (top, left)
    return (textSurf, textRect)
FPSCLOCK = pygame.time.Clock()
BASICFONT = pygame.font.Font('freesansbold.ttf', BASICFONTSIZE)



title_screen= pygame.image.load("Journey to Vallhalla.png")
DISPLAYSURF = pygame.display.set_mode((1300, 690))

PLAY_SURF , PLAY_RECT = makeText("Click to Play", textcolor, ButtonColor, 600,400)

title=DISPLAYSURF.blit(title_screen, (0,0 ))
playb=DISPLAYSURF.blit(PLAY_SURF, PLAY_RECT)
player=pygame.image.load("player.jpg")
playerx=650
playery=520
movex,movey=0,0
while True: # main game loop
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and PLAY_SURF.get_rect(topleft=(600, 400)).collidepoint(pygame.mouse.get_pos()):
            player1= DISPLAYSURF.blit(player,(650,520))
            DISPLAYSURF.fill(Gray)
        if event.type == KEYDOWN:
            if event.key==K_LEFT:
                movex = -10
            elif event.key==K_RIGHT:
                movex=+10
            elif event.key==K_DOWN:
                movey=+10
            elif event.key==K_SPACE:
                movey=-30

        if event.type==KEYUP:
            if event.key==K_LEFT:
                movex += 10
            elif event.key==K_RIGHT:
                movex=0
            elif event.key==K_DOWN:
                movey=0
            elif event.key==K_SPACE:
                movey=0


    movex+=playerx
    movey+=playery
    pygame.display.update()

I am getting this error:

Traceback (most recent call last): pygame.display.update() error: video system not initialized I am a pygame noob and do not know how to fix this please help!

I am now just adding a sentence so it will let me submit the post.dsijklasdvjnkjdlsnv ksdjvkjkjsndlknsdva kksajsdjdvkjlkvnsddlkvndk. sddvjvkdsvnlskdkdsjvjkvv skdvkvjnskjv


回答1:


I assume you mean this error occurs when you quit the game. pygame.quit() uninitializes all pygame modules, but the while loop still keeps running and when pygame.display.update() is called, the video system is not initialized anymore and the error is caused. To fix that problem do something like this:

running = True

while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False

    # Game code ...

# After the while loop is done, uninitialize pygame and exit the program.
pygame.quit()
sys.exit()  # import sys at the top of the module.


来源:https://stackoverflow.com/questions/43186020/pygame-display-update-error-video-system-not-initialized

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!