```python
import pygame
import math
初始化pygame
pygame.init()
设置窗口的大小
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
加载图片
image = pygame.image.load('your_image.png')
设置图片的位置和大小
image_rect = image.get_rect()
image_rect.center = (width // 2, height // 2)
旋转图片
angle = 45 旋转角度
image_rect = image_rect.rotate(angle)
清除屏幕
screen.fill((255, 255, 255))
绘制旋转后的图片
screen.blit(image, image_rect)
更新屏幕显示
pygame.display.flip()
控制主循环
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
退出pygame
pygame.quit()
```
在这个示例中,我们首先导入了pygame库和math库。然后,我们初始化了pygame并设置了一个窗口。接着,我们加载了一张图片并设置了它的位置和大小。之后,我们使用`rotate()`方法将图片旋转了45度。最后,我们在屏幕上绘制了旋转后的图片,并控制主循环直到用户关闭窗口。
请注意,这个示例假设你已经安装了pygame库,并且有一张图片文件名为`your_image.png`。你需要根据你的实际情况修改这些信息。