如何将图像 blit 到 pygame 中与矩形大小相同的表面
How do I blit an image to a surface in pygame that's the same size as a Rectangle
我正在用 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()
是必要的?
你应该这样做:
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()
我正在用 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()
是必要的?
你应该这样做:
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()