用代码制作一个外围带有360°刻度线的圆,在圆心处有三个90°的扇形框,分别标识为G1、G2、G3。在圆上有1个随机的黑点,此系统拥有翻转按钮,若G1可以覆盖黑点,就将其移动到黑点处
时间: 2024-05-06 20:22:13 浏览: 110
以下是Python代码实现:
```python
import pygame
import random
import math
# initialize pygame
pygame.init()
# set window size
window_width = 600
window_height = 600
window = pygame.display.set_mode((window_width, window_height))
# set font
font = pygame.font.SysFont('Arial', 16)
# set colors
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
# set circle parameters
circle_radius = 200
circle_center = (window_width // 2, window_height // 2)
circle_thickness = 10
# set angle parameters
angle_step = 10
angle_offset = -90
# set sector parameters
sector_width = 50
sector_height = 50
# set point parameters
point_radius = 10
# initialize angle and sector positions
angle_positions = []
sector_positions = []
for angle in range(0, 360, angle_step):
angle_positions.append(angle + angle_offset)
sector_positions.append((angle + angle_step // 2 + angle_offset) % 360)
# initialize point position
point_position = (random.randint(0, window_width), random.randint(0, window_height))
# initialize flip state
flip_state = False
# define function to draw circle
def draw_circle():
pygame.draw.circle(window, black, circle_center, circle_radius, circle_thickness)
for angle in angle_positions:
x1 = circle_center[0] + (circle_radius - circle_thickness // 2) * math.cos(math.radians(angle))
y1 = circle_center[1] + (circle_radius - circle_thickness // 2) * math.sin(math.radians(angle))
x2 = circle_center[0] + (circle_radius + circle_thickness // 2) * math.cos(math.radians(angle))
y2 = circle_center[1] + (circle_radius + circle_thickness // 2) * math.sin(math.radians(angle))
pygame.draw.line(window, white, (x1, y1), (x2, y2), 2)
# define function to draw sectors
def draw_sectors():
for angle in sector_positions:
x = circle_center[0] + (circle_radius - sector_width // 2) * math.cos(math.radians(angle))
y = circle_center[1] + (circle_radius - sector_height // 2) * math.sin(math.radians(angle))
sector_rect = pygame.Rect(x, y, sector_width, sector_height)
pygame.draw.rect(window, white, sector_rect, 2)
sector_text = font.render('G' + str(sector_positions.index(angle) + 1), True, white)
sector_text_rect = sector_text.get_rect(center=(x + sector_width // 2, y + sector_height // 2))
window.blit(sector_text, sector_text_rect)
# define function to draw point
def draw_point():
pygame.draw.circle(window, black, point_position, point_radius)
# define function to check if point is inside sector
def check_sector(point, angle):
x = circle_center[0] + (circle_radius - sector_width // 2) * math.cos(math.radians(angle))
y = circle_center[1] + (circle_radius - sector_height // 2) * math.sin(math.radians(angle))
sector_rect = pygame.Rect(x, y, sector_width, sector_height)
return sector_rect.collidepoint(point)
# define main loop
def main_loop():
global point_position
global flip_state
# loop until user quits
while True:
# event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
flip_state = not flip_state
# clear screen
window.fill(blue)
# draw circle, sectors, and point
draw_circle()
draw_sectors()
draw_point()
# check if point is inside any sector
in_sector = False
for angle in sector_positions:
if check_sector(point_position, angle):
in_sector = True
break
# flip point if required
if flip_state and in_sector:
for angle in sector_positions:
if check_sector(point_position, angle):
x = circle_center[0] + (circle_radius - sector_width // 2) * math.cos(math.radians(angle))
y = circle_center[1] + (circle_radius - sector_height // 2) * math.sin(math.radians(angle))
point_position = (int(x + sector_width // 2), int(y + sector_height // 2))
break
# update screen
pygame.display.update()
# start main loop
main_loop()
```
阅读全文