如何在python中,在特定图像区域内对图像进行blit? [英] How to blit an image, in python, inside the area of a specific image?

查看:1738
本文介绍了如何在python中,在特定图像区域内对图像进行blit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作游戏,我需要在特定图像区域内对象进行blit。我不想让我的表面对这些图像进行blit。可能吗? (我正在使用pygame)

I'm making a game and I need to blit my objects inside the area of a specific image. I don't want to need my surface to blit these images. Is it possible? (I'm using pygame)

推荐答案

如果你能解释你想做什么,将来会更好有点好,因为它会给你更多的答案:)

It would be better in the future if you would explain what you are trying to do a bit better since it would give you more answers :)

据我所知,你想把图像blit到另一张图像上:

From what I have understood you want to blit an image onto another image:

要使此代码生效,请设置以下前提:

For this code to work the following premises are set:


  • 包含程序的文件夹还包含arbritary图像名为testimage.png和testimage0.jpg

  • 已安装PyGame

我写了以下内容如果你遵循上述前提,你可以运行一段代码:

I have written the following piece of code which you can run if you follow the above premises:

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

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

Images are just surfaces with a sprite or picture on. The coordinate system of a surface always starts at the upper left (0,0), which is why (0,0) of image1 isn't the same as the (0,0) of screen.

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

I took a picture of the program and edited some arrows in to explain my point:

希望这是一个帮助,如果你认为可以接受,请记得接受答案:)

Hope this was a help and remember to accept as the answer if you find it acceptable :)

这篇关于如何在python中,在特定图像区域内对图像进行blit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆