import java.awt.*;
import java.lang.System;
import java.awt.image.*;

class Playfield extends Raster {
  /**
   *The playfield is the picture that is drawn on the screen.  It includes
   *all the Sprites (yes, you can have as many as you want) and saves
   *the background so that we don't lose it after drawing the current
   *frame of the animation.
   */
  Raster background;  //background image
  AnimatedSprite sprite[];  //list of sprites on this playfield
  int totalSprites; //the total number of sprites
  
  public Playfield (Image bgnd, int numSprites) {
    /**
     *creates a playfield by saving the background and allocating
     *an array for the given number of sprites
     */
    super(bgnd); //Set the current playfield to be just the background
    background = new Raster(bgnd);  //save the background for later refreshing
    sprite= new AnimatedSprite[numSprites]; //allocate space for sprites array
    totalSprites = numSprites;  //save the total numbe of sprites
  }

  public void addSprite(int i, AnimatedSprite s) {
    /**
     *Adds the given sprite to the array of sprites
     */
    if (i<totalSprites)
      sprite[i]= s;
  }
    
  public void Draw() {
    /**
     *Draws the next frame for the playfield.  This includes the background,
     *and all the sprites on top of the background.  Later sprites are drawn
     *on top of earlier sprites
     *Also, as each sprite is drawn, it's nextState() method is called to
     *create the animation sequence
     */
    //first, copy the background into playfield
    System.arraycopy(background.pixel, 0, super.pixel, 0, height*width);
    //draw each of the sprites on the playfield
    for (int s=0; s<totalSprites; s++) {
      sprite[s].Draw(this);
      sprite[s].nextState();  //increment each sprite to it's next state
    }
  }
}






