【问题标题】:How do I blit an image to a surface in pygame that's the same size as a Rectangle如何在pygame中将图像粘贴到与矩形大小相同的表面
【发布时间】:2015-04-15 23:44:07
【问题描述】:

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

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() 是必要的?

【问题讨论】:

    标签: python image pygame blit pygame-surface


    【解决方案1】:

    你应该这样做:

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

    【讨论】:

    • 太棒了,就是这样。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 2023-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多