pygame.error:视频系统未初始化(pycharm)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pygame.error:视频系统未初始化(pycharm)相关的知识,希望对你有一定的参考价值。

我试图通过复制和检查这个来学习pygame:https://www.wikihow.com/Program-a-Game-in-Python-with-Pygame#Adding_a_Player_Object_sub

但是当我运行上面看到的原始版本(步骤4)或我的代码时,它会给我带来黑屏并出现此错误:

Traceback (most recent call last):
  File "C:/Users/Mohamed/Desktop/mopy/pys/first pycharm.py", line 77, in <module>
    game().gameloo()
  File "C:/Users/Mohamed/Desktop/mopy/pys/first pycharm.py", line 60, in gameloo
    self.handle()
  File "C:/Users/Mohamed/Desktop/mopy/pys/first pycharm.py", line 74, in handle
    for event in pygame.event.get():
pygame.error: video system not initialized

这是我的代码:

import pygame
from pygame.locals import *

pygame.init()

resolution = (400, 350)
white = (250, 250, 250)
black = (0, 0, 0)
red = (250, 0, 0)
green = (0, 250, 0)

screen = pygame.display.set_mode(resolution)

class Ball:
    def __init__(self, xPos=resolution[0] / 2, yPos=resolution[1] / 2, xVel=1, yVel=1, rad=15):
        self.x = xPos
        self.y = yPos
        self.dx = xVel
        self.dy = yVel
        self.radius = rad
        self.type = "ball"

    def draw(self, surface):
        pygame.draw.circle(surface, black, (int(self.x), int(self.y)), self.radius)

    def update(self):
        self.x += self.dx
        self.y += self.dy
        if (self.x <= 0 or self.x >= resolution[0]):
            self.dx *= -1
        if (self.y <= 0 or self.y >= resolution[1]):
            self.dy *= -1

class player:
    def __init__(self, rad=20):
        self.x = 0
        self.y = 0
        self.radius = rad

    def draw(self, surface):
        pygame.draw.circle(surface, red, (self.x, self.y))

ball = Ball()

class game():
    def __init__(self):
        self.screen = pygame.display.set_mode(resolution)
        self.clock = pygame.time.Clock()
        self.gameobjct = []
        self.gameobjct.append(Ball())
        self.gameobjct.append(Ball(100))

    def handle(self):
        while True:
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()

    def gameloo(self):
        self.handle()
        for gameobj in self.gameobjct:
            gameobj.update()
        screen.fill(green)
        for gameobj in self.gameobjct:
            gameobj.draw(self.screen)
        pygame.display.flip()
        self.clock.tick(60)
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()

game().gameloo()
答案

你已经将窗口初始化了两次pygame.display.set_mode()。删除全局窗口初始化,但保留并使用设置为类.screengame属性的窗口。 方法handle应该只执行事件循环,但方法gameloo应该包含主循环。在主循环内部,事件必须由self.handle()处理:

screen = pygame.display.set_mode(resolution)

class game():
    def __init__(self):
        self.screen = pygame.display.set_mode(resolution)
        self.clock = pygame.time.Clock()
        self.gameobjct = []
        self.gameobjct.append(Ball())
        self.gameobjct.append(Ball(100))

    def handle(self):
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()

    def gameloo(self):
        while True:
            self.handle()
            for gameobj in self.gameobjct:
                gameobj.update()
            self.screen.fill(green)
            for gameobj in self.gameobjct:
                gameobj.draw(self.screen)
            pygame.display.flip()
            self.clock.tick(60)

game().gameloo()

以上是关于pygame.error:视频系统未初始化(pycharm)的主要内容,如果未能解决你的问题,请参考以下文章

pygame.error没有足够大的视频模式

Pygame:pygame.error:无法打开文件.ogg

Iexpress:pygame.error:文件不是 Windows BMP 文件

Python3错误。"pygame.error: video system not initialized"

Python之使用pip安转第三方库时报错ERROR: Could not find a version that satisfies the requirement pygame ERROR: No

Python之使用pip安转第三方库时报错ERROR: Could not find a version that satisfies the requirement pygame ERROR: No