Reputation: 906
After I paint my surface black in pygame. I get the error
pygame.error: display Surface quit.
Full error:
>
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)>
I tried instead of:
_display_surf.fill((0,0,0))
using:
_display_surf.fill(pygame.color("black"))
But that didnt work either.
Here is my full source code:
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()
I expect that I have a black surface and nothing happens. But instead, it crashes when I try to paint it black. Hope somebody can help
Solution: For anybody who is intrested in
instead of
if event.type == pygame.quit():
print("quiet")
self._running = False
i need to to
if event.type == pygame.QUIT:
print("quiet")
self._running = False
Upvotes: 2
Views: 4807
Reputation: 210878
The code
if event.type == pygame.quit(): print("quiet")
doesn't do what you expect it to do. pygame.quit()
is a function call and uninitializes all pygame modules. The function returns None
and so the condition fails. The code runs through and crashes at the next instruction which tries to access a pygame module.
You've to compare event.type
to the event enumerator constant pygame.QUIT
, which identifies the quit event instead:
if event.type == pygame.QUIT:
print("quiet")
self._running = False
See the documentation of pygame.event
.
Upvotes: 2