I need my character image to change when it hits a certain coordinate

import pygame  #importing pygame onto this program

pygame.init()  #initialising the pygame

display_width = 1000  #making the width for the window 800
display_height = 700 #making the height for the window 600

gameDisplay = pygame.display.set_mode((display_width,display_height))  #storing the 
width and height of the window onto a variable
pygame.display.set_caption('The Dark Knight')  #setting the name for the window as 
the dark knight

background = pygame.image.load('/Users/arnav/Downloads/GothamCity.jpg')

appwallpaper = pygame.image.load('/Users/arnav/Downloads/AppWallpaper.jpeg') #setting 
the apps wallpaper storing it in a variable

pygame.display.set_icon(appwallpaper) #setting the apps wallpaper as the poster

Joker = pygame.image.load('/Users/arnav/Downloads/Joker.png') #loading the image of 
the joker and storing it in a variable
JokerX = 1000
JokerY = 400
JokerX_change = 0
JokerY_change = 0

def TheJoker(x,y):

#looping the game
running = True

while running:

    gameDisplay.blit(background, (0,0))

    for event in pygame.event.get():  #storing every event in pygame under event
        if event.type == pygame.QUIT:
            running = False    
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT or event.key == ord('a'):
                JokerX_change = -1
            if event.key == pygame.K_RIGHT or event.key == ord('d'):    
                JokerX_change = 1
            if event.key == pygame.K_UP or event.key == ord('w'):
                JokerY_change = -1 
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == ord('a') or event.key == 
pygame.K_RIGHT or event.key == ord('d') or event.key == pygame.K_UP or event.key== 
                JokerX_change = 0
                JokerY_change = 0
            if event.key == pygame.K_UP or event.key == ord('w'):
                JokerY_change = 1             
    JokerX += JokerX_change
    JokerY += JokerY_change

    spriteWidth, spriteHeight = Joker.get_rect().size
    JokerX = min(max(JokerX, 0), display_width - spriteWidth)
    JokerY = min(max(JokerY, 150), display_height - spriteHeight)




I need my character (Joker) to invert when it reaches the end of the screen. The inverted png is “JokerInverted.png”. The X coordinates at the end of the screen is 0. I also need it to revert back to normal at the X coordinate 850. I was thinking to use an if statement so like if JokerX == 0: reload the image but I do not know the code to reload an image.


Load 2 Images. Introduce a new variable (currentJokerImg) that indicates the current representation of the wildcard Joker:

jokerImg = pygame.image.load('/Users/arnav/Downloads/Joker.png')
jokerInvertImg = pygame.image.load('/Users/arnav/Downloads/JokerInverted.png')
currentJokerImg = jokerImg 

Use currentJokerImg instead of Joker in the TheJoker function:

def TheJoker(x,y):

Now you can change currentJokerImg whenever you want. e.g.:

while running:
    # [...]

    JokerX += JokerX_change
    JokerY += JokerY_change
    if JokerX <= 0:
        currentJokerImg = jokerInvertImg
    elif JokerX >= 850:
        currentJokerImg = jokerImg

    # [...]