如何摆脱 pygame 表面?
在下面的代码中,在任何给定时间点屏幕上不只有一个圆圈。 我想修复这个问题,使其看起来只有一个圆圈,而不是在鼠标光标所在的地方留下污迹。
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)
In the following code, there is not just one circle 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在再次进行位块传输之前,您需要专门擦除该圆圈。根据场景的复杂程度,您可能需要尝试不同的方法。一般来说,我所做的是有一个“背景”表面,每帧都会 blit 到屏幕,然后将精灵/其他表面传送到新位置(Pygame 中的 blit 非常快,所以即使在相当大的屏幕上,我也没有这样做的速度问题)。对于上面的代码,只需使用
surface.fill(COLOR)
就足够简单了,其中COLOR
是背景颜色;例如,(255,255,255) 代表白色:编辑以回答您的评论:可以以更面向对象的方式执行此操作。
您需要有一个与屏幕关联的背景 Surface(我通常有一个 Display 或 Map 类(取决于游戏类型)来执行此操作)。然后,使您的对象成为 pygame.sprite 的子类。这要求您具有 self.image 和 self.rect 属性(图像是您的表面,矩形是带有位置的 Pygame.rect )。将所有精灵添加到
pygame.group
对象中。现在,在每一帧中,您都可以在组上调用draw
方法,并且在更新显示后(即使用 pygame.display.update()),您可以调用clear
团体方法。此方法要求您提供目标表面(即上面的屏幕)和背景图像。例如,您的主循环可能看起来更像是这样:
请参阅文档 Sprite 和 Group 类以获取更多信息。
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)
whereCOLOR
is your background color; eg, (255,255,255) for white:Edit in 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 haveself.image
andself.rect
attributes (the image being your surface and the rect being a Pygame.rect with the location). Add all of your sprites to apygame.group
object. Now, every frame, you call thedraw
method on the group and, after you update the display (ie, with pygame.display.update()), you call theclear
method on the group. This method requires that you provide both the destination surface (ie,screen
above) and a background image.For example, your main loop may look more like this:
See the documentation on the Sprite and Group classes for more information.