pygame.display.update()错误:视频系统未初始化

时间:2017-04-03 13:31:19

标签: python pygame

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()

我收到此错误:

  

追踪(最近一次通话):       pygame.display.update()       错误:视频系统未初始化   我是一个pygame菜鸟,不知道如何解决这个请帮忙!

我现在只是添加一个句子,所以它会让我提交post.dsijklasdvjnkjdlsnv ksdjvkjkjsndlknsdva kksajsdjdvkjlkvnsddlkvndk。 sddvjvkdsvnlskdkdsjvjkvv skdvkvjnskjv

1 个答案:

答案 0 :(得分:2)

我认为你的意思是当你退出游戏时会出现这个错误。 pygame.quit()取消初始化所有pygame模块,但while循环仍然保持运行,当调用pygame.display.update()时,视频系统不再被初始化并导致错误。要解决这个问题,请执行以下操作:

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.