Search code examples
pythonpython-3.xerror-handlingkeypygame

pygame.key.get_pressed() - doesn't work - pygame.error: video system not initialized


I have two problems with my program:

  1. When I close my program it has error: keys = pygame.key.get_pressed() pygame.error: video system not initialized
  2. Square moves while I'm pressing 'd' and when I press something (or move mouse)

Important part of code is:

import pygame
from pygame.locals import*

pygame.init()

screen = pygame.display.set_mode((1200, 700))
ticket1 = True

# ...

c = 550
d = 100

# ...

color2 = (250, 20, 20)

while ticket1 == True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            ticket1 = False
            pygame.quit()
            pygame.display.quit()

        keys = pygame.key.get_pressed()

        if keys[pygame.K_d]:
            c += 1

    # ...

    screen.fill((255, 250, 245))

    pygame.draw.rect(screen, color2, pygame.Rect(c, d, 50, 75))

    pygame.display.flip()

If I write keys = pygame.key.get_pressed() in just while loop it doesn't have error but it seems slower.

I also have another error: pygame.error: display Surface quit, but I always and in all my pygame programs have it and it isn'n so important but other things are important.


Solution

  • 1.--------------

    After pygame.quit() you don't need pygame.display.quit() but sys.exit(). pygame.quit() doesn't exit program so program still try to call screen.fill() and other function below pygame.quit()

    Or you have to put pygame.quit() outside while ticket == True: (and then you don't need sys.exit())

    You can use while ticket1: in place of while ticket == True: - it is more pythonic.

    while ticket1: # it is more pythonic
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                ticket1 = False
    
            keys = pygame.key.get_pressed()
    
            if keys[pygame.K_d]:
                c += 1
    
        # ...
    
        screen.fill((255, 250, 245))
    
        pygame.draw.rect(screen, color2, pygame.Rect(c, d, 50, 75))
    
        pygame.display.flip()
    
    pygame.quit()
    

    2.--------------

    if keys[pygame.K_d]: c += 1 is inside for event loop so it is call only when event is happend - when mouse is moving, when key is pressed or "unpressed". Move it outside of for event loop.

    while ticket1: # it is more pythonic
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                ticket1 = False
    
            keys = pygame.key.get_pressed()
    
        # outside of `for event` loop  
        if keys[pygame.K_d]:
            c += 1
    
        # ...
    
        screen.fill((255, 250, 245))
    
        pygame.draw.rect(screen, color2, pygame.Rect(c, d, 50, 75))
    
        pygame.display.flip()
    
    pygame.quit()
    

    Some people do it without get_pressed()

    # clock = pygame.time.Clock()
    move_x = 0
    
    while ticket1 == True:
    
        # events
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                ticket1 = False
    
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    ticket1 = False
    
                elif event.key == pygame.K_d:
                    move_x = 1
    
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_d:
                    move_x = 0
    
        # variable modification
    
        c += move_x
    
        # ...
    
        # draws
    
        screen.fill((255, 250, 245))
    
        pygame.draw.rect(screen, color2, pygame.Rect(c, d, 50, 75))
    
        pygame.display.flip()
    
        # 60 FPS (Frame Per Second) to make CPU cooler
    
        # clock.tick(60)
    
    pygame.quit()
    

    BTW: use pygame.time.Clock() to get the same FPS on fast and slow computers. Without FPS program refresh screen thousends times per second so CPU is busy and hot.

    If you use FPS you have to add to c bigger value to get the same speed then before.