简体   繁体   English

如何摆脱pygame表面?

[英]How to get rid of pygame surfaces?

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)

You need to specifically erase the circle before you blit it again. 在再次进行blit之前,您需要专门擦除圆圈。 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). 一般来说,我所做的是有一个“背景”表面,每一帧对屏幕都是一个blit,然后在他们的新位置blit sprite /其他表面(Pygame中的blits非常快,所以即使在相当大的屏幕中我还没有速度问题这样做)。 For your code above, it's simple enough just to use surface.fill(COLOR) where COLOR is your background color; 对于上面的代码,只需使用surface.fill(COLOR) ,其中COLOR是你的背景颜色; eg, (255,255,255) for white: 例如,(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)

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). 你需要有一个与你的屏幕相关联的背景Surface(我通常有一个Display或Map类(取决于游戏的类型)这样做)。 Then, make your object a subclass of pygame.sprite . 然后,使您的对象成为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). 这需要你有self.imageself.rect属性(图像是你的表面,而rect是Pygame.rect的位置)。 Add all of your sprites to a pygame.group object. 将所有精灵添加到pygame.group对象。 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. 现在,每个帧,您在组上调用draw方法,并在更新显示后(即使用pygame.display.update()),在组上调用clear方法。 This method requires that you provide both the destination surface (ie, screen above) and a background image. 此方法要求您提供目标曲面(即上面的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 documentation on the Sprite and Group classes for more information. 有关更多信息,请参阅Sprite和Group类的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM