【问题标题】:Rotate image using pygame [duplicate]使用pygame旋转图像[重复]
【发布时间】:2013-10-19 10:51:19
【问题描述】:

我是 pygame 的新手,想编写一些代码,每 10 秒将图像旋转 90 度。我的代码如下所示:

    import pygame
    import time
    from pygame.locals import *
    pygame.init()
    display_surf = pygame.display.set_mode((1200, 1200))
    image_surf = pygame.image.load("/home/tempuser/Pictures/desktop.png").convert()
    imagerect = image_surf.get_rect() 
    display_surf.blit(image_surf,(640, 480))
    pygame.display.flip()
    start = time.time()
    new = time.time()
    while True:
        end = time.time()
        if end - start > 30:
            break
        elif end - new  > 10:
            print "rotating"
            new = time.time()
            pygame.transform.rotate(image_surf,90)
            pygame.display.flip()

此代码不起作用,即图像未旋转,尽管终端每 10 秒打印一次“旋转”。谁能告诉我我做错了什么?

【问题讨论】:

    标签: python python-2.7 pygame


    【解决方案1】:

    pygame.transform.rotate 不会将Surface 旋转到位,而是返回一个新的、旋转的Surface。即使它会改变现有的Surface,您也必须再次在显示表面上对其进行 blit。

    您应该做的是跟踪变量中的角度,每 10 秒将其增加 90,然后将新的 Surface blit 到屏幕上,例如

    angle = 0
    ...
    while True:
        ...
        elif end - new  > 10:
            ...
            # increase angle
            angle += 90
            # ensure angle does not increase indefinitely
            angle %= 360 
            # create a new, rotated Surface
            surf = pygame.transform.rotate(image_surf, angle)
            # and blit it to the screen
            display_surf.blit(surf, (640, 480))
            ...
    

    【讨论】:

      猜你喜欢
      • 2016-07-31
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      • 2022-08-07
      • 2011-05-10
      相关资源
      最近更新 更多