Pygame Is Installed Properly, But Why Does It Not Display Anything?

Fellow human beings!

I have recently installed pygame, following the awesome guide made by Bryson Payne that you can find here . I installed all of the following dependencies and programs, as per the order in the instructions.

  1. Installed XQuartz
  2. Homebrew was already installed (it was the second step).
  3. Installed a version of python 3 from homebrew especifically for pygame use
  4. Executed the following commands into the terminal: brew install mercurial, brew install sdl sdl_image sdl_mixer sdl_ttf portmidi, brew tap homebrew/headonly (this command failed, but pygame still let me download it! I read that if I did not have this, it should not let me download, so I just brushed it off. If you think this is the problem, tell me!), and, finally, brew install smpeg
  5. Then, sudo pip3 install hg+http://bitbucket.org/pygame/pygame. This did not work on the first run, so I then installed it via 'pip3 install pygame', and it worked. I then tried the original command again, and it did download this time.

Additionally, I installed pgzero afterwards via the terminal, and then to pycharm via the project interpreter. The same was done to the normal pygame. And, something peculiar happened. The IDLE that was supposedly pygame enabled (python 3.7) actually did not work. I then tried to use the other IDLE I had before, 3.8.2, and that did work.

In the first try, which was a while ago (around 2 weeks), I really did not think much of the installation, since it had downloaded correctly. Then, when I tried to run my simple program on both PyCharm and IDLE, none worked! The code on IDLE displayed no error message... IDLE just runned the code but nothing happened, as python started running the program: but it appeared after a couple minutes, that the code was doing nothing. The following window, which executes the programs, appeared.

The window that executes programs

But PyCharm, running the same code, displays a different message. It goes as follows (notice that in the place above the .py file, there is the photo that was to be used).

See the image here

The new window did not show a >>> prompt, even after letting it run for various minutes. Python Launcher bounced on the dock, as if loading, until it stopped after 3 minutes.

The python launcher bouncing

So, to clarify, the code ran with no errors but did not display, even a basic window! This happened with various small programs. The following are some examples.

import pygame
background_colour = (255,255,255)
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Tutorial 1')
screen.fill(background_colour)
pygame.display.flip()
running = True
while running:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      running = False

I also ran some more complex programs, like the ones in the book 'Mission Python'. You can download the game files here.

Another small program I ran was:

import pygame
pygame.init()
img =pygame.image.load('astronaut.png')
display = pygame.display.set_mode((400, 400))
display.blit(img, [0, 0])
pygame.display.update()

The final one was...

import pygame as pg
import sys

pg.init()
screen = pg.display.set_mode((800,600))
done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True

    pg.display.update()
pg.quit()
sys.exit()

After these failures (the program was not running), I uninstalled and then installed pygame and pgzero (in both IDLE and PyCharm) again. 3 times (none of those worked). After each install, I restarted the computer. Heck! I even tried to install by various methods (like sudo pip3 install pygame, pip3 install pygame, sudo pip install pygame, and finally, sudo pip3 instal pygame). When I tried to instal it using pip3, it installed pygame, and when I tried using only pip, it also installed pygame (this was done one command after the other).

I ask for help, because all of my development software/IDEs are pygame-enabled (after all, they don't show any error message). So what could it be? The code is copied from the pygame docs, that suggest using some of those snippets of code to prove pygame is working correctly. Really, I am desperate, since my goal is just to make pygame work.

Is it my usage of images incorrectly? But I am storing the files in the same directory as the images (same folder). I call my images as per my book (this is only applicable to the longer program found in the Mission Python files linked previously)


Remember, the main issue is that pygame is enabled but does not display. The text before is just some of the things I did to potentially solve this issue.


Is it the brew tap homebrew/headonly command that did not work? What else can I do to replace this command and get pygame to work?

Upvotes: 0

Views: 1636

Answers (1)

Ted Klein Bergman
Ted Klein Bergman

Reputation: 9746

There could be many reasons, so I'll tackle the one's I can think of.


Your code exits immediately.

When running the following code (or similarly structured codes),

import pygame
pygame.init()
img =pygame.image.load('astronaut.png')
display = pygame.display.set_mode((400, 400))
display.blit(img, [0, 0])
pygame.display.update()

the code will exit immediately, so you'll never notice the game actually running. It'll only run for a fraction of a second before it terminates. You must have some form of loop for it to keep open. This seems to be the problem in most of your examples (for example when you ran through IDLE).

I'd suggest you try with this code

import pygame
pygame.init()

screen  = pygame.display.set_mode((800, 600))
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((255, 0, 0))
    pygame.display.update()

You should see a red window.


Your images is in the wrong path or have wrong names.

According to one of your pictures, you cannot load an image. This means that the program is working, but it cannot locate the picture rescue_ship.png. When running from an IDE, all files can be accessed by relative path that is relative to the project root.

I don't know what that is in your case, but I'd suggest that you try to use the absolute path instead, just to see if it's working. It could also be possible that you misspelled your image name; it has to be exact.


You're wrong python version.

Make sure you're not using Python 3.8 by accident. I don't think pygame supports it and I've seen many other questions that have problems using it. This might be difficult to notice since everything will work fine until it suddenly doesn't.

Also, make sure you're using the Python version that has pygame installed. For example, I have Python 3.5, 3.6 and 3.8, but only Python 3.5 has pygame installed. So if I run my pygame scripts with Python 3.6, it won't work (it'll complain with "No module named pygame").

Upvotes: 1

Related Questions