设计一个程序,首先确在屏幕的中央设置圆心,生成一个半径为200的圆,之后以相同的圆心生成一个半径为200的直角扇形。此时,在圆内随机生成一个黑点,设置一个翻转按钮,按下此按钮,将扇形的中心移动到黑点处,并将此扇形全部变为红色,并且在控制台输出黑点的坐标和与圆心的角度
时间: 2024-05-19 10:16:12 浏览: 14
import pygame
import math
import random
# 初始化pygame
pygame.init()
# 设置窗口大小和标题
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("圆与扇形")
# 设置圆心坐标和圆的半径
circle_center = (400, 300)
circle_radius = 200
# 设置扇形的角度和半径
sector_angle = 90
sector_radius = circle_radius
# 生成圆和扇形的surface
circle_surface = pygame.Surface((800, 600), pygame.SRCALPHA)
sector_surface = pygame.Surface((800, 600), pygame.SRCALPHA)
# 在circle_surface上绘制圆
pygame.draw.circle(circle_surface, (255, 255, 255), circle_center, circle_radius)
# 在sector_surface上绘制扇形
start_angle = math.radians(-45)
end_angle = math.radians(sector_angle - 45)
pygame.draw.arc(sector_surface, (255, 0, 0), (circle_center[0] - sector_radius, circle_center[1] - sector_radius, sector_radius * 2, sector_radius * 2), start_angle, end_angle, 0)
pygame.draw.line(sector_surface, (255, 0, 0), circle_center, (circle_center[0] + sector_radius * math.cos(start_angle), circle_center[1] + sector_radius * math.sin(start_angle)), 2)
pygame.draw.line(sector_surface, (255, 0, 0), circle_center, (circle_center[0] + sector_radius * math.cos(end_angle), circle_center[1] + sector_radius * math.sin(end_angle)), 2)
# 随机生成一个黑点
black_point = (random.randint(circle_center[0] - circle_radius, circle_center[0] + circle_radius), random.randint(circle_center[1] - circle_radius, circle_center[1] + circle_radius))
while math.sqrt((black_point[0] - circle_center[0]) ** 2 + (black_point[1] - circle_center[1]) ** 2) > circle_radius:
black_point = (random.randint(circle_center[0] - circle_radius, circle_center[0] + circle_radius), random.randint(circle_center[1] - circle_radius, circle_center[1] + circle_radius))
pygame.draw.circle(sector_surface, (0, 0, 0), black_point, 5)
# 设置按钮的位置和大小
button_rect = pygame.Rect(50, 50, 100, 50)
# 游戏循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
# 点击按钮
if button_rect.collidepoint(event.pos):
# 计算扇形中心移动的距离和角度
distance = math.sqrt((black_point[0] - circle_center[0]) ** 2 + (black_point[1] - circle_center[1]) ** 2)
angle = math.atan2(black_point[1] - circle_center[1], black_point[0] - circle_center[0])
# 移动扇形并变为红色
screen.blit(circle_surface, (0, 0))
pygame.draw.arc(sector_surface, (255, 0, 0), (black_point[0] - sector_radius, black_point[1] - sector_radius, sector_radius * 2, sector_radius * 2), angle - start_angle, angle - end_angle, 0)
pygame.draw.line(sector_surface, (255, 0, 0), black_point, (black_point[0] + sector_radius * math.cos(angle - start_angle), black_point[1] + sector_radius * math.sin(angle - start_angle)), 2)
pygame.draw.line(sector_surface, (255, 0, 0), black_point, (black_point[0] + sector_radius * math.cos(angle - end_angle), black_point[1] + sector_radius * math.sin(angle - end_angle)), 2)
# 输出黑点的坐标和角度
print("黑点的坐标为:", black_point)
print("黑点与圆心的角度为:", math.degrees(angle))
# 更新屏幕
pygame.display.update()
# 绘制圆和扇形
screen.blit(circle_surface, (0, 0))
screen.blit(sector_surface, (0, 0))
# 绘制按钮
pygame.draw.rect(screen, (0, 255, 0), button_rect)
font = pygame.font.Font(None, 36)
text = font.render("翻转", True, (0, 0, 0))
text_rect = text.get_rect(center=button_rect.center)
screen.blit(text, text_rect)
# 更新屏幕
pygame.display.update()
相关推荐
![application/octet-stream](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)