簡體   English   中英

如何在pygame中水平翻轉圖像?

[英]How do I flip an image horizontally in pygame?

這是在pygame中。 當我按下左箭頭鍵時,如何翻轉圖像(讓我們看一下看向右邊的豬的圖像)向左看,即使我沒有按任何鍵或按下上下箭頭鍵。 然后,當我按下右箭頭鍵並使其保持這樣狀態時,即使我沒有按任何鍵或按下向上和向下箭頭鍵,如何再次將其切換回右側。

. 我知道我必須使用 但是,我不知道如何將它放在我的代碼中。

這是主要游戲:

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)

pygame.transform.flip有三個參數:

  1. 你要翻轉的表面
  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)

您還可以將兩個self.image存儲在Pig類中,並根據self.moving_rightself.moving_leftself.direction屬性的狀態self.moving_left self.direction

添加豬方向變量。 按鍵設置,按鍵時不要改回。 運動依賴於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

通過這種方式,您可以分離顯示和移動邏輯。

嘗試添加以下內容:

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'''

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM