How to blit an image, in python, inside the area of a specific image?

白昼怎懂夜的黑 提交于 2019-12-31 05:04:12

问题


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)


回答1:


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

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

For this code to work the following premises are set:

  • The folder which contains the program also contains the arbritary images named testimage.png and testimage0.jpg
  • PyGame is installed

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

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.

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.



来源:https://stackoverflow.com/questions/19336585/how-to-blit-an-image-in-python-inside-the-area-of-a-specific-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!