【问题标题】:pygame.display.update() error: video system not initializedpygame.display.update() 错误:视频系统未初始化
【发布时间】:2017-04-03 22:52:55
【问题描述】:
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(最近一次调用最后一次): pygame.display.update() 错误:视频系统未初始化 我是一个 pygame 菜鸟,不知道如何解决这个问题,请帮忙!

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

【问题讨论】:

  • 它对我有用。我不知道我能提供什么帮助;(

标签: python pygame


【解决方案1】:

我假设您的意思是退出游戏时会发生此错误。 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.

【讨论】:

  • 是的,就是这样,但我还有一个问题,如何让某物移动?
  • 我会更改我上面的代码以向您展示我到目前为止所拥有的
  • @CooperGreen 不要编辑您的问题来创建新问题。如果这解决了原来的问题,要么接受这个答案,要么写一条评论来解释什么是错的。如果您还有其他问题,请创建一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多