如何使圆圈画成一个圆圈。有点复杂

how to make circles draw in a circle. Kind of complicated

所以我正在编写一个绘制螺旋图的代码,每当我 运行 代码时,它都会绘制一个螺旋图。正常吧?唯一的问题是,我希望呼吸描记器从第四象限绘制到第三象限,从第三象限绘制到第二象限,然后从第二象限绘制到第一象限。发生的情况是,呼吸描记器从第 4 象限开始绘制圆圈,然后是第 3 象限,然后到达第 2 和第 1 象限的中间,并绘制像喷泉一样下降的圆圈。当您 运行 代码时,您会明白我的意思。所以我想要的是让呼吸描记器在第四象限、第三象限、第二象限、第一象限上画一个圆圈。完毕。任何帮助,将不胜感激。这是代码:

import time

import pygame
import math
import sys

# setting colors
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
ORANGE = (255, 127, 0)
YELLOW = (255, 255, 0)
PURPLE = (160, 32, 240)
# setting what order the colors go in
listCircleColor = (RED, BLUE, GREEN, ORANGE, YELLOW, PURPLE, WHITE)
# how many circles per color
intGroup = 5
# the space between each circle
turnangle = 360 / 35
# width of screen
width = 600
# height of screen
height = 600
# radius of circles
radius = 100
# making the screen
screen = pygame.display.set_mode((width, height))
# if the code is running, then continue
running = True
clock = pygame.time.Clock()

##.draw.circle(screen, BLUE, (0, 0), radius, width=2)
alpha = turnangle
circles = []
# draw
alpha = turnangle
for i in range(intGroup):
    for cl in listCircleColor:
        if alpha > 0 and alpha < 90:
            x = 300 + radius * math.cos(math.radians(alpha))
            y = 300 + radius * math.sin(math.radians(alpha))
            pygame.draw.circle(screen, cl, (x, y), radius, width=2)
            # second quarter of circles
        if alpha > 90 and alpha < 180:
            x = 300 - radius * math.cos(math.radians(180 - alpha))
            y = 300 + radius * math.sin(math.radians(180 - alpha))
            pygame.draw.circle(screen, cl, (x, y), radius, width=2)
            # third quarter of circles
        if alpha > 180 and alpha < 270:
            x = 300 - radius * math.cos(math.radians(270 - alpha))
            y = 300 - radius * math.sin(math.radians(270 - alpha))
            pygame.draw.circle(screen, cl, (x, y), radius, width=2)
            # last quarter of circles
        if alpha > 270 and alpha < 360:
            x = 300 + radius * math.cos(math.radians(360 - alpha))
            y = 300 - radius * math.sin(math.radians(360 - alpha))
            pygame.draw.circle(screen, cl, (x, y), radius, width=2)

        circles.append(([x, y], cl, alpha))
        alpha = alpha + turnangle
        pygame.display.update()
        clock.tick(10)
        #circle = [pygame.draw.circle(screen, cl, (300 + radius * math.cos(math.radians(alpha)), 300 + radius * math.sin(math.radians(alpha))), radius, width=2)]
        #circles = {'circles': circle.get_rect()}

# move"
trangleedge = radius
movetimes = 1
time.sleep(2)



# exit only when user clicks on exit button
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    screen.fill((0, 0, 0))
    while movetimes <= 100:
        trangleedge = trangleedge + 1
        for circle in circles:
            #x = circle[0][0]
            #y = circle[0][1]
            alpha = circle[2]
            cl = circle[1]

            if alpha > 0 and alpha < 90:
                x = 300 + trangleedge * math.cos(math.radians(alpha))
                y = 300 + trangleedge * math.sin(math.radians(alpha))
                #pygame.draw.circle(screen, cl, (x, y), radius, width=2)
                # second quarter of circles
            if alpha > 90 and alpha < 180:
                x = 300 - trangleedge * math.cos(math.radians(180 - alpha))
                y = 300 + trangleedge * math.sin(math.radians(180 - alpha))
                #pygame.draw.circle(screen, cl, (x, y), radius, width=2)
                # third quarter of circles
            if alpha > 180 and alpha < 270:
                x = 300 - trangleedge * math.cos(math.radians(270 - alpha))
                y = 300 - trangleedge * math.sin(math.radians(270 - alpha))
                #pygame.draw.circle(screen, cl, (x, y), radius, width=2)
                # last quarter of circles
            if alpha > 270 and alpha < 360:
                x = 300 + trangleedge * math.cos(math.radians(360 - alpha))
                y = 300 - trangleedge * math.sin(math.radians(360 - alpha))
                #pygame.draw.circle(screen, cl, (x, y), radius, width=2)

            circle[0][0] = x
            circle[0][1] = y
            screen.fill((0, 0, 0))
            for center, color, alpha in circles:
                pygame.draw.circle(screen, color, center, radius, 2)

            pygame.display.flip()
        clock.tick(60)
        movetimes += 1

解决此问题的一种方法是更改​​此部分:

            # third quarter of circles
        if alpha > 180 and alpha < 270:
            x = 300 - radius * math.cos(math.radians(270 - alpha))
            y = 300 - radius * math.sin(math.radians(270 - alpha))
            pygame.draw.circle(screen, cl, (x, y), radius, width=2)

为此:

            # third quarter of circles
        if alpha > 180 and alpha < 270:
            x = 300 - radius * math.cos(math.radians(180 + alpha))
            y = 300 - radius * math.sin(math.radians(180 + alpha))
            pygame.draw.circle(screen, cl, (x, y), radius, width=2)

现在它画一个连续的圆,然后继续。

您在 NW 象限中交换了坐标轴。您需要:

        if alpha > 180 and alpha < 270:
            x = 300 - radius * math.cos(math.radians(alpha - 180))
            y = 300 - radius * math.sin(math.radians(alpha - 180))

而且,顺便说一下,不需要检查角度的下限。 angle 不能小于零。因此,您可以将第一个循环替换为:

alpha = turnangle
for i in range(intGroup):
    for cl in listCircleColor:
        if alpha < 90:
            x = 300 + radius * math.cos(math.radians(alpha))
            y = 300 + radius * math.sin(math.radians(alpha))
            # second quarter of circles
        elif alpha < 180:
            x = 300 - radius * math.cos(math.radians(180 - alpha))
            y = 300 + radius * math.sin(math.radians(180 - alpha))
            # third quarter of circles
        elif alpha < 270:
            x = 300 - radius * math.cos(math.radians(alpha - 180))
            y = 300 - radius * math.sin(math.radians(alpha - 180))
            # last quarter of circles
        elif alpha < 360:
            x = 300 + radius * math.cos(math.radians(360 - alpha))
            y = 300 - radius * math.sin(math.radians(360 - alpha))
        pygame.draw.circle(screen, cl, (x, y), radius, width=2)

        circles.append(([x, y], cl, alpha))
        alpha += turnangle
        pygame.display.update()
        clock.tick(10)