这里给出一个示例代码,演示如何在画布上绘制多个圆,并且保留之前画过的圆。
import pygame
pygame.init()
# 设置画布的尺寸
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
# 设置画布的背景颜色
background_color = (255, 255, 255)
screen.fill(background_color)
# 存储所有圆的坐标和半径
circles = []
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
# 鼠标点击事件,获取点击位置和半径
pos = pygame.mouse.get_pos()
radius = 20
# 将圆的坐标和半径添加到圆列表中
circles.append((pos, radius))
# 清空画布
screen.fill(background_color)
# 绘制所有的圆
for circle in circles:
pygame.draw.circle(screen, (255, 0, 0), circle[0], circle[1])
# 更新画布
pygame.display.flip()
pygame.quit()
在这个示例中,我们使用pygame
库来创建一个窗口,并且在窗口中绘制圆。circles
列表用于存储所有画过的圆的坐标和半径。当鼠标点击事件发生时,我们将新圆的坐标和半径添加到circles
列表中。在每次循环中,我们先清空画布,然后绘制所有的圆,并且更新窗口显示。这样就可以保留之前画过的圆。
上一篇:不要移除特定的DataFrame
下一篇:不要以错误的数量生成精灵