问题描述
我是一个初学者,我在做一个需要背景褪色的游戏。我编写的程序会使背景褪色,但是并不能保持其刚刚褪色的颜色,因为它会跳回到一段时间后的颜色。我希望它也可以在特定时间使其褪色,例如,我希望程序在游戏进入20,000毫秒时就褪色,而不是按MOUSEBUTTONDOWN键,这就是为什么我写了'print(pygame。 time.get_ticks()),但我在使用它时遇到麻烦,如果还有其他方法,请对其进行评论以供我参考。谢谢!
import pygame
import time
pygame.init()
disPLAY_HEIGHT = 700
disPLAY_WIDTH = 900
screen = pygame.display.set_mode((disPLAY_WIDTH,disPLAY_HEIGHT))
clock = pygame.time.Clock()
FPS = 60
#colors
WHITE = (255,255,255)
BLACK = (0,0)
SKY_BLUE = (102,178,255)
def fade(width,height):
fade = pygame.Surface((width,height))
fade.fill((31,97,141))
for alpha in range(0,300):
fade.set_alpha(alpha)
screen.blit(fade,(0,0))
pygame.display.update()
print(pygame.time.get_ticks())
screen.fill((31,141))
running = True
while running:
screen.fill(SKY_BLUE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
fade(disPLAY_WIDTH,disPLAY_HEIGHT)
clock.tick(FPS)
pygame.display.update()
解决方法
要淡化背景并保持背景,您需要创建一个可以通过淡入功能和主循环访问的共享表面。在主体上,检查计数器并运行淡入淡出过程。这将暂停主循环的淡入淡出。淡入淡出完成后,检查淡入淡出表面变量并将其变薄(如果已设置)。
这是更新的代码:
fade = None # before fade process
def dofade(width,height):
global fade # share variable with main loop
fade = pygame.Surface((DISPLAY_WIDTH,DISPLAY_HEIGHT))
fade.fill((31,97,141))
for alpha in range(0,300):
fade.set_alpha(alpha)
screen.blit(fade,(0,0))
pygame.display.update()
#print(pygame.time.get_ticks())
clock.tick(FPS)
#screen.fill((31,141))
running = True
while running:
screen.fill(SKY_BLUE) # initial color
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN: # can start fade early
dofade(DISPLAY_WIDTH,DISPLAY_HEIGHT)
tks = pygame.time.get_ticks()
if (tks >= 20000 and not fade): # if hit counter and fade not processed yet
dofade(DISPLAY_WIDTH,DISPLAY_HEIGHT) # loop paused for fade process
if fade: # if fade process done
screen.blit(fade,0)) # always show fade surface
clock.tick(FPS)
pygame.display.update()
,
只需将新颜色分配给变量SKY_BLUE
,它将一直使用它
def fade(width,height):
global SKY_BLUE
# ... your code ...
print('after fade')
SKY_BLUE = (31,141)
我的版本有其他更改
import pygame
# --- constants ---
DISPLAY_HEIGHT = 700
DISPLAY_WIDTH = 900
WHITE = (255,255,255)
BLACK = (0,0)
SKY_BLUE = (102,178,255)
FPS = 60
# --- functions ---
def fade(width,height):
global SKY_BLUE
fade = pygame.Surface((width,height))
fade.fill((31,141))
for alpha in range(0,256):
fade.set_alpha(alpha)
screen.blit(fade,0))
clock.tick(FPS)
pygame.display.update()
print('after fade')
SKY_BLUE = (31,141)
# --- main ---
pygame.init()
screen = pygame.display.set_mode((DISPLAY_WIDTH,DISPLAY_HEIGHT))
clock = pygame.time.Clock()
running = True
while running:
screen.fill(SKY_BLUE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
fade(DISPLAY_WIDTH,DISPLAY_HEIGHT)
clock.tick(FPS)
pygame.display.update()
# - end -
pygame.quit()
您也可以使用表面。
首次使用颜色为SKY_BLUE
的表面
background = pygame.Surface((DISPLAY_WIDTH,DISPLAY_HEIGHT))
background.fill(SKY_BLUE)
running = True
while running:
screen.blit(background,0))
并在褪色后将其替换
def fade(width,height):
global background
# ... code ...
print('after fade')
background = fade
顺便说一句:在这个地方,最好对表面fade
和函数fade()
使用不同的名称,因为这可能会误导我。
import pygame
# --- constants ---
DISPLAY_HEIGHT = 700
DISPLAY_WIDTH = 900
WHITE = (255,height):
global background
surface = pygame.Surface((width,height))
surface.fill((31,256):
surface.set_alpha(alpha)
screen.blit(surface,0))
clock.tick(FPS)
pygame.display.update()
print('after fade')
background = surface
# --- main ---
pygame.init()
screen = pygame.display.set_mode((DISPLAY_WIDTH,DISPLAY_HEIGHT))
clock = pygame.time.Clock()
background = pygame.Surface((DISPLAY_WIDTH,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
fade(DISPLAY_WIDTH,DISPLAY_HEIGHT)
clock.tick(FPS)
pygame.display.update()
# - end -
pygame.quit()
对于2万毫秒后的衰落,您可以使用pygame.time.get_ticks()
获取当前时间并计算何时开始衰落(start_fading
),然后在循环中再次获取当前时间并与{{1}比较}开始褪色。
它可能需要布尔变量(start_fading
)才能在第一次淡入淡出之后不再执行。
True/False
如果要重复衰落,请不要使用current_time = pygame.time.get_ticks()
start_fading = current_time + 20000
faded = False
running = True
while running:
screen.blit(background,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#elif event.type == pygame.MOUSEBUTTONDOWN:
# fade(DISPLAY_WIDTH,DISPLAY_HEIGHT)
current_time = pygame.time.get_ticks()
if not faded and current_time >= start_fading:
fade(DISPLAY_WIDTH,DISPLAY_HEIGHT)
faded = True
clock.tick(FPS)
pygame.display.update()
,而要在衰落后使用faded
start_fading
如果希望更多的元素延迟运行,则可以将它们保留在列表中,并使用 if current_time >= start_fading:
fade(DISPLAY_WIDTH,DISPLAY_HEIGHT)
start_fading = current_time + 20000
循环,并在运行某些功能后将其从此列表中删除。但是我不会在这里使用它。
for
还有另一种方法-您可以将pygame.time.set_timer()与import pygame
# --- constants ---
DISPLAY_HEIGHT = 700
DISPLAY_WIDTH = 900
WHITE = (255,DISPLAY_HEIGHT))
background.fill(SKY_BLUE)
current_time = pygame.time.get_ticks()
start_fading = current_time + 20000
faded = False
running = True
while running:
screen.blit(background,DISPLAY_HEIGHT)
faded = True
clock.tick(FPS)
pygame.display.update()
# - end -
pygame.quit()
结合使用,以生成自定义事件,并且会有所延迟
pygame.USEREVENT
然后您可以使用FADE_EVENT = pygame.USEREVENT
pygame.time.set_timer(FADE_EVENT,2000)
来捕获它并执行for event in pygame.event.get():
fade()
if event.type == FADE_EVENT:
pygame.time.set_timer(FADE_EVENT,0) # turn off after one use
fade(DISPLAY_WIDTH,DISPLAY_HEIGHT)