Как мне отобразить круги в центре моей программы - PullRequest
2 голосов
/ 01 мая 2019

Так что в основном эта программа работает почти правильно, она должна отображать симуляцию концентрических окружностей, единственная большая проблема в том, что я не знаю, как отображать круги в центре, так как в середине есть только пустое пространство

Я пытался использовать ту же концепцию, что и в остальных кругах, и изменил только диапазон, чтобы он оставался посередине, но он не работал так, как я хотел. Поэтому я просто позволил программе отобразить окружность посередине, которая должна иметь радиус, подходящий для всех частот

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

Я ищу алгоритм для отображения кругов посередине или другое возможное решение

1 Ответ

1 голос
/ 01 мая 2019

Минимальный радиус круга равен 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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...