def colour(): switcher = { 0: 0x2FD596,1: 0x2FC3D5,2: 0x2F6BD5,3: 0x432FD5,4: 0x702FD5,5: 0xBC2FD5,6: 0xD52F91,7: 0xD52F43,8: 0xD57F2F,9: 0xD5D52F,10: 0x64D52F,11: 0x2FD557,} return switcher.get(round((datetime.datetime.Now() - starting_time).total_seconds()%11))
但这在颜色和看起来笨重之间有很大的进步.
解决方法 关键是要简单计算每一步改变每个通道(a,r,g和b)的程度. Pygame的color类非常方便,因为它允许在每个通道上进行迭代,并且它的输入很灵活,所以你可以改变它.在下面的例子中,’blue’到0x2FD596,它仍然会运行.这是一个简单的运行示例:
import pygameimport itertoolspygame.init()screen = pygame.display.set_mode((800,600))colors = itertools.cycle(['green','blue','purple','pink','red','orange'])clock = pygame.time.Clock()base_color = next(colors)next_color = next(colors)current_color = base_colorFPS = 60change_every_x_seconds = 3.number_of_steps = change_every_x_seconds * FPSstep = 1Font = pygame.Font.SysFont('Arial',50)running = Truewhile running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False text = Font.render('fading {a} to {b}'.format(a=base_color,b=next_color),True,pygame.color.color('black')) step += 1 if step < number_of_steps: # (y-x)/number_of_steps calculates the amount of change per step required to # fade one channel of the old color to the new color # We multiply it with the current step counter current_color = [x + (((y-x)/number_of_steps)*step) for x,y in zip(pygame.color.color(base_color),pygame.color.color(next_color))] else: step = 1 base_color = next_color next_color = next(colors) screen.fill(pygame.color.color('white')) pygame.draw.circle(screen,current_color,screen.get_rect().center,100) screen.blit(text,(230,100)) pygame.display.update() clock.tick(FPS)
如果您不想依赖于帧速率而是使用基于时间的方法,则可以将代码更改为:
...change_every_x_milliseconds = 3000.step = 0running = Truewhile running: ... if step < change_every_x_milliseconds: current_color = [x + (((y-x)/change_every_x_milliseconds)*step) for x,pygame.color.color(next_color))] else: ... ... pygame.display.update() step += clock.tick(60)@H_403_37@ 总结
以上是内存溢出为你收集整理的python – 如何在pygame中从一种颜色淡化到另一种颜色?全部内容,希望文章能够帮你解决python – 如何在pygame中从一种颜色淡化到另一种颜色?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)