2

I am making a game in pygame and my friend came across following issue when trying to run the following code.

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()

I ran this code fine on my linux distro, but my friend, who's running OSX 10.13.6, came across an error when he tried to run it saying "Illegal instruction: 4".

The only thread that provided any solution was this one: Illegal instruction: 4 on MacOS High Sierra

When we changed the line "pygame.init()" to "pygame.font.init()" the code worked fine on his machine as well as on mine, which is strange because pygame.font.init() should only initialize pygame.font?

Does anybody know why this works and/or have a better solution to this problem?

Python version is 3.6, pygame version is 1.9.4.

matt
  • 23
  • 2

1 Answers1

1

According to the pygame.org docs

You can always initialize individual modules manually, but pygame.init() is a convenient way to get everything started.

That tells us that it is not even necessary to call init() which explains why your code still works even though it's missing. The only thing that needs to be initialized in your code example is the display module but for some curious reason the display module initializes itself when you call pygame.display.set_mode((640, 480)). You can see it happening with this code example:

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 and check if they need to be initialized.

Now, the reason your friend is getting Illegal instruction: 4 is very likely due to the issue explained in this thread. I'd recommend following the instructions from the answer (and read the reason for why it's happening), try uninstalling pygame and then install again with

$ pip install --no-binary pygame pygame

this will very likely resolve his issue. Hopefully this answered your questions.

Alexander Freyr
  • 569
  • 1
  • 6
  • 19