北京理工大学-Python游戏开发入门学习笔记10

pygame.Color 色彩表达

Color类用于表达色彩,使用RGB或RGBA色彩模式,A可选

Color类可以用于色彩名字、RGBA值、HTML色彩格式等方式定义

Color(name) 例如:Color(“gery”)

Color(r,g,b,a) 例如: Color(190,190,190,255)

a为alpha通道,alpha通道表示不透明度,取值0-255,默认255
alpha通道值越大,不透明度越高,255表示不透明

pygame. Color.r 获得Color类的红色值r
pygame. Color.g 获得Color类的绿色值g
pygame. Color.b 获得Color类的蓝色值b
pygame. Color.a 获得Color类的alpha值a
pygame. Color.normalize 将RGBA各通道值归一到0-1之间

Color(rgbvalue) 例如: Color("#BEBEBEFF")

色彩型壁球

#RGB颜色定义
# R:壁球水平距离/窗体宽度,取值0-255
# G:壁球垂直距离/窗体高度,取值0-255
# B:壁球水平和垂直速度差别,最小速度/最大速度,取值0-255
import pygame,sys

pygame.init()
size=width,height=600,400
speed=[1,1]    #设置速度
black=0,0,0    #设置刷新的颜色
screen=pygame.display.set_mode(size,pygame.RESIZABLE)   #需要改变相应的图片大小和区域
pygame.display.set_caption("壁球操作型")
ball=pygame.image.load(r'D:\Python\Workspace\113741.jpg')  #载入图片,绝对路径前加r
ballrect=ball.get_rect()
icon=pygame.image.load(r"D:\Python\Workspace\360wallpaper.jpg")  #设置图标
pygame.display.set_icon(icon)
fps=300           #设置刷新帧的次数
fclock=pygame.time.Clock()    #创建一个Clock对象,用于操作时间
still=False     # 控制小球移动的变量
bgcolor=pygame.Color("black")
# 初始化部分完成

def RGBchannel(a):   #设置RGB的值,为整数
    return 0 if a<0 else (255 if a>255 else int(a))
while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()
        elif event.type==pygame.KEYDOWN:    #事件类型为键盘输入
            if event.key==pygame.K_UP:
                speed[1]=speed[1]+1 if speed[1]>0 else speed[1]-1
                 # 语句1(条件成立) if 条件 else 语句2(条件不成立)
            elif event.key==pygame.K_DOWN:
                speed[1] = speed[1] if speed[1]==0 else (abs(speed[1]) - 1) * int(speed[1] / abs(speed[1]))
            elif event.key==pygame.K_LEFT:
                speed[0] = speed[0]  if speed[0]==0 else (abs(speed[0]) - 1) * int(speed[0] / abs(speed[0]))
            elif event.key==pygame.K_RIGHT:
                speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
            elif event.key==pygame.K_ESCAPE:     #对应于退出键的操作
                sys.exit()
        elif event.type==pygame.VIDEORESIZE:
            size=width,height=event.w,event.h
            screen=pygame.display.set_mode(size,pygame.RESIZABLE)
        elif event.type==pygame.MOUSEBUTTONDOWN:
            if event.button==1:
                still=True
        elif event.type==pygame.MOUSEBUTTONUP:
            still=False
            if event.button==1:
                ballrect=ballrect.move(event.pos[0]-ballrect.left,event.pos[1]-ballrect.top)
                #当按键释放时,壁球移动到鼠标的位置,并继续移动;move移动相对距离,所以需要做一个减法运算
        elif event.type==pygame.MOUSEMOTION:
            if event.buttons[0]==1:
                ballrect=ballrect.move(event.pos[0]-ballrect.left,event.pos[1]-ballrect.top)
    if pygame.display.get_active() and not still:
        ballrect=ballrect.move(speed[0],speed[1])  #壁球的移动
    if ballrect.left<0 or ballrect.right>width:   #反弹设置
        speed[0]=-speed[0]
        if ballrect.right>width and ballrect.right+speed[0]>ballrect.right:    #考虑到鼠标将壁球移动到边缘不反弹的情况
            speed[0]=-speed[0]
    if ballrect.top<0 or ballrect.bottom>height:
        speed[1]=-speed[1]
        if ballrect.bottom > height and ballrect.bottom + speed[1] > ballrect.bottom:
            speed[1] = -speed[1]
        #事件完成

    bgcolor.r=RGBchannel(ballrect.left/width*255)
    bgcolor.g=RGBchannel(ballrect.top/height*255)
    bgcolor.b=RGBchannel(min(speed[0],speed[1])/max(speed[0],speed[1],1)*255)  #最大速度不能为0
    screen.fill(bgcolor)
    screen.blit(ball,ballrect)   #壁球绘制在对应矩形上
    pygame.display.update()     #完成刷新
    fclock.tick(fps)   #控制帧速度,即窗口刷新速度

ps:学习链接 https://www.icourse163.org/course/BIT-1001873001

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值