“pygame.display.update()"不清除 blit 图像 [英] "pygame.display.update()" does not clear blit image

查看:194
本文介绍了“pygame.display.update()"不清除 blit 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主循环,可以在某个点更新图像:

循环:

虽然为真:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:系统退出()self.spaceship.buttons()self.spaceship.update(self.screen)pygame.display.update()

self.spaceship.buttons() 在哪里:

def 按钮(自我):键 = pygame.key.get_pressed()距离 = 1如果键[pygame.K_LEFT]:self.x -= distelif 键 [pygame.K_RIGHT]:self.x += dist

并且定义了self.spaceship.update(self.screen):

def 更新(自我,表面):surface.blit(self.image, (self.x, self.y))

在主循环更新按键并将图像blits"到屏幕上后,它应该更新显示(pygame.display.update()),但屏幕没有得到清除.这是移动前后的显示效果:

这是在不同位置的 blit 之前图像的样子

这是 blit 在不同位置(移动后)后的图像

为什么 pygame.display.update() 不清除图像的前几帧?

解决方案

很简单,pygame 不会为你做这个.
pygame.display.update() 更新整个场景以向用户显示您闪烁"的新内容.它不会抹去过去.

它还可用于仅更新您知道已完成某些更新的某些区域(以加快速度).

或者,您执行一个 flip() 来更新整个图形缓冲区,使其仅包含自上次翻转以来您已闪烁的新内容(请注意,您将丢失所有不在此内容中的内容)循环):

虽然为真:...pygame.display.flip()

或者,您只需在调用 update 之前绘制"一个黑框.
然而,这更加费力.

虽然为真:...self.screen.fill((0,0,0))pygame.display.update()

我的建议是您要么使用 draw_rect并在飞船所在的前一个区域绘制一个黑色矩形,或者简单地使用 flip() 切换到 GPU 中的新缓冲区页面.在大多数实现中,执行 flip() 的速度非常快.

我来自 Pyglet 背景,所以 flip()update()draw_rect() 的速度表现可能会有所不同在 Pygame 中,但基本原理应该是相同的,我的回答应该是有效的,如果不是,请指出这一点.

I have this main loop that updates an image at a point:

The loop:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    self.spaceship.buttons()
    self.spaceship.update(self.screen)
    pygame.display.update()

Where self.spaceship.buttons() is:

def buttons(self):
    key = pygame.key.get_pressed()
    dist = 1
    if key[pygame.K_LEFT]:
        self.x -= dist
    elif key[pygame.K_RIGHT]:
        self.x += dist

And self.spaceship.update(self.screen) is defined:

def update(self, surface):
    surface.blit(self.image, (self.x, self.y))

After the main loop updates the key presses and "blits" the image onto the screen, it is supposed to update the display (pygame.display.update()), but the screen doesn't get cleared. This is what the display looks like, before and after moving:

This is what the image looks like before the blit at a different position

This is what the image looks like after the blit is at a different position (after moving)

Why does pygame.display.update() not clear the previous frames of the image?

解决方案

It's quite simple, pygame doesn't do this for you.
pygame.display.update() updates the entire scene to show the user the new content you've "blipped". It doesn't erase the past.

It can also be used to only update certain areas where you know you've done some updates (to speed up stuff).

Either, you do a flip() to update the entire graphical buffer to only contain the new content that you've blipped since the last flip (note that you'll loose everything not in this cycle):

while True:
    ...
    pygame.display.flip()

Or, you simply "draw" a black box before calling update.
This is however, much more taxing.

while True:
    ...
    self.screen.fill((0,0,0))
    pygame.display.update()

My recommendation is that you either use draw_rect and draw a black rectangle over the previous area where the spaceship was, or simply use flip() to swap to a new buffer page in the GPU. Doing a flip() is extremely fast in most implementations.

I come from a Pyglet background, so the speed performance of flip(), update() and draw_rect() might work differently in Pygame, but the fundamentals should be the same and my answer should be valid, if not please point this out.

这篇关于“pygame.display.update()"不清除 blit 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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