用python的pygame画一个爱心
时间: 2023-11-04 22:06:06 浏览: 68
一篇文章教你用python画动态爱心表白
5星 · 资源好评率100%
以下是用Python的pygame库画一个爱心的代码:
```python
import pygame
# 初始化pygame
pygame.init()
# 设置窗口大小和标题
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Draw Heart with Pygame")
# 定义颜色
red = (255, 0, 0)
# 画爱心
heart_center_x, heart_center_y = width // 2, height // 2
heart_size = 200
heart_thickness = 5
pygame.draw.circle(screen, red, (heart_center_x - heart_size // 4, heart_center_y - heart_size // 4), heart_size // 2, heart_thickness)
pygame.draw.circle(screen, red, (heart_center_x + heart_size // 4, heart_center_y - heart_size // 4), heart_size // 2, heart_thickness)
pygame.draw.polygon(screen, red, [(heart_center_x - heart_size // 2, heart_center_y), (heart_center_x + heart_size // 2, heart_center_y), (heart_center_x, heart_center_y + heart_size // 2)], heart_thickness)
# 刷新屏幕
pygame.display.flip()
# 等待退出
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
```
运行以上代码,就会在屏幕中央画出一个红色的爱心。可以根据需要调整爱心的大小和颜色等参数。
阅读全文