python pygame blit function

2019-08-14 06:30发布

When I use blit function, it does not delete the previous loaded sprite to make sprites move until I call the "display.fill(bgcolor)" function. The problem is that I have a multicolored background. so how do I update the image without affecting my background? NOTE - already tried "pygame.display.update()" and "pygame.display.flip()" - it doesn't help :(

class states():
def __init__(self, goku1,goku2, x, y):
    self.image=goku1
    keys=pygame.key.get_pressed()
    if keys[K_RIGHT]:
        self.image=goku2
    if keys[K_LEFT]:
        self.image=goku2

while True:
pygame.display.flip()
pygame.display.update()
obj=states(goku1, goku2, x, y)

call=position()
DISPLAYSURF.blit(obj.image, (x, y))

am stuck for long :(

2条回答
小情绪 Triste *
2楼-- · 2019-08-14 06:36

Blit never delete previous element - it can't - all blitted elements create one bitmap.

You have to blit all elements again in all loop.

Or you have to keep part of background before you blit sprite and use it later to blit this part in place of sprite to remove it.

You can also use pygame.display.update() with arguments to blit only some parts of background.

查看更多
一纸荒年 Trace。
3楼-- · 2019-08-14 06:53

You would blit the background first, and then blit the new location for the sprite that is moving. It would look something like this:

window= pygame.display.set_mode(WINDOWSIZE, 0, 32)

while True:
    #update events

    window.blit(your_multi_colored_background, (0, 0))
    window.blit(obj.image, (x, y))
    pygame.display.update()

Hope this helps.

查看更多
登录 后发表回答