Pygame角色运动

发布于 2025-01-24 18:18:06 字数 1353 浏览 1 评论 0原文

我正在使用Pygame练习,不知道如何使我的角色移动。如果我提出“打印”语句,它可以正常运行,并在按下“ a”时打印任何我想要的东西,但是角色留在他的位置。我对上课一无所知,所以我认为这是问题

import pygame

pygame.init()
pygame.font.init() 
width, height = 924, 500
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Priest Beast')
clock = pygame.time.Clock()

BG = pygame.transform.scale2x(pygame.image.load('art/background.png')).convert_alpha()

music = pygame.mixer.Sound('sound/music.mp3')

class Player(pygame.sprite.Sprite):

    def __init__(self, x, y):
        super().__init__()
        self.image = pygame.transform.scale2x(pygame.image.load('art/Player.png')).convert_alpha()
        self.rect = self.image.get_rect(center = (800, 300))
        self.x = x 
        self.y = y

    def move(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a]:
            self.rect.x += 5

    def update(self):
        self.move()
            

#Loop and exit
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
   
    # Sounds
    music.play()
    music.set_volume(0.1)

    screen.blit(BG, (0, 0))

    player = pygame.sprite.GroupSingle()
    player.add(Player(800,200))

    #Update everything
    player.draw(screen)
    player.update()
    pygame.display.update()
    clock.tick(60)

I'm practicing with pygame and don't know how to make my character move. If I put 'print' statement, it works, and prints whatever I want when I press 'a' for example, but character stays on his place. I know little about classes, so I think that's the problem

import pygame

pygame.init()
pygame.font.init() 
width, height = 924, 500
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Priest Beast')
clock = pygame.time.Clock()

BG = pygame.transform.scale2x(pygame.image.load('art/background.png')).convert_alpha()

music = pygame.mixer.Sound('sound/music.mp3')

class Player(pygame.sprite.Sprite):

    def __init__(self, x, y):
        super().__init__()
        self.image = pygame.transform.scale2x(pygame.image.load('art/Player.png')).convert_alpha()
        self.rect = self.image.get_rect(center = (800, 300))
        self.x = x 
        self.y = y

    def move(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a]:
            self.rect.x += 5

    def update(self):
        self.move()
            

#Loop and exit
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
   
    # Sounds
    music.play()
    music.set_volume(0.1)

    screen.blit(BG, (0, 0))

    player = pygame.sprite.GroupSingle()
    player.add(Player(800,200))

    #Update everything
    player.draw(screen)
    player.update()
    pygame.display.update()
    clock.tick(60)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灵芸 2025-01-31 18:18:06

您会不断地以其初始位置重新创建对象。您需要在应用程序循环之前创建 sprite group

player = pygame.sprite.GroupSingle() # <-- INSERT
player.add(Player(800,200))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
   
    # [...]

    screen.blit(BG, (0, 0))

    # DELETE
    # player = pygame.sprite.GroupSingle()
    # player.add(Player(800,200))

    # [...]

You continuously recreate the object in its initial position. You need to create the Sprite and Group before the application loop:

player = pygame.sprite.GroupSingle() # <-- INSERT
player.add(Player(800,200))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
   
    # [...]

    screen.blit(BG, (0, 0))

    # DELETE
    # player = pygame.sprite.GroupSingle()
    # player.add(Player(800,200))

    # [...]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文