|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 lzb1001 于 2022-7-9 06:22 编辑
# 设置透明度
import pygame
import sys
from pygame.locals import *
pygame.init()
size = width, height = 640, 480
bg = (0, 0, 0)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption('FishC Demo')
turtle = pygame.image.load(r'd:\\work\tortoise.png').convert_alpha()
background = pygame.image.load(r'd:\\work\\grass.jpg').convert()
position = turtle.get_rect()
position.center = width // 2, height // 2
print(position, '\n上:', position.top, '\n下:', position.bottom, '\n左:', position.left, '\n右:', position.right, '\n宽度:', position.width, '\n高度:', position.height, '\n中心点:', position.center)
def blit_alpha(
target: pygame.Surface,
source: pygame.Surface,
location: tuple[int, int],
opacity: int
) -> None:
x, y = location[0], location[1]
temp = pygame.Surface((source.get_width(), source.get_height())).convert()
temp.blit(target, (-x, -y))
temp.blit(source, (0, 0))
temp.set_alpha(opacity)
target.blit(temp, location)
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
screen.blit(background, (0, 0))
blit_alpha(target=screen, source=turtle, opacity=200, location=position)
pygame.display.flip()
clock.tick(30)
------------------------------------------
Windows 10 专业版 | Python 3.7.6
------------------------------------------
【我的问题】以上代码
---在IDLE中执行后返回错误提示如下:
Traceback (most recent call last):
File "D:\work\p16_5_pg_3_turtle.py", line 51, in <module>
location: tuple[int, int],
TypeError: 'type' object is not subscriptable
---尝试在pycharm中执行,返回错误提示如下:
Traceback (most recent call last):
File "D:/work/p16_5_pg_3_turtle.py", line 51, in <module>
location: tuple[int, int],
TypeError: 'type' object is not subscriptable
libpng warning: iCCP: known incorrect sRGB profile
两个结果相比,后者多了最后一行
但仍不知道哪里错了?是不是因为python版本的问题?
******************************
感谢大神不吝赐教,为新手解疑释惑。
赠人玫瑰,手有余香,好人一生平安! |
|