Python 为什么pygame中的框会闪烁颜色?

Python 为什么pygame中的框会闪烁颜色?,python,pygame,Python,Pygame,所以,在黑盒收缩然后展开之后,我想让这两个彩色框从黑色背景中展开(听起来有点混乱,但你会明白我的意思)。有没有一个原因,这些五颜六色的盒子,我试图使闪烁,而不是停留 import os os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(250, 10) import pygame import random pygame.init() SIZE = (650, 650) screen = pygame.display.set_mode(SIZE

所以,在黑盒收缩然后展开之后,我想让这两个彩色框从黑色背景中展开(听起来有点混乱,但你会明白我的意思)。有没有一个原因,这些五颜六色的盒子,我试图使闪烁,而不是停留

import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(250, 10)
import pygame
import random
pygame.init()
SIZE = (650, 650)   
screen = pygame.display.set_mode(SIZE)



RED = (255, 0, 0)
BLACK = (0, 0, 0)


x = 650
y = 650
count = 0
secx = 1
secy = 1
firstsx = 325
firstsy = 325


run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    if x >= 1:
        x -= 1
        y -= 1
        count += 1

        randcolor = (random.randint(0,255)), (random.randint(0,255)),(random.randint(0,255))

        pygame.draw.rect(screen, randcolor, (1,1, x, y))
        pygame.draw.rect(screen, BLACK, (0,0, x, y))

        pygame.display.flip()
        pygame.time.wait(3)

    if count > 649:
        count += 1
        secx = count-650
        secy = count-650

        pygame.draw.rect(screen, BLACK, (0,0, secx, secy))
        pygame.display.flip()
        pygame.time.wait(3) 

    if count > 1399:
        count += 1
        firstsx -= 1
        firstsy -= 1

        randcolor = (random.randint(0,255)), (random.randint(0,255)),(random.randint(0,255))
        pygame.draw.rect(screen, randcolor, (325,0, firstsx, firstsy))
        pygame.draw.rect(screen, randcolor, (0,325, firstsx, firstsy))
        pygame.display.flip()
        pygame.time.wait(3) 

您实际要做的是更新显示并在绘制第一个框后等待。此时,根本不绘制第二个方框。
只需在应用程序循环结束时执行1
pygame.display.flip()和
pygame.time.wait(3)

此外,您必须在主应用程序循环之前定义随机颜色。快速变化的颜色使方框闪烁:

randcolor=(random.randint(0255)),(random.randint(0255)),(random.randint(0255))
randcolor1=(random.randint(0255)),(random.randint(0255)),(random.randint(0255))
randcolor2=(random.randint(0255)),(random.randint(0255)),(random.randint(0255))
运行=真
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
如果x>=1:
x-=1
y-=1
计数+=1
pygame.draw.rect(屏幕,随机色,(1,1,x,y))
pygame.draw.rect(屏幕,黑色,(0,0,x,y))
如果计数>649:
计数+=1
secx=count-650
secy=count-650
pygame.draw.rect(屏幕,黑色,(0,0,secx,secy))
如果计数>1399:
计数+=1
firstsx-=1
Firsty-=1
pygame.draw.rect(屏幕,随机颜色1,(325,0,firstsx,firstsy))
pygame.draw.rect(屏幕,随机色2,(0325,firstsx,firstsy))
如果firstsx==0:
x、 y=650650
计数=0
firstsx,firstsy=325325
pygame.display.flip()
pygame.time.wait(3)

我尝试了此代码,但它仍在闪烁。还有什么我能做的吗?@conyieie是的,在循环之前定义颜色。我更改了答案。@Rabbid76是的,谢谢你提醒我,我在你的答案上打了勾