python 如何摆脱pygame表面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2215227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get rid of pygame surfaces?
提问by lotsofsloths
In the following code, there is not just onecircle on the screen at any given point in time. I want to fix this to make it so that it looks like there is only one circle, instead of leaving a smudge trail where ever the mouse cursor has been.
在下面的代码中,在任何给定的时间点屏幕上都不止一个圆圈。我想解决这个问题,使它看起来只有一个圆圈,而不是在鼠标光标所在的地方留下污迹。
import pygame,sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640,400),0,32)
radius = 25
circle = pygame.Surface([radius*2]*2,SRCALPHA,32)
circle = circle.convert_alpha()
pygame.draw.circle(circle,(25,46,100),[radius]*2,radius)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(circle,(pygame.mouse.get_pos()[0],100))
pygame.display.update()
pygame.time.delay(10)
回答by Daniel G
You need to specifically erase the circle before you blit it again. Depending on how complicated your scene is, you may have to try different methods. Generally what I do is have a "background" surface that a blit to the screen every frame and then blit the sprites/other surfaces in their new positions (blits in Pygame are very fast, so even in fairly large screens I haven't had speed issues doing this). For your code above, it's simple enough just to use surface.fill(COLOR)
where COLOR
is your background color; eg, (255,255,255) for white:
在再次 blit 之前,您需要专门擦除圆圈。根据场景的复杂程度,您可能需要尝试不同的方法。一般来说,我所做的是有一个“背景”表面,每帧都会在屏幕上闪烁,然后在新位置上对精灵/其他表面进行闪烁(Pygame 中的 blit 非常快,所以即使在相当大的屏幕上我也没有这样做的速度问题)。对于上面的代码,只需使用您的背景颜色surface.fill(COLOR)
在哪里COLOR
就足够简单了;例如,(255,255,255) 表示白色:
# ...
screen = pygame.display.set_mode((640,400),0,32)
backgroundColor = (255,255,255)
# ...
while True:
# ...
screen.fill(backgroundColor)
screen.blit(circle,(pygame.mouse.get_pos()[0],100))
pygame.display.update()
pygame.time.delay(10)
Editin answer to your comment: It is possible to do this in a more object-oriented way.
编辑以回答您的评论:可以以更面向对象的方式执行此操作。
You will need to have a background Surface associated with your screen (I usually have a Display or Map class (depending on the type of game) that does this). Then, make your object a subclass of pygame.sprite
. This requires that you have self.image
and self.rect
attributes (the image being your surface and the rect being a Pygame.rect with the location). Add all of your sprites to a pygame.group
object. Now, every frame, you call the draw
method on the group and, after you update the display (ie, with pygame.display.update()), you call the clear
method on the group. This method requires that you provide both the destination surface (ie, screen
above) and a background image.
您需要有一个与屏幕相关联的背景 Surface(我通常有一个 Display 或 Map 类(取决于游戏类型)来执行此操作)。然后,使您的对象成为pygame.sprite
. 这要求您拥有self.image
和self.rect
属性(图像是您的表面,矩形是带有位置的 Pygame.rect)。将所有精灵添加到一个pygame.group
对象中。现在,每一帧,你都调用draw
组上的方法,在你更新显示之后(即使用 pygame.display.update()),你调用clear
组上的方法。此方法要求您提供目标表面(即screen
上方)和背景图像。
For example, your main loop may look more like this:
例如,您的主循环可能看起来更像这样:
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
circle.rect.center = (pygame.mouse.get_pos()[0],100)
circleGroup.draw(screen)
pygame.display.update()
circleGroup.clear(screen, backgroundSurface)
pygame.time.delay(10)
See the documentationon the Sprite and Group classes for more information.
有关更多信息,请参阅有关 Sprite 和 Group 类的文档。