python - 如何将图像blit到pygame中与矩形大小相同的表面

标签 python image pygame blit pygame-surface

我正在用 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() 是必要的?

最佳答案

你应该这样做:

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

关于python - 如何将图像blit到pygame中与矩形大小相同的表面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29662908/

相关文章:

php - 从 Wordpress 媒体库获取单个特定图像

image - 使用 Exiftool 提取二进制数据

python - 带偏移的鼠标位置

python - pygame代码组织

python - 如何在 pygame 窗口和函数之间转换

Python 3.4子进程FileNotFoundError WinError 2

python - 如果我不关闭一个 mysql 连接并打开另一个连接,会发生什么情况?

java - JApplet 中的 JLabel 和图像 - 图像未显示

Python - 做一些事情直到按键或超时

python - python 中奇怪的关闭行为