2012-07-02 2 views
1

PyGame을 사용하면 깜박 거림이 생깁니다. 상자, 원, 텍스트, 모든 깜박임. 내 루프 사이의 대기 시간을 늘려서이 값을 줄일 수는 있지만, 모든 것을 개별적으로 수행하는 대신 화면에 모두 그려서 제거 할 수도 있습니다. 나에게 일어나는 일의 간단한 예가 있습니다 :한 번에 모든 업데이트 표시 PyGame

 
    import pygame, time 
    pygame.init() 
    screen = pygame.display.set_mode((400, 300)) 
    loop = "yes" 
    while loop=="yes": 
     screen.fill((0, 0, 0), (0, 0, 400, 300)) 
     font = pygame.font.SysFont("calibri",40) 
     text = font.render("TextA", True,(255,255,255)) 
     screen.blit(text,(0,0)) 
     pygame.display.update() 

     font = pygame.font.SysFont("calibri",20) 
     text = font.render("Begin", True,(255,255,255)) 
     screen.blit(text,(50,50)) 
     pygame.display.update() 
     time.sleep(0.1) 

"시작"버튼이 깜박입니다. 그냥 느린 컴퓨터 일 수도 있지만 깜박임을 줄이거 나 없앨 수있는 방법이 있습니까? 더 복잡한 작업을 할 때 정말 안좋아집니다. 감사!

답변

1

나는 한 번 이상 'pygame.display.update()'를 호출하고 있다고 생각한다. 메인 루프 동안 한 번만 호출 해보십시오.

다른 어떤 최적화 :

  • 보다는 loop = True를 수행

    • 당신은 당신이 수, loop = "yes"
    • 가 일관성 FPS를 위해 일을 속도를 메인 루프에서 글꼴 생성 코드를 걸릴 수 있습니다 파이 게임의 clock 클래스 사용

    그래서 ...

    import pygame 
    pygame.init() 
    
    screen = pygame.display.set_mode((400, 300)) 
    loop = True 
    
    # No need to re-make these again each loop. 
    font1 = pygame.font.SysFont("calibri",40) 
    font2 = pygame.font.SysFont("calibri",20) 
    
    fps = 30 
    clock = pygame.time.Clock() 
    
    while loop: 
        screen.fill((0, 0, 0), (0, 0, 400, 300)) 
    
        text = font1.render("TextA", True,(255,255,255)) 
        screen.blit(text,(0,0)) 
    
        text = font2.render("Begin", True,(255,255,255)) 
        screen.blit(text,(50,50)) 
    
        pygame.display.update() # Call this only once per loop 
        clock.tick(fps)  # forces the program to run at 30 fps. 
    
  • 2

    화면을 첫 번째 텍스트 (TextA)를 그리기위한 루프와 두 번째 텍스트 (Begin)를위한 루프로 차례로 두 번 업데이트합니다.

    처음 업데이트 한 후에 첫 번째 텍스트 만 나타나므로 첫 번째 업데이트와 두 번째 업데이트 사이에 begin 텍스트가 표시되지 않습니다. 이로 인해 깜박 거림이 발생합니다.

    모든 항목을 그린 후에 화면을 업데이트하십시오. 귀하의 경우 먼저 pygame.display.update()을 삭제하십시오.

    1

    파이 게임이 깜박임을 피하기 위해 버퍼 시스템을 가지고, 그래서 당신은 당신이하고있는 그들을 그릴 수 있지만 갱신 한 번만 마지막에해야

    ... 
    while loop=="yes": 
        screen.fill((0, 0, 0), (0, 0, 400, 300)) 
        font = pygame.font.SysFont("calibri",40) 
        text = font.render("TextA", True,(255,255,255)) 
        screen.blit(text,(0,0)) 
    
        font = pygame.font.SysFont("calibri",20) 
        text = font.render("Begin", True,(255,255,255)) 
        screen.blit(text,(50,50)) 
    
        pygame.display.update() # only one update 
    
        time.sleep(0.1) 
    

    을 그리고 파이 게임은 시간보다 더 나은입니다 Clock Class 있습니다. 당신이 프레임 속도를 유지하는 것을 원하지 않는다면 잠 (0.1).

    2

    0.1 초마다 전체 화면의 내용을 다시 그려야합니다. 실제 변경 내용을 추적하고 실제로 변경된 내용이 포함 된 rect 만 업데이트하는 것이 훨씬 더 일반적이며 빠릅니다. 루프 외부의 모든 것을 그려서 이벤트가 화면을 수정하고 실제로 변경된 사각형을 추적하게하십시오.

    이 같은

    그래서 뭔가 :

    import pygame, time 
    pygame.init() 
    screen = pygame.display.set_mode((400, 300)) 
    screen.fill((0, 0, 0), (0, 0, 400, 300)) 
    font = pygame.font.SysFont("calibri",40) 
    text = font.render("TextA", True,(255,255,255)) 
    screen.blit(text,(0,0)) 
    font = pygame.font.SysFont("calibri",20) 
    text = font.render("Begin", True,(255,255,255)) 
    screen.blit(text,(50,50)) 
    loop = "yes" 
    counter = 0 
    dirty_rects = [] 
    while loop=="yes": 
        pygame.display.update() 
        time.sleep(0.1) 
        # Handle events, checks for changes, etc. Call appropriate functions like: 
        counter += 1 
        if counter == 50: 
         font = pygame.font.SysFont("calibri",20) 
         text = font.render("We hit 50!", True,(255,255,255)) 
         screen.blit(text,(50,100)) 
         dirty_rects.append(Rect((50,100),text.get_size())) 
    
    관련 문제