title: My pygame code will not run, gives me errors
body: <p>I am making a game where you control a helicopter and dodge other things. This is my code:</p>
  <pre><code>import pygame, random
  from pygame.locals import (
      K_UP,
      K_DOWN,
      K_LEFT,
      K_RIGHT,
      KEYDOWN,
      QUIT,
  )
  print('Use the window above to play')
  pygame.init()
  pygame.font.init()
  clock = pygame.time.Clock()
  
  score = 0
  display_width = 800
  display_height = 600
  X = 400
  Y = 400
  play = 1
  SCREEN_WIDTH = display_width
  SCREEN_HEIGHT = display_height
  screen = pygame.display.set_mode((display_width,display_height))
  
  
  def death():
     print('You died')
      myfont = pygame.font.SysFont('Comic Sans MS', 100)
      textsurface = myfont.render('You Died.', True, (0, 0, 0))
      screen.blit(textsurface,(0,0))
      print('Score: {}'.format(score))
  
  def win():
      global alive
      myfont = pygame.font.SysFont('Comic Sans MS', 100)
      textsurface = myfont.render('You won!', True, (0, 0, 0))
      screen.blit(textsurface,(0,0))
      alive = False
  
  class Player(pygame.sprite.Sprite):
      def __init__(self, x, y):
          super(Player, self).__init__()
          global play
          now = True
          while now == True:
              if play == 1:
                  self.surf = pygame.image.load(&quot;Heli 1.png&quot;)
              play = 2
              if play == 2:
                  self.surf = pygame.image.load(&quot;Heli 2.png&quot;)
              play = 3
              if play == 3:
                  self.surf = pygame.image.load(&quot;Heli 3.png&quot;)
              play = 1
      
          self.rect = self.surf.get_rect(topleft = (x, y))
  
      def update(self, pressed_keys):
          if pressed_keys[K_UP]:
              self.rect.move_ip(0, -5)
          if pressed_keys[K_DOWN]:
              self.rect.move_ip(0, 5)
          if pressed_keys[K_LEFT]:
              self.rect.move_ip(-5, 0)
          if pressed_keys[K_RIGHT]:
              self.rect.move_ip(5, 0)
  
          if self.rect.left &lt; 0:
              self.rect.left = 0
          if self.rect.right &gt; SCREEN_WIDTH:
              self.rect.right = SCREEN_WIDTH
          if self.rect.top &lt;= 0:
              self.rect.top = 0
          if self.rect.bottom &gt;= SCREEN_HEIGHT:
              self.rect.bottom = SCREEN_HEIGHT
  
  class Enemy(pygame.sprite.Sprite):
      def __init__(self):
          super(Enemy, self).__init__()
          self.surf = pygame.Surface((20, 10))
          global play
          now = True
          while now == True:
              if play == 1:
                  self.surf = pygame.image.load(&quot;Rocket 1.png&quot;)
              if play == 2:
                  self.surf = pygame.image.load(&quot;Rocket 2.png&quot;)
              if play == 3:
                  self.surf = pygame.image.load(&quot;Rocket 3.png&quot;)
  
          self.rect = self.surf.get_rect(
              center=(
                 random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
                  random.randint(0, SCREEN_HEIGHT),
              )
          )
          self.speed = random.randint(5, 10)
  
      def update(self):
          self.rect.move_ip(-self.speed, 0)
          if self.rect.right &lt; 0:
              global score
              score += 1
              self.kill()
  
  class Bomb(pygame.sprite.Sprite):
      def __init__(self):
          super(Bomb, self).__init__()
          self.surf = pygame.Surface((50, 25))
          if play == 1:
              self.surf = pygame.image.load(&quot;Plane 1.png&quot;)
          if play == 2:
              self.surf = pygame.image.load(&quot;Plane 2.png&quot;)
          if play == 3:
              self.surf = pygame.image.load(&quot;Plane 3.png&quot;).convert()
          self.rect = self.surf.get_rect(
              center=(
                  random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
                  random.randint(0, SCREEN_HEIGHT),
              )
          )
          self.speed = random.randint(2, 5)
  
      def update(self):
          self.rect.move_ip(-self.speed, 0)
          if self.rect.right &lt; 0:
              global score
              score += 2
              self.kill()
  
  class Blimp(pygame.sprite.Sprite):
      def __init__(self):
          super(Blimp, self).__init__()
          self.surf = pygame.Surface((75, 35))
          self.surf = pygame.image.load(&quot;Blimp.png&quot;).convert()
          self.rect = self.surf.get_rect(
              center=(
                  random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
                  random.randint(0, SCREEN_HEIGHT),
              )
          )
          self.speed = random.randint(1, 3)
  
          def update(self):
          self.rect.move_ip(-self.speed, 0)
          if self.rect.right &lt; 0:
              global score
              score += 3
              self.kill()
  
  ADDENEMY = pygame.USEREVENT + 1
  pygame.time.set_timer(ADDENEMY, random.randrange(600, 1000))
  
  player = Player(display_width // 2, display_height // 2)
  
  enemies = pygame.sprite.Group()
  bomb = pygame.sprite.Group()
  blimp = pygame.sprite.Group()
  all_sprites = pygame.sprite.Group()
  all_sprites.add(player)
  
  alive = True
  while alive:
      for event in pygame.event.get():
          print(event)
          if event.type == pygame.QUIT:
              alive = False
          elif event.type == ADDENEMY:
              new_opponent = random.choice([Enemy(), Bomb(), Blimp()])
              enemies.add(new_opponent)
              all_sprites.add(new_opponent)
  
      pressed_keys = pygame.key.get_pressed()
      player.update(pressed_keys)
  
      if play == 1:
          pygame.time.wait(500)
          play = 2
      if play == 2:
          pygame.time.wait(500)
          play = 3
      if play == 3:
          pygame.time.wait(500)
          play = 1
  
      enemies.update()
  
      screen.fill((20,50,80))
  
      for entity in all_sprites:
          screen.blit(entity.surf, entity.rect)
  
      if pygame.sprite.spritecollideany(player, enemies):
          player.kill()
          alive = False
          death()
  
      if score &gt; 100:
          print('You won!')
          print('Score: {}'.format(score))
          win()
  
      screen.blit(player.surf, player.rect)
  
      pygame.display.flip()
  
      pygame.display.update()
      clock.tick(60)
  </code></pre>
  <p>I've checked it over a lot, and I didn't see anything wrong. But when I run it, it doesn't work and gives me these errors:</p>
  <pre><code>pygame 1.9.6
  Hello from the pygame community. https://www.pygame.org/contribute.html
  Use the window above to play
  xcb_connection_has_error() returned true
  ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
  ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_card_driver returned error: No such                 
  file or directory
  ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
  ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_concat returned error: No such file     
  or directory
  ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
  ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file     
  or directory
  ALSA lib conf.c:5007:(snd_config_expand) Evaluate error: No such file or directory
  ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM default
  </code></pre>
  <p>So when it says <code>use the window above to play</code>, that means it is listening to line 10 in the code. Then, it just spews out all of those errors and the window won't work.
  can somebody please help me?
  UPDATE: I pressed Ctrl+c and it gave me a new error. I don't know if it is just because I shouldn't have pressed that, but the error it gave me seemed relevant:</p>
  <pre><code>  File &quot;main.py&quot;, line 155, in &lt;module&gt;
      player = Player(display_width // 2, display_height // 2)
    File &quot;main.py&quot;, line 51, in __init__
      self.surf = pygame.image.load(&quot;Heli 2.png&quot;)
  KeyboardInterrupt
  </code></pre>