h5或flash中,可以直接对矢量对象,比如line, rectange旋转,但是pygame中,仅支持对image旋转,本以为这个是很简单的事情,但是发现还是有很多小猫腻的,记录一下:
先看一个错误的版本:
1 import pygame 2 import sys 3 4 pygame.init() 5 6 SIZE = WIDTH, HEIGHT = 200, 400 7 BLACK = 0, 0, 0 8 angle = 1 9 10 screen = pygame.display.set_mode(SIZE) 11 leaf = pygame.image.load("leaf.png") 12 leafRect = leaf.get_rect() 13 # 定位到舞台中心 14 leafRect = leafRect.move((WIDTH - leafRect.width) / 2, (HEIGHT - leafRect.height) / 2) 15 16 clock = pygame.time.Clock() 17 18 while True: 19 20 for event in pygame.event.get(): 21 if event.type == pygame.QUIT: 22 sys.exit() 23 24 # 旋转图片 25 leaf = pygame.transform.rotate(leaf, angle) 26 angle += 1 27 28 # 默认背景为白色,所以每渲染一帧,要对背景重新填充,否则会有上一帧的残影 29 screen.fill(BLACK) 30 # 将旋转后的图象,渲染矩形里 31 screen.blit(leaf, leafRect) 32 # 正式渲染 33 pygame.display.update() 34 # 控制帧数<=100 35 clock.tick(100)