【问题标题】:Pygame: Move picture while key is being heldPygame:按住键时移动图片
【发布时间】:2017-04-11 17:35:15
【问题描述】:

我试图在按住键时移动图片,但它总是只移动一个像素。在与此类似的问题中,我认为这是正确的,但它对我不起作用。

import pygame, sys

x = 0
y = 0


class Ship:
    def __init__(self, image):
        self.image = pygame.image.load(image)

    def Play(self, screen, x, y):
        self.screen = screen
        self.screen.blit(self.image, [x, y])


if __name__ == "__main__":
    ship = Ship('Lod.png')
    while True:
        window = pygame.display.set_mode([1000, 1000])
        window.fill([0, 105, 148])

        Key = pygame.key.get_pressed()
        if Key[pygame.K_LEFT]:
            x -= 1
        if Key[pygame.K_RIGHT]:
            x += 1

        ship.Play(window, x, y)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        pygame.display.flip()

【问题讨论】:

  • pump 有帮助吗?
  • 值得怀疑。有电话到event.get
  • 您正在调整每一帧的窗口大小。这可能是一个问题,所以在循环之前移动pygame.display.set_mode。我相信每次调整窗口大小时都会重置/清除窗口上的图像,但这只是我尚未尝试过的推测。
  • 是的,while 循环中的pygame.display.set_mode 调用是问题所在。如果您将其移出循环,它将正常工作。

标签: python image python-2.7 pygame


【解决方案1】:

pygame.display.set_mode 移出循环有帮助。谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 2022-12-10
    • 1970-01-01
    • 2022-05-29
    • 1970-01-01
    相关资源
    最近更新 更多