如何在程序中显示中间的圆

2024-05-18 08:18:51 发布

您现在位置:Python中文网/ 问答频道 /正文

所以这个程序基本上是正确的,它应该显示一个同心圆的模拟,唯一的大问题是,我不知道如何显示中间的圆,因为中间只有一个空的空间

我试着用与其他圆圈相同的概念,只改变了范围,使它停留在中间,但它并没有按我想要的那样工作。所以我让程序在中间显示一个圆,它的半径应该适合所有频率

import pygame
import math
import time


pygame.init()
pygame.display.set_caption("Kreise")
screen = pygame.display.set_mode([1000,700])

width_2 = int(screen.get_width() / 2)
width_3 = int(screen.get_width() / 3)
height_center = int(screen.get_height() / 2 )



black = (0,0,0)
keep_going = True
onecircle = False
twocircles = False
threecircles = False
white = (255,255,255)
blue = (0,0,255)
red = (255,0,0)
green = (0,255,0)
freq = 0
circle1spot = (0,0)
circle2spot = (0,0)
circle3spot = (0,0)
freq2 = 0
freq3 = 0
pointradius = 3
num_circles = 0
num_circles2 = 0
num_circles3 = 0
radius = 0
radius2 = 0
radius3 = 0
centerradius = 14
howmanycircles = int(input("How may circles, MIN [1], MAX [3], DEFAULT [1] \n"))




if howmanycircles == 1:
    onecircle = True        
    freqinput = int(input("Frequency first circle, MIN [1], MAX [148]: \n"))
    if freqinput > 148 or freqinput == 0:
        print("Answer not correct, 120 selected as default")
        freqinput = 120
    freq = 150 - freqinput


elif howmanycircles == 2:
    twocircles = True

    freqinput = int(input("Frequency first circle, MIN [1], MAX [148], DEFAULT [120]: \n"))
    if freqinput > 148 or freqinput == 0:
        print("Answer not correct, 120 selected as default")
        freqinput = 120
    freq = 150 - freqinput

    freqinput2 = int(input("Frequency second circle, MIN [1], MAX [148], DEFAULT [120]: \n"))
    if freqinput2 > 148 or freqinput2 == 0:
        print("Answer not correct, 120 selected as default")
        freqinput2 = 120
    freq2 = 150 - freqinput2


elif howmanycircles == 3:
    threecircles = True
    freqinput = int(input("Frequency first circle, MIN [1], MAX [148], DEFAULT [120]: \n"))
    if freqinput > 148 or freqinput == 0:
        print("Answer not correct, 120 selected as default")
        freqinput = 120
    freq = 150 - freqinput

    freqinput2 = int(input("Frequency second circle, MIN [1], MAX [148], DEFAULT [120]: \n"))
    if freqinput2 > 148 or freqinput2 == 0:
        print("Answer not correct, 120 selected as default")
        freqinput2 = 120
    freq2 = 150 - freqinput2

    freqinput3 = int(input("Frequency third circle, MIN [1], MAX [148], DEFAULT [120]: \n"))
    if freqinput3 > 148 or freqinput3 == 0:
        print("Answer not correct, 120 selected as default")
        freqinput3 = 120
    freq3 = 150 - freqinput3

else:
    print("Answer not correct, 120 selected as default")
    onecircle = True
    freqinput = int(input("Frequency first circle, MIN [1], MAX [148], DEFAULT [120]: \n"))
    if freqinput > 148 or freqinput < 0:
        print("Answer not correct, 120 selected as default")
        freqinput = 120
    freq = 150 - freqinput


def circle1(radius, centerradius):
        radius = radius + 1
        num_circles = math.ceil(radius / freq)
    #screen.fill(white)

        radiusMax = num_circles * freq

        pace = freq / radiusMax

        #centerradius = int(((pace * (num_circles - 2)) + pace) * radiusMax)# + (radius % freq)



        for y in range(num_circles, 1, -1):


        # 1, -1

            radiusY = int(((pace * (num_circles - y)) + pace) * radiusMax) + (radius % freq)




            pygame.draw.circle(screen, black, circle1spot, centerradius, 1 )
            pygame.draw.circle(screen, black, circle1spot, radiusY, 1)



    #pygame.display.update() 
        return radius

def circle2(radius2, centerradius):
        radius2 = radius2 + 1
        num_circles2 = math.ceil(radius2 / freq2)
    #screen.fill(white)

        radiusMax = num_circles2 * freq2

        pace = freq2 / radiusMax


        for y in range(num_circles2, 1, -1):
        # 1, -1

            radiusY = int(((pace * (num_circles2 - y)) + pace) * radiusMax) + (radius2 % freq2)





            pygame.draw.circle(screen, red, circle2spot, centerradius, 1 )
            pygame.draw.circle(screen, red, circle2spot, radiusY, 1)

    #pygame.display.update() 
        return radius2

def circle3(radius3, centerradius):
        radius3 = radius3 + 1
        num_circles3 = math.ceil(radius3 / freq3)

        radiusMax = num_circles3 * freq3

        pace = freq3 / radiusMax


        for y in range(num_circles3, 1, -1):

            radiusY = int(((pace * (num_circles3 - y)) + pace) * radiusMax) + (radius3 % freq3)


            pygame.draw.circle(screen, green, circle3spot, centerradius, 1 )
            pygame.draw.circle(screen, green, circle3spot, radiusY, 1)

        return radius3












while keep_going:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            keep_going = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            if pygame.mouse.get_pressed()[0]:
            #mousedownleft = True
                circle1spot = pygame.mouse.get_pos()
                print(circle1spot)



            elif pygame.mouse.get_pressed()[2]:
            #mousedownright = True
                circle2spot = pygame.mouse.get_pos()

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                circle3spot = pygame.mouse.get_pos()




    pygame.draw.circle(screen, blue, (width_3,height_center), pointradius, 3 )
    pygame.draw.circle(screen, blue, ((width_3*2),height_center), pointradius, 3 )  
    pygame.draw.circle(screen, blue, ((width_2),height_center), pointradius, 3 )    
#pygame.display.update()

    if onecircle == True:
        radius = circle1(radius,centerradius)
    #pygame.display.update()

    elif twocircles == True:
        radius = circle1(radius,centerradius)
        radius2 = circle2(radius2, centerradius)

    elif threecircles == True:
        radius = circle1(radius,centerradius)
        radius2 = circle2(radius2, centerradius)
        radius3 = circle3(radius3, centerradius)



    #clock.tick(500)



    pygame.display.update()
    screen.fill(white)

    #pygame.time.wait(100)
    #pygame.time.delay(10)

    #time.sleep(.005)
    #screen.fill(white)
    #pygame.display.update() 




pygame.quit()

我在寻找一个算法来显示中间的圆,或者一个不同的可能的解决方案


Tags: trueifscreenpygamenumintfreqcircle
1条回答
网友
1楼 · 发布于 2024-05-18 08:18:51

圆的最小半径是int(pace * radiusMax)。你知道吗

用这个半径画一个“中心”圆:

例如

centerradius1 = int(pace * radiusMax)    
pygame.draw.circle(screen, black, circle1spot, centerradius1, 1)

如果要将参数(如动画的圆心、频率和颜色)传递给函数圆,则可以对所有动画使用一个函数circle

def circle(radius, center, freq, color):
    radius = radius + 1
    num_circles = math.ceil(radius / freq)
    radiusMax = num_circles * freq
    pace = freq / radiusMax

    centerradius = int(pace * radiusMax)    
    pygame.draw.circle(screen, color, center, centerradius, 1 )

    for y in range(num_circles, 1, -1):
        radiusY = int(((pace * (num_circles - y)) + pace) * radiusMax) + (radius % freq)
        pygame.draw.circle(screen, color, center, radiusY, 1)
    return radius

像这样调用函数circle

while keep_going:

    # [...]

    radius = circle(radius, circle1spot, freq, black)

    if twocircles == True or threecircles == True:
        radius2 = circle(radius2, circle2spot, freq2, red)

    if threecircles == True:
        radius3 = circle(radius3, circle3spot, freq3, green)

相关问题 更多 >