【问题标题】:How do I flip an image horizontally in pygame?如何在pygame中水平翻转图像?
【发布时间】:2018-01-17 23:01:04
【问题描述】:

这是在 pygame.我如何翻转图像(让我们说一个图像 猪向右看)向左看时 我按左箭头键,然后保持原样 即使我不按任何键或按向上和向下箭头键。那么当我按下右箭头键时如何将它再次切换回向右看,即使我不按任何键或按下向上和向下箭头键,它也保持不变。

我知道我必须使用 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()

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

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)

【问题讨论】:

  • 要正确格式化代码,您必须在提交窗口中选择它并按“Ctrl-K”(在每行代码之前添加四个额外的空格)。

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


【解决方案1】:

添加猪的方向变量。将其设置为按键向下,不要将其重新设置为按键向上。让运动依赖于moving_direction 变量,显示的精灵依赖于方向变量。

像这样改变 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

这样你就可以分离显示和移动逻辑。

【讨论】:

  • 你能详细说明一下吗?
  • 查看我的编辑。我认为它应该澄清(注意你可能需要稍微调整一下)。
【解决方案2】:

pygame.transform.flip 接受三个参数:

  1. 你想翻转的surface
  2. 一个布尔值,表示表面应该水平翻转
  3. 一个布尔值,表示表面应该垂直翻转

要仅垂直翻转表面,请将True 作为第三个参数:

flipped_surface = pygame.transform.flip(surface, False, True)

关于您的具体问题,您可以这样做:

PIG_RIGHT = pygame.image.load('pig.png').convert()
PIG_LEFT = pygame.transform.flip(PIG_RIGHT, True, False)

pig = Pig(screen, PIG_RIGHT)  # Assign this image to `self.image`.
clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                pig.moving_right = True
                pig.image = PIG_RIGHT
            elif event.key == pygame.K_LEFT:
                pig.moving_left = True
                pig.image = PIG_LEFT

    screen.fill(blue_sky)

    pig.blitme()
    pig.update()

    pygame.display.flip()
    clock.tick(30)

您还可以将两个表面存储在您的 Pig 类中,并根据 self.moving_right、self.moving_left 或 self.direction 属性的状态切换 self.image。

【讨论】:

  • 当我按下左箭头键时,它会向左翻转(所以猪正在向左看)但是当我释放它时,它会翻转回原始图像(所以猪现在正在看再次向右)。即使我释放钥匙,如何让它保持向左看?
  • 您可以这样做:首先加载原始图像并在全局范围内创建翻转版本。当玩家按下左键时,将pig.image设置为左图,当玩家按下右键时,将pig.image设置回右图。
  • 旁注:我建议添加pygame.time.Clock 来限制帧速率。
  • 另外,请注意,在游戏运行期间不断翻转或缩放表面比预先创建表面和切换self.image 属性的成本要高得多。
【解决方案3】:

尝试添加如下内容:

elif event.key == pygame.K_RIGHT:
    pig.image = pygame.transform.flip(pig.image, True, False)
    pygame.display.update()'''I dont think you need this'''
elif event.key == pygame.K_LEFT:
    pig.image = pygame.transform.flip(pig.image, True, False)
    pygame.display.update()'''Or this'''

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多