首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在撞墙后倒置图像

如何在撞墙后倒置图像
EN

Stack Overflow用户
提问于 2021-01-21 19:51:35
回答 1查看 161关注 0票数 1

我希望图像在右行驶时向右移动,在左行驶时向左移动。我不知道该怎么做.这是一个任务和翻转图片是不必要的,但我仍然想学习如何做它。

代码语言:javascript
运行
复制
"""
    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()

我做了一些研究,人们推荐使用pygame.transform.flip(),我不知道如何使用。顺便说一下,我认为您不需要我导入的任何文件,但是如果您需要的话,我很乐意提供资源。任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-21 19:59:30

请参阅pygame.transform.flip()

flip(Surface, xbool, ybool) -> Surface

这可以垂直、水平或同时翻转曲面。

pygame.transform.flip()翻转从源曲面创建一个新的曲面,这个曲面是翻转的。例如:

代码语言:javascript
运行
复制
my_flipped_image = pygame.transform.flip(my_image, True, False); 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65834797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档