Search code examples
pythonpygamepython-3.8

Pygame showing only a black screen


I am new to Pygame, so I was doing some testing before starting an actual game but when I executed the code, all I am getting is a black screen even though I have specified a color and I also tried using the blit method but is still showing only a black screen. Here is my code and the output on Ubuntu 20.04 and Python 3.8:

import pygame

pygame.init()

s = pygame.display.set_mode((1000,500))

testImg = pygame.image.load('player.png')
testX = 500
testY = 250

def test(x, y):
    s.blit(testImg, (x,y))

# Game Loop:
running = True
while running:
    s.fill((113,113,113))

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

    test(testX, testY)

Here is the output: enter image description here


Solution

  • You missed to update the display by pygame.display.flip() at the end of the application loop:

    running = True
    while running:
        s.fill((113,113,113))
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    
        test(testX, testY)
    
        pygame.display.flip() # <---
    

    A minimal pygame application loop has to: