【问题标题】:How can I display both circles without flashing caused by double refresh如何显示两个圆圈而不会因双重刷新而闪烁
【发布时间】:2019-09-17 05:49:18
【问题描述】:

代码应根据您的要求显示 1 或 2 个圆圈,并且程序会询问用户两个圆圈的频率。 我很确定代码可以正常工作,但问题是当显示圆圈时,有一个圆圈一直闪烁,但我不知道如何调整它。

我试过玩弄它,在之前或之后移动更新,更新两次,之后或之前,反正我卡住了,我不知道该怎么做

import pygame
import math
import time
clock = pygame.time.Clock()


pygame.init()
pygame.display.set_caption("circles")
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
white = (255,255,255)
blue = (0,0,255)
red = (255,0,0)
freq = 0
circle1spot = (0,0)
circle2spot = (0,0)
freq2 = 0
pointradius = 3
num_circles = 0
num_circles2 = 0
radius = 0
radius2 = 0
centerradius = 20
howmanycircles = int(input("How many circles?  \n"))
if howmanycircles == 1:
    onecircle = True
elif howmanycircles == 2:
    twocircles = True
else:
    print("Answer not correct, 1 circle selected by default")
    onecircle = True

if howmanycircles == 1:

    freqinput = int(input("Frequency 1 circle, MIN [1], MAX [148]: \n"))
    freq = 150 - freqinput

elif howmanycircles == 2:

    freqinput = int(input("Frequency 1 circle, MIN [1], MAX [148]: \n"))
    freq = 150 - freqinput

    freqinput2 = int(input("Frequency 2 circle, MIN [1], MAX [148]: \n"))
    freq2 = 150 - freqinput2

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

        radiusMax = num_circles * freq

        pace = freq / radiusMax


        for y in range(num_circles, 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(raggio2, 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):

            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


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)


            if pygame.mouse.get_pressed()[2]:
                #mousedownright = True
                circle2spot = 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 )    



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



    elif twocircles == True:

        radius = circle1(radius,centerradius)   #this is the critical zone
        pygame.display.update()             #this is the critical zone

        radius2 = circle2(radius2, centerradius)    #this is the critical zone
        pygame.display.update()     #this is the critical zone
        screen.fill(white)      #this is the critical zone





pygame.quit()

我正在寻找一种可能的解决方案以使其正常工作并正确刷新

【问题讨论】:

    标签: python pygame refresh delay


    【解决方案1】:

    在主循环结束时做一个pygame.display.update()就足够了。

    1. 清除显示
    2. 画完所有的画
    3. 更新显示
    while keep_going:
    
        # [...]
    
        # 1. clear the display
    
        screen.fill(white) 
    
    
        # 2. do all the drawing
    
        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 )    
    
        if onecircle == True:
            radius = circle1(radius,centerradius)
        elif twocircles == True:
            radius = circle1(radius,centerradius)
            radius2 = circle2(radius2, centerradius) 
    
    
        # 3. update the display
    
        pygame.display.update()
    

    【讨论】:

      猜你喜欢
      • 2012-02-06
      • 2013-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-30
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      相关资源
      最近更新 更多