错误:在使用pygame时,视频系统未初始化。

huangapple go评论77阅读模式
英文:

Error: video system not initialized when using pygame

问题

我每次尝试运行这段代码时都遇到错误:

import pygame
pygame.init()
print(pygame.display.Info())

WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game")

def main():
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.quit:
                run = False

pygame.quit()

if __name__ == "__main__":
    main()

我尝试添加了 pygame.init() 但没有帮助。

英文:

I keep having the error whenever I try to run this code:

import pygame
pygame.init()
print(pygame.display.Info())

WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game")

def main():
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.quit:
                run = False

pygame.quit()

if __name__ == "__main__":
    main()

I've tried adding pygame.init() but it didn't help.

答案1

得分: 0

这里有一些问题。错误的直接原因是你对pygame.quit()的调用不在main函数内部,所以它在程序启动时立即被调用。你需要修复这个问题:

import pygame
pygame.init()
print(pygame.display.Info())

WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game")

def main():
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    pygame.quit()

if __name__ == "__main__":
    main()

这将使你的程序能够运行...但它永远不会退出。你正在比较event.type == pygame.quit,但pygame.quit是一个函数;这个比较将始终为False。你需要将其与pygame.QUIT进行比较,这是一个整数值,将与event.type匹配,如预期的那样:

import pygame
pygame.init()
print(pygame.display.Info())

WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game")

def main():
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    pygame.quit()

if __name__ == "__main__":
    main()
英文:

You have a couple of problems here. The immediate cause of the error is that your call to pygame.quit() isn't inside the main function, so it gets called immediately when the program starts. You need to fix that:

import pygame
pygame.init()
print(pygame.display.Info())

WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game")

def main():
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.quit:
                run = False

pygame.quit()

if __name__ == "__main__":
    main()

This will allow your program to run...but it's never going to quit. You're comparing event.type == pygame.quit, but pygame.quit is a function; this comparison will always be False. You need to compare it to pygame.QUIT, which is an integer value that will match event.type as expected:

import pygame
pygame.init()
print(pygame.display.Info())

WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game")

def main():
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    pygame.quit()

if __name__ == "__main__":
    main()

huangapple
  • 本文由 发表于 2023年2月19日 21:52:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定