python - 如何在 python 中将图像 block 传输到特定图像的区域内?

标签 python image object pygame blit

我正在制作一个游戏,我需要将我的对象复制到特定图像的区域内。我不想需要我的表面来传输这些图像。是否可以? (我正在使用 pygame)

最佳答案

如果您能更好地解释一下您正在尝试做什么,那么将来会更好,因为它会给您更多答案:)

根据我的理解,你想将一个图像复制到另一个图像上:

要使此代码正常工作,需要设置以下前提:

  • 包含该程序的文件夹还包含名为 testimage.png 和 testimage0.jpg 的任意图像
  • PyGame 已安装

我编写了以下代码,如果您遵循上述前提,就可以运行它:

import pygame

screen = pygame.display.set_mode([800, 800], 0, 32)
#initiates screen

image1 = pygame.image.load('testimage0.jpg')
#testimage0.jpg is loaded into the variable image1

image2 = pygame.image.load('testimage.png').convert_alpha()
#testimage.png is loaded into the variable image2

while True:
    screen.fill([0, 0, 0])
    #screen is filled with a black background

    screen.blit(image1, [200, 200]) 
    #here image1 is blitted onto screen at the coordinates (200,200)

    image1.blit(image2, [0, 0])
    #here image2 is blitted onto image1 at the coordinates (0,0) which starts at the upper left of image1

    pygame.display.update()
    #updates display, which you can just ignore

图像只是带有 Sprite 或图片的表面。表面的坐标系始终从左上角 (0,0) 开始,这就是为什么 image1 的 (0,0) 与屏幕的 (0,0) 不同。

我拍了一张程序的照片并编辑了一些箭头来解释我的观点:

希望这对您有所帮助,如果您认为可以接受,请记得接受作为答案。

关于python - 如何在 python 中将图像 block 传输到特定图像的区域内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19336585/

相关文章:

c++ - 错误 : No matching function found

python - python 中的智能类型转换

python - 确定请求的速率限制

将文本文件读入 if 语句的 Python 解决方案

image - 为什么这张图片是从右到左加载的?

html - 按比例调整表格大小以适合 div

c++ - 为什么我的 C++ 链接器仅在删除 boost 共享目标文件后才起作用?

python - 如何计算数组 block 内值的总和

Java:图像的读取、转换和大小调整

javascript - Angular 2如何将对象数组放入另一个对象数组