import pygame import sys # Initialize Pygame pygame.init() # Set up the screen screen_width = 400 screen_height = 300 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Pygame Button Test") # Define colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) GRAY = (200, 200, 200) # Define button parameters button_width = 100 button_height = 50 button_x = (screen_width - button_width) // 2 button_y = (screen_height - button_height) // 2 # Function to draw text on the button def draw_text(text, font, color, surface, x, y): text_obj = font.render(text, True, color) text_rect = text_obj.get_rect() text_rect.center = (x, y) surface.blit(text_obj, text_rect) # Main game loop def main(): running = True while running: screen.fill(WHITE) # Check for events for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() if button_x <= mouse_pos[0] <= button_x + button_width and \ button_y <= mouse_pos[1] <= button_y + button_height: print("test") # Draw the button pygame.draw.rect(screen, GRAY, (button_x, button_y, button_width, button_height)) draw_text("Click me", pygame.font.Font(None, 30), BLACK, screen, button_x + button_width // 2, button_y + button_height // 2) pygame.display.flip() pygame.quit() sys.exit() if __name__ == "__main__": main()