我在 pygame 中翻转精灵时遇到问题(我可以让它向右移动,但我希望 img 在左键上翻转),
我研究了如何翻转图像并找到了pygame.transform.flip
,但作为 pygame 的初学者,我'不知道如何使用它,教程对我来说没有意义。
任何人都可以帮助我使用下面的代码(我不确定我是否将 self.img1 放在正确的位置)?
import pygame, sys, glob
from pygame import *
class Player:
def __init__(self):
self.x = 200
self.y = 300
self.ani_speed_init = 6
self.ani_speed = self.ani_speed_init
self.ani = glob.glob("walk\Pinks_w*.png")
self.ani.sort()
self.ani_pos = 0
self.ani_max = len(self.ani)-1
self.img = pygame.image.load(self.ani[0])
self.img1 = pygame.transform.flip(self.ani[0], True, False)
self.update(0)
def update(self, pos):
if pos != 0:
self.ani_speed-=1
self.x+=pos
if self.ani_speed == 0:
self.img = pygame.image.load(self.ani[self.ani_pos])
self.ani_speed = self.ani_speed_init
if self.ani_pos == self.ani_max:
self.ani_pos = 0
else:
self.ani_pos+=1
screen.blit(self.img,(self.x,self.y))
h = 400
w = 800
screen = pygame.display.set_mode((w,h))
clock = pygame.time.Clock()
player1 = Player()
pos = 0
while True:
screen.fill((0,0,0))
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == KEYDOWN and event.key == K_RIGHT:
pos = 1
elif event.type == KEYUP and event.key == K_RIGHT:
pos = 0
elif event.type == KEYDOWN and event.key == K_LEFT:
pos = -1
elif event.type == KEYUP and event.key == K_LEFT:
pos = 0
player1.update(pos)
pygame.display.update()