Python壁球小游戏(4):pygame色彩机制

pygame使用pygame.Color类表达色彩。Color类使用RGB或者RGBA色彩模式,A可选。Color类可以使用色彩名字、RGBA值、HTML色彩格式等方式定义。比如对于灰色,我们可以这样表示:

Color(name)    例如Color("grey")

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

Color(rgbvalue)  例如Color("#BEBEBEFF")  表示RGB色彩模式的十六进制格式

一些常用颜色的RGB值和名称:

 

RGBA色彩模式是在RGB色彩模式之外的一种扩展,增加了第四个参数:alpha通道,表示不透明度,取值0-255,默认255。alpha通道值越大,不透明度越高,255表示不透明

下面就可以根据这个知识再优化一下之前的壁球小游戏:根据壁球的移动修改游戏背景的颜色。那么如何实现呢:

我们让壁球与窗体左侧的距离除以窗体宽度再将其扩大一下让其取值范围在0-255之间,并且变成一个整数,让其表示红色值;

壁球与窗体顶端的距离除以窗体高度再将其扩大一下让其取值范围在0-255之间,并让其变成一个整数,让其表示绿色值;

壁球最小速度除以最大速度并且扩大到0-255之间,让其表示蓝色值。

具体实现思路:先增加壁球背景的通道值。

bgcolor.r = RGBChannel(ballrect.left*255/width)
bgcolor.g = RGBChannel(ballrect.top*255/height)
bgcolor.b = RGBChannel(min(speed[0],speed[1])*255/max(speed[0],speed[1],1))

其中的RGBChannel()是一个用户自定义函数,它将输入的参数映射成0-255之间的整数。

接下来就是完成这个用户自定义函数:

def RGBChannel(a):
    return 0 if a<0 else (255 if a>255 else int(a))

然后就可以实现让壁球在运动的过程中背景颜色不断的变换啦!下面是完整的代码(在之前版本的基础之上完善的代码):

import sys
import pygame

pygame.init()
#icon = pygame.image.load("biu.jpg")
#pygame.display.set_icon(icon)          #设置窗口图标
#Vinfo = pygame.display.Info()       #获取当前操作系统的屏幕尺寸
#size = width, height = Vinfo.current_w, Vinfo.current_h     #将系统的屏幕尺寸作为显示窗口的尺寸
size = width,height = 900 ,600               #设置屏幕的长度和宽度
speed = [1,1]                                #设置移动速度[x,y]
BLACK = 0,0,0                                #设置背景颜色
#screen = pygame.display.set_mode(size)       #初始化显示窗口
screen = pygame.display.set_mode(size,pygame.RESIZABLE)       #将屏幕设置为大小可调
#screen = pygame.display.set_mode(size,pygame.NOFRAME)         #将屏幕设置为无边框
#screen = pygame.display.set_mode(size,pygame.FULLSCREEN)       #将屏幕设置为全屏模式
pygame.display.set_caption("壁球游戏")       #将窗口命名为“壁球游戏”
ball = pygame.image.load("biu.jpg")          #加载图片
ballrect = ball.get_rect()                   #返回一个覆盖图像的矩形Rect对象,载入图像的外切矩形
"""Rect对象有一些重要的属性,比如top、bottom、left、right表示上下左右
width、height表示宽度和高度"""
fps = 300
fclock = pygame.time.Clock()
still = False
bgcolor = pygame.Color("black")


def RGBChannel(a):
    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_LEFT:
                speed[0] = -speed[0]
            #按上键改变壁球上下移动方向,按一次方向取反
            elif event.key == pygame.K_UP:
                speed[1] = -speed[1]
            elif event.key == pygame.K_ESCAPE:
                sys.exit("游戏结束!")
        elif event.type == pygame.VIDEORESIZE:
            size = width, height = event.size[0], event.size[1]
            screen = pygame.display.set_mode(size,pygame.RESIZABLE)
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                still = True
                """当鼠标左键按下时,将控制壁球移动的变量设置为True,
                让壁球停止移动,因为后边是用if pygame.display.get_active() and not still:
                来控制壁球移动的,将still取反了"""
        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])     #在横轴和纵轴移动x,y像素
        """ 设置窗口感知,当显示为界面的时候,返回True,图标化的时候返回False
        这样设置如果将窗口最小化,壁球会停止运动
        当打开的时候壁球将会继续运动"""
    #判断图像的边缘位置,如果碰到上下左右边缘,使其速度取反
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
        #sys.exit("Game Over!")
        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]
        #sys.exit("Game Over")
        if ballrect.bottom > height and ballrect.bottom + speed[1] > ballrect.bottom:
            speed[1] = - speed[1]


    bgcolor.r = RGBChannel(ballrect.left*255/width)
    bgcolor.g = RGBChannel(ballrect.top*255/height)
    bgcolor.b = RGBChannel(min(speed[0],speed[1])*255/max(speed[0],speed[1],1))

    screen.fill(bgcolor)          #填充背景颜色
    screen.blit(ball,ballrect)
    """将一个图像绘制在另一个图像上,即将ball绘制到ballrect位置上。
    通过Rect对象引导对移动图像的绘制,让图像跟着外切矩形的移动而移动
    这样就形成了图像的移动"""
    pygame.display.update()     #刷新屏幕,仅仅重新绘制窗口中有变化的地方,比flip函数速度更快,flip函数重新绘制整个窗口
    fclock.tick(fps)



 

发布了61 篇原创文章 · 获赞 35 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_41575507/article/details/96836998