如何使对象在 pygame 中褪色 [英] How to make an object fades color in pygame
问题描述
我想要一个矩形作为生命条从绿色渐变为红色,但在互联网上找不到简单的方法.
I want a rectangle to fade from green to red for a life-bar, but couldn't find easy way to do it on internet.
我是这样试的(只是为了从红色变成黄色):
I tried like this (just for going from red to yellow):
colorR = 0
colorG = 255
colorB = 0
change = True
while change:
pygame.draw.rect(win, (colorR, colorG, colorB), (10, 10, 200, 50), 0)
colorR += 1
if colorR == 255:
print("done")
我也尝试使用 for i in range(0, 255):
它说 pygame.draw.rect(win, (colorR, colorG, colorB), (10, 10, 200, 50), 0)类型错误:无效的颜色参数
但是当我像这样绘制矩形时:
But when I just draw the rectangle like this :
colorR = 0
colorG = 255
colorB = 0
pygame.draw.rect(win, (colorR, colorG, colorB), (10, 10, 200, 50), 0)
矩形显示为绿色.
我的方法是否正确,或者我有.以完全不同的方式做到这一点?谢谢解答
Is my method correct, or do I have. to do it in a complete other way ? Thanks for the answer
这是整个程序,其中可能存在错误,因为矩形在获得最终颜色之前没有出现
edit : here's the whole program, there are maybe mistakes in there because the rectangle doesn't appear before it gets its final color
import pygame, math
pygame.init()
length = 1440
heigth = 700
win = pygame.display.set_mode((length, heigth))#, pygame.FULLSCREEN)
menuImg = pygame.image.load('menu_img.jpg')
laser = pygame.image.load('projectile.png')
clock = pygame.time.Clock()
game = False
def rgw():
win.fill((0, 0, 255))
if game:
print("game")
colorR = 0
colorG = 255
colorB = 0
change = True
while change:
pygame.draw.rect(win, (colorR, colorG, colorB), (10, 10, 200, 50), 0)
colorR += 1
if colorR == 255:
change = False
pygame.display.flip()
pygame.display.update()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
game = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_BACKSPACE:
run = False
rgw()
pygame.quit()
推荐答案
要更改显示表面可见,您必须调用 pygame.display.flip()
或 pygame.display.update()
.此外,我建议通过 pygame.event.get()
或 pygame.event.pump()
处理事件.
请注意,您必须在循环中进行延迟,否则颜色会迅速褪色.如果颜色不在 [0, 255] 范围内,您将收到无效颜色参数"错误.发生这种情况是因为您示例中的循环永远不会终止.你错过了change = False
.
To make changes to the display surface visible you have to call pygame.display.flip()
or pygame.display.update()
. Furthermore I recommend to handle the events by either pygame.event.get()
or pygame.event.pump()
.
Note you have to do a delay in the loop, otherwise the color will fade rapidly. If the color is not in range [0, 255] you'll get "invalid color argument" error. That happens because the loop in your example never terminates. You missed change = False
.
无论如何,由于您有一个应用程序循环,因此无需在单独的循环中执行淡入淡出.使用主应用程序循环:
anyway, since you have a application loop, there is no need to do the fade in a separate loop. Use the main application loop:
game = True
colorR = 0
colorG = 255
colorB = 0
def rgw():
global colorR, colorG, colorB
win.fill((0, 0, 255))
if game:
pygame.draw.rect(win, (colorR, colorG, colorB), (10, 10, 200, 50), 0)
if colorR < 255:
colorR += 1
pygame.display.update()
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
game = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_BACKSPACE:
run = False
rgw()
pygame.quit()
这篇关于如何使对象在 pygame 中褪色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!