pygame缩放图片
时间: 2023-12-23 09:26:37 浏览: 165
可以使用pygame中的transform模块来缩放图片。具体步骤如下:
1.导入pygame和sys模块
```python
import pygame
import sys
```
2.初始化pygame
```python
pygame.init()
```
3.设置窗口大小
```python
WIDTH, HEIGHT = 640, 480
```
4.创建窗口
```python
screen = pygame.display.set_mode((WIDTH, HEIGHT))
```
5.加载图片
```python
image = pygame.image.load('in.jpg')
```
6.缩放图片
```python
scale_img = pygame.transform.scale(image, (100, 100))
```
7.获取缩放后图片的位置和大小
```python
img_rect = scale_img.get_rect()
```
8.将缩放后图片的中心移动到窗口中心
```python
img_rect.center = (WIDTH//2, HEIGHT//2)
```
9.将缩放后的图片绘制到窗口中
```python
screen.blit(scale_img, img_rect)
```
10.刷新窗口
```python
pygame.display.flip()
```
完整代码如下:
```python
import pygame
import sys
pygame.init()
WIDTH, HEIGHT = 640, 480
screen = pygame.display.set_mode((WIDTH, HEIGHT))
image = pygame.image.load('in.jpg')
scale_img = pygame.transform.scale(image, (100, 100))
img_rect = scale_img.get_rect()
img_rect.center = (WIDTH//2, HEIGHT//2)
screen.blit(scale_img, img_rect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
```
阅读全文