用pygame画一朵花
时间: 2024-05-26 19:08:05 浏览: 71
Python~画花的练习.
使用 Pygame 可以轻松地绘制一朵花,下面是实现方法:
1. 导入 pygame 模块
```python
import pygame
```
2. 初始化 Pygame
```python
pygame.init()
```
3. 设置画布大小和标题
```python
screen = pygame.display.set_mode((400, 400))
pygame.display.set_caption('Flower')
```
4. 定义花瓣的形状和颜色
```python
petal_shape = pygame.Surface((40, 20))
pygame.draw.circle(petal_shape, (255, 0, 255), (20, 10), 10)
pygame.draw.circle(petal_shape, (255, 0, 255), (10, 10), 10)
pygame.draw.circle(petal_shape, (255, 0, 255), (30, 10), 10)
```
5. 绘制花朵
```python
for i in range(8):
petal = pygame.transform.rotate(petal_shape, i * 45)
screen.blit(petal, (160 - petal.get_width() // 2, 160 - petal.get_height() // 2))
pygame.draw.circle(screen, (255, 255, 0), (200, 200), 20)
```
6. 更新屏幕
```python
pygame.display.flip()
```
7. 循环监听事件
```python
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
```
阅读全文