逐渐透明问题
这次的问题是按下对应的键(a)角色无法逐渐透明了不明白是怎么回事,请各位帮忙看看,谢谢代码如下:import pygame
import sys
from pygame.locals import *
pygame.init()
er = True
w = True
running = True
size = width, height = 600, 400
screen = pygame.display.set_mode(size)
bmf = pygame.image.load('Chomper.gif').convert_alpha()
p = (300, 200)
bg = pygame.image.load('170731094492.jpg')
bgp = (0, 0)
def blit_alpha(t, t1, s, l, o):
#print('正在调用函数中...')
x = l
y = l
temp = pygame.Surface((s.get_width(), s.get_height())).convert()
temp.blit(t1, (-x, -y))
temp.blit(s, (0, 0))
temp.set_alpha(o)
t.blit(temp, l)
def rGraduallyTransparent(t, t1, s, l, n):
z = n + 1
for i in range(1, z):
#print('执行中... x ', i)
blit_alpha(t, t1, s, l, i)
#print(f'此时blit_alpha的参数有{t}, {t1}, {s}, {l}, {i}')
pygame.display.flip()
pygame.time.delay(10)
def GraduallyTransparent(t, t1, s, l, n):
z = n + 1
for i in range(1, z, -1):
print(1)
blit_alpha(t, t1, s, l, i)
#print(f'此时blit_alpha的参数有{t}, {t1}, {s}, {l}, {i}')
pygame.display.flip()
pygame.time.delay(10)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.blit(bg, bgp)
k = pygame.key.get_pressed()
if k:
rGraduallyTransparent(screen, bg, bmf, p, 100)
if k:
GraduallyTransparent(screen, bg, bmf, p, 100)
screen.blit(bmf, p)
pygame.display.flip() 问题出在两个循环中的范围设置有误,应该从1到n+1。此外,应该将screen.blit(bg, bgp)和screen.blit(bmf, p)放在循环外面,否则会导致重复绘制背景和角色。修改后的代码如下:
import pygame
import sys
from pygame.locals import *
pygame.init()
er = True
w = True
running = True
size = width, height = 600, 400
screen = pygame.display.set_mode(size)
bmf = pygame.image.load('Chomper.gif').convert_alpha()
p = (300, 200)
bg = pygame.image.load('170731094492.jpg')
bgp = (0, 0)
def blit_alpha(t, t1, s, l, o):
x = l
y = l
temp = pygame.Surface((s.get_width(), s.get_height())).convert()
temp.blit(t1, (-x, -y))
temp.blit(s, (0, 0))
temp.set_alpha(o)
t.blit(temp, l)
def rGraduallyTransparent(t, t1, s, l, n):
z = n + 1
for i in range(1, z):
blit_alpha(t, t1, s, l, i)
pygame.display.flip()
pygame.time.delay(10)
def GraduallyTransparent(t, t1, s, l, n):
z = n + 1
for i in range(z, 1, -1):
blit_alpha(t, t1, s, l, i)
pygame.display.flip()
pygame.time.delay(10)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.blit(bg, bgp)
k = pygame.key.get_pressed()
if k:
rGraduallyTransparent(screen, bg, bmf, p, 100)
GraduallyTransparent(screen, bg, bmf, p, 100)
screen.blit(bmf, p)
pygame.display.flip()
请注意,我也修复了if语句中的判断条件,现在只有在按下‘a’键时才会调用透明效果函数。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]