微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我如何将这个动画移动到玩家的位置?

如何解决我如何将这个动画移动到玩家的位置?

我使用教程制作了此动画,并将其设置为面向右侧的位置以激活动画,但是我尝试将动画移动到玩家位置的所有尝试都失败了。动画需要使用空格键激活,并发生在 onpress[pygame.K_Space]: 函数内。我不太关心代码在哪里。

# --- Imports ---
import pygame,os,random
# --- Screen Dimensions ---
SCR_WIDTH = 1020
SCR_HEIGHT = 510
# --- Colors ---
WHITE = [240,240,240]
# --- Game Constants ---
FPS = 60
# --- Fonts ---
pygame.font.init()
TNR_FONT = pygame.font.SysFont('Times_New_Roman',27)
TNR_LARGE_FONT = pygame.font.SysFont('Times_New_Roman',65)
# --- Dictionaries ---
images = {}
# --- Classes ---
class MySprite(pygame.sprite.Sprite):
    def __init__(self):
        super(MySprite,self).__init__()
        self.images = []
        self.images.append(pygame.image.load('Desktop/Files/Dungeon Minigame/rka1.png'))
        self.images.append(pygame.image.load('Desktop/Files/Dungeon Minigame/rka2.png'))
        self.images.append(pygame.image.load('Desktop/Files/Dungeon Minigame/rka3.png'))
        self.images.append(pygame.image.load('Desktop/Files/Dungeon Minigame/rka4.png'))
        self.images.append(pygame.image.load('Desktop/Files/Dungeon Minigame/rka5.png'))
        self.images.append(pygame.image.load('Desktop/Files/Dungeon Minigame/rka6.png'))
        self.images.append(pygame.image.load('Desktop/Files/Dungeon Minigame/rka7.png'))
        self.images.append(pygame.image.load('Desktop/Files/Dungeon Minigame/rka8.png'))
        self.index = 0
        self.image = self.images[self.index]
        self.rect = pygame.Rect(5,5,150,198)
    def update(self):
        self.index += 1
        if self.index >= len(self.images):
            self.index = 0
        self.image = self.images[self.index]
# --- Functions
def clip(value,lower,upper):
    return min(upper,max(value,lower))
def load_images():
    path = 'Desktop/Files/Dungeon Minigame/'
    filenames = [f for f in os.listdir(path) if f.endswith('.png')]
    for name in filenames:
        imagename = os.path.splitext(name)[0]
        images[imagename] = pygame.image.load(os.path.join(path,name))
def game():
    my_sprite = MySprite()
    my_group = pygame.sprite.Group(my_sprite)
    score = 0
    lives = 3
    playerX = 80
    playerY = 100
    direction = 'left'
            def move(self,speed = 1):
                if self.x > playerX:
                    self.x -= speed
                elif self.x < playerX:
                    self.x += speed

                if self.y < playerY:
                    self.y += speed
                elif self.y > playerY:
                    self.y -= speed

            def draw(self):
                screen.blit(images['r_zombie'],(self.x,self.y))
    while True:
        screen.blit(images['background'],(0,0))
        score_text = TNR_FONT.render('score: ' + str(score),True,WHITE)
        lives_text = TNR_FONT.render('Lives: ',WHITE)
        screen.blit(score_text,(20,20))
        screen.blit(lives_text,(840,20))
        heart_images = ["triple_empty_heart","single_heart","double_heart","triple_heart"]
        lives = clip(lives,3)
        screen.blit(images[heart_images[lives]],(920,0))
        onpress = pygame.key.get_pressed()

        dx = 0
        if onpress[pygame.K_a]:
            dx -= 3
            direction = 'left'
        if onpress[pygame.K_d]:
            dx += 3
            direction = 'right'
        playerX += dx

        dy = 0
        if onpress[pygame.K_w]:
            dy -= 3
        if onpress[pygame.K_s]:
            dy += 3
        playerY += dy

        if onpress[pygame.K_SPACE]:
            if direction == 'right':
                my_group.update()
                my_group.draw(screen)

        if dx > 0:
            screen.blit(images['r_knight'],(playerX,playerY))
        elif dx < 0:
            screen.blit(images['l_knight'],playerY))
        if direction == 'left':
            screen.blit(images['l_knight'],playerY))
        else:
            screen.blit(images['r_knight'],playerY))

        playerX = clip(playerX,8,949)
        playerY = clip(playerY,440)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()

        clock.tick(FPS)
        pygame.display.update()
# --- Main ---
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((SCR_WIDTH,SCR_HEIGHT))
pygame.display.set_caption('Dungeon Minigame')
load_images()
game()

解决方法

您必须设置 rect 对象的 MySprite 属性的位置。

my_sprite.rect.topleft = (playerX,playerY)

pygame.sprite.Group.draw()pygame.sprite.Group.update() 是由 pygame.sprite.Group 提供的方法。

前者将 委托给包含的 pygame.sprite.Spritesupdate 方法 - 您必须实现该方法。见pygame.sprite.Group.update()

对组中的所有 Sprite 调用 update() 方法 [...]

后者使用包含的 imagerectpygame.sprite.Sprite 属性来绘制对象 - 您必须确保 pygame.sprite.Sprite 具有所需的属性。见pygame.sprite.Group.draw()

将包含的精灵绘制到 Surface 参数。这对源表面使用 Sprite.image 属性和 Sprite.rect。 [...]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。