1

我在 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()
4

2 回答 2

2

要翻转,你会这样做:

# input
if event.type == KEYDOWN:
    if event.key == K_RIGHT:
        flip_x = True
    elif event.key == K_LEFT:
        flip_x = False
    elif event.key == K_UP:
        flip_y = True
    elif event.key == K_DOWN:
        flip_y = False

# then to flip
new_image = pygame.transform.flip(original_image, flip_x, flip_y)
于 2013-02-14T00:16:24.857 回答
1

你的Player课可读性不是很好。因为,你的名字不容易理解。
在我的代码版本中,我只更改了名称,并且添加了对值的检查pos并在需要时应用翻转。因此,您可能需要更改检查(如果条件),以获得所需的结果。

是的,您没有pygame.Sprite,所以在这种情况下, sprite 这个词具有误导性。

我的你的Player班级版本:

class Player:
    def __init__(self):      
        self.x = 200  
        self.y = 300   
        self.speed_init = 6
        self.images = [pygame.image.load(img) for img in glob.glob("walk\Pinks_w*.png")]
        self.index = 0
        self.max_index = len(self.images)-1
        self.speed = 0
        self.img = self.images[self.index]

    def update(self, pos):
        if pos != 0:
            self.speed -= 1
            self.x += pos
            if not self.speed:
                self.speed = self.speed_init
                if self.index < self.max_index:
                    self.index += 1
                else:
                    self.index = 0
            self.img = self.images[self.index]
        # change True to False if needed, or change the operator.
        if pos > 0:
            self.img = pygame.transform.flip(self.img,True,False)
        screen.blit(self.img,(self.x,self.y))

更新
功能 有一个小问题update。问题是,由于速度始终是恒定的,而不是0,所以if not self.speed:不起作用。因此,将update函数更改为:

def update(self, pos):
    if pos != 0:
        self.speed -= 1
        self.x += pos
        # no more self.speed checks
        if self.index < self.max_index:
            self.index += 1
        else:
            self.index = 0
        self.img = self.images[self.index]
    # change True to False if needed, or change the operator.
    if pos < 0:
        self.img = pygame.transform.flip(self.img,True,False)
    screen.blit(self.img,(self.x,self.y))

更新 2
您的代码中似乎有某种错字,
这是(我的版本)代码,整个事情。

import pygame, sys, glob 
from pygame import *

class Player:
    def __init__(self):      
        self.x = 200  
        self.y = 300   
        self.speed_init = 6
        self.images = [pygame.image.load(img) for img in glob.glob("walk\Pinks_w*.png")]
        self.index = 0
        self.max_index = len(self.images)-1
        self.speed = 0
        self.img = self.images[self.index]
        print self.max_index

    def update(self, pos):
        if pos != 0:
            self.speed -= 1
            self.x += pos
            print self.index, self.max_index
            if self.index < self.max_index:
                self.index += 1
            else:
                self.index = 0
            self.img = self.images[self.index]
        # change True to False if needed, or change the operator.
        if pos < 0:
            self.img = pygame.transform.flip(self.img,True,False)
        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(20)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == KEYDOWN and event.key == K_RIGHT:
            pos = 2
        elif event.type == KEYUP and event.key == K_RIGHT:
            pos = 0
        elif event.type == KEYDOWN and event.key == K_LEFT:
            pos = -2
        elif event.type == KEYUP and event.key == K_LEFT:
            pos = 0

    player1.update(pos)

    pygame.display.update()
于 2013-02-14T11:54:02.770 回答