Advertisement
bitwise_gamgee

Untitled

Jun 8th, 2023
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1. def handle_events(rectangles):
  2.     active_rect = None
  3.  
  4.     for event in pygame.event.get():
  5.         if event.type == pygame.QUIT:
  6.             pygame.quit()
  7.             sys.exit()
  8.  
  9.         if event.type == pygame.MOUSEBUTTONDOWN:
  10.             for rect in rectangles:
  11.                 if rect.collidepoint(event.pos):
  12.                     active_rect = rect
  13.                 else:
  14.                     active_rect = None
  15.  
  16.         if event.type == pygame.KEYDOWN and active_rect:
  17.             if event.key == pygame.K_BACKSPACE:
  18.                 active_rect.user_input = active_rect.user_input[:-1]
  19.             else:
  20.                 active_rect.user_input += event.unicode
  21.  
  22.     return active_rect
  23.  
  24.  
  25. def draw_rectangles(rectangles, active_rect):
  26.     for rect in rectangles:
  27.         color = rect.active_color if rect == active_rect else rect.passive_color
  28.         pygame.draw.rect(screen, color, rect.rect)
  29.  
  30.  
  31. def draw_text(rectangles):
  32.     for rect in rectangles:
  33.         text_surface = rect.font.render(rect.user_input, True, pygame.Color('white'))
  34.         screen.blit(text_surface, (rect.rect.x + 18, rect.rect.y + 5))
  35.  
  36.  
  37. class InputRectangle:
  38.     def __init__(self, rect, font, active_color, passive_color):
  39.         self.rect = rect
  40.         self.font = font
  41.         self.active_color = active_color
  42.         self.passive_color = passive_color
  43.         self.user_input = ""
  44.  
  45.  
  46. def main_screen():
  47.     # Create input rectangles
  48.     rectangles = [
  49.         InputRectangle(pygame.Rect(280, 375, 50, 30), input_font, pygame.Color('black'), pygame.Color('white')),
  50.         InputRectangle(pygame.Rect(280, 435, 50, 30), input_font, pygame.Color('black'), pygame.Color('white'))
  51.     ]
  52.  
  53.     # Define necessary text
  54.     texts = [
  55.         main_font.render('Wheel Of Monotony', True, pygame.Color('black')),
  56.         description_font.render("In the box below, please enter what you would like", True, pygame.Color('black')),
  57.         description_font.render("the max. of the range you have to guess to be:", True, pygame.Color('black')),
  58.         description_font.render("In the box below, please enter what you would think", True, pygame.Color('black')),
  59.         description_font.render("the number I'm thinking of is: ", True, pygame.Color('black'))
  60.     ]
  61.  
  62.     while True:
  63.         screen.blit(background_image, [0, 0])  # Background
  64.  
  65.         for i, text in enumerate(texts):
  66.             screen.blit(text, (75, 200 + 75 * i))  # Text showing
  67.  
  68.         active_rect = handle_events(rectangles)  # Event handling
  69.         draw_rectangles(rectangles, active_rect)  # Draw rectangles
  70.         draw_text(rectangles)  # Draw text
  71.  
  72.         pygame.display.flip()
  73.         clock.tick(60)
  74.  
  75.     return [rect.user_input for rect in rectangles]
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement