title: How can I create an interactive object in pygame?
body: <p>In a pygame program I am creating, I have need for an interactive object on the screen that will call a function when the player character moves onto it and presses the enter key. Here is the code I have so far:</p>
  
  <pre><code>import pygame
  
  pygame.init()
  screen = pygame.display.set_mode((800, 600))
  done = False
  x = 30
  y = 30
  
  clock = pygame.time.Clock()
  
  while not done:
          for event in pygame.event.get():
                  if event.type == pygame.QUIT:
                          done = True
  
      pressed = pygame.key.get_pressed()
      if pressed[pygame.K_UP] and y &gt; 0: y -= 5
      if pressed[pygame.K_DOWN] and y &lt; 600 - 60: y += 5
      if pressed[pygame.K_LEFT] and x &gt; 0: x -= 5
      if pressed[pygame.K_RIGHT] and x &lt; 800 - 60: x += 5
  
      screen.fill((0, 0, 0))
      color = (0, 128, 255)
      pygame.draw.rect(screen, color, pygame.Rect(x, y, 60, 60))
  
      myfont = pygame.font.SysFont("monospace", 15)
  
      label = myfont.render("Start the experiment simulator", 1, (255,255,255))
      screen.blit(label, (100, 100))
  
      label2 = myfont.render("Start the quiz", 1, (255,255,255))
      screen.blit(label2, (550, 100))
  
      label3 = myfont.render("Quit game", 1, (255,255,255))
      screen.blit(label3, (350, 400))
  
      pygame.draw.rect(screen, red, pygame.Rect(600, 125, 30, 30))
      pygame.draw.rect(screen, red, pygame.Rect(225, 125, 30, 30))
      pygame.draw.rect(screen, red, pygame.Rect(375, 425, 30, 30))
  
      pygame.display.flip()
      clock.tick(60)
  </code></pre>
  
  <p>At the moment, the object is just a test so would be best to be a small red rectangle, about half the size of the player, that I can replace with an icon later on. This rectangle should be placed below the 'quit game' label on the pygame window and quit the game when interacted with. This is one method that I have tried so far:</p>
  
  <pre><code>if pressed[pygame.K_RETURN] and x &gt;= 375 or x &lt;= 405 and y &gt;=425 or y &lt;= 455:
              pygame.display.quit()
              pygame.quit()
              sys.exit()
  </code></pre>
  
  <p>Where theoretically the system checks if the user is in a specific area and has pressed the enter key before performing the command.</p>