【问题标题】:how to make circles draw in a circle. Kind of complicated如何让圆圈画成一个圆圈。有点复杂
【发布时间】:2022-01-07 05:34:07
【问题描述】:

所以我正在编写一个绘制螺旋图的代码,每当我运行代码时,它都会绘制一个螺旋图。正常,对吧?唯一的事情是我希望螺旋计从第四象限画到第三象限,第三象限画到第二象限,第二象限画到第一象限。发生的情况是,螺旋计从第 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

【问题讨论】:

    标签: python pygame pygame-surface


    【解决方案1】:

    您在 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)
    

    【讨论】:

      【解决方案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)
      

      到这里:

                  # 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)
      

      现在它在一个连续的圆圈中绘制圆圈,然后继续。

      【讨论】:

        猜你喜欢
        • 2012-11-12
        • 1970-01-01
        • 2018-12-15
        • 2016-10-12
        • 1970-01-01
        • 2021-06-24
        • 1970-01-01
        • 1970-01-01
        • 2019-09-04
        相关资源
        最近更新 更多