gpt4 book ai didi

python - pygame.错误 : display Surface quit after filling the surface black

转载 作者:太空宇宙 更新时间:2023-11-04 01:58:14 25 4
gpt4 key购买 nike

在 pygame 中将我的表面涂成黑色之后。我得到错误

pygame.error: display Surface quit.

完整错误:

>
D:\Programme\Anaconda3\envs\gameDev\python.exe "C:/Users/xxx/PycharmProjects/Workspace/pygame snake/main.py"
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "C:/Users/xxx/PycharmProjects/Workspace/pygame snake/main.py", line 81, in <module>
thegame.execute()
File "C:/Users/xxx/PycharmProjects/Workspace/pygame snake/main.py", line 74, in execute
self.render(self._display_surf)
File "C:/Users/xxx/PycharmProjects/Workspace/pygame snake/main.py", line 41, in render
_display_surf.fill((0,0,0))
pygame.error: display Surface quit
<Surface(Dead Display)>

我尝试代替:

_display_surf.fill((0,0,0))

使用:

_display_surf.fill(pygame.color("black"))

但这也没有用。

这是我的完整源代码:

import pygame
from pygame.locals import *


class Player(object):
x = 10
y = 10
speed = 1

def moveRight(self):
self.x = self.x + self.speed

def moveLeft(self):
self.x = self.x - self.speed

def moveUp(self):
self.y = self.y - self.speed

def moveDown(self):
self.y = self.y + self.speed


class game:
def __init__(self):
self.resolution = (800, 500)
self._running = True
self._display_surf = None
self.player = Player()

def on_init(self):
pygame.init()
self._display_surf = pygame.display.set_mode(self.resolution)
pygame.display.set_caption("Snake!")
self._running = True

def on_cleanup(self):
pygame.quit()

def render(self, _display_surf):
print(_display_surf)
_display_surf.fill((0,0,0))
pygame.draw.rect(self._display_surf, (0, 128, 255), pygame.Rect(self.player.x, self.player.y, 30, 30))
pygame.display.flip()

def loop(self):
pass

def on_event(self, event):
if event.type == pygame.quit():
print("quiet")
self._running = False

elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
self.player.moveRight()

elif event.key == pygame.K_LEFT:
self.player.moveLeft()

elif event.key == pygame.K_UP:
self.player.moveUp()

elif event.key == pygame.K_DOWN:
self.player.moveDown()

def execute(self):
if self.on_init() == False:
self._running = False

while self._running:
for event in pygame.event.get():
self.on_event(event)

self.render(self._display_surf)
self.loop()
self.on_cleanup()


if __name__ == "__main__":
thegame = game()
thegame.execute()

我希望我有一个黑色的表面并且没有任何反应。但是相反,当我尝试将其涂成黑色时它崩溃了。希望有人能帮忙

解决方案:对于任何有兴趣的人

代替

        if event.type == pygame.quit():
print("quiet")
self._running = False

我需要

        if event.type == pygame.QUIT:
print("quiet")
self._running = False

最佳答案

代码

 if event.type == pygame.quit():
print("quiet")

没有按照您的预期去做。 pygame.quit()是一个函数调用并取消初始化所有 pygame 模块。该函数返回 None,因此条件失败。代码运行并在下一条试图访问 pygame 模块的指令处崩溃。

您必须将 event.type 与事件枚举器常量 pygame.QUIT 进行比较,后者标识 quit 事件:

if event.type == pygame.QUIT:
print("quiet")
self._running = False

请参阅 pygame.event 的文档.

关于python - pygame.错误 : display Surface quit after filling the surface black,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56303548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com