Can't put "Game Over" when I lose the game

edited April 2017 in Library Questions

I tried to have a text pop up that said "Game Over" but it keeps giving me an error.

Error: (IndexOutOfBoundsException; Index; 1 ,Size: 1)

I would also LOVE to add a score board (with names and high scores) if someone's kind enough to help me through that.

import processing.video.*;
Movie intro;
Movie bg;

import ddf.minim.*;
import ddf.minim.effects.*;
Minim minim;
AudioPlayer transition1;
AudioPlayer music1;

ArrayList x = new ArrayList(), y = new ArrayList();
int w = 50, h = 50, bs = 20, dir = 2, computerx = 12, computery = 10;

int stage;
int[] dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0}; 
boolean gameover = false;
int score;

void setup() {
  stage = 1;
  size( 1000, 1000, P3D);
  //size(w * bs, h * bs, P3D);
  minim = new Minim(this);
  intro = new Movie(this, "I_LUV_U.mp4");

  intro.loop();
  

  bg = new Movie(this, "background.mp4");
  frameRate(30);
  x.add(5);
  y.add(5);
}

void movieEvent(Movie m) {
  m.read();
}

void keyPressed() {
  if (keyCode == ENTER) {
    transition1 = minim.loadFile("transition1.mp3", 2048);
    transition1.play();
    music1 = minim.loadFile("song2_01.mp3", 2048);
    music1.loop();
    //////////////////////////////////////
  } else if (keyPressed == true) {
    int newdir = key=='s' ? 0 : (key=='w' ? 1 : (key=='d' ? 2 : (key=='a' ? 3 : -1)));
    //////////////////////////////////////
    if (newdir != -1 && (x.size() <= 1 || !(x.get(1) ==x.get(0) + dx[newdir] && y.get (1) == y.get(0) + dy[newdir]))) dir = newdir;
  }
}

void draw() {
  image(intro, 0, 0);
  fill(255);
  textSize(15);
  text("Press ENTER to begin!", 10, 990);
  if (keyCode == ENTER) {
    stage = 2;
  }
  //////////////////////////////////////
  if (stage == 2) {
    intro.stop();
    image(bg, 0, 0);
    bg.play();
    for (int i = 0; i < w; i++) line (i *bs, 0, i *bs, height);
    for (int i = 0; i < h; i++) line (0, i *bs, width, i *bs);
    for (int i = 0; i < x.size(); i++) {
      fill(255);
      rect(x.get(i)*bs, y.get(i)*bs, bs, bs);
    }
    //////////////////////////////////////
    if (!gameover) {
      fill(100, 150, 250);
      rect(computerx*bs, computery*bs, bs, bs);
      //////////////////////////////////////
      if (frameCount%2==0) {
        x.add(0, x.get(0)+ dx[dir]);
        y.add(0, y.get(0)+ dy[dir]);
        //////////////////////////////////////
        if (x.get(0) < 0 || y.get(0) < 0 || x.get(0) >= w || y.get(0) >= h) gameover = true;
        for (int i = 1; i < x.size(); i++) if (x.get(0) == x.get(i) &&  y.get(0) == y.get(i)) gameover = true;
        if (x.get(0)==computerx && y.get(0)==computery) {
          computerx= (int)random(0, w);
          computery= (int)random(0, h);
          //////////////////////////////////////
        } else {
          x.remove(x.size()-1);
          y.remove(y.size()-1);
        }
        //////////////////////////////////////
      }
    } else {
      fill(0);
      textSize(100);
      textAlign(CENTER);
      text("GAME OVER", width/2, height/2);
      if (keyPressed&&key==' ')
        x.clear();
      y.clear();
      x.add(5);
      y.add(5);
      gameover = false;
    }
    if (x.get(0)==computerx && y.get(0)==computery) {
      computerx = (int)random(0, w);
      computery = (int)random(0, h);
    }
  }
}

Thank you!

Tagged:

Answers

  • Format your code.
    Press the gear icon to edit your post.
    Select your code and press ctrl + o.
    Leave a line above and below.

  • You are using stages/states - can't game over just be an additional stage?

  • Sorry about that, I'm a pretty new user to the forums and to Processing itself. I just formatted the code for you so it's easier :)

  • Which line number: IndexOutOfBoundsException?

  • Answer ✓

    I think you want lines 99 to 103 inside of {

    }

  • Thank you so much, adding the brackets works! How do you think I can add a points counter to the game?

  • before setup say

    int score=0; 
    

    add an appropriate place say score++;

    somewhere in the corner say text(score,18,18);

  • Thank you so much! I had to play around with the code but I followed what you said as a start :)

Sign In or Register to comment.