pygame.error:视频系统未初始化python代码错误

时间:2018-05-29 12:08:48

标签: python pygame

运行此代码后,出现错误:

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

任何人都可以帮助我并解释这个错误的含义以及如何纠正它吗?

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

把你的代码:

run_game()

while陈述之前