[英]“pygame.error: display Surface quit” when running the code
I am trying to make a simple moving game with Pygame since I am currently learning it. 我正在尝试与Pygame进行简单的移动游戏,因为我正在学习它。 Whenever i try to run the code I keep on getting a problem saying: "pygame.error: display Surface quit"
每当我尝试运行代码时,我都会遇到问题:“pygame.error:display Surface quit”
I've tried adding "break" at the end but the window closes immediately! 我试过在最后添加“休息”,但窗口立即关闭! I've tried searching for the solution but I can't find one that helps my code.
我已经尝试过搜索解决方案,但我找不到一个可以帮助我的代码。
import pygame
import random
pygame.init()
# Window setup
size = [400, 400]
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
# player position
x = size[0] // 2
y = size[1] // 2
# ball position
ballX = random.randrange(0, size[0])
ballY = random.randrange(0, size[1])
# colours
red = pygame.color.Color('#FF8080')
blue = pygame.color.Color('#8080FF')
white = pygame.color.Color('#FFFFFF')
black = pygame.color.Color('#000000')
def CheckOffScreenX(x):
if x > size[0]:
x = 0
elif x < 0:
x = size[0]
return x
def CheckOffScreenY(y):
if y > size[1]:
y = 0
elif y < 0:
y = size[1]
return y
# Game loop
done = False
while not done:
screen.fill(black)
keys = pygame.key.get_pressed()
#player movement
if keys[pygame.K_w]:
y -=1
if keys[pygame.K_s]:
y +=1
if keys[pygame.K_a]:
x -=1
if keys[pygame.K_d]:
x +=1
# Check offscreen
x = CheckOffScreenX(x)
y = CheckOffScreenY(y)
# draw player
pygame.draw.circle(screen, red, [x, y], 6)
pygame.display.flip()
# draw ball
pygame.draw.circle(screen, blue, [ballX, ballY], 6)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
clock.tick(32)
pygame.quit()
Any help would be appreciated! 任何帮助,将不胜感激!
The issue is the pygame.quit()
insider the main loop. 问题是
pygame.quit()
内部主循环。 pygame.quit()
uninitialize all pygame modules. pygame.quit()
uninitialize所有pygame模块。 After the modules are uninitialized all further calls to pygyme instructions (in the next frame) will cause a crash. 在模块未初始化之后,所有对pygyme指令的进一步调用(在下一帧中)将导致崩溃。
Do pygame.quit()
after the main loop, when the application has end. 在主循环之后,当应用程序结束时执行
pygame.quit()
。
done = False
while not done:
screen.fill(black)
# [...]
# pygame.quit() <----- delete
pygame.quit() # <---- add
Note, probably you've added an Indentation when you copied the code. 请注意,您可能在复制代码时添加了缩进 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.