【问题标题】:pygame.error: video system not initialized python code errorpygame.error:视频系统未初始化python代码错误
【发布时间】:2018-05-29 20:21:54
【问题描述】:

运行这段代码后,出现错误:

pygame.error: video system not initialized

我的代码:

import sys
import pygame
def run_game():
    # Initialize game and create a screen object.
    pygame.init()
    screen = pygame.display.set_mode((1200, 800))
    pygame.display.set_caption("Alien Invasion")
# Start the main loop for the game.
while True:
    # Watch for keyboard and mouse events.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    # Make the most recently drawn screen visible.
    pygame.display.flip()
run_game()

谁能帮我解释一下这个错误是什么意思以及如何纠正它?

【问题讨论】:

标签: python pygame


【解决方案1】:

引发错误是因为调用 pygame.event.get 时没有初始化显示 (pygame.display.set_mode)。问题是您的 while 循环没有正确缩进,因此它在调用 run_game 函数之前执行。循环应该在run_game 函数内。

import sys
import pygame


def run_game():
    # Initialize game and create a screen object.
    pygame.init()
    screen = pygame.display.set_mode((1200, 800))
    pygame.display.set_caption("Alien Invasion")

    # Start the main loop for the game.
    while True:
        # Watch for keyboard and mouse events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        # Make the most recently drawn screen visible.
        pygame.display.flip()

run_game()

【讨论】:

    【解决方案2】:

    输入你的代码:

    run_game()
    

    while 声明之前`

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多