import pygame # Initialize Pygame pygame.init() # Set up the screen screen_width = 400 screen_height = 400 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Golf Handicap Calculator") # Set up the fonts title_font = pygame.font.SysFont('Arial', 24, bold=True) input_font = pygame.font.SysFont('Arial', 20) # Draw the title title_text = title_font.render("Enter your last 10 scores:", True, (0, 0, 0)) title_rect = title_text.get_rect(center=(screen_width//2, 50)) screen.blit(title_text, title_rect) # Draw the input boxes score_boxes = [] for i in range(10): rect = pygame.Rect(80 + i*50, 220, 40, 40) pygame.draw.rect(screen, (255, 255, 255), rect, 2) score_boxes.append(rect) # Draw the calculate button calc_button_rect = pygame.Rect(250, 280, 100, 50) pygame.draw.rect(screen, (0, 255, 0), calc_button_rect) calc_button_font = pygame.font.SysFont('Arial', 20) calc_text = calc_button_font.render("Calculate", True, (255, 255, 255)) calc_text_rect = calc_text.get_rect(center=calc_button_rect.center) screen.blit(calc_text, calc_text_rect) # Update the display pygame.display.update() # Set up the game loop scores = [] while True: # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() elif event.type == pygame.MOUSEBUTTONDOWN: # Check if the user clicked the calculate button if calc_button_rect.collidepoint(event.pos): # Calculate the handicap if len(scores) < 10: error_text = input_font.render("Please enter 10 scores", True, (255, 0, 0)) error_rect = error_text.get_rect(center=(screen_width//2, 170)) screen.blit(error_text, error_rect) else: avg_score = sum(scores) / len(scores) handicap = round((avg_score - 59) * 0.8) # Draw the handicap result result_text = input_font.render(f"Your handicap for the McLaren's Falls course is +{handicap}", True, (0, 0, 0)) result_rect = result_text.get_rect(center=(screen_width//2, 170)) screen.blit(result_text, result_rect) pygame.display.update() elif event.type == pygame.KEYDOWN: # Check if the user pressed a number key if event.unicode.isdigit(): # Add the score to the list of scores score = int(event.unicode) scores.append(score) index = len(scores) - 1 # Draw the score in the appropriate input box score_text = input_font.render(str(score), True, (0, 0, 0)) score_rect = score_text.get_rect(center=score_boxes[index].center) screen.blit(score_text, score_rect) pygame.display.update()