【问题标题】:Pygame - Image rotation while keydownPygame - 按键时图像旋转
【发布时间】:2017-01-05 15:37:45
【问题描述】:

当按下左/右箭头键时,如何使图像/精灵旋转到左/右? ...就像一个轮子。

下面是运行游戏循环的代码:

def gameplay_loop():
    exitgame = False
    cellpos_x = 0
    cellpos_y = cancer_cell.get_rect().height*2
    while not exitgame:
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                exitgame = True
                quitgame()
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_LEFT]:
            cellpos_x -= 10
        if key_pressed[pygame.K_RIGHT]:
            cellpos_x += 10

        gameplay_bg = pygame.image.load("/Users/nedimkanat/Desktop/python/img/gameplay_bg.png").convert()
        main_screen.fill(white)
        main_screen.blit(gameplay_bg, [0,0])
        main_screen.blit(cancer_cell, [cellpos_x, cellpos_y])
        pygame.display.flip()
        clock.tick(20)

我试图在 key_pressed 条件下添加它,但什么也没发生,我按照这个家伙说的做了:https://stackoverflow.com/a/19316827/6256879

精灵被称为cancer_cell,正如我上面提到的,我希望它在按住左/右键的同时旋转到左/右。

【问题讨论】:

  • 你已经拥有了什么?本网站的目的不是为您编写整个程序。
  • 在 Pygame 中,您必须使用 pygame.transform.rotozoom 函数将您的图像作为 pygame 表面传递给它,但是您需要围绕该调用的整个程序才能使其工作。
  • 1. 了解如何处理 key presses 等事件。 2. 了解rotate Surfaces 的功能。 3. 在 Stack Overflow 上尝试、犯错误、重试、获得经验并找出答案或就您不了解的内容提出更具体的问题,向我们展示您的尝试,您的预期和出了什么问题。
  • 在这篇文章中查看我对您的回答的评论:stackoverflow.com/questions/39201171/pygame-smoother-movement/…。我认为这对你来说很重要,这取决于你是否希望精灵在按下键时保持旋转,或者每次按下键时只旋转一次给定的量。正如 Ted Klein 建议的那样,检查 Pygame 文档以进行旋转,并在您尝试几次后通过代码示例提出更具体的问题。
  • 更新问题..

标签: python-2.7 pygame


【解决方案1】:

如果您想在每次单击左箭头或右箭头时旋转 1 度,这样的方法可能会起作用。如果您希望图像连续旋转,您需要使用get_pressed() 作为感兴趣的关键点,以便在每一帧都应用角度。

def gameplay_loop():
    exitgame = False
    cellpos_x = 0
    cellpos_y = cancer_cell.get_rect().height*2
    while not exitgame:
        angle = 0 #reset the angle at each iteration
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                exitgame = True
                quitgame()
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_LEFT]:
            cellpos_x -= 10
            angle += 1.0
        if key_pressed[pygame.K_RIGHT]:
            cellpos_x += 10
            angle -= 1.0

        gameplay_bg = pygame.image.load("/Users/nedimkanat/Desktop/python/img/gameplay_bg.png").convert()
        main_screen.fill(white)
        main_screen.blit(gameplay_bg, [0,0])
        cancer_cell = pygame.transform.rotate(cancer_cell,angle)
        main_screen.blit(cancer_cell, [cellpos_x, cellpos_y])
        pygame.display.flip()
        clock.tick(20)

对于连续旋转,您可以这样做:

def gameplay_loop():
    exitgame = False
    cellpos_x = 0
    cellpos_y = cancer_cell.get_rect().height*2
    while not exitgame:
        angle = 0 #reset the angle at each iteration
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                exitgame = True
                quitgame()
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_LEFT]:
            cellpos_x -= 10

        if key_pressed[pygame.K_RIGHT]:
            cellpos_x += 10


        gameplay_bg = pygame.image.load("/Users/nedimkanat/Desktop/python/img/gameplay_bg.png").convert()
        main_screen.fill(white)
        main_screen.blit(gameplay_bg, [0,0])
        key_pressed = pygame.key.get_pressed()
        if key_pressed[pygame.K_LEFT]: #do the same for the other key
               angle += 1              #and put -= instead of +=

        cancer_cell = pygame.transform.rotate(cancer_cell,angle)
        main_screen.blit(cancer_cell, [cellpos_x, cellpos_y])
        pygame.display.flip()
        clock.tick(20)

【讨论】:

  • UnboundLocalError: local variable 'cancer_cell' referenced before assignment
  • 第 81 行:cellpos_y = cancer_cell.get_rect().height*2 第 99 行:cancer_cell = pygame.transform.rotate(cancer_cell,angle)
  • 这很正常,请查看您提供的问题中的代码,在分配之前也使用了cancer_cell。我假设您已经在代码中的其他地方定义了 cancer_cell 表面。要使此代码正常工作,cancer_cell 变量需要是 Pygame 表面。
  • 我有,哈哈。第 28 行:cancer_cell = pygame.image.load("/Users/wolf/Desktop/python/img/thornyball.png").convert_alpha()
  • 很抱歉,我在您的代码中看不到这一点。只需进一步定义它,这应该可以解决您的问题。
猜你喜欢
  • 2016-07-31
  • 1970-01-01
  • 2012-06-15
  • 1970-01-01
  • 2013-10-19
  • 1970-01-01
  • 2022-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多