Search code examples
pythonpygamecharacter

How do I remove a sprite in pygame by clicking a button on my keyboard?


Ok so I want to remove a sprite when I click the a button to show that the sprite is moving left. Here is my code

import pygame
import os

pygame.init()

#### CONSTANTS ####


WIDTH, HEIGHT = 1000, 600

WIN = pygame.display.set_mode((WIDTH, HEIGHT))

BACKGROUND = pygame.transform.scale(pygame.image.load(os.path.join('Assets', 'Background.jpg')), (WIDTH, HEIGHT))

MAIN_CHARACHTER = pygame.transform.scale(pygame.image.load(os.path.join('Assets', 'Main_sprite.png')), (70, 90))

MAIN_CHARACTER_LEFT = pygame.transform.scale(pygame.image.load(os.path.join('Assets', 'Main_sprite_left.png')), (70, 90))

FPS = 60

VEL = 8


###################

pygame.display.set_caption('Cog runnr')

def draw_window(man):
    WIN.blit(BACKGROUND, (0, 0))
    WIN.blit(MAIN_CHARACHTER, (man.x, man.y))

    

def handle_movement(keys_pressed, man):
    if keys_pressed[pygame.K_a] and man.x + 10 > 0: #LEFT
        WIN.remove(MAIN_CHARACHTER)

def main():
    man = pygame.Rect(350, 375, 70, 90)
    gameLoop = True
    clock = pygame.time.Clock()
    while gameLoop:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameLoop = False
                pygame.QUIT
        keys_pressed = pygame.key.get_pressed()
        draw_window(man)
        handle_movement(keys_pressed, man)
        
        pygame.display.update()

if __name__ == '__main__':
    main()

Please check the handle_movement function and tell me how I can remove the main character to shwo the Main_sprite_left image

Here is my error

Traceback (most recent call last):
  File "d:\Programming\Python\pygame\Cog_runner\main.py", line 55, in <module>
    main()
  File "d:\Programming\Python\pygame\Cog_runner\main.py", line 50, in main
    handle_movement(keys_pressed, man)
  File "d:\Programming\Python\pygame\Cog_runner\main.py", line 36, in handle_movement
    WIN.remove(MAIN_CHARACHTER)
AttributeError: 'pygame.Surface' object has no attribute 'remove'

Solution

  • You cannot "remove" something drawn on the screen. The objects on the screen are just a bunch of colored pixels. If you don't want to see something just don't draw it. The scene is redrawn in every frame.

    Draw the object depending on a state variable (draw_man). Change the state when the key is pressed:

    def draw_window(man, draw_man):
        WIN.blit(BACKGROUND, (0, 0))
        if draw_man:
            WIN.blit(MAIN_CHARACHTER, (man.x, man.y))
    
    def handle_movement(keys_pressed, man):
        draw_man = True
        if keys_pressed[pygame.K_a] and man.x + 10 > 0: #LEFT
            draw_man = False
        return draw_man
    
    def main():
        man = pygame.Rect(350, 375, 70, 90)
        draw_man = True
        gameLoop = True
        clock = pygame.time.Clock()
        while gameLoop:
            clock.tick(FPS)
            
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    gameLoop = False
                    pygame.QUIT
            
            keys_pressed = pygame.key.get_pressed()
            draw_man = handle_movement(keys_pressed, man)
    
            draw_window(man, draw_man)
            pygame.display.update()
    
    if __name__ == '__main__':
        main()