微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

闪烁的矩形pygame

如何解决闪烁的矩形pygame

所以我用 pygame.draw.rect 创建了按钮,我用它来滚动背景,所以看起来字符(黑色矩形)正在移动。 但是当我点击任何一个按钮时,所有的按钮都会闪烁。 怎么解决

import sys
import time
pygame.init()

width = 2000
height = 3000

bg= pygame.image.load("bg1.jpg")
bg = pygame.transform.scale(bg,(width,height))
display = pygame.display.set_mode((width,height))

clock = pygame.time.Clock()

run = False

font = pygame.font.SysFont(None,101)

class npc:
    def __init__(self,x,y):
        self.x = x
        self.y = y
    
        pygame.draw.rect(display,(125,125,125),(self.x,self.y,100,200))
        
        

def ts(string):
    text = ''
    for i in range(len(string)):
        
        
        
        text += string[i]
        text_surface = font.render(text,True,(0,0))
        text_rect = text_surface.get_rect()
        text_rect.center = (width/2,height/2)
        
        display.fill((130,190,255),text_rect)
        
        display.blit(text_surface,text_rect)
        
        
        pygame.time.wait(100)
        

p_x = 50
p_y = height/2 + 178
bgx = 0
bgx2 = bg.get_width()

bgy = 0
bgy2 = bg.get_height()

def move():
    
    global bgx
    global bgx2

    bgx -= 50
    bgx2 -= 50
    
    display.blit(bg,[bgx,bgy])
    display.blit(bg,[bgx2,bgy2])
    
def movex():
    global bgx
    global bgx2
    bgx += 50
    bgx2 += 50
    
    display.blit(bg,bgy2])
    
def move_2():
    global bgy
    global bgy2
    
    bgy -= 50
    bgy2 -= 50
    
    display.blit(bg,bgy2])
    
def movey():
    global bgy
    global bgy2
    
    bgy += 50
    bgy2 += 50
    
    display.blit(bg,bgy2])
    
fun = [movey,move_2,movex,move]


while not run:
    
    

    btn1 = pygame.Rect(210,110,110)
    btn2 = pygame.Rect(210,420,110)
    btn3 = pygame.Rect(95,265,110)
    btn4 = pygame.Rect(325,110)
    
    btn = [btn1,btn2,btn3,btn4]
    
    

    
    
    mouse_pos = (0,0)
    
    
    if bgy > bg.get_height():
        bgy = bg.get_height() * -1
        
    if bgy2 > bg.get_height():
        bgy2= bg.get_height() * -1
        
    if bgy < bg.get_height() * -1:
        bgy = bg.get_height()
        
    if bgy2 < bg.get_height() * -1:
        bgy2 = bg.get_height()
    
    
    if bgx < bg.get_width() * -1:
        bgx = bg.get_width()
        
    if bgx2 < bg.get_width() * -1:
        bgx2 = bg.get_width()
        
    if bgx > bg.get_width():
        bgx = bg.get_width() * -1
        
    if bgx2 >= bg.get_width():
        bgx2 = bg.get_width() * -1
        

    for event in pygame.event.get():
        
        if event.type == pygame.QUIT:
            exit_game = True
            
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = event.pos
            
    display.blit(bg,bgy2])
    

    

    x = 0
    for i in btn:
        
        pygame.draw.rect(display,(255,255,i)
        
        if i.collidepoint(mouse_pos):
            fun[x]()
        x += 1
            
            
    
    np_c = npc(900,400)

    pygame.draw.rect(display,(225,1950,90,90 ))
    
    pygame.draw.rect(display,(90,2060,0),(p_x,p_y,150,90 ))
    
    
    
    
    
    
    pygame.display.flip()

    clock.tick(30)
    
pygame.quit()
quit()

当我单击任何按钮滚动背景时,只有按钮闪烁,而不是字符矩形或其他对象 是因为 blit 功能吗? 另外我怎样才能使这段代码更加优化? 感谢帮助

解决方法

按钮闪烁,因为您在 move 函数中的按钮顶部绘制了背景。

无需在 move 函数中重新绘制背景。更新变量 bgxbgx2 就足够了。后台在应用循环中自动更新:

def move():
    global bgx
    global bgx2
    width = bg.get_width()
    bgx = (bgx - 50) % width
    bgx2 = bgx - width
    
def movex():
    global bgx
    global bgx2
    bgx = (bgx + 50) % width
    bgx2 = bgx - width
    
def move_2():
    global bgy
    global bgy2
    height = bg.get_height()
    bgy = (bgy - 50) % height
    bgy2 = bgy - height
    
def movey():
    global bgy
    global bgy2
    height = bg.get_height()
    bgy = (bgy + 50) % height
    bgy2 = bgy - height

如果背景在 x 和 y 方向移动,则需要绘制 4 次背景:

while not run:
    # [...]

    display.blit(bg,[bgx,bgy])
    if bgx != 0:
        display.blit(bg,[bgx2,bgy])
    if bgy != 0:
        display.blit(bg,bgy2])
        if bgx != 0:
            display.blit(bg,bgy2])  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。