Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

python - How to Flip Image After Hitting Wall - Pygame

I want the image to face right when travelling right and face left when travelling left. I'm not sure what to do here... This is for an assignment and flipping the image isn't necessary, but I'd still like to learn how to do it.

"""
    Author: victor Xu

    Date: January 21st, 2021

    Description: Animating 3 images with background music and 3 sound effects.
"""

import pygame


def main():
    '''This function defines the 'mainline logic' for our game.'''
    # I - INITIALIZE
    pygame.init()

    # DISPLAY
    screen = pygame.display.set_mode((640, 480))
    pygame.display.set_caption("Crazy Shapes Animation")

    # ENTITIES
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((255, 255, 255))  # white background

    pygame.mixer.music.load("music.ogg")  # background music
    pygame.mixer.music.set_volume(0.5)
    pygame.mixer.music.play(-1)

    cherry_wav = pygame.mixer.Sound("cherry.wav")
    cherry_wav.set_volume(0.5)  # cherry sfx

    pacman_wav = pygame.mixer.Sound("pacman.wav")
    pacman_wav.set_volume(0.5)  # pacman sfx

    ghost_wav = pygame.mixer.Sound("ghost.wav")
    ghost_wav.set_volume(0.5)  # ghost sfx

    pacman = pygame.image.load("PacMan.png")
    pacman.set_colorkey((0, 0, 0))

    cherry = pygame.image.load("Cherry.png")
    cherry.set_colorkey((0, 0, 0))

    ghost = pygame.image.load("Ghost.png")
    ghost.set_colorkey((0, 0, 0))

    # A - ACTION (broken into ALTER steps)

    # ASSIGN
    clock = pygame.time.Clock()
    keepGoing = True

    pacman_x = 0  # Assign starting (x,y)
    pacman_y = 200  # for our red box
    move_pacman_x = 5

    ghost_x = 300  # Assign starting (x,y)
    ghost_y = 200  # for our green rectangle
    move_ghost_y = 5

    cherry_x = 640  # Assign starting (x,y)
    cherry_y = 0  # for our blue circle
    move_cherry_x = 5
    move_cherry_y = 5

    # LOOP
    while keepGoing:

        # TIMER
        clock.tick(30)

        # EVENT HANDLING
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False

        # change x coordinate of red box
        pacman_x += move_pacman_x
        # check boundaries, to bounce off wall
        if pacman_x >= screen.get_width():
            pacman_wav.play()
            pacman_x = screen.get_width()
            move_pacman_x *= -1
        if pacman_x <= 0:
            pacman_wav.play()
            pacman_x = 0
            move_pacman_x *= -1

        # change y coordinate of green rectangle
        ghost_y += move_ghost_y
        # check boundaries, to bounce off wall
        if ghost_y >= screen.get_height():
            ghost_wav.play()
            ghost_y = screen.get_height()
            move_ghost_y *= -1
        if ghost_y <= 0:
            ghost_wav.play()
            ghost_y = 0
            move_ghost_y *= -1

        # change x coordinate of blue circle
        cherry_x += move_cherry_x
        # check boundaries, to bounce off wall
        if cherry_x >= screen.get_width():
            cherry_wav.play()
            cherry_x = screen.get_width()
            move_cherry_x *= -1
        if cherry_x <= 0:
            cherry_wav.play()
            cherry_x = 0
            move_cherry_x *= -1

        # change y coordinate of blue circle
        cherry_y += move_cherry_y
        # check boundaries, to bounce off wall
        if cherry_y >= screen.get_height():
            cherry_wav.play()
            cherry_y = screen.get_height()
            move_cherry_y *= -1
        if cherry_y <= 0:
            cherry_wav.play()
            cherry_y = 0
            move_cherry_y *= -1

        # REFRESH (update window)
        screen.blit(background, (0, 0))
        screen.blit(ghost, (ghost_x, ghost_y))  # blit rectangle at new (x,y) location
        screen.blit(pacman, (pacman_x, pacman_y))  # blit box at new (x,y) location
        screen.blit(cherry, (cherry_x, cherry_y))  # blit circle at new (x, y) location
        pygame.display.flip()

    # Close the game window
    pygame.quit()


# Call the main function
main()

I did some research and people recommended using pygame.transform.flip(), which I have no idea how to use. By the way, I don't think you'll need any of the files that I imported, but I'll be glad to provide the resources if you need it. Any help would be greatly appreciated!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See pygame.transform.flip():

flip(Surface, xbool, ybool) -> Surface

This can flip a Surface either vertically, horizontally, or both.

pygame.transform.flip() flips creates a new Surface from the source Surface which is flipped. e.g:

my_flipped_image = pygame.transform.flip(my_image, True, False); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...