|
20鱼币
这是我根据小甲鱼的需求和代码,在自己理解的基础上写的。这里面有一截没看懂。
- '''
- 综合练习
- 改变小乌龟的速度,大小,透明度
- 改变窗口的大小
- '''
- import pygame
- from pygame.locals import *
- import sys
- '''
- 目标元素
- 在pygame中显示的元素, 定义其各种参数和动作
- '''
- class Target:
- def __init__(self, image_path, direction=1,speed=[1,1], transparent=255, ratio=1.0):
- # 加载后的图片
- image = pygame.image.load( image_path )
- self.image = image
- self.oimage = image
- # 方向, 1右0左
- self.direction = direction
- # 位置
- self.oposition = image.get_rect()
- self.position = image.get_rect()
- # 速度, [x,y]
- self.speed = list(speed)
- # 透明度, 0-255, 越来越不透明
- self.transparent = transparent
- # 放大缩小比例
- self.ratio = ratio
- '''
- 移动位置, 可以外部给定速度
- speed: [speed_x, speed_y]
- '''
- def move(self, speed=None):
- if speed==None:
- speed = self.speed
- self.position = self.position.move( speed )
- self.correct()
- '''
- 纠正位置
- 可能在各种变化后, 元素的位置不在限定范围了
- limit: [limit_x, limit_y]
- '''
- def correct(self, limit=None):
- if limit == None:
- t = pygame.display.get_surface().get_rect()
- limit = t.width, t.height
- p = self.position
- if p.left < 0:
- p.left = 0
- self.speed[0] *= -1
- self.image = pygame.transform.flip(self.image,True, False)
- self.direction = 1
- elif p.right > limit[0]:
- p.right = limit[0]
- self.speed[0] *= -1
- self.image = pygame.transform.flip(self.image,True, False)
- self.direction = 0
- elif p.top < 0:
- p.top = 0
- self.speed[1] *= -1
- elif p.bottom > limit[1]:
- p.bottom = limit[1]
- self.speed[1] *= -1
- '''
- 变化大小
- '''
- def change_ratio(self, d_ratio):
- if d_ratio == 0:
- return
- ratio = self.ratio + d_ratio
- if ratio < 0.5 or ratio > 2:
- return
- self.ratio = ratio
- op = self.oposition
- image = pygame.transform.smoothscale(self.oimage,(int(op.width*ratio),int(op.height*ratio)) )
- if self.direction == 0:
- image = pygame.transform.flip(image,True, False)
- self.image = image
- p = image.get_rect()
- self.position.width, self.position.height = p.width, p.height
-
- '''
- 改变速度
- d_speed: [d_speed_x, d_speed_y]
- '''
- def change_speed(self, d_speed):
- if isinstance(d_speed, int):
- d_speed = (d_speed, d_speed)
- t = self.speed[0]
- self.speed[0] += d_speed[0]
- self.speed[1] += d_speed[1]
- if t * self.speed[0] < 0 \
- or (t==0 and self.speed[0]<0 and self.direction==1) \
- or (t==0 and self.speed[0]>0 and self.direction==0):
- self.image = pygame.transform.flip(self.image,True, False)
- self.direction = 1 - self.direction
-
- '''
- 改变透明度
- '''
- def change_transparent(self, d_transparent):
- t = self.transparent + d_transparent
- if t < 0:
- t = 0
- elif t > 255:
- t = 255
- self.transparent = t
-
- # 初始化 Pygame
- pygame.init()
- pygame.mixer.init()
- pygame.mixer.music.load('刘珂矣 - 半壶纱.mp3')
- pygame.mixer.music.set_volume(0.2)
- pygame.mixer.music.play(-1)
- # 窗口大小
- sizes = pygame.display.list_modes()
- size_i = len(sizes) - 1
- # 全屏标志
- fullscreen = False
- # 加载元素
- turtle = Target('turtle.png')
- # 加载背景图片
- obg = pygame.image.load( 'background.jpg' )
- bg = obg
- # 时钟
- clock = pygame.time.Clock()
- # 创建指定大小的窗口
- screen = pygame.display.set_mode(sizes[size_i])
- # 设置窗口标题
- pygame.display.set_caption("综合练习")
- # 通过按键键值获取按键代码
- def fun(key, info):
- try:
- d = fun.love
- # print('已有 love')
- return d[key]
- except AttributeError:
- print('没有 love, 创建')
- fun.love = dict()
- except KeyError:
- print('键 %d 不存在' % key)
- for x in info:
- if eval(x) == key:
- fun.love[key] = x
- return x
- print('没有这样的 key ?:',key)
-
- info = dir()
- def blit_alpha(target, source, location, opacity):
- x = location[0]
- y = 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 == pygame.QUIT:
- pygame.mixer.music.stop()
- pygame.display.quit()
- sys.exit()
-
- if event.type == KEYDOWN:
- print('key :', event.key, fun(event.key,info))
- # F11 全屏, ESC退出全屏
- if event.key == K_F11 or ( event.key == K_ESCAPE and fullscreen ):
- fullscreen = not fullscreen
- if fullscreen:
- screen = pygame.display.set_mode(sizes[0], FULLSCREEN | HWSURFACE)
- else:
- screen = pygame.display.set_mode(sizes[size_i])
-
- # (CTRL + X,S)放大缩小屏幕
- elif event.key in (K_x,K_s) and event.mod & KMOD_CTRL:
- d = 1 if event.key==K_s else -1
- size_i = ( size_i + d ) % len(sizes)
- if size_i == 0:
- fullscreen = True
- screen = pygame.display.set_mode(sizes[0], FULLSCREEN | HWSURFACE)
- else:
- fullscreen = False
- screen = pygame.display.set_mode(sizes[size_i])
- bg = pygame.transform.scale(obg, sizes[size_i])
-
- # (+,-,空格)放大缩小乌龟
- elif event.key in (K_KP_PLUS,K_EQUALS,K_KP_MINUS,K_MINUS,K_SPACE):
- if event.key in (K_KP_PLUS,K_EQUALS):
- turtle.change_ratio( 0.1 )
- elif event.key in (K_KP_MINUS,K_MINUS):
- turtle.change_ratio( -0.1 )
- else:
- turtle.change_ratio( 1.0 - turtle.ratio )
-
- # (方向键)乌龟速度控制
- elif event.key in (K_UP,K_DOWN,K_LEFT,K_RIGHT):
- dx = 1 if event.key == K_RIGHT else ( -1 if event.key == K_LEFT else 0)
- dy = 1 if event.key == K_DOWN else ( -1 if event.key == K_UP else 0)
- turtle.change_speed( (dx, dy) )
- elif event.type == MOUSEBUTTONDOWN:
- print('button :', event.button)
- # (鼠标滑轮)乌龟透明度控制
- if event.button in (4,5):
- turtle.change_transparent( 5 if event.button == 4 else -5)
-
-
- turtle.move()
-
- # 填充背景
- screen.blit(bg, (0,0) )
- # 更新图像
- blit_alpha(screen, turtle.image, turtle.position, turtle.transparent)
- # 更新界面
- pygame.display.flip()
- # 延迟 10 毫秒
- # pygame.time.delay(10)
-
- # 帧
- clock.tick(30)
复制代码
这段代码的功能按键有些和小甲鱼的不一样,这不是重点。重点是:这段代码我没有理解到:
- def blit_alpha(target, source, location, opacity):
- x = location[0]
- y = 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)
复制代码
知道他的功能是实现透明度控制,但是为啥是 (-x, -y ),期待大神对这段代码的讲解。。
-x,-y是以temp为参照的位置,对于screen就是0,0
相当于在temp区域重画了这部分的背景,并在temp的0,0重画了甲鱼
将temp这个surface对象一起设置透明度
|
最佳答案
查看完整内容
-x,-y是以temp为参照的位置,对于screen就是0,0
相当于在temp区域重画了这部分的背景,并在temp的0,0重画了甲鱼
将temp这个surface对象一起设置透明度
|