title: Key press recognition within while loops. Python
body: <p>I am attempting to program a simple dialogue box in a game. I am using a while loop for the overall game loop. and a while loop within this loop activated by the player pressing space. I want the player to press space(speech() function) to enter the second while loop and press A (speech_end()) to exit this loop. However, speech_end does not work within this second loop unless I am already holding the A. I have tested it outside of this loop and it works fine. Any help is appreciated. </p>
  
  <pre><code>#Classes
  class Bertha(object):
  
      image = pygame.image.load("bertha.png")
  
      def __init__(self, berthax, berthay, screen):
          self.screen = screen
          self.screen_rect = screen.get_rect()
  
  
          self.rect = self.image.get_rect()
          self.rect.x = berthax
          self.rect.y = berthay
  
      def draw(self, screen):
          screen.blit(self.image, self.rect)
  
  
  
  class Player(object):
  
      # one image for all instances
      image = pygame.image.load('player.png')
  
      def __init__(self, playerx, playery, screen):
          self.screen = screen
          self.screen_rect = screen.get_rect()
  
          # every instance can have own image
          #self.image = pygame.image.load('test.jpg')
  
          self.rect = self.image.get_rect()
          self.rect.x = playerx
          self.rect.y = playery
  
  
  #moving with Arrow keys
      def movement(self):
  
          for event in pygame.event.get():
  
              self.dist_x = 0
              self.dist_y = 0
              keys = pygame.key.get_pressed()
  
              if keys[pygame.K_RIGHT]:
                  self.dist_x += 5
                  if keys[pygame.K_UP]:
                      self.dist_y -= 5
                  elif keys[pygame.K_DOWN]:
                      self.dist_y += 5
  
              elif keys[pygame.K_LEFT]:
                  self.dist_x -= 5
                  if keys[pygame.K_UP]:
                      self.dist_y -= 5
                  elif keys[pygame.K_DOWN]:
                      self.dist_y += 5
  
              elif keys[pygame.K_UP]:
                  self.dist_y -= 5
                  if keys[pygame.K_RIGHT]:
                      self.dist_x += 5
                  elif keys[pygame.K_LEFT]:
                      self.dist_x -= 5
  
              elif keys[pygame.K_DOWN]:
                  self.dist_y += 5
                  if keys[pygame.K_RIGHT]:
                      self.dist_x += 5
                  elif keys[pygame.K_LEFT]:
                      self.dist_x -= 5
  
  
  
  
  #------------Confirming player position--------------
  
  
          self.rect.x += self.dist_x
          self.rect.y += self.dist_y
  
  
      def draw(self, screen):
          screen.blit(self.image, self.rect)
  
  
  
  
  
  # --------Talking to another character----------------
  
  
  
  def speech():
  
      keys1 = pygame.key.get_pressed()
      global talking
      if keys1[pygame.K_SPACE]:
          talking = 1
  
  
  
  def speech_end():
  
      keys2 = pygame.key.get_pressed()
      global talking
      if keys2[pygame.K_a]:
          talking = 2
          print("speech_end")
  
  
  
  
  
  
  
  
  
  
  # ----- Bertha Messages
  
  
  
  def bertha_text(screen, font):
  
      text = font.render("spacebar worked!", True, BLACK)
  
      text_rect = text.get_rect()
  
      text_rect.center = (400, 20)
  
      screen.blit(text, text_rect)
  
      pygame.display.update()
  
  
  
  
  
  
  # - other -
  
  font = pygame.font.SysFont("moon_get-Heavy.ttf", 40)
  clock = pygame.time.Clock()
  
  playerx = 100
  playery = 100
  
  berthax = 400
  berthay = 400
  
  
  player_one = (Player(playerx, playery, screen))
  bertha_npc = (Bertha(berthax, berthay, screen))
  
  
  
  
  talking = 0
  
  # - mainloop -
  
  
  running = True
  
  
  while running:
  
      talking = 0
  
  
      screen.blit(map_img, (0, 0))
  
  #Player
  
  
      player_one.movement()
      player_one.draw(screen)
  
  
  
  #Bertha
      bertha_npc.draw(screen)
  #------Show the text------
  
  
      speech()
  
      while talking == 1:
  
          bertha_text(screen, font)
          speech_end()
  
          print(talking)
  
          if talking == 2:
  
              print("talking == False")
              talking -= 2
              break
  
  
  
  
  
  #UPDATE
  
      pygame.display.update()
          # - FPS -
      clock.tick(60)
  
  
  
  # - end -
  
  pygame.quit()
  quit()
  </code></pre>