pygame всплывающее сообщение
я делаю кнопки, и всплывающие сообщения необходимы, для обьяснения игроку что он натворил. но всплытие происходит только от последней кнопки. я хотел бы чтоб он всплывал с любых кнопок.Я анимацию не могу поставить на всплытие сообщений, чтобы он после нажатия на кнопку - медленно исчезал. Надеюсь на вашу профессиональность и благородство, и искреннее спасибо тем, кто не поскупился временем помочь столь бедствующему заблудшего
import pygame
# === CONSTANTS === (UPPER_CASE names)
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
HIDE = (0,0,0)
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400
# === CLASSES === (CamelCase names)
class Button():
def __init__(self, text, x = 0, y=0, width = 100, height = 50, command = None):
self.text = text
self.command = command
self.image_normal = pygame.Surface((width, height))
self.image_normal.fill(GREEN)
self.image_hovered = pygame.Surface((width, height))
self.image_hovered.fill(RED)
self.image_hide = pygame.Surface((width, height))
self.image_hide.fill(HIDE)
self.image = self.image_normal
self.image_hided = self.image_hide
self.rect = self.image.get_rect()
self.rect_hided = self.image_hided.get_rect()
print(self.rect)
font = pygame.font.Font('freesansbold.ttf', 15)
text_image = font.render(text, True, WHITE)
text_hide = font.render('i was hided', True, HIDE)
text_rect = text_image.get_rect(center = self.rect.center)
self.image_normal.blit(text_image, text_rect)
self.image_hovered.blit(text_image, text_rect)
self.image_hide.blit(text_hide, text_rect)
self.rect.topleft = (x,y)
self.hovered = False
self.active = False
def update(self):
if self.hovered:
self.image = self.image_hovered
else:
self.image = self.image_normal
if self.active:
self.image_hided = self.image_normal
def draw(self, Surface):
Surface.blit(self.image, self.rect)
Surface.blit(self.image_hided, self.rect_hided)
def handle_event(self, event):
if event.type == pygame.MOUSEMOTION:
self.hovered = self.rect.collidepoint(event.pos)
elif event.type == pygame.MOUSEBUTTONDOWN:
print(self.text)
if self.hovered:
if self.rect.collidepoint(event.pos):
self.active = True
print('clicked by ',self.text)
#active = True
if self.command:
command()
def main():
#active = False
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen_rect = screen.get_rect()
print(screen_rect)
clock = pygame.time.Clock()
run = False
btn1 = Button('hello', 200, 50, 100, 50)
btn2 = Button('bye', 200, 150, 100, 50)
print(btn1)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
if event.type == pygame.K_ESCAPE:
run = False
btn1.handle_event(event)
btn2.handle_event(event)
btn1.update()
btn2.update()
screen.fill(BLACK)
btn1.draw(screen)
btn2.draw(screen)
# if active:
# bt3 = Button('clicked!!', 200, 200,300,50)
# btn3.update()
# btn3.draw(screen)
pygame.display.update()
clock.tick(25)
pygame.quit()
if __name__ == '__main__':
main()