如何在pygame中将图像blit到一个与Rectangle大小相同的表面

时间:2015-04-15 23:44:07

标签: python image pygame blit pygame-surface

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

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()需要?

1 个答案:

答案 0 :(得分:2)

您应该执行以下操作:

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