【问题标题】:How do I display the circles in the center in my program如何在程序的中心显示圆圈
【发布时间】:2019-05-01 12:41:11
【问题描述】:

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

我尝试使用与其他圆圈相同的概念,只更改范围,使其保持在中间,但它没有按我的意愿工作。所以我只是让程序在中间显示一个圆,该圆的半径应该适合所有频率

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

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

【问题讨论】:

  • centerradius "14" 是所有画在中间的圆的共同半径
  • 好的,那么什么不起作用?在 3 个动画的中心绘制了一个带有 centerradius 的圆圈。怎么了?
  • 如果你运行代码你会注意到,如果你设置一个高频或低频,你会看到用 centerradius 绘制的圆并不完全从动画的第一个圆开始的地方开始,这是由于 14 只是一个适合所有人的近似半径,但它并不完全正确。edumedia-sciences.com/de/media/645-kreiswellen-wasserwellen 这应该让您了解我想要什么

标签: python loops pygame geometry


【解决方案1】:

圆的最小半径是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)

【讨论】:

  • @MILO.PY 谢谢。别客气。您可以简化代码,请参阅我现在添加的答案扩展。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-17
  • 2013-03-01
  • 1970-01-01
  • 2020-08-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多