How do you remove an image from pygame

Viewed 315

I attempted to create a customisation menu for my game and for it, I made a cursor so that the user is able to choose from 1 of 3 characters, and the error is that the image of the cursor is duplicated onto the next position and the original one stays, I was wondering if there was a way to remove the original cursor.

One_Right_Key = pygame.K_RIGHT

# Creating the customisation menu for the game
def Customisation(players):
    #Initiating TwoX in case there is no second player to make sure the program works correctly
    TwoX = 100
    # Placing the background onto the game
    background = pygame.image.load('Menu/Customisation Background.png')
    screen.blit(background, (0, 0))
    # Creating an array for each of the x positions of the 3 options of characters
    x = [157, 545, 954]
    # Creating the Cursor for the first player
    CursorOne = pygame.image.load('Misc/P1 Cursor.png')
    CursorOne = pygame.transform.scale(CursorOne, (124, 100))
    CursorOne_move = CursorOne.get_rect()
    # Creating a variable to give the position of the cursor and placing it onto the screen
    OneX = 0
    screen.blit(CursorOne, (x[OneX], 61))
    # Checking if there is one or two players playing the game
    if players == 2:
        # Creating the cursor for the second player
        CursorTwo = pygame.image.load('Misc/P2 Cursor.png')
        CursorTwo = pygame.transform.scale(CursorTwo, (124, 100))
        # Creating a variable to give the position of the second cursor
        TwoX = 1
        screen.blit(CursorTwo, (x[TwoX], 61))
    # Creating the back button
    Back_Text = BackButtonFont.render("Back", True, (255, 255, 255))
    # Placing the text onto the screen
    screen.blit(Back_Text, (37, 628))
    # Updating the screen
    pygame.display.update()
    # Keeps the program running
    Running = True
    while Running == True:
        for event in pygame.event.get():
            # If the user clicks on the back button
            if event.type == pygame.MOUSEBUTTONUP:
                mouse = pygame.mouse.get_pos()
                if mouse[0] >= 0 and mouse[0] <= 280 and mouse[1] > 611 and mouse[1] <= 719:
                    # Goes to the Main Menu Procedure
                    MainMenu()
                    # Exits the customisation procedure
                    exit(Customisation())
            # If the player presses a key
            if event.type == pygame.KEYDOWN:
                if event.key == One_Right_Key:
                    OneX = OneX + 1
                    # If OneX is the same as TwoX, it moves to the next one
                    if OneX == TwoX:
                        OneX = OneX + 1
                    # If OneX is over, it goes back to the first position
                    if OneX == 3:
                        OneX = 0
                    CursorOne_move.move_ip(x[OneX], 61)
                    screen.blit(CursorOne, CursorOne_move)
                pygame.display.flip()
            # If the user clicks on the cross on the top right of the window
            if event.type == pygame.QUIT:
                exit()

All the files for the game

1 Answers

You can not "remove" an image drawn on the screen. You must redraw the complete scene in each frame. Deleting (or "removing") an object means not to draw it in the next frame.

The typical PyGame application loop has to:

Related