title: Keyboard Event - Pygame
body: <p>Does anyone know how I can make it so my code only fires a bullet if the space bar is pressed? Right now any key makes my character shoot a bullet but I want to change it if possible, heres my code so far:</p>
  
  <pre><code>import pygame, sys, random
  
  pygame.init()
  
  screen_width = 800
  screen_height = 600
  screen = pygame.display.set_mode([screen_width, screen_height])
  all_sprites_list = pygame.sprite.Group()
  block_list = pygame.sprite.Group()
  bullet_list = pygame.sprite.Group()
  BLACK    = (   0,   0,   0)
  WHITE    = ( 255, 255, 255)
  background = pygame.image.load('space.jpg')
  pygame.display.set_caption("Alien Invasion!")
  explosion = pygame.mixer.Sound('explosion.wav')
  score = 0
  
  class Block(pygame.sprite.Sprite):
      def __init__(self, color):
          pygame.sprite.Sprite.__init__(self)
  
          self.image = pygame.image.load("spaceship.png")
          self.rect = self.image.get_rect()
  
  class Player(pygame.sprite.Sprite):
      def __init__(self):
          pygame.sprite.Sprite.__init__(self)
  
          self.x=0
          self.y=0
          self.image = pygame.image.load("alien.png")
          self.rect = self.image.get_rect()
  
      def update(self):
  
          pos = pygame.mouse.get_pos()
          self.rect.x = pos[0]
  
      def render(self):
  
          if (self.currentImage==0):
              screen.blit(self.image, (self.x, self.y))
  
  class Bullet(pygame.sprite.Sprite):
      def __init__(self):
          pygame.sprite.Sprite.__init__(self)
          self.image = pygame.image.load("bullet.png")
          self.rect = self.image.get_rect()
  
      def update(self):
  
          self.rect.y -= 3
  
  for i in range(30):
      block = Block(BLACK)
      block.rect.x = random.randrange(screen_width)
      block.rect.y = random.randrange(330)
      block_list.add(block)
      all_sprites_list.add(block)
  
  player = Player()
  
  all_sprites_list.add(player)
  
  done = False
  
  clock = pygame.time.Clock()
  
  player.rect.y = 480
  
  while not done:
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              done = True
  
          elif event.type == pygame.KEYDOWN:
              bullet = Bullet()
              bullet.rect.x = player.rect.x
              bullet.rect.y = player.rect.y
              all_sprites_list.add(bullet)
              bullet_list.add(bullet)
  
      all_sprites_list.update()
  
      for bullet in bullet_list:
  
          block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
  
          for block in block_hit_list:
              explosion.play()
              bullet_list.remove(bullet)
              all_sprites_list.remove(bullet)
              score += 10
  
          if bullet.rect.y &lt; -10:
              bullet_list.remove(bullet)
              all_sprites_list.remove(bullet)
  
      font = pygame.font.Font(None, 36)
      text = font.render("Score: " + str(score), True, WHITE)
      textpos = text.get_rect(centerx=screen.get_width()/12)
  
      screen.blit(background, (0,0))
  
      screen.blit(text, textpos)
  
      all_sprites_list.draw(screen)
  
      pygame.display.update()
  
      clock.tick(80)
  
  pygame.quit()
  </code></pre>
  
  <p>Also, if possible, I want to make it so that once the escape key is pressed, the game exits. For this I have this code:</p>
  
  <pre><code>    if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
  </code></pre>
  
  <p>However it doesnt seem to work properly for some reason :( Im new to python so any help would be appreciated!</p>