pygame--颜色变化

#_*_coding:utf-8_*_
import pygame
from pygame.locals   import *
from sys import exit

pygame.init()
screen=pygame.display.set_mode((640,480),0,32)

def create_scales(height):
    red_scale_surface=pygame.surface.Surface((640,height))
    green_scale_surface=pygame.surface.Surface((640,height))
    blue_scale_surface=pygame.surface.Surface((640,height))
    for x in range(640):
        c=int((x/640)*255)
        red=(c,0,0)
        green=(0,c,0)
        blue=(0,0,c)
        line_rect=Rect(x,0,1,height)
        pygame.draw.rect(red_scale_surface,red,line_rect)
        pygame.draw.rect(green_scale_surface,green,line_rect)
        pygame.draw.rect(blue_scale_surface,blue,line_rect)

    return red_scale_surface,green_scale_surface,blue_scale_surface
red_scale,green_scale,blue_scale=create_scales(80)
color=[127,127,127]

while True:
    for event in pygame.event.get():
        if event.type==QUIT:
            exit()
    screen.fill((0,0,0))
    #screen.blit(red_scale,(0,00))
    #screen.blit(green_scale,(0,80))
    #screen.blit(blue_scale,(0,160))
    #获取鼠标点击位置
    x,y=pygame.mouse.get_pos()
    if pygame.mouse.get_pressed()[0]:
        for component in range(3):
            if y > component*80 and y < (component+1)*80:
                color[component]=int((x/639.)*255)
        pygame.display.set_caption("PyGame Color Test-"+str(tuple(color)))
    for component in range(3):
        pos=(int((color[component]/255.)*639),component*80+40)
        pygame.draw.circle(screen,(255,255,255),pos,20)
    pygame.draw.rect(screen,tuple(color),(0,240,640,240))
    pygame.display.update()
View Code

通过拖动三个小球,形成不同颜色的混搭颜色。

转载于:https://www.cnblogs.com/superxuezhazha/p/7406405.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我来回答你的问题。 在 Pygame 中实现生命值/血条处理相对来说比较简单,我们可以使用矩形和颜色填充来实现。 首先,我们需要定义一个变量来表示生命值,可以是一个整数类型的变量。接着,我们需要在游戏界面中绘制一个矩形来表示血条,可以使用 Pygame 中的 draw.rect() 函数来实现。然后,我们可以根据生命值的变化来改变血条的长度,可以使用颜色填充来实现这一点。 下面是一个简单的示例代码: ```python import pygame # 初始化 Pygame pygame.init() # 设置游戏界面的大小和标题 screen_width, screen_height = 600, 400 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption('Life Bar Demo') # 定义生命值 life = 100 # 定义血条的位置和大小 bar_width, bar_height = 200, 20 bar_x, bar_y = (screen_width - bar_width) // 2, screen_height // 2 # 游戏主循环 while True: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # 绘制血条的背景 pygame.draw.rect(screen, (255, 0, 0), (bar_x, bar_y, bar_width, bar_height)) # 绘制血条的前景 bar_width_current = int(bar_width * life / 100) pygame.draw.rect(screen, (0, 255, 0), (bar_x, bar_y, bar_width_current, bar_height)) # 更新屏幕 pygame.display.update() ``` 在这个示例代码中,我们定义了一个生命值变量 `life`,并且设置了血条的位置和大小。在游戏主循环中,我们使用 `pygame.draw.rect()` 函数绘制了血条的背景和前景,前景的长度根据生命值的变化而改变。最后,我们使用 `pygame.display.update()` 函数更新了屏幕。 你可以根据自己的需求来修改血条的颜色、大小和位置等参数。希望这个示例代码能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值