1

I am trying to remove an image from memory to increase the efficiency of my code without the use of SpriteGroups as I am unfamiliar with that concept.

import pygame
import sys
pygame.init()

#constants
LENGTH = 454
SCREEN = pygame.display.set_mode((LENGTH, LENGTH))


def show(img, x, y):
    image = pygame.image.load(img)
    SCREEN.blit(image, (x, y))


def home():
    show('image1.png', 10, 10)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                #code to remove the image
                pass


def main():
    while True:
        SCREEN.fill((0, 0, 0))
        home()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        pygame.display.update()


main()

Here is a simple minimal code to keep the problem as general as possible all this code does is to remove the image on pressing space. Earlier I tried approaching it by either taking it outside the screen, or just rendering other objects over it. However the image isn't deleted completely I want to remove it not only from play but also from memory.

From past few days the 'www.pygame.org' site has been in solidarity hence unable to refer to the documentation

Divij Mahajan
  • 76
  • 1
  • 6

1 Answers1

1

There are multiple problems in your code.

pygame.event.get() get all the messages and remove them from the queue. See the documentation:

This will get all the messages and remove them from the queue. [...]

If pygame.event.get() is called in multiple event loops, only one loop receives the events, but never all loops receive all events. As a result, some events appear to be missed. Get the events once per frame and use them in multiple loops or pass the list of events to functions and methods where they are handled (see Faster version of 'pygame.event.get()'. Why are events being missed and why are the events delayed?).

Do not load the image in each frame. pygame.image.load is a very expensive operations. It has to load the image from the volume and interpret the image data. Load the image once at initialization.

Removing an image just means not to draw the image. Set image = None and only draw the image if image != None. Use the global statement to change a variable in gloabl namespace within a function:

import pygame
import sys
pygame.init()

#constants
LENGTH = 454
SCREEN = pygame.display.set_mode((LENGTH, LENGTH))

image = pygame.image.load('image1.png')

def show(img, x, y):
    SCREEN.blit(img, (x, y))

def home(event_list):
    global image
    for event in event_list:
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                image = None

    if image:
        show(image, 10, 10)

def main():
    run = True
    while run:
        event_list = pygame.event.get()
        for event in event_list:
            if event.type == pygame.QUIT:
                run = False

        SCREEN.fill((0, 0, 0))
        home(event_list)
        pygame.display.update()

    pygame.quit()
    sys.exit()

main()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174