Serge Alkhalil
Serge Alkhalil

Reputation: 37

Pygame is not running correctly

I am not able to run anything using pygame as whenever I run anything, even a very simple program displaying a circle, the program yields a black screen which does nothing.

The black screen I am talking about is this black screen

What is this exactly? and is there a way to fix it?

Edit: I forgot to mention that the program appears to be running well and I get no errors.

Edit #2: This is my very simple program:

import pygame
pygame.init()

screen = pygame.display.set_mode([500, 500])

running = True
while running:

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

screen.fill((255, 255, 255))

SCREEN_TITLE = 'Chess Game'
pygame.display.set_caption(SCREEN_TITLE)

pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)

pygame.display.flip()

pygame.quit()

Edit #3: a picture of what is showing on the python console after I press the exit button

before I press the exit button

Upvotes: 1

Views: 1813

Answers (1)

Cristian Ramon-Cortes
Cristian Ramon-Cortes

Reputation: 1888

You may experience two separated issues:

Issue 1: Pygame installation on MacOS

There are some documented problems when running PyGame with MacOS. Please check that you have correctly installed and setup pygame in your machine. This post might be useful.

Issue 2: Code incorrections

Apart from that, your code has several issues. Your running loop does not display anything since it is stuck processing events and nothing more. Therefore, you see a black screen. Notice that you are printing the screen and the circle when the execution is over.

When using pygame I suggest to differentiate between:

  • Initialisation: Setup pygame and screen. Render any static content.
  • Running loop: Process events and render any dynamic content.
  • End: Display any end animation/object and finalise pygame.

I suggest the following modifications:

  • Render first the screen
  • In the running loop just process the events and render the circle
  • I have added debug messages. You can enable and disable them by running python mygame.py and python -O mygame.py. Be aware that the print statements inside the running loop will print a lot of messages.

Here is the code:

#!/usr/bin/python

# -*- coding: utf-8 -*-

# For better print formatting
from __future__ import print_function

# Imports
import pygame


#
# MAIN FUNCTION
#
def main():
    # Setup display and static content
    if __debug__:
        print("Initialising pygame")
    pygame.init()
    SCREEN_TITLE = 'Chess Game'
    pygame.display.set_caption(SCREEN_TITLE)
    screen = pygame.display.set_mode([500, 500])
    screen.fill((255, 255, 255))
    pygame.display.flip()    

    # Running loop
    running = True
    while running:
        if __debug__:
            print("New iteration")
        # Process events
        if __debug__:
            print("- Processing events...")
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        # Display any dynamic content
        if __debug__:
            print("- Rendering dynamic content...")
        pygame.draw.circle(screen, (0, 0, 255), (250, 250), 75)

        # Update display
        if __debug__:
            print("- Updating display...")
        pygame.display.flip()

    # End
    if __debug__:
        print("End")
    pygame.quit()


#
# ENTRY POINT
#
if __name__ == "__main__":
    main()

Debug output:

$ python mygame.py 
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Initialising pygame
New iteration
- Processing events...
- Rendering dynamic content...
- Updating display...
.
.
.
New iteration
- Processing events...
- Rendering dynamic content...
- Updating display...
End

Display:

Game display

Upvotes: 1

Related Questions