I have a program where I am using mouse controls to move objects. When the objects move, the program resets the screen to solid white so that there are no trails of the object using:
我有一个使用鼠标控制来移动对象的程序。当物体移动时,程序会将屏幕重置为纯白,这样就不会出现物体的痕迹,方法是:
screen.fill(255,255,255)
What I am trying to do now is have the background a created background I made in a method.
我现在想做的是让背景成为我用一种方法制作的背景。
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)
How can I make my background the background that refreshes every time an object is moved?
如何使我的背景成为每次移动对象时都会刷新的背景?
更多回答
优秀答案推荐
Rather than drawing directly on the screen surface, you should create a new surface, let's call it background_surface
.
不是直接在屏幕表面上绘制,而是应该创建一个新的表面,让我们将其命名为BACKGROUND_SERFACE。
By doing so, you will only have to draw the background once (at the very beginning of your application) and any blitting afterwards will be done a lot quicker, since no pygame.draw.xxx
is being done.
通过这样做,您将只需要绘制一次背景(在应用程序的最开始),之后的任何blit都将更快地完成,因为不会执行pygame.dra.xxx。
To fill the screen surface with background_surface
, you'll just use screen.blit()
.
要使用BACKGROUND_Surface填充屏幕表面,只需使用creen.blit()即可。
A quick snippet:
下面是一个简短的代码片段:
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()
更多回答
This is just giving an error saying background_surface is not defined
这只是给出了一个错误,提示没有定义BACKGROUND_SERFACE
That is caused by background_surface
being out-of-scope. Shouldn't be much trouble to fix that error. The code I provided was just an example.
这是由于BACKGROUND_Surface超出范围所致。修复这个错误应该不会有太大麻烦。我提供的代码只是一个示例。
我是一名优秀的程序员,十分优秀!