在pygame中创建永久背景

时间:2013-12-21 04:03:46

标签: python background pygame

我有一个程序,我使用鼠标控件移动对象。当对象移动时,程序会将屏幕重置为纯白色,这样就不会使用以下对象的跟踪:

screen.fill(255,255,255)

我现在要做的是让背景成为我在方法中创建的背景。

black = (0,0,0)
brown = (218,134,10)
white = (255,255,255)
pygame.draw.rect(screen,white,(0,0,1000,600))

first =pygame.draw.rect(screen,brown,(150,150,50,400),0)
second = pygame.draw.rect(screen,brown,(450,150,50,400),0)
third = pygame.draw.rect(screen,brown,(750,150,50,400),0)

first_out = pygame.draw.rect(screen,black,(150,150,50,400),2)
second_out = pygame.draw.rect(screen,black,(450,150,50,400),2)
third_out= pygame.draw.rect(screen,black,(750,150,50,400),2)

如何在每次移动对象时刷新背景?

1 个答案:

答案 0 :(得分:0)

您应该创建一个新曲面,而不是直接在屏幕表面上绘制,我们称之为background_surface

通过这样做,您只需要绘制背景一次(在应用程序的最开始时),之后的任何blitting将会更快地完成,因为没有pygame.draw.xxx正在完成。

要使用background_surface填充屏幕表面,您只需使用screen.blit()


快速摘录:

WIDTH, HEIGHT = 800, 600
background_surface = pygame.Surface((WIDTH, HEIGHT))

# do all the drawings on the background surface
background_surface.fill((255, 255, 255))

while application_running:
    # processinput
    # update objects
    # draw everything
    screen.blit(background_surface, (0, 0))
    pygame.display.update()