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

2024-09-27 07:34:15 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我得到了这个错误:

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!

我现在只是添加一个句子,这样我就可以提交post.dsijklasdvjnkjdlsnv后ksdjvkjkjsndlknsdva kksajsdjdvkjlkvnsddlkvndk。SDDVJVKDSVNLSKDKDSJV skdvkvjnskjv


Tags: keyrecteventplaygetiftypepygame
1条回答
网友
1楼 · 发布于 2024-09-27 07:34:15

我想你是说这个错误发生在你退出游戏的时候。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.

相关问题 更多 >

    热门问题