title: Trying to switch bullet patterns in this game
body: <p>I'm trying to make a bullet-hell style game and I've <em>almost</em> got it to work except the opponent won't change from shooting one bullet pattern to another.</p>
  
  <p>It's supposed to shoot 3 blue bullets 8 times, then switch to shooting 2 purple bullets 8 times. There's a sequence of patterns but I've only got two.</p>
  
  <p>So it should iterate through each pattern every time the current pattern shoots a certain amount of times. When all the patterns are done it should stop shooting completely.</p>
  
  <p>I've seen people try to make these but it's always java and I'm on python.</p>
  
  <p>The code is very long but I can't cut it down any more. The original is in multiple files but I've put it into one script. It's virtually impossible to simplify.</p>
  
  <pre><code>import sys
  import time
  import itertools
  import pygame
  from pygame.locals import *
  
  class Player(pygame.sprite.Sprite):
  
      #this sprite variable is a placeholder
      sprite = pygame.image.load("Sprites/player.png")
  
      def __init__(self, *groups):
          super().__init__(*groups)
          self.image = Player.sprite
          self.rect = self.image.get_rect(topleft=(445, 550))
          self.pos = pygame.Vector2(self.rect.topleft)
  
      def update(self):
          key = pygame.key.get_pressed()
          dist = 3
          if key[pygame.K_DOWN]:
              self.rect.y += dist
          elif key[pygame.K_UP]:
              self.rect.y -= dist
          if key[pygame.K_RIGHT]:
              self.rect.x += dist
          elif key[pygame.K_LEFT]:
              self.rect.x -= dist
  
  
  class Spell:
      def __init__(self, bullet, pattern, speed, loop, tick_delay):
          self.bullet = bullet
          self.pattern = pattern
          self.speed = speed
          self.loop = loop
          self.tick_delay = tick_delay
  
  
  
  class Opponent(pygame.sprite.Sprite):
  
      def __init__(self, sprite, sequence, *groups):
          super().__init__(*groups)
          self.image = sprite
          self.rect = self.image.get_rect(topleft=(425, 30))
          self.pos = pygame.Vector2(self.rect.topleft)
          self.path = itertools.cycle((self.rect.topleft, ))
          self.next_point = pygame.Vector2(next(self.path))
          self.speed = 1
          self.ticks = 1000
          self.queue = []
          self.sequence = sequence
          self.spellno = 0
          self.currentspell = sequence[self.spellno]
  
      def update(self):
          #this function basically does most of the stuff in this class
          move = self.next_point - self.pos
          move_length = move.length()
          if move_length != 0:
              move.normalize_ip()
              move = move * self.speed
              self.pos += move
  
          #later on down the line i want to make the opponent sprite move
          if move.length() == 0 or move_length &lt; self.speed:
              self.next_point = pygame.Vector2(next(self.path))
  
          self.rect.topleft = self.pos
  
          for i in range(0, self.currentspell.loop):
              if pygame.time.get_ticks() - self.ticks &gt; self.currentspell.tick_delay:
                  self.ticks = pygame.time.get_ticks()
                  self.shoot()
  
          time_gone = pygame.time.get_ticks() - self.ticks
          for bullet in self.queue:
              if bullet[0] &lt;= time_gone:
                  Bullet(self.rect.center, bullet[1], self.currentspell.bullet, sprites, bullets)
  
          self.queue = [bullet for bullet in self.queue if bullet[0] &gt; time_gone]
          return
  
      def shoot(self):
          pattern = self.currentspell.pattern
          self.queue = pattern
  
  
  
  class Bullet(pygame.sprite.Sprite):
  
      def __init__(self, pos, direction, image, *groups):
          super().__init__(*groups)
          self.image = image
          self.rect = self.image.get_rect(topleft=pos)
          self.direction = direction
          self.pos = pygame.Vector2(self.rect.topleft)
  
      def update(self):
          self.pos += self.direction
          self.rect.topleft = (self.pos.x, self.pos.y)
          if not screen.get_rect().colliderect(self.rect):
              self.kill()
  
  sprites = pygame.sprite.Group()
  bullets = pygame.sprite.Group()
  
  opponentgroup = pygame.sprite.Group()
  mi1 = Spell(pygame.image.load("Sprites/lightblue-glowey.png"),(
              (0, pygame.Vector2(-0.5, 1) * 4),
              (0, pygame.Vector2(0, 1) * 4),
              (0, pygame.Vector2(0.5, 1) * 4)),
              10, 8, 340
              )
  
  mi2 = Spell(pygame.image.load("Sprites/purple-glowey.png"),(
              (0, pygame.Vector2(1, 1) * 4),
              (0, pygame.Vector2(-1, 1) * 4)),
              4,  8, 340
              )
  
  minty_spells = [mi1, mi2]
  
  player = Player(sprites)
  Minty = Opponent(pygame.image.load("Sprites/minty.png"), minty_spells, opponentgroup)
  
  opponents = [Minty]
  
  pygame.init()
  
  SCREENWIDTH = 1000
  SCREENHEIGHT = 650
  screen = pygame.display.set_mode([SCREENWIDTH, SCREENHEIGHT])
  
  screen.fill((255, 123, 67))
  pygame.draw.rect(screen, (0, 255, 188), (50, 50, 900, 575), 0)
  background = screen.copy()
  
  clock = pygame.time.Clock()
  
  currentopponent = 0
  def closegame():
      pygame.quit()
      return
  def stage(opponent, background, bgm):
      currentopponent = opponent
      for spell in opponents[opponent].sequence:
          op = opponents[opponent]
          op.update()
          op.spellno += 1
  
  def main():        
      running = True
      while running:   
          for events in pygame.event.get():
              if events.type == pygame.QUIT:
                  pygame.quit()
                  return
  
          # update all sprites
          sprites.update()
          sprites.add(opponents[currentopponent])
          # draw everything
          screen.blit(background, (0, 0))
          stage(0, "", "") # "" means placeholder. i'm working on them
  
          sprites.draw(screen)
          pygame.display.update()
  
          clock.tick(100)
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>Original Code and assets on my GitHub: <a href="https://github.com/E-Lee-Za/Eleeza-Crafter-The-Game" rel="nofollow noreferrer">https://github.com/E-Lee-Za/Eleeza-Crafter-The-Game</a></p>