编写程序,程序开始运行时在界面中显示一个白色的圆有黑色边框,当在圆中按下鼠标键时颜色变为蓝色,释放鼠标时颜色为红色。
时间: 2024-03-06 16:52:14 浏览: 69
好的,这是一个基于 Python3 的 Pygame 库的程序,可以在 Pygame 环境下运行。以下是代码:
```python
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 400))
pygame.display.set_caption("Colorful Circle")
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
circle_color = WHITE
circle_radius = 50
circle_position = (200, 200)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if pygame.mouse.get_pos()[0] >= circle_position[0] - circle_radius and \
pygame.mouse.get_pos()[0] <= circle_position[0] + circle_radius and \
pygame.mouse.get_pos()[1] >= circle_position[1] - circle_radius and \
pygame.mouse.get_pos()[1] <= circle_position[1] + circle_radius:
circle_color = BLUE
elif event.type == pygame.MOUSEBUTTONUP:
if pygame.mouse.get_pos()[0] >= circle_position[0] - circle_radius and \
pygame.mouse.get_pos()[0] <= circle_position[0] + circle_radius and \
pygame.mouse.get_pos()[1] >= circle_position[1] - circle_radius and \
pygame.mouse.get_pos()[1] <= circle_position[1] + circle_radius:
circle_color = RED
screen.fill(WHITE)
pygame.draw.circle(screen, BLACK, circle_position, circle_radius, 2)
pygame.draw.circle(screen, circle_color, circle_position, circle_radius - 2)
pygame.display.update()
```
程序运行后会在 Pygame 窗口中显示一个白色的圆,当你按下鼠标键在圆内时,圆的颜色会变为蓝色,松开鼠标键时颜色会变为红色。
阅读全文