title: Pygame enemy display random when player moves by keyboard
body: <p>I am building a football game (american) that has a Player class move an image by the keyboard in one program:</p>
  
  <pre><code>import pygame
  import os
  import random
  
  black = (0,0,0)
  white = (255,255,255)
  red = (255, 0, 0)
  green = (0, 100, 0)
  
  
  # This class represents the bar at the bottom that the player controls
  class Player(object):
      def __init__(self):
          self.image = pygame.image.load("player_one.png").convert()
          self.image.set_colorkey(white)
          self.width = 15
          self.height = 15
          self.x = 940
          self.y = 240
  
      def handle_keys(self):
          key = pygame.key.get_pressed()
          if key[pygame.K_DOWN]:
              if self.y &lt; 470:
                  self.y += self.height
          elif key[pygame.K_UP]:
              if self.y &gt; 0:
                  self.y -= self.height
          if key[pygame.K_RIGHT]:
              if self.x &lt; 940:
                  self.x += self.width
          elif key[pygame.K_LEFT]:
              if self.x &gt; 0:
                  self.x -= self.width
  
      def draw(self, surface):
          surface.blit(self.image, (self.x, self.y))
  
  
  pygame.init()
  screen = pygame.display.set_mode((1000, 500))
  
  player = Player()
  clock = pygame.time.Clock()
  
  running = True
  while running:
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              pygame.quit()
              running = False
  
      player.handle_keys()
  
      screen.fill(green)
  
      for x in range(60,940,35):
          pygame.draw.line(screen, white, [x, 0], [x, 500], 1)
  
      player.draw(screen)
      pygame.display.update()
  
      clock.tick(20)
  </code></pre>
  
  <p>And another program that displays the enemy randomly on a background image when any key is pressed they change position:</p>
  
  <pre><code>import random
  import pygame
  
  WHITE = (255,255,255)
  BLACK = (0  ,0  ,0  )
  
  #----------------------------------------------------------------------
  
  class Enemy():
  
      def __init__(self, image, x=0, y=0):
  
          self.image = pygame.image.load(image).convert()
          self.image.set_colorkey(WHITE)
          self.rect = self.image.get_rect()
  
          self.rect.centerx = x
          self.rect.centery = y
  
      #------------
  
      def draw(self, screen):
          screen.blit(self.image, self.rect)
  
      #------------
  
      def update(self):
          # here change randomly positon
          self.rect.topleft = random.randint(60,220+1), random.randint( 0, 475+1)
  
  #----------------------------------------------------------------------
  
  class Game():
  
      def __init__(self):
  
          pygame.init()
  
          self.screen = pygame.display.set_mode((1400,500))
  
          self.background = pygame.image.load("GameField1.png").convert()
          self.multi_enemies = []
          self.players = []
  
          # create 3 enemies 0...2
  
          for i in range(0,3):
              enemy = Enemy("enemy_"+str(i)+".png")
              enemy.update() # set random position on start
              self.multi_enemies.append(enemy)
  
  
      #------------
  
      def run(self):
  
          clock = pygame.time.Clock()
          RUNNING = True
  
          while RUNNING:
  
              # --- events ---
  
              for event in pygame.event.get():
  
                  if event.type == pygame.QUIT:
                      RUNNING = False
  
                  if event.type == pygame.KEYDOWN:
                      if event.key == pygame.K_ESCAPE:
                          RUNNING = False
  
                      # changes position when key is pressed
  
  
                      for enemy in self.multi_enemies:
                          enemy.update()
  
                      for player in self.players:
                          player.handle_keys()
  
              # --- updates ---- 
  
              # place for updates
  
              # --- draws ---
  
  
              self.screen.fill(BLACK)
  
              self.screen.blit(self.background, self.background.get_rect())
  
              for enemy in self.multi_enemies:
                  enemy.draw(self.screen)
  
  
              pygame.display.update()
  
              pygame.display.flip()
  
              # --- FPS ---
  
              clock.tick(20)
  
          # --- quit ---
  
          pygame.quit()
  
  #----------------------------------------------------------------------
  
  Game().run()
  </code></pre>
  
  <p>First - Thank you for the people who helped me get this far. Second - I need to combine the Player class to the second program. I need to add collision detection so that if the player makes it to the left end zone his Score increases +7 and he goes back to the start. Also, if the player runs into an Enemy then he goes back to the start. I want the game to be on a 2 min timer so the goal is to score as much within the timeframe before the game ends.</p>
  
  <p>I know a lot of people are going to recommend Sprites and I expect that but could you please provide code/explanation. Attached are my images.</p>
  
  <p><img src="https://i.stack.imgur.com/5g7AI.png" alt="player1"></p>
  
  <p><img src="https://i.stack.imgur.com/nHOwO.png" alt="enemy2"></p>
  
  <p><img src="https://i.stack.imgur.com/rbGcP.png" alt="enemy1"></p>
  
  <p><img src="https://i.stack.imgur.com/7EDAd.png" alt="enemy0"></p>
  
  <p><img src="https://i.stack.imgur.com/RHzex.png" alt="gamefield"></p>