Pygame动画对象未被绘制

时间:2017-05-03 22:47:22

标签: python animation pygame

我正在使用pygame为类编写一个简单的python游戏。我编码了很短的时间,但直到这个项目才使用图形。我已经创造了一个'火球'从玩家移动到点击的点。 我正在为一个战士工作,他有一把剑和他一起旅行。我也有我想要的斜线运动。你按空格键让剑改变角度,然后它回到正常位置。我遇到的问题是,一旦图像/对象(剑)在向下位置绘制,它将保持在该位置。虽然原始图像上升到原始位置。我希望有人能指出正确的方向。这是我的第一个图形游戏。我知道这通常与主要绘制对象的行有关,但是,我已经搞砸了相当多的没有运气。此外,如果我这样做错误,不要犹豫,不要说什么。 感谢任何帮助,谢谢。

class Player(pygame.sprite.Sprite):

def __init__(self, name):
    pygame.sprite.Sprite.__init__(self)
    self.name = name
    self.level = 1
    self.xp = 0
    self.health = 100
    self.power = 100
    self.disc = 0
    self.currentAtk = 1


def draw(self, screen):
    screen.blit(self.image, (self.rect.x, self.rect.y))

def update(self):
    key = pygame.key.get_pressed()

    if key[K_SPACE]:
        if self.disc == 1 and self.currentAtk == 1:
            self.warsword.stab()

class Sword(pygame.sprite.Sprite):

image = pygame.image.load('images\\sword.png')

def __init__(self, x1, y1, angle = -25, stabtime = 5):
    pygame.sprite.Sprite.__init__(self)
    self.angle = angle
    self.image = pygame.transform.rotate(Sword.image, self.angle)
    self.rect = self.image.get_rect()
    self.rect.x, self.rect.y = x1, y1
    self.stabtime = stabtime
    self.repeat = 2

def update(self):
    key = pygame.key.get_pressed()

    if key[K_a] and self.rect.x >= 72:
        self.rect.x -= 1
    if key[K_d] and self.rect.x <= 572:
        self.rect.x += 1
    if key[K_w] and self.rect.y >= 154:
        self.rect.y -= 1
    if key[K_s] and self.rect.y <= 309:
        self.rect.y += 1

def draw(self, screen, x, y):
    screen.blit(self.image, (x, y))

def stab(self):
    if self.angle != -90 and self.stabtime == 5:
        self.angle = -90
        self.rect.y += 15
    if self.stabtime >= -5:
        self.image = pygame.transform.rotate(self.image, self.angle)
    else:
        pygame.sprite.Sprite.update(self)
    self.stabtime -= 1

类战士(玩家):

image = pygame.image.load('images\\warrior.png')
SwordX = 0
SwordY = 0
def __init__(self, name, x, y):
    super(Warrior, self).__init__(name)
    self.disc = 1
    self.image = Warrior.image
    self.rect = self.image.get_rect()
    self.rect.x = x
    self.rect.y = y
    Warrior.SwordX = self.rect.x + 12
    Warrior.SwordY = self.rect.y + 10
    self.warsword = Sword(Warrior.SwordX, Warrior.SwordY)
    atks.add(self.warsword)

def main():

chars = pygame.sprite.Group(())
screen = pygame.display.set_mode((640, 480))
a = Warrior('Fago', 250, 200)
chars.add(a)
screen = pygame.display.set_mode((640, 480))
while True:

    chars.update()
    atks.update()
    screen.blit(pygame.image.load('images\\arena.png'), (0,0))
    atks.draw(screen)

    chars.draw(screen)

    pygame.display.update()
    CLOCK.tick(75)
    pygame.event.pump()

main()的

1 个答案:

答案 0 :(得分:0)

这是一种方法。首先在Sword类中存储原始图像的副本,并为其指定stabbing属性。致电stab()会轮播图片并设置self.stabbing = True。现在使用update方法查看它是否仍在刺伤,递减self.stabtime,如果它<= 0重置图像和角度。

SWORD_IMAGE = pygame.image.load('images\\sword.png').convert()

class Sword(pygame.sprite.Sprite):

    def __init__(self, x1, y1, angle=-25, stabtime=30):
        super().__init__()
        self.angle = angle
        self.image = pygame.transform.rotate(SWORD_IMAGE, self.angle)
        self.orig_image = self.image
        self.rect = self.image.get_rect(topleft=(x1, y1))
        self.stabtime = stabtime
        self.stabbing = False

    def update(self):
        if self.stabbing:
            if self.stabtime <= 0:
                self.reset_stab()
            self.stabtime -= 1

    def stab(self):
        self.angle = -65
        self.image = pygame.transform.rotate(self.orig_image, self.angle)
        self.stabbing = True

    def reset_stab(self):
        self.stabtime = 30
        self.image = self.orig_image
        self.angle = -25
        self.stabbing = False

附注:

不要在while循环内的硬盘中反复加载图像,只需将它们全局加载一次并在程序中重复使用它们。此外,使用convert()convert_alpha()方法转换图像以提高性能。

不要多次调用pygame.key.get_pressed(),在while循环中调用它,然后将key元组传递给需要它的对象。