如何在pygame中移动精灵?

时间:2017-12-09 16:51:09

标签: python animation pygame sprite

我知道这个问题已经以其他形式得到了解答,但我似乎无法让它适合我的游戏。我试图让精灵移动并将他留在他搬到的地方。这是我的代码的一部分:

class player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)

    def draw(self, x, y):

        player = pygame.image.load("stand_down.png").convert_alpha()
        player = pygame.transform.scale(player, (100, 200))
        screen.blit(player, (x, y))
        pygame.display.update()
        for event in pygame.event.get():
            key = pygame.key.get_pressed()
            if event.type == KEYDOWN :
                if key [K_LEFT] :
                    player = pygame.image.load("standleft.png").convert_alpha()
                    player = pygame.transform.scale(player, (100, 200))
                    x -= 50
                    screen.blit(player, (x, y))
                    pygame.display.update()
                    pygame.sprite.Sprite.update(player)
                if key [K_RIGHT]:
                    player = pygame.image.load("standright.png").convert_alpha()
                    player = pygame.transform.scale(player, (100, 200))
                    x += 50
                    screen.blit(player, (x, y))
                    pygame.display.update()
                    pygame.sprite.Sprite.update(player)

    def update(self):
        pygame.sprite.Sprite.update(player)

然后这是我主要功能的一部分:

player.draw(screen, x, y)
player.update(screen)

我已经尝试了所有我能想到的东西,但是玩家只是在不同的点闪烁,然后回到之前的位置。如果你能提供帮助,我们将不胜感激!

1 个答案:

答案 0 :(得分:0)

我无法测试,但我会这样做

# --- classes ---

class Player(pygame.sprite.Sprite): # UpperCaseName for class

    def __init__(self, x, y):
        super().__init__()

        temp_image = pygame.image.load("stand_down.png").convert_alpha()
        self.image_down = pygame.transform.scale(temp_image, (100, 200))

        temp_image = pygame.image.load("standleft.png").convert_alpha()
        self.image_left = pygame.transform.scale(temp_image, (100, 200))

        temp_image = pygame.image.load("standright.png").convert_alpha()
        self.image_right = pygame.transform.scale(temp_image, (100, 200))

        self.image = self.image_down

        # keep position and size in pygame.Rect()
        # to use it in collision checking
        self.rect = self.image.get_rect(x=x, y=y)

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

    def handle_event(self):#, event)
        self.image = self.image_down

        key = pygame.key.get_pressed()
        if key[K_LEFT]:
            self.rect.x -= 50
            self.image = self.image_left
        if key[K_RIGHT]:
            self.rect.x += 50
            self.image = self.image_right

    def update(self, enemy):
        # check collision with eneny
        if self.rect.colliderect(enemy.rect):
            print("I'm killed by enemy")

# --- main ---

player = Player(400, 400)
enemy = Enemy(100, 100)

while True:

    # - events -

    for event in pygame.event.get():
        #player.handle_event(event)

    player.handle_event()

    # - updates -

    enemy.update()
    player.update(enemy)

    # - draws -

    screen.fill( (0,0,0) )                  
    player.draw(screen)
    enemy.draw(screen)
    pygame.display.update()