python-3.x Pygame动画移动精灵的问题

uurity8g  于 2023-02-26  发布在  Python
关注(0)|答案(1)|浏览(189)

我正在尝试制作一个游戏,当我想制作一个移动的精灵时,它后面有痕迹。当我删除它时,精灵没有动画。当我尝试修复它时,它停止移动。
据我所知,这个问题是与组精灵添加后没有被删除有关的。但是如果我在游戏循环中使用移除或评价组来删除组,它会阻止对象被动画化
下面是代码:(想要一个解释,我做错了什么太)

import pygame, sys, random
pygame.init()
screen = pygame.display.set_mode((1280, 720))
pygame.display.set_caption("Game")
clock = pygame.time.Clock()
x = 100
y = 100
state_change_timer = 0
state = 0
go_back_part1 = False
go_back_part2 = False

# assets
bg = (20, 20, 20)
color_1 = (50, 10, 50)
color_2 = (70, 10, 70)
color_3 = (90, 10, 90)
color_4 = (110, 110, 10)
block_1 = pygame.Surface((100, 100))
block_2 = pygame.Surface((100, 100))
block_3 = pygame.Surface((100, 100))
block_4 = pygame.Surface((100, 100))
block_1.fill(color_1)
block_2.fill(color_2)
block_3.fill(color_3)
block_4.fill(color_4)
BG = pygame.Surface((1280, 720))
BG.fill((20, 20, 20))
# classes
class player(pygame.sprite.Sprite):
    state_change_timer = 0
    state = 0
    go_back_part1 = False
    go_back_part2 = False
    def __init__(self, pos_x, pos_y):
        super().__init__()
        self.animate = False
        self.sprites = [block_1, block_2, block_3, block_4]
        self.current_sprite = 0
        self.image = self.sprites[self.current_sprite]
        pygame.transform.smoothscale(self.image, (128,128))

        self.rect = self.image.get_rect()
        self.rect.topleft = [pos_x, pos_y]
    def is_animating(self):
        self.animate = True
    def update(self):
        player.state_change_timer += 1
        if player.state_change_timer == 5:
            if player.state == 3:
                player.state -= 1
                player.go_back_part1 = True
            if player.go_back_part1 == True:
                player.state -= 1
                player.go_back_part1 = False
                player.go_back_part2 = True
            if player.go_back_part2 == True:
                player.state -= 1
                player.go_back_part2 = False
            else:
                player.state += 1
            player.state_change_timer = 0
        self.current_sprite = player.state
        self.image = self.sprites[self.current_sprite]

# sprite groups
moving_sprites = pygame.sprite.Group()
Player = player(x, y)
moving_sprites.add(Player)

# game loop

while True:
    keys = pygame.key.get_pressed()
    Player = player(x, y)
    moving_sprites.add(Player)
    moving_sprites.remove(Player)
    pre_x = x
    pre_y = y
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if keys[pygame.K_w]:
            y -= 1
            Player.is_animating()
        if keys[pygame.K_s]:
            y += 1
            Player.is_animating()
        if keys[pygame.K_a]:
            x -= 1
            Player.is_animating()
        if keys[pygame.K_d]:
            x += 1
            Player.is_animating()
    # drawing
    screen.blit(BG, (0, 0))
    moving_sprites.draw(screen)
    moving_sprites.update()
    pygame.display.flip()
    clock.tick(60)
bis0qfac

bis0qfac1#

你必须在应用程序循环之前创建sprite并将其添加到组中,而不是在循环中不断地重新创建sprite。此外,你还需要修改播放器属性(Player.rect),而不是xy

Player = player(x, y)
moving_sprites.add(Player)
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    move_x = (keys[pygame.K_d] - keys[pygame.K_a])
    move_y = (keys[pygame.K_s] - keys[pygame.K_w])
    Player.rect.x += move_x 
    Player.rect.y += move_y 
    Player.animate = move_x != 0 or move_y != 0
    moving_sprites.update()
    
    screen.blit(BG, (0, 0))
    moving_sprites.draw(screen)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
sys.exit()

相关问题