何将图像blit到pygame中与Rectangle大小相同的表

何将图像blit到pygame中与Rectangle大小相同的表

本文介绍了如何将图像blit到pygame中与Rectangle大小相同的表面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用pygame制作游戏,每个Sprite都有一个矩形和一个图像。如何将此图像blit到曲面,使其与矩形的大小相同?

I'm making a game with pygame and each Sprite has a rectangle and an image. How would I blit this image to a surface so that it's the same size as the rectangle?

pygame.init()
screen = pygame.display.set_mode((720, 480))
pygame.display.set_caption('My game')

background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))

charRect = pygame.Rect((0,0),(10, 10))
print os.path.abspath("airbender.png")
charImage = pygame.image.load(os.path.abspath("ImageName.png"))
charImage = charImage.convert()

background.blit(charImage, charRect) #This just makes it in the same location
                                     #and prints it the same size as the image

screen.blit(background,(0,0))
pygame.display.flip()

另外为什么最后 pygame.display.flip()是必要的?

Also why is that last pygame.display.flip() necessary?

推荐答案

你应该这样做:

pygame.init()
screen = pygame.display.set_mode((720, 480))
pygame.display.set_caption('My game')

background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))

charRect = pygame.Rect((0,0),(10, 10))
print os.path.abspath("airbender.png")
charImage = pygame.image.load(os.path.abspath("ImageName.png"))
charImage = pygame.transform.scale(charImage, charRect.size)
charImage = charImage.convert()

background.blit(charImage, charRect) #This just makes it in the same location
                                     #and prints it the same size as the image

screen.blit(background,(0,0))
pygame.display.flip()

这篇关于如何将图像blit到pygame中与Rectangle大小相同的表面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 15:06