python - pygame.错误: video system not initialized(pycharm)

标签 python pygame

我正在尝试通过复制和检查以下内容来学习 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() 。删除全局窗口初始化,但保留并使用设置为 game 类的 .screen 属性的窗口。
方法handle 应该只执行事件循环,但方法gameloo 应该包含主循环。在主循环内,事件必须由 self.handle() 处理:

屏幕 = pygame.display.set_mode(分辨率)

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

关于python - pygame.错误: video system not initialized(pycharm),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55007434/

相关文章:

python - 如何找到最近的可能的开放位置进行 pygame 碰撞检测?

python - 使用 replace() 替换多个符号

python - Pygame错误: Unsupported Image Format

javascript - 使用python检索动态网站的源(绕过onclick)

python - 为什么没有构造函数参数的类需要括号

python - Pygame 不返回操纵杆轴移动而不显示

python - 增加计数变量而不等待上面一行代码完成

python - 如何在pygame中获取键盘输入?

python - 在 Cython 中传递有界方法作为参数

python - 在仅容器开发环境之外运行 django `makemigrations` 的正确做法是什么