Making an "avoid the falling objects game", having array problems

Hello, im trying to make a game where you keep a lit match from getting hit by water. The problem I am having now is that I want the drops of water to appear over time and not all at once. I have a drop class and a water class (this has an array of drop objects). Just curious as to how that might work, any general tips for anything you see are helpful! thanks!

/*
Final project by: Lucas Kern

 Last updated: 12/01/14

 Idea: Have a user controlled match that when struck has a flame animation.
 If this flame gets close enough to a fire sprinkler then it activates sending
 water everywhere. While fire is going a song about heat is playing in background.
 colors flash and other stuff happening. Array for water effect, PImage and frames for
 flame. User input for location. Possible text in the background.
 */

        Match m1;
        Flicker f1;
        Water w1;

        float xLoc;
        float yLoc;

        boolean pause = true;
int gameState = 0;

int difficulty = 200;

void setup()
{
  size(900, 900, P2D);
  colorMode(HSB);

  m1 = new Match (6, "Match");
  f1 = new Flicker(mouseX, mouseY, width / 4);
  w1 = new Water(difficulty, 10);
}


void draw()
{
  background(20);

  //gets mouse coords for flicker
  xLoc = mouseX;
  yLoc = mouseY;

  if (gameState == 0)
  {

    m1.display(width / 2, height / 2, 0);
  } else if (gameState == 1)
  {
    //start the match animations and allow it to be moved
    f1.display();
    m1.display(mouseX, mouseY, 1);
    w1.display();

    //Make water begin falling
  }
}

void keyPressed()
{
  if (key == 's')
  {
    gameState = 1;
  } else if (key == 'r')
  {
    //pause = true;
    gameState = 0;
  }
}


class Drop
{

  PShape droplet;

  float pointX = 0;
  float pointY = 0;

  float dropStart;
  float fallSpeed;

  Drop(float speed, float xStart)
  {
    fallSpeed = speed;
    dropStart = xStart;

    droplet = createShape();
    droplet.beginShape();
    droplet.fill(150, 175, 200, 100);
    droplet.noStroke();
    droplet.vertex(pointX, pointY);
    droplet.vertex(pointX - 12, pointY + 20);
    droplet.vertex(pointX - 10, pointY + 21);
    droplet.vertex(pointX - 8, pointY + 22);
    droplet.vertex(pointX - 6, pointY + 23);
    droplet.vertex(pointX - 4, pointY + 24);
    droplet.vertex(pointX - 2, pointY + 25);
    droplet.vertex(pointX, pointY + 25);
    droplet.vertex(pointX + 2, pointY + 25);
    droplet.vertex(pointX + 4, pointY + 24);
    droplet.vertex(pointX + 6, pointY + 23);
    droplet.vertex(pointX + 8, pointY + 22);
    droplet.vertex(pointX + 10, pointY + 21);
    droplet.vertex(pointX + 12, pointY + 20);
    droplet.vertex(pointX, pointY);
    droplet.endShape();
  }

  void move()
  {
  }

  void display()
  {
    shape(droplet, dropStart, (height / 15) + fallSpeed);
  }
}


class Water
{
  Drop[] waterDrops;

  int dropSpeed;

  Water(int difficulty, int speed)
  {
    dropSpeed = speed;

    waterDrops = new Drop[difficulty] ;
    if (frameCount % dropSpeed == 0)
    {
      for (int i = 0; i < difficulty; i++)
      {
        waterDrops[i] = new Drop(20, random(width));
      }
    }
  }

  void move()
  {
  }

  void display()
  {

    for (int i = 0; i < difficulty; i++)
      waterDrops[i].display();
  }
}

class Match
{
  PImage[] images;
  int imageCount;
  int frame;


  //constructor that brings in the name of the file and the number of frames
  Match(int count, String imagePrefix)
  {
    imageCount = count;
    images = new PImage[imageCount];

    for (int i = 0; i < imageCount; i++)
    {
      String fileName = "Frames/" + imagePrefix + i + ".png";
      images[i] = loadImage(fileName);
    }
  }

  void display(float xLoc, float yLoc, int frameMove)
  {
    //only transition frame after an alotted time
    if (frameCount % 10 == 0) {
      frame = (frame+frameMove) % imageCount;
    }

    image(images[frame], xLoc, yLoc - width / 7.14, width / 5, height / 7.5);
  }
}         

class Flicker
{
  //used for mouse coords
  float xLoc;
  float yLoc;
  float lightW;

  //constructor that brings in mouse coords
  Flicker(float xCenter, float yCenter, float rayWidth)
  {
    xLoc = xCenter;
    yLoc = yCenter;
    lightW = rayWidth;
  }

  void display()
  {
    noStroke();

    //makes a gradient of circles that shimmer and flicker
    for (float i = lightW; i > 0; i--)
    {
      fill(40, lightW - i, lightW - i);
      ellipse(mouseX + width / 5.8, mouseY - width / 18, random(i), random(i));
    }
  }
}
Tagged:

Answers

  • Here is the full file if you want it, it has the match images so the program can run. link

  • in the class Drops give them random values -200,-20 for y

    in the constructor

    thus they come out one after another

    when one drop dies, assign it the same y random and a x random to restart it

    ;-)

  • edited December 2014

    for gamestate and key you can use switch()

    also the 0 and 1 you could give names

    final int gamestatePlay = 1;

    make it all upper case since it's a constant

    there are empty methods like move- delete them

    try more comments in draw()

    hit ctrl-t to auto-format

    ;-)

  • Thanks! I think I see what you're saying, looks good! The empty methods are filled in now, I just had not gotten to them yet. Ill update later if I can get everything working.

  • great!

    ;-)

Sign In or Register to comment.