title: Why is the enemy not moving? And also, sometimes the enemy doesn't show up at all. Some the code I commented out was just size changes I made
body: <p>Sometimes, the enemy doesn't appear on the screen and doesn't move at all. It's supposed to move left to the right and sits there, not doing anything. I've tried to change the size of the ground and reviewed the code. This is part of a tutorial I am following <a href="https://coderslegacy.com/python/pygame-rpg-game-tutorial/" rel="nofollow noreferrer">https://coderslegacy.com/python/pygame-rpg-game-tutorial/</a>. I've noticed there are a lot of mistakes in this tutorial, but I've been able to figure some of them out. I'm sorry if this isn't worded great, this is my first time posting here.</p>
  <pre><code>import pygame
  from pygame.locals import *
  import sys
  import random
  import time
  from tkinter import filedialog
  from tkinter import *
  
  
  pygame.init()
  
  vec = pygame.math.Vector2
  HEIGHT = 350
  WIDTH = 700
  ACCELERATION = 0.3  # For game physics
  FRICTION = -0.10  # For game physics
  FPS = 60
  FPS_CLOCK = pygame.time.Clock()
  COUNT = 0
  # hit_cooldown = pygame.USEREVENT + 1
  
  displaysurface = pygame.display.set_mode((WIDTH, HEIGHT))
  pygame.display.set_caption(&quot;Game&quot;)
  
  # Run animation for the right
  run_ani_r = [pygame.image.load(&quot;run_right/Player_Sprite_R.png&quot;), pygame.image.load(&quot;run_right/Player_Sprite2_R.png&quot;),
               pygame.image.load(&quot;run_right/Player_Sprite3_R.png&quot;), pygame.image.load(&quot;run_right/Player_Sprite4_R.png&quot;),
               pygame.image.load(&quot;run_right/Player_Sprite5_R.png&quot;), pygame.image.load(&quot;run_right/Player_Sprite6_R.png&quot;),
               pygame.image.load(&quot;run_right/Player_Sprite_R.png&quot;)]
  
  # Run animation for the left
  run_ani_l = [pygame.image.load(&quot;run_left/Player_Sprite_L.png&quot;), pygame.image.load(&quot;run_left/Player_Sprite2_L.png&quot;),
               pygame.image.load(&quot;run_left/Player_Sprite3_L.png&quot;), pygame.image.load(&quot;run_left/Player_Sprite4_L.png&quot;),
               pygame.image.load(&quot;run_left/Player_Sprite5_L.png&quot;), pygame.image.load(&quot;run_left/Player_Sprite6_L.png&quot;),
               pygame.image.load(&quot;run_left/Player_Sprite_L.png&quot;)]
  
  # Attack animation for the right side
  attack_ani_r = [pygame.image.load(&quot;player_attack_right/Player_Sprite_R.png&quot;),
                  pygame.image.load(&quot;player_attack_right/Player_Attack_R.png&quot;),
                  pygame.image.load(&quot;player_attack_right/Player_Attack2_R.png&quot;),
                  pygame.image.load(&quot;player_attack_right/Player_Attack2_R.png&quot;),
                  pygame.image.load(&quot;player_attack_right/Player_Attack3_R.png&quot;),
                  pygame.image.load(&quot;player_attack_right/Player_Attack3_R.png&quot;),
                  pygame.image.load(&quot;player_attack_right/Player_Attack4_R.png&quot;),
                  pygame.image.load(&quot;player_attack_right/Player_Attack4_R.png&quot;),
                  pygame.image.load(&quot;player_attack_right/Player_Attack5_R.png&quot;),
                  pygame.image.load(&quot;player_attack_right/Player_Attack5_R.png&quot;),
                  pygame.image.load(&quot;player_attack_right/Player_Sprite_R.png&quot;)]
  
  # Attack animation for the left side
  attack_ani_l = [pygame.image.load(&quot;player_attack_left/Player_Sprite_L.png&quot;),
                  pygame.image.load(&quot;player_attack_left/Player_Attack_L.png&quot;),
                  pygame.image.load(&quot;player_attack_left/Player_Attack2_L.png&quot;),
                  pygame.image.load(&quot;player_attack_left/Player_Attack2_L.png&quot;),
                  pygame.image.load(&quot;player_attack_left/Player_Attack3_L.png&quot;),
                  pygame.image.load(&quot;player_attack_left/Player_Attack3_L.png&quot;),
                  pygame.image.load(&quot;player_attack_left/Player_Attack4_L.png&quot;),
                  pygame.image.load(&quot;player_attack_left/Player_Attack4_L.png&quot;),
                  pygame.image.load(&quot;player_attack_left/Player_Attack5_L.png&quot;),
                  pygame.image.load(&quot;player_attack_left/Player_Attack5_L.png&quot;),
                  pygame.image.load(&quot;player_attack_left/Player_Sprite_L.png&quot;)]
  
  
  class Background(pygame.sprite.Sprite):
      def __init__(self):
          super().__init__()
          self.bgimage = pygame.image.load(&quot;Background.png&quot;)
          self.rectBGimg = self.bgimage.get_rect()
          self.bgY = 0
          self.bgX = 0
          # self.bgimage = pygame.image.load('background.jpg')
          # self.rectBGimg = self.bgimage.get_rect()
          # self.bgY = 0
          # self.bgX = 0
  
      def render(self):
          displaysurface.blit(self.bgimage, (self.bgX, self.bgY))
  
  
  class Ground(pygame.sprite.Sprite):
      def __init__(self):
          super().__init__()
          self.image = pygame.image.load(&quot;Ground.png&quot;)
          self.rect = self.image.get_rect(center = (350, 350))
          self.bgX1 = 0
          self.bgY1 = 285
          # self.image = pygame.image.load(&quot;Ground.png&quot;)
          # self.image = pygame.transform.scale(pygame.image.load('Ground.png'), [1600, 195])
          # self.rect = self.image.get_rect(center=(700, 700))
          # self.bgX1 = 0
          # self.bgY1 = 580
  
      def render(self):
          displaysurface.blit(self.image, (self.bgX1, self.bgY1))
  
  
  class Player(pygame.sprite.Sprite):
      def __init__(self):
          super().__init__()
          self.image = pygame.image.load('run_right/Player_Sprite_R.png')
          self.rect = self.image.get_rect()
  
          # Position and direction
          self.vx = 0
          self.pos = vec((340, 240))
          self.vel = vec(0, 0)
          self.acc = vec(0, 0)
          self.direction = &quot;RIGHT&quot;
  
          # Movement
          self.jumping = False
          self.running = False
          self.move_frame = 0
  
          # Combat
          self.attacking = False
          self.cooldown = False
          self.attack_frame = 0
  
      def move(self):
          # Keep a constant acceleration of 0.5 in the downwards direction
          self.acc = vec(0, 0.5)
          # Will setting running to False if the player has
          # slowed down to a certain extent
          if abs(self.vel.x) &gt; 0.3:
              self.running = True
          else:
              self.running = False
  
          pressed_keys = pygame.key.get_pressed()
  
          # Accelerates the player in the direction of the key press
          if pressed_keys[K_LEFT]:
              self.acc.x = -ACCELERATION
          if pressed_keys[K_RIGHT]:
              self.acc.x = ACCELERATION
  
          # Formulas to calculate velocity while accounting for friction
          self.acc.x += self.vel.x * FRICTION
          self.vel += self.acc
          self.pos += self.vel + 0.5 * self.acc # Updates Position with new values
  
          if self.pos.x &gt; WIDTH:
              self.pos.x = 0
          if self.pos.x &lt; 0:
              self.pos.x = WIDTH
  
          self.rect.midbottom = self.pos # Update rect with new pos
  
      def _update(self):
          # Return to base frame if at end of movement sequence
          if self.move_frame &gt; 6:
              self.move_frame = 0
              return
  
          # Move the character to the next frame if conditions are met
          if self.jumping is False and self.running is True:
              if self.vel.x &gt; 0:
                  self.image = run_ani_r[self.move_frame]
                  self.direction = &quot;RIGHT&quot;
              else:
                  self.image = run_ani_l[self.move_frame]
                  self.direction = &quot;LEFT&quot;
              self.move_frame += 1
          # Returns to base frame if standing still and incorrect frame is showing
          if abs(self.vel.x) &lt; 0.2 and self.move_frame != 0:
              self.move_frame = 0
              if self.direction == &quot;RIGHT&quot;:
                  self.image = run_ani_r[self.move_frame]
              elif self.direction == &quot;LEFT&quot;:
                  self.image = run_ani_l[self.move_frame]
  
      def attack(self):
          # If attacking frame has reached the end of the sequence, return to base frame
          if self.attack_frame &gt; 10:
              self.attack_frame = 0
              self.attacking = False
  
          # Check direction for correct animation to display
          if self.direction == &quot;RIGHT&quot;:
              self.image = attack_ani_r[self.attack_frame]
          elif self.direction == &quot;LEFT&quot;:
              self.correction()
              self.image = attack_ani_l[self.attack_frame]
  
          # Update the current attack frame
          self.attack_frame += 1
  
      def correction(self):
          # Function is used to correct an error
          # With character position on left attack frames
          if self.attack_frame == 1:
              self.pos.x -= 20
          if self.attack_frame == 10:
              self.pos.x += 20
  
      def jump(self):
          self.rect.x += 1
  
          # Check to see if the player is in contact with the ground
          hits = pygame.sprite.spritecollide(self, ground_group, False)
  
          self.rect.x -= 1
  
          # If touching the ground, and not currently jumping cause the player
          # to jump
          if hits and not self.jumping:
              self.jumping = True
              self.vel.y = -12
  
      def gravity_check(self):
          hits = pygame.sprite.spritecollide(player, ground_group, False)
          if self.vel.y &gt; 0:
              if hits:
                  lowest = hits[0]
                  if self.pos.y &lt; lowest.rect.bottom:
                      self.pos.y = lowest.rect.top + 1
                      self.vel.y = 0
                      self.jumping = False
  
      def player_hit(self):
          if not self.cooldown:
              self.cooldown = True
              pygame.time.set_timer(hit_cooldown, 1000)
  
              print(&quot;hit&quot;)
              pygame.display.update()
  
  
  class Enemy(pygame.sprite.Sprite):
      def __init__(self):
          super().__init__()
          self.image = pygame.image.load(&quot;Enemy.png&quot;)
          self.rect = self.image.get_rect()
          self.pos = vec(0, 0)
          self.vel = vec(0, 0)
  
          self.direction = random.randint(0, 1)  # 0 for right, 1 for left
          self.vel.x = random.randint(2, 6) / 2  # Randomized velocity of the generated enemy
  
          # Sets the initial position of the enemy
          if self.direction == 0:
              self.pos.x = 0
              self.pos.y = 235
              # self.pos.x = 0
              # self.pos.y = 540
              # self.pos.x += self.vel.x
          if self.direction == 1:
              self.pos.x = 700
              self.pos.y = 235
              # self.pos.x -= self.vel.x
              # self.pos.x = 700
              # self.pos.y = 540
  
      def move(self):
          # Causes the enemy to change directions upon reaching the end of the screen
          if self.pos.x &gt;= (WIDTH - 20):
              self.direction = 1
          elif self.pos.x &lt;= 0:
              self.direction = 0
  
          if self.direction == 0:
              self.pos.x += self.vel.x
          if self.direction == 1:
              self.pos.x -= self.vel.x
  
          self.rect.center = self.pos
  
      def __update(self):
          # Check for collision with the player
          hits = pygame.sprite.spritecollide(self, Playergroup, False)
  
          # Activates upon either of the two expressions being true
          if hits and player.attacking == True:
              self.kill()
  
          # If collision has occured and player not attacking, call &quot;hit&quot; function
          elif hits and player.attacking == False:
              player.player_hit()
  
      def render(self):
          #Display the enemy on the screen
          displaysurface.blit(self.image, (self.pos.x, self.pos.y))
  
  
  player = Player()
  enemy = Enemy()
  Playergroup = pygame.sprite.Group()
  Playergroup.add(player)
  
  
  
  background = Background()
  
  ground = Ground()
  ground_group = pygame.sprite.Group()
  ground_group.add(ground)
  
  hit_cooldown = pygame.USEREVENT + 1
  
  
  while True:
      player.gravity_check()
  
      for event in pygame.event.get():
          # Will run when the close button is clicked
          if event.type == QUIT:
              pygame.quit()
              sys.exit()
  
          if event.type == pygame.MOUSEBUTTONDOWN:
              pass
  
          if event.type == pygame.KEYDOWN:
              if event.key == pygame.K_SPACE:
                  player.jump()
              if event.key == pygame.K_RETURN:
                  if not player.attacking:
                      player.attack()
                      player.attacking = True
  
          if event.type == hit_cooldown:
              player_cooldown = False
              pygame.time.set_timer(hit_cooldown, 0)
  
      player._update()
      if player.attacking:
          player.attack()
      player.move()
  
      background.render()
      ground.render()
  
      displaysurface.blit(player.image, player.rect)
      enemy.render()
  
      pygame.display.update()
      FPS_CLOCK.tick(FPS)
  
  
  </code></pre>