【问题标题】:Pygame keeps making the kernel exitPygame不断使内核退出
【发布时间】:2019-08-05 20:03:14
【问题描述】:

所以我尝试使用 pygame 在屏幕上显示文本。但是,在使用 pyzo 执行第三次或第四次之后,它可以工作,内核刚刚退出,我必须再次重新启动它。它非常不稳定和烦人。

我想知道我可能忘记了什么或做错了什么。 我感谢任何花时间研究这个问题的人。

编辑:

错误是: 内核进程退出。 (3221225477)

running = True
while running :
    # completely fill the surface object 
    # with black color 
    display_surface.fill(black) 

    # copying the text surface object 
    # to the display surface object 
    # at the center coordinate. 
    update_text_screen(text_to_display)

    # iterate over the list of Event objects 
    # that was returned by pygame.event.get() method. 


    for event in pygame.event.get() : 
        if event.type == pygame.QUIT:
            running = False

    pygame.display.update()

pygame.display.quit()
pygame.quit()
class text:
    def __init__(self, message, colour, state, X, Y) :
        global text_to_display
        self.text_to_print = font_text.render(message, True, colour)
        self.X = X
        self.Y = Y
        self.state = state

    def v(self):
        display_surface.blit(self.text_to_print,[self.X,self.Y])



def update_text_screen(list_of_text):
    global C_px, C_py
    for i in range(len(list_of_text)) :
        if list_of_text[i].state:
            C_py = i*30
            list_of_text[i].X = C_px
            list_of_text[i].Y = C_py
        list_of_text[i].v()

Edit2 : 这是 Valentino 询问的代码

【问题讨论】:

  • pygame.display.update() 必须在主循环中,而不是在事件循环中 (Indentation)。
  • 谢谢,我放到主循环了,可惜还是crash
  • 可以加update_text_screen的代码吗?其余代码似乎没问题,除了 Rabbit76 已经提到的缩进错误。如果它没有解决问题,你也应该在你的问题中解决这个问题。另外,如果您从命令行运行代码而不是使用 pyzo?问题是否仍然存在?
  • 给你。如果代码似乎无法理解,我可以将其全部发布。

标签: python-3.x pygame pyzo


【解决方案1】:

我有完全相同的问题,当我删除文本绘图时它似乎消失了! 因此,经过一些测试,当我像这样在程序末尾删除字体对象时,它似乎可以工作:

fontobj= pygame.font.SysFont("Arial", 25)
...
while ...
  ...
del(fontobj)
pygame.quit()

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 1970-01-01
    • 2017-02-19
    相关资源
    最近更新 更多