简体   繁体   English

在 Pygame (python) 中翻转图像的问题

[英]Problem with flipping images in Pygame (python)

I am trying to create a game in which you can move a character with arrow keys.我正在尝试创建一个游戏,您可以在其中使用箭头键移动角色。 When moving left or right, I want the character (an image) to flip and point left/right accordingly.向左或向右移动时,我希望角色(图像)翻转并相应地指向左/右。 The original image/character is pointing left.原始图像/字符指向左侧。 But I cannot get the character to flip, please help me, thanks.但是我无法让角色翻转,请帮助我,谢谢。

import pygame
pygame.init()#initiate pygame
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
display_width = 1200
display_height = 800
display = pygame.display.set_mode((display_width,display_height))
characterimg = pygame.image.load(r'/Users/ye57324/Desktop/Make/coding/python/characterimg.png')
def soldier(x,y):
    display.blit(characterimg, (x,y))
x = (display_width * 0.45)
y = (display_height * 0.1)
pygame.display.set_caption('Game')
clock = pygame.time.Clock()#game clock
flip_right = False
x_change = 0
y_change = 0
start = True


while start:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            start = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_change += -5
                if flip_right == True:
                    pygame.transform.flip(characterimg, True, False)
                    flip_right = False
            elif event.key == pygame.K_RIGHT:
                x_change += 5
                if flip_right == False:
                    pygame.transform.flip(characterimg, True, False)
                    flip_right = True
            elif event.key == pygame.K_UP:
                y_change += -5
            elif event.key == pygame.K_DOWN:
                y_change += 5
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                x_change += 5
            elif event.key == pygame.K_RIGHT:
                x_change += -5
            elif event.key == pygame.K_UP:
                y_change += 5
            elif event.key == pygame.K_DOWN:
                y_change += -5

    x += x_change
    y += y_change
    display.fill(white)
    soldier(x,y)
    pygame.display.update()
    clock.tick(60)#fps

Pygame's doc-page about pygame.transform.flip states Pygame 的关于pygame.transform.flip状态的文档页面

Flipping a Surface is non-destructive and returns a new Surface with the same dimensions.翻转 Surface 是非破坏性的,并返回具有相同尺寸的新 Surface。

This means that the surface you pass to that method remains unaffected.这意味着您传递给该方法的表面不受影响。 You have to keep the return -value around in order to see any effect.您必须保留返回值才能看到任何效果。

In practice, this means that you should replace实际上,这意味着您应该替换

pygame.transform.flip(characterimg, True, False)

with

characterimg = pygame.transform.flip(characterimg, True, False)

So that characterimg point to the new, flipped version of the image.因此characterimg指向图像的新翻转版本。

Note: Performance wise, this is not a very good method of doing things.注意:在性能方面,这不是一个很好的做事方法。 Each time you call pygame.transform.flip , pygame has to allocated new memory for the new surface, go over each pixel of the original surface and copy it over to the new surface, transforming its position in the process.每次调用pygame.transform.flip ,pygame 都必须为新表面分配新内存,遍历原始表面的每个像素并将其复制到新表面,在此过程中变换其位置。 You'd better flip the image once at the beginning of your program, so that you have a variable characterimg_left and characterimg_right and then just assign those to your characterimg variable.你最好在程序开始时翻转图像一次,这样你就有了一个变量characterimg_leftcharacterimg_right然后将它们分配给你的characterimg变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM