title: Why does my Python pygame program close immediately after launch?
body: <p>I'm trying to make a game using Pygame. But the program closes immediately after it launches.</p>
  
  <p>I followed a tutorial in YT, and copied the function exactly as it was, but still got the error.</p>
  
  <hr>
  
  <p>Here is the code:</p>
  
  <pre><code>import pygame
  import sys
  import random as rd
  
  pygame.init()
  
  width = 800
  height = 600
  
  red = (255, 0, 0)
  black = (0, 0, 0)
  blue = (0, 0, 255)
  
  playerPosition = [400, 500]
  playerSize = 35
  
  enemySize = 50
  enemyPosition = [rd.randint(0, width - enemySize), 0]
  enemySpeed = 10
  
  screen = pygame.display.set_mode((width, height))
  title = pygame.display.set_caption("Dodge Game by Ishak")
  
  
  def collision(playerPosition, enemyPosition):
      playerX = playerPosition[0]  # player x coordinate
      playerY = playerPosition[1]  # player y coordinate
  
      enemyX = enemyPosition[0]  # enemy x coordinate
      enemyY = enemyPosition[1]  # enemy y coordinate
  
      if (enemyX &gt;= playerX and enemyX &lt; (playerX + playerSize)) or (playerX &gt;= enemyX and playerX &lt; (enemyX + enemySize)):
          if (enemyY &gt;= playerY and enemyY &lt; (playerY + playerSize)) or (playerY &gt;= enemyY and playerY &lt; (enemyY + enemySize)):
              return False
      return True
  
  
  clock = pygame.time.Clock()
  
  gameOver = False
  # game loop
  while not gameOver:
  
      # QUIT
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              sys.exit()
  
      # keyboard
      if event.type == pygame.KEYDOWN:
  
          x = playerPosition[0]
          y = playerPosition[1]
  
          if event.key == pygame.K_RIGHT:
              x += 13
          elif event.key == pygame.K_LEFT:
              x -= 13
  
          playerPosition = [x, y]
  
      if enemyPosition[1] &gt;= 0 and enemyPosition[1] &lt; height:
          enemyPosition[1] += enemySpeed
      else:
          enemyPosition[0] = rd.randint(0, width - enemySize)  # sets a random postion of the enemy
          enemyPosition[1] = 0
  
      if collision(playerPosition, enemyPosition):
          gameOver = True
  
      screen.fill(black)
  
      pygame.draw.rect(screen, red, (playerPosition[0], playerPosition[1], playerSize, playerSize))  # player shape
      pygame.draw.rect(screen, blue, (enemyPosition[0], enemyPosition[1], enemySize, enemySize))  # enemy shape
  
      clock.tick(30)
  
      pygame.display.update()
  </code></pre>
  
  <hr>
  
  <p>This problem occurred after I added the collision function and implemented it on the main game loop, but I can't figure out what is wrong with it.</p>