python pygame blit function

Deadly 提交于 2019-12-13 17:46:18

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/24843679/python-pygame-blit-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!