python - 如何在我的程序中显示中心的圆圈

标签 python loops pygame geometry

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

我已经尝试使用与其他圆圈相同的概念,并且只更改了范围,使其保持在中间,但它没有达到我想要的效果。所以我只是让程序在中间显示一个圆圈,它的半径应该适合所有频率

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

我正在寻找一种在中间显示圆圈的算法,或者其他可能的解决方案

最佳答案

圆的最小半径是int(pace * radiusMax)

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

例如

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

如果您将动画的中心点、频率和颜色等参数传递给一个函数 circle,那么您将能够对所有动画使用一个函数 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)

关于python - 如何在我的程序中显示中心的圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55936456/

相关文章:

python - 无法在 Django 中保存不同的日期格式

xml - 每年使用 XSLT 循环打印

python - 将移动控件移至玩家类别后,我无法移动 Sprite

javascript - 在纯函数式 JavaScript : Iterate over a string's characters, 中,如果满足条件则返回一个值

python - 有没有办法在任务栏中设置 Pygame 图标? set_icon() 似乎只影响实际窗口中的小图标

python - 如何使 pygame.key.get_pressed() 响应更快?

python - 学习 : How to apply dimensionality reduction on huge data set?

python - 如何使 Django blocktrans 中的空格和缩进无关紧要?

python - 覆盖扩展 Enum 的类的 __new__

java - Android 中带有计时器的 Foreach String[]