如何在pygame中水平翻转图像? [英] How do I flip an image horizontally in pygame?
问题描述
这是在pygame中.我如何翻转图像(让我们说一个 猪向右看) 我按向左箭头键,然后像这样停留 即使我不按任何键或按向上和向下箭头键.然后,当我按向右箭头键时,即使不按任何键或按向上和向下箭头键,也要再次将其切换回右侧,以保持原来的状态.
This is in pygame. How do I flip an image (lets say an image of a pig looking to the right) to look to the left when I press the left arrow key, and stay like that even if I don't press any keys or if I press the up and down arrow keys. Then how do I switch it back again to look to the right when I press the right arrow key and make it stay like that even if I don't press any key or if I press the up and down arrow keys.
我知道我必须使用 pygame.transform.flip().但是,我不知道如何将其放入代码中.
I know I have to use pygame.transform.flip(). But, I don't know how to put it in my code.
这是主要游戏:
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类:(这是缩进的.我只是不知道如何在此处正确复制和粘贴我的代码)
The pig class: (This is indented. I just don't know how to properly copy and paste my code here)
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)
推荐答案
添加猪的方向变量.将其设置为按下键,请勿将其更改为重新按下键.使运动依赖于moving_direction变量,显示的sprite依赖于direction变量.
Add a pig orientation variable. Set it on key down, don't change it back on key up. Have movement rely on the moving_direction variable and the sprite displayed rely on the orientation variable.
像这样更改blitme:
Change blitme like so:
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)
然后,您可以像这样设置按键逻辑:
Then you can have your key press logic like so:
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
通过这种方式,您可以将显示和移动逻辑分开.
In this way you can separate display and movement logic.
这篇关于如何在pygame中水平翻转图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!