import pygame from pygame.locals import * pygame.init() SCREEN_WIDTH = 500 SCREEN_HEIGHT = 400 BUTTON_COLOR = (70, 130, 180) BUTTON_HOVER_COLOR = (100, 149, 237) BACKGROUND_COLOR = (240, 248, 255) TEXT_COLOR = (255, 255, 255) window = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('Button Game') # set up the text font = pygame.font.SysFont(None, 60) text = font.render('Click Me!', True, TEXT_COLOR) textRect = text.get_rect(center=window.get_rect().center) # set up the button around text PADDING = 40 button = Rect(textRect.x - PADDING, textRect.y - PADDING, textRect.width + (PADDING * 2), textRect.height + (PADDING * 2)) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1 and button.collidepoint(event.pos): # left mouse button over the button print('Clicked') mousePoint = pygame.mouse.get_pos() if button.collidepoint(mousePoint): color = BUTTON_HOVER_COLOR else: color = BUTTON_COLOR window.fill(BACKGROUND_COLOR) pygame.draw.rect(window, color, button) window.blit(text, textRect) pygame.display.update() pygame.quit()