Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pygame旋转图像_Python_Python 2.7_Pygame - Fatal编程技术网

Python 使用pygame旋转图像

Python 使用pygame旋转图像,python,python-2.7,pygame,Python,Python 2.7,Pygame,我是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/deskto

我是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秒打印一次“旋转”。有人能告诉我我做错了什么吗?

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

您应该做的是跟踪变量中的角度,每10秒将其增加
90
,然后将新的
曲面
显示在屏幕上,例如

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))
        ...