Pygame擦除背景上的图像 [英] Pygame Erasing Images on Backgrounds

查看:130
本文介绍了Pygame擦除背景上的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将图像 blit 到表面上用作背景.然后你按下按钮 X 在同一表面上闪烁图像,你如何擦除图像?到目前为止,我已经有了这个,但后来我的背景中间出现了一个白色矩形.

You blit an image onto your surface to use as your background. Then you press button X to blit an image on the same surface, how do you erase the image? I have this so far, but then I end up with a white rectangle in the middle of my background.

screen = pygame.display.set_mode(1280, 512)

screen.blit(background, (0,0))

while True:   
    pygame.display.flip() #flip is same as update
    for event in pygame.event.get():
        if (event.type == pygame.KEYDOWN):        
            if event.key == pygame.K_SPACE:
                screen.blit(player, (x, y))
            if event.key == pygame.K_BACKSPACE:
                pygame.draw.rect(screen, [255,255,255], (x, y, 62,62))

推荐答案

基本上你可以在这里做两件事.你可以走简单的路线,再画一次背景......

There are basically two things you can do here. You can go the simple route and just draw the background again...

if event.key == pygame.K_BACKSPACE:
   screen.blit(background, (0,0))

或者,如果你想让它更高效一点,你可以让 blit 方法只绘制播放器图像覆盖的背景部分.你可以这样做...

Or, if you want it to be a little more efficient, you can have the blit method only draw the part of the background that the player image is covering up. You can do that like this...

screen.blit(background, (x, y), pygame.Rect(x, y, 62, 62))

第三个参数中的 Rect 将导致 blit 仅绘制位于图像上 x,y 位置的背景"的 62x62 像素部分.

The Rect in the third argument will cause the blit to only draw the 62x62 pixel section of 'background' located at position x,y on the image.

当然,这假设播放器图像始终位于背景内.如果播放器图像仅与背景部分重叠,我不完全确定会发生什么.它可能会抛出异常,或者它只会环绕到图像的另一侧.

Of course this assumes that the player image is always inside the background. If the player image only partially overlaps the background I'm not entirely sure what will happen. It'll probably throw an exception, or it'll just wrap around to the other side of the image.

如果这是一个相当小的项目,第一个选项应该没问题,但如果您出于某种原因担心性能,请尝试第二个选项.

Really the first option should be just fine if this is a fairly small project, but if you're concerned about performance for some reason try the second option.

这篇关于Pygame擦除背景上的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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