📜  如何使用pygame添加色彩微风效果?

📅  最后修改于: 2022-05-13 01:55:37.578000             🧑  作者: Mango

如何使用pygame添加色彩微风效果?

Pygame是一个Python库,可专门用于设计和构建游戏。 Pygame 仅支持使用不同精灵构建的 2d 游戏。 Pygame 并不是特别适合设计游戏,因为它使用起来非常复杂,并且没有像 unity 这样的适当 GUI,但它确实为更复杂的项目构建了逻辑。

安装:

在初始化 pygame 库之前,我们需要安装它。要安装它,请在终端中键入以下命令。

pip install pygame

彩色微风效果:

Pygame 包含以包含三个值的元组格式的颜色编码,这些值表示三种核心颜色的强度,即红色、蓝色、绿色。可以更改各个颜色的值以制作另一种独特的颜色。由于元组的值在运行时也是可变的,它使我们可以灵活地添加一些颜色效果,使我们的游戏/应用程序更加独特和美观。

颜色效果之一是微风效果,微风效果是颜色从一种色调平滑地变化到另一种色调而没有突然或突然变化的效果。这些效果可以在 RGB 键盘和鼠标中看到。

例子:

Python3
import pygame
import random
import sys
 
 
# initializing the constructor
pygame.init()
 
# setting up variable screen
screen = pygame.display.set_mode((720,720))
 
# three arguments of the color tuple
c1 = random.randint(0,255)
c2 = random.randint(0,255)
c3 = random.randint(0,255)
 
# setting up variable clock
clock = pygame.time.Clock()
 
while True:
    for ev in pygame.event.get():
        if ev.type == pygame.QUIT:
            pygame.quit()
             
    # increases the shade of
    # the current color
    if 0 < c1 < 255:
        c1 += 1
         
    # if value of c1 exceeds
    # 255 it resets it to 0
    elif c1 >= 255:
        c1 -= 255
         
    # if value of c1 precedes 0
    # it is incremented by 3
    elif c1 <= 0:
        c1 += 3
         
    # sets game fps to 60
    clock.tick(60)
     
    # sets bg color to tuple
    # (c1,c2,c3)
    screen.fill((c1,c2,c3))
     
    # updates the frames of
    # the game
    pygame.display.update()


输出: