【问题标题】:Pygame Illegal Instruction 4?Pygame非法指令4?
【发布时间】:2019-06-15 03:21:48
【问题描述】:

我正在 pygame 中制作游戏,我的朋友在尝试运行以下代码时遇到了以下问题。

import pygame

pygame.init()

screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('winter gam')
pygame.display.update()

running = True

clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False

    clock.tick(60)

    screen.fill((0, 0, 0))

    pygame.draw.rect(screen, (255, 0, 0), [10, 10, 100, 100])

    pygame.display.update()

pygame.quit()

我在我的 linux 发行版上运行了这段代码,但我的朋友正在运行 OSX 10.13.6,当他尝试运行它时遇到一个错误,说“非法指令:4”。

提供任何解决方案的唯一线程是这个:Illegal instruction: 4 on MacOS High Sierra

当我们将“pygame.init()”行更改为“pygame.font.init()”时,代码在他的机器和我的机器上都可以正常工作,这很奇怪,因为 pygame.font.init() 应该只初始化pygame.font?

有人知道为什么会这样和/或有更好的解决方案吗?

Python 版本是 3.6,pygame 版本是 1.9.4。

【问题讨论】:

    标签: python-3.x macos pygame


    【解决方案1】:

    According to the pygame.org docs

    您始终可以手动初始化各个模块,但是 pygame.init() 是开始一切的便捷方式。

    这告诉我们,甚至不需要调用init(),这解释了为什么您的代码即使丢失了仍然可以工作。在您的代码示例中唯一需要初始化的是display 模块,但出于某种奇怪的原因,display 模块会在您调用pygame.display.set_mode((640, 480)) 时自行初始化。您可以通过以下代码示例看到它发生的情况:

    import pygame
    print("Before: " + str(pygame.display.get_init()))
    screen = pygame.display.set_mode((640, 480))
    print("After : " + str(pygame.display.get_init()))
    

    You can see all of the pygame module indexes here 并检查它们是否需要初始化。

    现在,您的朋友收到Illegal instruction: 4 的原因很可能是issue explained in this thread。我建议按照答案中的说明进行操作(并阅读发生这种情况的原因),尝试卸载 pygame 然后再次安装

    $ pip install --no-binary pygame pygame
    

    这很可能会解决他的问题。希望这能回答您的问题。

    【讨论】:

      猜你喜欢
      • 2021-06-14
      • 1970-01-01
      • 2018-03-20
      • 2017-09-10
      • 1970-01-01
      • 2015-01-09
      • 2018-07-20
      • 2019-03-11
      • 2022-01-16
      相关资源
      最近更新 更多