我希望图像不淡入而是消失 pygame
I want the image to not fade in but disappear in pygame
import pygame, pygame_menu, sys, random
#Main Window
screen_width = 1280
screen_height = 720
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Classics")
#Variables
bg = pygame.image.load("bg.png")
pong_btn_img = pygame.image.load("Button2.png")
pong_btn_img_p = pygame.image.load("pongpressed.png")
default_img_size = (300, 195)
pong_btn_img = pygame.transform.scale(pong_btn_img, default_img_size)
pong_btn_img_p = pygame.transform.scale(pong_btn_img_p, default_img_size)
#General setup
pygame.init()
clock = pygame.time.Clock()
#BUtton Class
class Button():
def __init__(self, x, y, image):
self.image = image
self.rect = self.image.get_rect()
self.rect.topleft = (x,y)
def draw(self):
# Get mouse pos
pos= pygame.mouse.get_pos()
#check mouse hover and click conditions
if self.rect.collidepoint(pos):
screen.blit(pong_btn_img_p, (self.rect.x, self.rect.y + 20))
if pygame.mouse.get_pressed()[0]:
exec(open("Game.py").read())
else:
#draw button on screen
screen.blit(self.image, (self.rect.x, self.rect.y))
# button instances
pong_btn = Button(525, 300, pong_btn_img)
while True:
#Handling Input
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
bg.set_alpha(20)
screen.blit(bg, (0,0))
pong_btn.draw()
#Updating the screen
pygame.display.flip()
clock.tick(60)
在函数 draw() 中,在 if else 语句中,我正在显示一个图像,该图像在鼠标悬停在图像上时显示,如果鼠标没有悬停在该图像上则显示不同的图像。当鼠标从不悬停变为悬停时,图像淡出和淡入。我希望它立即消失
[...] the image fades out and in. I want it to disappear immediately
只需删除与背景混合的 alpha。画一个坚实的背景。删除行:
bg.set_alpha(20)
如果你想让背景在一开始就淡入,你需要在一定时间后分别改变背景的alpha通道帧数。背景的 Alpha 通道在一定时间内必须为 20(例如:1 秒或 60 帧)。之后,背景必须是不透明的,alpha通道必须是255:
no_of_frames = 0
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
bg_alpha = 20 if no_of_frames < 60 else 255
bg.set_alpha(bg_alpha)
screen.blit(bg, (0,0))
pong_btn.draw()
pygame.display.flip()
clock.tick(60)
no_of_frames += 1
pygame.quit()
sys.exit()
import pygame, pygame_menu, sys, random
#Main Window
screen_width = 1280
screen_height = 720
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Classics")
#Variables
bg = pygame.image.load("bg.png")
pong_btn_img = pygame.image.load("Button2.png")
pong_btn_img_p = pygame.image.load("pongpressed.png")
default_img_size = (300, 195)
pong_btn_img = pygame.transform.scale(pong_btn_img, default_img_size)
pong_btn_img_p = pygame.transform.scale(pong_btn_img_p, default_img_size)
#General setup
pygame.init()
clock = pygame.time.Clock()
#BUtton Class
class Button():
def __init__(self, x, y, image):
self.image = image
self.rect = self.image.get_rect()
self.rect.topleft = (x,y)
def draw(self):
# Get mouse pos
pos= pygame.mouse.get_pos()
#check mouse hover and click conditions
if self.rect.collidepoint(pos):
screen.blit(pong_btn_img_p, (self.rect.x, self.rect.y + 20))
if pygame.mouse.get_pressed()[0]:
exec(open("Game.py").read())
else:
#draw button on screen
screen.blit(self.image, (self.rect.x, self.rect.y))
# button instances
pong_btn = Button(525, 300, pong_btn_img)
while True:
#Handling Input
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
bg.set_alpha(20)
screen.blit(bg, (0,0))
pong_btn.draw()
#Updating the screen
pygame.display.flip()
clock.tick(60)
在函数 draw() 中,在 if else 语句中,我正在显示一个图像,该图像在鼠标悬停在图像上时显示,如果鼠标没有悬停在该图像上则显示不同的图像。当鼠标从不悬停变为悬停时,图像淡出和淡入。我希望它立即消失
[...] the image fades out and in. I want it to disappear immediately
只需删除与背景混合的 alpha。画一个坚实的背景。删除行:
bg.set_alpha(20)
如果你想让背景在一开始就淡入,你需要在一定时间后分别改变背景的alpha通道帧数。背景的 Alpha 通道在一定时间内必须为 20(例如:1 秒或 60 帧)。之后,背景必须是不透明的,alpha通道必须是255:
no_of_frames = 0
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
bg_alpha = 20 if no_of_frames < 60 else 255
bg.set_alpha(bg_alpha)
screen.blit(bg, (0,0))
pong_btn.draw()
pygame.display.flip()
clock.tick(60)
no_of_frames += 1
pygame.quit()
sys.exit()