Little turtle 83 lesson detailed explanation of the problem of picture transparency setting python

Let me talk about the premise of setting transparency. You need to use pictures without alpha channel to set transparency, otherwise it will not achieve the purpose (the specific reason is not clear to the blogger (manually laugh and cry))

First of all, let’s talk about the idea. What we need to get is to achieve transparency for the turtle on the background image. The overall idea is to create a suface object with no alpha channel in the background and an alpha channel in the pasted image. The specific ideas are as follows:

  (1) First, create a rectangular surface object temp that is as large as the picture without alpha

(2) Draw the background on temp, the position of the target relative to temp becomes (-x,-y), at this time temp gets the same size as the picture, and draws the background surface object

(3) Draw the picture with alpha channel, the position of the target picture relative to the rectangular frame at this time is (0, 0)

(4) At this time, temp has become a complete picture with a background picture and the target turtle. Since temp is a surface object without alpha at the beginning of the setting, the transparency of the whole picture can be set using the set_alpha() method

(5) Paste the temp with the transparency set as an independent picture to the specified location.

 

The detailed code is as follows:

import pygame
import sys
from pygame.locals import *

pygame.init()

size = width, height = 640, 480
bg = (0, 0, 0)

clock = pygame.time.Clock()
#绘制size尺寸的背景
screen = pygame.display.set_mode(size)
#显示标题
pygame.display.set_caption("FishC Demo")
#带alpha的目标图片
turtle = pygame.image.load("turtle.png").convert_alpha()
#不带alpha的背景图片
background  = pygame.image.load("background.jpg").convert()
position = turtle.get_rect()
#turtle 位置中心
position.center = width // 2, height // 2

#target 背景 sorce 图片 location 图片的实时位置  opacity 透明度
def blit_alpha(target, source, location, opacity):
    #小甲鱼的位置坐标
    x = location[0]
    y = location[1]
    #temp:一个与图片等大的不带alpha的矩形surface对象
    temp = pygame.Surface((source.get_width(), source.get_height())).convert()
    #在temp上绘制背景,target相对于temp的位置变成了(-x,-y),此时temp
    #得到的是与图片大小一样,绘制着背景的surface对象
    temp.blit(target, (-x, -y ))
    #将带alpha通道的图片绘制上去,此时的目标图片相对于矩形框的位置为(0,0)
    temp.blit(source, (0, 0))
    #使用set_alpha()方法temp的透明度,透明度设置为200
    temp.set_alpha(opacity)
    #将设置好透明度的temp“贴到”指定位置
    target.blit(temp, location)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
    #背景位置为(0,0)
    screen.blit(background, (0, 0))
    #调用绘制函数
    blit_alpha(screen, turtle, position, 200)
    # 更新整个待显示的Surface对象到屏幕上
    pygame.display.flip()
    #延迟30ms
    clock.tick(30)

The effect is as follows:

 

 

The blogger is also a novice, so if you have any questions, just spray. .

Guess you like

Origin blog.csdn.net/progess_every_day/article/details/108561090