python - 如何在 pygame 中水平翻转图像?

标签 python python-2.7 python-3.x pygame pygame-surface

这是在 pygame.如何翻转图像(假设一个图像 pig 向右看)时向左看 我按向左箭头键,保持这样 即使我不按任何键或者按向上和向下箭头键。那么当我按下向右箭头键时,如何再次将其切换回向右看,并使其保持这样,即使我不按任何键或按下向上和向下箭头键。

我知道我必须使用pygame.transform.flip()。但是,我不知道如何将其放入我的代码中。

这是主要游戏:

import sys

import pygame

from pig import Pig

pygame.init()

screen_width = 800
screen_height = 600

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Flying Pig")

blue_sky = 135, 206, 250
brown = 139, 69, 19

pig = Pig(screen)

while True:

    # Accept events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        # Keydown events
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                pig.moving_right = True
            elif event.key == pygame.K_LEFT:
                pig.moving_left = True
            elif event.key == pygame.K_UP:
                pig.moving_up = True
            elif event.key == pygame.K_DOWN:
                pig.moving_down = True

        # Keyup events
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                pig.moving_right = False
            elif event.key == pygame.K_LEFT:
                pig.moving_left = False
            elif event.key == pygame.K_UP:
                pig.moving_up = False
            elif event.key == pygame.K_DOWN:
                pig.moving_down = False

    screen.fill(blue_sky)

    pig.blitme()
    pig.update()

    pygame.display.flip()

pig 类:(这是缩进的。我只是不知道如何在此处正确复制并粘贴我的代码)

import pygame 

class Pig():

    def __init__(self, screen):
        """Initialize the pig and set its starting position."""
        self.screen = screen

        # Load the pig image and set pig and screen to rect.
        self.image = pygame.image.load('pig.png')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        # Start the pig at the bottom center of the screen.
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        # Speed of the pig
        self.pig_speed = 1.5
        self.center = float(self.pig_speed)

        # Set a variable for each movement.
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False

        self.direction = ['right', 'left']

    def update(self):
        """Update the position of the pig."""

        if self.rect.right <= self.screen_rect.right:
            if self.moving_right:
                self.rect.centerx += self.pig_speed

        if self.rect.left > 0:
            if self.moving_left:
                self.rect.centerx -= self.pig_speed

        if self.rect.top > 0:
            if self.moving_up:
                self.rect.bottom -= self.pig_speed

        if self.rect.bottom <= self.screen_rect.bottom:
            if self.moving_down:
                self.rect.bottom += self.pig_speed

    def blitme(self):
        """Draw the pig at its current location."""
        self.screen.blit(self.image, self.rect)

最佳答案

添加 pig 的方向变量。将其设置为按下键,不要在按下键时将其更改回来。让移动依赖于 moving_direction 变量,显示的 Sprite 依赖于orientation 变量。

像这样改变blitme:

def blitme(self):
    if self.orientation == "Right":
        self.screen.blit(self.image, self.rect)
    elif self.orientation == "Left":
        self.screen.blit(pygame.transform.flip(self.image, False, True), self.rect)

然后你就可以像这样拥有按键逻辑:

elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                pig.moving_right = True
                pig.orientation = "Right"
            elif event.key == pygame.K_LEFT:
                pig.moving_left = True
                pig.orientation = "Left"
            elif event.key == pygame.K_UP:
                pig.moving_up = True
            elif event.key == pygame.K_DOWN:
                pig.moving_down = True

通过这种方式,您可以分离显示和移动逻辑。

关于python - 如何在 pygame 中水平翻转图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45601109/

相关文章:

python - 如何使服务器接受来自多个端口的连接?

python - 将文本文件拆分为带有特殊分隔符行的部分 - python

python - 如何使 absolute_import 成为所有模块的默认值

python - 来自其他 IP 而不是本地主机的请求在长时间等待时没有响应

python - 为图形图例创建一个双色矩形的 matplotlib mpatches

python - 随着主窗口大小的调整而调整 QDialog 的大小

python - 如何通过嵌套字典值迭代字典的字典

python - 使用正则表达式 python 查找电子邮件

python-2.7 - Pyspark 合并数据框中的 WrappedArrays

python - 从 python 中的数据框中删除多列