如何摆脱pygame表面? [英] How to get rid of pygame surfaces?

查看:128
本文介绍了如何摆脱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)

解决方案

您需要先擦除圆,然后再再次涂抹圆.根据场景的复杂程度,您可能必须尝试不同的方法.通常,我要做的是在每个帧上先将其背景涂成背景"表面,然后再将精灵/其他表面涂成新的位置(Pygame中的缺陷很快,所以即使在相当大的屏幕上,我也没有速度问题).对于上面的代码,只需使用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)

编辑以回答您的评论:可以以更面向对象的方式执行此操作.

您将需要与屏幕关联的背景Surface(通常,我需要执行此操作的Display或Map类(取决于游戏类型)).然后,将您的对象作为pygame.sprite的子类.这要求您具有self.imageself.rect属性(图像是您的表面,矩形是具有位置的Pygame.rect).将所有的精灵添加到pygame.group对象.现在,每帧,您都在组上调用draw方法,并在更新显示后(即使用pygame.display.update()),在组上调用clear方法.此方法要求您同时提供目标表面(即上面的screen)和背景图像.

例如,您的主循环可能看起来像这样:

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)

有关更多信息,请参见Sprite和Group类上的文档./p>

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. 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:

# ...
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). 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.

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.

这篇关于如何摆脱pygame表面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆