Create your own adventure game -- adding images

I was making this choose your own adventure game and couldn’t figure out how to add images correlating to each situation (NOT options). I’m not done with the code, but could someone please help me out.

class Pair {
  String txt;
  int id;
  Pair(int iid, String itxt) {
    id = iid;
    txt = itxt;
  }
}
class State {
  Pair here;
  ArrayList links;
  State (int iid, String itxt) {
    here = new Pair(iid, itxt);
    links = new ArrayList();
  }
  void add_link(int to_id, String option_txt) {
    links.add(new Pair(to_id, option_txt));
  }
  void draw() {
    text(here.txt, 20, 20, width-40, height/2-20);
    for (int i=0; i< links.size(); i++) {
      text(links.get(i).txt, 20, height/2+20+(i <em>height/4), width-40, height/4);
    }
  }
  void mouse() {
    if (mouseY&gt;
    3</em> height/4) {
      if (links.size()&gt;
      1) {
        state=links.get(1).id;
      } 
      return;
    }
    if (mouseY&gt;
    height/2) {
      state= links.get(0).id;
      return;
    }
  }
}

State[] states = new State[1000];
int state = 0;

void setup() {
  size(2000, 900, P3D);

  states[0] = new State(0, "You just survived a plane crash and now have to fend for yourself on the Island");
  states[0].add_link( 1, "Look for shelter. --&gt;");
  states[0].add_link( 2, "Look for other survivors. --&gt;");

  states[1] = new State( 1, "You find a uninhabited cave and the sun is almost setting");
  states[1].add_link( 3, "Look for food.");
  states[1].add_link( 4, "Look for wood to make a fire. --&gt;");

  states[2] = new State( 2, "You find one survivor but he’s just a useless tourist and the sun is almost setting.");
  states[2].add_link( 5, "Look for shelter. --&gt;");
  states[2].add_link( 6, "Look for food. --&gt;");

  states[3] = new State( 3, "You search for food but couldn’t find anything. You encounter a pack of wolves and are killed. ");
  states[3].add_link( 0, "GAME OVER. --&gt;");

  states[4] = new State( 4, "You return back to your cave and make a fire. It’s also nightime.");
  states[4].add_link( 7, "Go to sleep. --&gt;");
  states[4].add_link( 8, "Find food. --&gt;");

  states[5] = new State( 5, "You and your friend were only able to find a tree to sleep in for the night.");
  states[5].add_link(9, "Sleep to the next day");
  states[5].add_link(10, "Find food ");

  states[6] = new State(6, "You look for food but couldnt find any. You encounter a brown bear and it mauls you to death");
  states[6].add_link(0, "GAME OVER");

  states[7] = new State( 7, "You spend the night in your cave and wake up the next morning.Your starting to get hungry and thristy.");
  states[7].add_link(11, "Find food ");
  states[7].add_link(12, "Find water ");

  states[8] = new State( 8, "You scavange for food in the middle of the night.You trip on a branch and snap your neck");
  states[8].add_link(0, "GAME OVER");

  states[9] = new State( 9, "You and your friend spend the night in your tree and wake up the next morning. You’re hungry and thirsty.");
  states[9].add_link(13, "Find water ");
  states[9].add_link(14, "Find food ");

  states[10] = new State( 10, "You and your friend find and eat a couple of wild berries.");
  states[10].add_link(15, "Go back to your tree and sleep");

  states[11] = new State( 11, "You were able to find a full grown deer and a rabbit .");
  states[11].add_link(16, "Kill the rabbit");
  states[11].add_link(17, "Kill the deer ");

  states[12] = new State( 12, "You head down to a nearby stream and drink from it. You’re are still hungry.");
  states[12].add_link(18, "Find some plants to eat");
  states[12].add_link(19, "Find an animal to eat");

  states[13] = new State( 13, "The two of you search for water and find a small .");
  states[13].add_link(20, "Kill the rabbit");
  states[13].add_link(21, "Kill the deer ");
}
void draw() {
  background(0);
  fill(255);
  states[state].draw();
  if ( frameCount == 1 ) {
  }
}
void mousePressed() {
  states[state].mouse();
}

Example of images we want to use:

image

1 Like

nicely done! Congrats!

for image: since the image is part of the screen (situation) you are in, make PImage scene; part of the class State.

in setup() say something like states[0].add_Image( "Beach.jpg" );

in the class State make a function add_Image and use loadImage here
look at loadImage in the reference

https://www.processing.org/reference/loadImage_.html

and in states[state].draw();
use image() : image(scene, width/2, height/2);

Formatting and Posting

When coding in processing use ctrl-t regularly, it gives you auto-format especially for indents.

Use it before posting in the forum. When posting here, tell the forum software where your code is.
Mark it with mouse and hit </> in the command bar.

Chrisir

2 Likes

As Chrisir say, format is important for posting here. And if you can, format the title of the post too. One of the objetives of this forum is make a space for asking for help, so its not necesary ask for it in the title.

Thanks you for helping me! Here’s what we got so far, but we’re still running into errors.

class Pair {
  String txt;
  int id;
  Pair(int iid, String itxt) {
    id = iid;
    txt = itxt;
  }
}
class State {
  PImage scene;

  Pair here;
  ArrayList<Pair> links;
  State (int iid, String itxt) {
    here = new Pair(iid, itxt);
    links = new ArrayList();
  }
  void add_link(int to_id, String option_txt) {
    links.add(new Pair(to_id, option_txt));
  }
  void draw() {
    text(here.txt, 20, 20, width-40, height/2-20);
    for (int i=0; i< links.size(); i++) {
      text(links.get(i).txt, 20, height/2+20+(i*height/4), width-40, height/4);
    }
  }
  void mouse() {
    if (mouseY>3*height/4) {
      if (links.size()>1) {
        state=links.get(1).id;
      } 
      return;
    }
    if (mouseY>height/2) {
      state= links.get(0).id;
      return;
    }
  }
  void add_Image() { 
    scene = loadImage("Beach.jpg");
  }
}

State[] states = new State[1000];
int state = 0;

void setup() {
  size(2000, 900, P3D);
  states[0].add_Image("Beach.jpg");



  states[0] = new State(0, "You just survived a plane crash and now have to fend for yourself on the Island");
  states[0].add_link( 1, "Look for shelter. -->");
  states[0].add_link( 2, "Look for other survivors. -->");


  states[1] = new State( 1, "You find a uninhabited cave and the sun is almost setting");
  states[1].add_link( 3, "Look for food.");
  states[1].add_link( 4, "Look for wood to make a fire. -->");

  states[2] = new State( 2, "You find one survivor but he's just a useless tourist and the sun is almost setting.");
  states[2].add_link( 5, "Look for shelter. -->");
  states[2].add_link( 6, "Look for food. -->");

  states[3] = new State( 3, "You search for food but couldn't find anything. You encounter a pack of wolves and are killed. ");
  states[3].add_link( 0, "GAME OVER.  -->");


  states[4] = new State( 4, "You return back to your cave and make a fire. It's also nightime.");
  states[4].add_link( 7, "Go to sleep. -->");
  states[4].add_link( 8, "Find food. -->");

  states[5] = new State( 5, "You and your friend were only able to find a tree to sleep in for the night.");
  states[5].add_link(9, "Sleep to the next day"); 
  states[5].add_link(10, "Find food ");

  states[6] = new State(6, "You look for food but couldnt find any. You encounter a brown bear and it mauls you to death");
  states[6].add_link(0, "GAME OVER. ");

  states[7] = new State( 7, "You spend the night in your cave and wake up the next morning.Your starting to get hungry and thristy.");
  states[7].add_link(11, "Find food "); 
  states[7].add_link(12, "Find water ");

  states[8] = new State( 8, "You scavange for food in the middle of the night.You trip on a branch and snap your neck");
  states[8].add_link(0, "GAME OVER. Man if only you had night vison"); 

  states[9] = new State( 9, "You and your friend spend the night in your tree and wake up the next morning. You're hungry and thirsty.");
  states[9].add_link(13, "Find water "); 
  states[9].add_link(14, "Find food ");


  states[10] = new State( 10, "You and your friend find and eat a couple of wild berries.");
  states[10].add_link(15, "Go back to your tree and sleep"); 


  states[11] = new State( 11, "You were able to find a full grown deer and a rabbit .");
  states[11].add_link(16, "Kill the rabbit"); 
  states[11].add_link(17, "Kill the deer ");

  states[12] = new State( 12, "You head down to a nearby stream and drink from it. You're are still hungry.");
  states[12].add_link(18, "Find some plants to eat"); 
  states[12].add_link(19, "Find an animal to eat");

  states[13] = new State( 13, "The two of you search for water and find a small nasty lake.Your friend takes a drink and tells you to drink it .");
  states[13].add_link(20, "Drink it "); 
  states[13].add_link(21, "Don't drink it ");

  states[14] = new State( 14, "You search for food and find youself near the shore with some eggs buried in the sand");
  states[14].add_link(22, "Dig up the eggs "); 
  states[14].add_link(23, "Leave the eggs alone ");

  states[15] = new State( 15, "You and your friend died in your sleep becuase of bad berries");
  states[15].add_link(0, "GAME OVER. At least they were tasty "); 

  states[16] = new State( 16, "You were able to capture and kill the rabbit.");
  states[16].add_link(24, "Eat it raw "); 
  states[16].add_link(25, "Cook it with a fire ");

  states[17] = new State( 17, "You try to attack the deer but it gorges you with its antlers");
  states[17].add_link(0, "GAME OVER; What did you think was gonna happen? "); 

  states[18] = new State( 18, "You found a tree that had red fruit and a bee hive.");
  states[18].add_link(26, "Look for honey"); 
  states[18].add_link(27, "Eat the red fruit ");

  states[19] = new State( 19, "You searched for an animal to eat and saw a wild boar go into some shrubs and a quail dart to the shore");
  states[19].add_link(28, "Dive into the shrubs "); 
  states[19].add_link(29, "Chase the quail ");

  states[20] = new State( 20, "You take a drink and suddleny feel sick. Both of you die because of typhoid days later.");
  states[20].add_link(0, "GAME OVER "); 


  states[21] = new State( 21, "You tell your friend you dont want to take a drink. He dies later from typhoid.");
  states[21].add_link(30, "Bury him at the shore "); 
  states[21].add_link(31, "Leave him at the lake");

  states[22] = new State( 22, "You dig up the eggs until you realsie your friend is attacked by alligators.");
  states[22].add_link(22, "Save him "); 
  states[22].add_link(23, "Run away");

  states[23] = new State( 23, "You leave the eggs alone and keep searching along the shore until you see a boat in the distance");
  states[23].add_link(32, "Yell for them "); 
  states[23].add_link(33, "Set up a fire");

  states[24] = new State( 24, "You ate the rabbit raw and died to Salmonella");
  states[24].add_link(0, "GAME OVER."); 

  states[25] = new State( 25, "You prepare the rabbit and eat it until you hear a loud noise close to the shore");
  states[25].add_link(34, "Stay in your home "); 
  states[25].add_link(35, "Go oustide");

  states[26] = new State( 26, "You tried to get some honey but were swarmed by thousands of bees. ");
  states[26].add_link(0, "GAME OVER. BTW you're allergic "); 

  states[27] = new State( 27, "You eat the fruit and feel a divine presence from above. You hear a voice and are struck by lightning");
  states[27].add_link(0, "GAME OVER. Maybe that was the forbidden fruit? "); 

  states[28] = new State( 28, "You chased the boar and dove head first into the shrubs. Unfortunatly there happened to be a Black Mamba.The snake bit you. ");
  states[28].add_link(0, "GAME OVER. Shouldve checked before you leaped. "); 

  states[29] = new State( 29, "You chase the quail to the shore when a helicopter happened to fly by and spotted you. They landed and took you back to civilization.");
  states[29] = new State(0, "YOU SURVIVED. Guess that quail was lucky.");

  states[30] = new State( 30, "You bury your friend at the shore and made a fire in honor of him.");
  states[30].add_link(36, "Mourn your friend for a little longer"); 
  states[30].add_link(37, "Search for your next meal");

  states[31] = new State( 31, "You decided to leave your friend for dead at the lake and he swore to curse you for the rest of your life");
  states[31].add_link(38, "Search for your next meal "); 
  states[31].add_link(39, "Go find a new home");

  states[32] = new State( 32, "You yell for help and waved your arms but the ship was too far from the island for them to see you. The ship sailed away and you lost your only chance to home.");
  states[32].add_link(0, "GAME OVER.  "); 

  states[33] = new State( 33, "You set up a fire and smoke went up in the air. The crew saw your signal and rescued you from the Island");
  states[33].add_link(0, "YOU SURVIVED. ");

  states[34] = new State( 34, "You decided to ignore the odd noise and stayed in your cave. Unfortunately, that was the search crew for the plane you were on. They couldn't find you in the dense forest and left. ");
  states[34].add_link(0, "GAME OVER. Guess you really wanted to finish that rabbit. "); 

  states[35] = new State( 35, "You go outside and happened to meet up with the search party for your plane. You tell them you were the only survivor and they take you back home.");
  states[35].add_link(0, "YOU SURVIVED "); 

  states[36] = new State( 36, "You decide to mourn your friend for a little longer until a navy ship honked their horn and rescued you. ");
  states[36].add_link(0, "YOU SURVIVED "); 

  states[37] = new State( 37, "You tried to find some food in the jungle and a spider latched onto you and bit you. You died to the deadly venom. ");
  states[37].add_link(0, "GAME OVER"); 

  states[38] = new State( 38, "You find a coconut tree, but when you tried to climb it, it mysteriously shaked and a coconut fell on your head.");
  states[38].add_link(0, "GAME OVER"); 

  states[39] = new State( 39, "You find a cave and the ghost of your friend appears laughing at you. The cave collapsed above you and crushed you to death.");
  states[39].add_link(0, "GAME OVER");
}
void draw() {
  background(0);
  fill(255);
  states[state].draw();
  image(scene, width/2, height/2);

  if ( frameCount == 1 ) {
  }
}
void mousePressed() {
  states[state].mouse();
}

Be more specific

What kind of errors

this line

states[0].add_Image("Beach.jpg");

must be AFTER

states[0] = new State(0, "You just survived a plane crash and now have to fend for yourself on the Island");


and

  void add_Image() { 
    scene = loadImage("Beach.jpg");
  }

must be

  void add_Image( String imgName )  { 
    scene = loadImage(imgName);
  }

These types of error.

The method add_Image() in the type CODE_FOR_FINAL_.State is not applicable for the arguments (String)

ok

did you apply my changes

Yes, I did and a new error popped up.
Here it is: Scene cannot be resolved to a variable

class Pair {
  String txt;
  int id;
  Pair(int iid, String itxt) {
    id = iid;
    txt = itxt;
  }
}
class State {
  PImage scene;

  Pair here;
  ArrayList<Pair> links;
  State (int iid, String itxt) {
    here = new Pair(iid, itxt);
    links = new ArrayList();
  }
  void add_link(int to_id, String option_txt) {
    links.add(new Pair(to_id, option_txt));
  }
  void draw() {
    text(here.txt, 20, 20, width-40, height/2-20);
    for (int i=0; i< links.size(); i++) {
      text(links.get(i).txt, 20, height/2+20+(i*height/4), width-40, height/4);
    }
  }
  void mouse() {
    if (mouseY>3*height/4) {
      if (links.size()>1) {
        state=links.get(1).id;
      } 
      return;
    }
    if (mouseY>height/2) {
      state= links.get(0).id;
      return;
    }
  }
  void add_Image( String imgName) { 
    scene = loadImage(imgName);
  }
}

State[] states = new State[1000];
int state = 0;

void setup() {
  size(2000, 900, P3D);




  states[0] = new State(0, "You just survived a plane crash and now have to fend for yourself on the Island");
  states[0].add_link( 1, "Look for shelter. -->");
  states[0].add_link( 2, "Look for other survivors. -->");
  states[0].add_Image("Beach.jpg");



  states[1] = new State( 1, "You find a uninhabited cave and the sun is almost setting");
  states[1].add_link( 3, "Look for food.");
  states[1].add_link( 4, "Look for wood to make a fire. -->");

  states[2] = new State( 2, "You find one survivor but he's just a useless tourist and the sun is almost setting.");
  states[2].add_link( 5, "Look for shelter. -->");
  states[2].add_link( 6, "Look for food. -->");

  states[3] = new State( 3, "You search for food but couldn't find anything. You encounter a pack of wolves and are killed. ");
  states[3].add_link( 0, "GAME OVER.  -->");


  states[4] = new State( 4, "You return back to your cave and make a fire. It's also nightime.");
  states[4].add_link( 7, "Go to sleep. -->");
  states[4].add_link( 8, "Find food. -->");

  states[5] = new State( 5, "You and your friend were only able to find a tree to sleep in for the night.");
  states[5].add_link(9, "Sleep to the next day"); 
  states[5].add_link(10, "Find food ");

  states[6] = new State(6, "You look for food but couldnt find any. You encounter a brown bear and it mauls you to death");
  states[6].add_link(0, "GAME OVER. ");

  states[7] = new State( 7, "You spend the night in your cave and wake up the next morning.Your starting to get hungry and thristy.");
  states[7].add_link(11, "Find food "); 
  states[7].add_link(12, "Find water ");

  states[8] = new State( 8, "You scavange for food in the middle of the night.You trip on a branch and snap your neck");
  states[8].add_link(0, "GAME OVER. Man if only you had night vison"); 

  states[9] = new State( 9, "You and your friend spend the night in your tree and wake up the next morning. You're hungry and thirsty.");
  states[9].add_link(13, "Find water "); 
  states[9].add_link(14, "Find food ");


  states[10] = new State( 10, "You and your friend find and eat a couple of wild berries.");
  states[10].add_link(15, "Go back to your tree and sleep"); 


  states[11] = new State( 11, "You were able to find a full grown deer and a rabbit .");
  states[11].add_link(16, "Kill the rabbit"); 
  states[11].add_link(17, "Kill the deer ");

  states[12] = new State( 12, "You head down to a nearby stream and drink from it. You're are still hungry.");
  states[12].add_link(18, "Find some plants to eat"); 
  states[12].add_link(19, "Find an animal to eat");

  states[13] = new State( 13, "The two of you search for water and find a small nasty lake.Your friend takes a drink and tells you to drink it .");
  states[13].add_link(20, "Drink it "); 
  states[13].add_link(21, "Don't drink it ");

  states[14] = new State( 14, "You search for food and find youself near the shore with some eggs buried in the sand");
  states[14].add_link(22, "Dig up the eggs "); 
  states[14].add_link(23, "Leave the eggs alone ");

  states[15] = new State( 15, "You and your friend died in your sleep becuase of bad berries");
  states[15].add_link(0, "GAME OVER. At least they were tasty "); 

  states[16] = new State( 16, "You were able to capture and kill the rabbit.");
  states[16].add_link(24, "Eat it raw "); 
  states[16].add_link(25, "Cook it with a fire ");

  states[17] = new State( 17, "You try to attack the deer but it gorges you with its antlers");
  states[17].add_link(0, "GAME OVER; What did you think was gonna happen? "); 

  states[18] = new State( 18, "You found a tree that had red fruit and a bee hive.");
  states[18].add_link(26, "Look for honey"); 
  states[18].add_link(27, "Eat the red fruit ");

  states[19] = new State( 19, "You searched for an animal to eat and saw a wild boar go into some shrubs and a quail dart to the shore");
  states[19].add_link(28, "Dive into the shrubs "); 
  states[19].add_link(29, "Chase the quail ");

  states[20] = new State( 20, "You take a drink and suddleny feel sick. Both of you die because of typhoid days later.");
  states[20].add_link(0, "GAME OVER "); 


  states[21] = new State( 21, "You tell your friend you dont want to take a drink. He dies later from typhoid.");
  states[21].add_link(30, "Bury him at the shore "); 
  states[21].add_link(31, "Leave him at the lake");

  states[22] = new State( 22, "You dig up the eggs until you realsie your friend is attacked by alligators.");
  states[22].add_link(22, "Save him "); 
  states[22].add_link(23, "Run away");

  states[23] = new State( 23, "You leave the eggs alone and keep searching along the shore until you see a boat in the distance");
  states[23].add_link(32, "Yell for them "); 
  states[23].add_link(33, "Set up a fire");

  states[24] = new State( 24, "You ate the rabbit raw and died to Salmonella");
  states[24].add_link(0, "GAME OVER."); 

  states[25] = new State( 25, "You prepare the rabbit and eat it until you hear a loud noise close to the shore");
  states[25].add_link(34, "Stay in your home "); 
  states[25].add_link(35, "Go oustide");

  states[26] = new State( 26, "You tried to get some honey but were swarmed by thousands of bees. ");
  states[26].add_link(0, "GAME OVER. BTW you're allergic "); 

  states[27] = new State( 27, "You eat the fruit and feel a divine presence from above. You hear a voice and are struck by lightning");
  states[27].add_link(0, "GAME OVER. Maybe that was the forbidden fruit? "); 

  states[28] = new State( 28, "You chased the boar and dove head first into the shrubs. Unfortunatly there happened to be a Black Mamba.The snake bit you. ");
  states[28].add_link(0, "GAME OVER. Shouldve checked before you leaped. "); 

  states[29] = new State( 29, "You chase the quail to the shore when a helicopter happened to fly by and spotted you. They landed and took you back to civilization.");
  states[29] = new State(0, "YOU SURVIVED. Guess that quail was lucky.");

  states[30] = new State( 30, "You bury your friend at the shore and made a fire in honor of him.");
  states[30].add_link(36, "Mourn your friend for a little longer"); 
  states[30].add_link(37, "Search for your next meal");

  states[31] = new State( 31, "You decided to leave your friend for dead at the lake and he swore to curse you for the rest of your life");
  states[31].add_link(38, "Search for your next meal "); 
  states[31].add_link(39, "Go find a new home");

  states[32] = new State( 32, "You yell for help and waved your arms but the ship was too far from the island for them to see you. The ship sailed away and you lost your only chance to home.");
  states[32].add_link(0, "GAME OVER.  "); 

  states[33] = new State( 33, "You set up a fire and smoke went up in the air. The crew saw your signal and rescued you from the Island");
  states[33].add_link(0, "YOU SURVIVED. ");

  states[34] = new State( 34, "You decided to ignore the odd noise and stayed in your cave. Unfortunately, that was the search crew for the plane you were on. They couldn't find you in the dense forest and left. ");
  states[34].add_link(0, "GAME OVER. Guess you really wanted to finish that rabbit. "); 

  states[35] = new State( 35, "You go outside and happened to meet up with the search party for your plane. You tell them you were the only survivor and they take you back home.");
  states[35].add_link(0, "YOU SURVIVED "); 

  states[36] = new State( 36, "You decide to mourn your friend for a little longer until a navy ship honked their horn and rescued you. ");
  states[36].add_link(0, "YOU SURVIVED "); 

  states[37] = new State( 37, "You tried to find some food in the jungle and a spider latched onto you and bit you. You died to the deadly venom. ");
  states[37].add_link(0, "GAME OVER"); 

  states[38] = new State( 38, "You find a coconut tree, but when you tried to climb it, it mysteriously shaked and a coconut fell on your head.");
  states[38].add_link(0, "GAME OVER"); 

  states[39] = new State( 39, "You find a cave and the ghost of your friend appears laughing at you. The cave collapsed above you and crushed you to death.");
  states[39].add_link(0, "GAME OVER");
}
void draw() {
  background(0);
  fill(255);
  states[state].draw();
  image(scene, width/2, height/2);

  if ( frameCount == 1 ) {
  }
}
void mousePressed() {
  states[state].mouse();
}

you need to concentrate more

Here it is: Scene cannot be resolved to a variable

  • —>>>>> where did you define scene? In the class. So you must use it inside the class, not in the main function draw()

Where in the class State do you display the screen? In draw() inside the class - the function has the same name but it’s not the same function

Thank you it worked!

1 Like

Sorry to bother again, my code ran into another problem. We got the picture to load in and we have options for our survival game and the options have links attached to them to drag you onto another screen for more options. But, once we put the image onto the screen, it would not allow me to press on the options and it would make me stay on the starting screen. We have tried to move the image code around the void draw inside the class State but nothing has worked. Do you have any ideas? Sorry for all the questions, we’ve been learning code for a few months now…still newbs

class Pair {
  String txt;
  int id;
  Pair(int iid, String itxt) {
    id = iid;
    txt = itxt;
  }
}
class State {
  PImage scene;

  Pair here;
  ArrayList<Pair> links;
  State (int iid, String itxt) {
    here = new Pair(iid, itxt);
    links = new ArrayList();
  }
  void add_link(int to_id, String option_txt) {
    links.add(new Pair(to_id, option_txt));
  }
  void draw() {
                 image(scene,  width/4, height/4);

    text(here.txt, 20, 20, width-40, height/2-20);
    for (int i=0; i< links.size(); i++) {
      text(links.get(i).txt, 20, height/2+20+(i*height/4), width-40, height/4);
    }

  }
  
  void mouse() {
    if (mouseY>3*height/4) {
      if (links.size()>1) {
        state=links.get(1).id;
      } 
      return;     

    }
    if (mouseY>height/2) {
      state= links.get(0).id;
      return;

    }

  }
  void add_Image( String imgName) { 
    scene = loadImage(imgName);
  }
}

State[] states = new State[1000];
int state = 0;

void setup() {
  size(2000, 900, P3D);
  
states[0] = new State(0, "You just survived a plane crash and now have to fend for yourself on the Island");
  states[0].add_link( 1, "Look for shelter. -->");
  states[0].add_link( 2, "Look for other survivors. -->");
  states[0].add_Image("Beach .jpg");



  states[1] = new State( 1, "You find a uninhabited cave and the sun is almost setting");
  states[1].add_link( 3, "Look for food.");
  states[1].add_link( 4, "Look for wood to make a fire. -->");
   

  states[2] = new State( 2, "You find one survivor but he's just a useless tourist and the sun is almost setting.");
  states[2].add_link( 5, "Look for shelter. -->");
  states[2].add_link( 6, "Look for food. -->");

  states[3] = new State( 3, "You search for food but couldn't find anything. You encounter a pack of wolves and are killed. ");
  states[3].add_link( 0, "GAME OVER.  -->");


  states[4] = new State( 4, "You return back to your cave and make a fire. It's also nightime.");
  states[4].add_link( 7, "Go to sleep. -->");
  states[4].add_link( 8, "Find food. -->");

  states[5] = new State( 5, "You and your friend were only able to find a tree to sleep in for the night.");
  states[5].add_link(9, "Sleep to the next day"); 
  states[5].add_link(10, "Find food ");

  states[6] = new State(6, "You look for food but couldnt find any. You encounter a brown bear and it mauls you to death");
  states[6].add_link(0, "GAME OVER. ");

  states[7] = new State( 7, "You spend the night in your cave and wake up the next morning.Your starting to get hungry and thristy.");
  states[7].add_link(11, "Find food "); 
  states[7].add_link(12, "Find water ");

  states[8] = new State( 8, "You scavange for food in the middle of the night.You trip on a branch and snap your neck");
  states[8].add_link(0, "GAME OVER. Man if only you had night vison"); 

  states[9] = new State( 9, "You and your friend spend the night in your tree and wake up the next morning. You're hungry and thirsty.");
  states[9].add_link(13, "Find water "); 
  states[9].add_link(14, "Find food ");


  states[10] = new State( 10, "You and your friend find and eat a couple of wild berries.");
  states[10].add_link(15, "Go back to your tree and sleep"); 


  states[11] = new State( 11, "You were able to find a full grown deer and a rabbit .");
  states[11].add_link(16, "Kill the rabbit"); 
  states[11].add_link(17, "Kill the deer ");

  states[12] = new State( 12, "You head down to a nearby stream and drink from it. You're are still hungry.");
  states[12].add_link(18, "Find some plants to eat"); 
  states[12].add_link(19, "Find an animal to eat");

  states[13] = new State( 13, "The two of you search for water and find a small nasty lake.Your friend takes a drink and tells you to drink it .");
  states[13].add_link(20, "Drink it "); 
  states[13].add_link(21, "Don't drink it ");

  states[14] = new State( 14, "You search for food and find youself near the shore with some eggs buried in the sand");
  states[14].add_link(22, "Dig up the eggs "); 
  states[14].add_link(23, "Leave the eggs alone ");

  states[15] = new State( 15, "You and your friend died in your sleep becuase of bad berries");
  states[15].add_link(0, "GAME OVER. At least they were tasty "); 

  states[16] = new State( 16, "You were able to capture and kill the rabbit.");
  states[16].add_link(24, "Eat it raw "); 
  states[16].add_link(25, "Cook it with a fire ");

  states[17] = new State( 17, "You try to attack the deer but it gorges you with its antlers");
  states[17].add_link(0, "GAME OVER; What did you think was gonna happen? "); 

  states[18] = new State( 18, "You found a tree that had red fruit and a bee hive.");
  states[18].add_link(26, "Look for honey"); 
  states[18].add_link(27, "Eat the red fruit ");

  states[19] = new State( 19, "You searched for an animal to eat and saw a wild boar go into some shrubs and a quail dart to the shore");
  states[19].add_link(28, "Dive into the shrubs "); 
  states[19].add_link(29, "Chase the quail ");

  states[20] = new State( 20, "You take a drink and suddleny feel sick. Both of you die because of typhoid days later.");
  states[20].add_link(0, "GAME OVER "); 


  states[21] = new State( 21, "You tell your friend you dont want to take a drink. He dies later from typhoid.");
  states[21].add_link(30, "Bury him at the shore "); 
  states[21].add_link(31, "Leave him at the lake");

  states[22] = new State( 22, "You dig up the eggs until you realsie your friend is attacked by alligators.");
  states[22].add_link(22, "Save him "); 
  states[22].add_link(23, "Run away");

  states[23] = new State( 23, "You leave the eggs alone and keep searching along the shore until you see a boat in the distance");
  states[23].add_link(32, "Yell for them "); 
  states[23].add_link(33, "Set up a fire");

  states[24] = new State( 24, "You ate the rabbit raw and died to Salmonella");
  states[24].add_link(0, "GAME OVER."); 

  states[25] = new State( 25, "You prepare the rabbit and eat it until you hear a loud noise close to the shore");
  states[25].add_link(34, "Stay in your home "); 
  states[25].add_link(35, "Go oustide");

  states[26] = new State( 26, "You tried to get some honey but were swarmed by thousands of bees. ");
  states[26].add_link(0, "GAME OVER. BTW you're allergic "); 

  states[27] = new State( 27, "You eat the fruit and feel a divine presence from above. You hear a voice and are struck by lightning");
  states[27].add_link(0, "GAME OVER. Maybe that was the forbidden fruit? "); 

  states[28] = new State( 28, "You chased the boar and dove head first into the shrubs. Unfortunatly there happened to be a Black Mamba.The snake bit you. ");
  states[28].add_link(0, "GAME OVER. Shouldve checked before you leaped. "); 

  states[29] = new State( 29, "You chase the quail to the shore when a helicopter happened to fly by and spotted you. They landed and took you back to civilization.");
  states[29] = new State(0, "YOU SURVIVED. Guess that quail was lucky.");

  states[30] = new State( 30, "You bury your friend at the shore and made a fire in honor of him.");
  states[30].add_link(36, "Mourn your friend for a little longer"); 
  states[30].add_link(37, "Search for your next meal");

  states[31] = new State( 31, "You decided to leave your friend for dead at the lake and he swore to curse you for the rest of your life");
  states[31].add_link(38, "Search for your next meal "); 
  states[31].add_link(39, "Go find a new home");

  states[32] = new State( 32, "You yell for help and waved your arms but the ship was too far from the island for them to see you. The ship sailed away and you lost your only chance to home.");
  states[32].add_link(0, "GAME OVER.  "); 

  states[33] = new State( 33, "You set up a fire and smoke went up in the air. The crew saw your signal and rescued you from the Island");
  states[33].add_link(0, "YOU SURVIVED. ");

  states[34] = new State( 34, "You decided to ignore the odd noise and stayed in your cave. Unfortunately, that was the search crew for the plane you were on. They couldn't find you in the dense forest and left. ");
  states[34].add_link(0, "GAME OVER. Guess you really wanted to finish that rabbit. "); 

  states[35] = new State( 35, "You go outside and happened to meet up with the search party for your plane. You tell them you were the only survivor and they take you back home.");
  states[35].add_link(0, "YOU SURVIVED "); 

  states[36] = new State( 36, "You decide to mourn your friend for a little longer until a navy ship honked their horn and rescued you. ");
  states[36].add_link(0, "YOU SURVIVED "); 

  states[37] = new State( 37, "You tried to find some food in the jungle and a spider latched onto you and bit you. You died to the deadly venom. ");
  states[37].add_link(0, "GAME OVER"); 

  states[38] = new State( 38, "You find a coconut tree, but when you tried to climb it, it mysteriously shaked and a coconut fell on your head.");
  states[38].add_link(0, "GAME OVER"); 

  states[39] = new State( 39, "You find a cave and the ghost of your friend appears laughing at you. The cave collapsed above you and crushed you to death.");
  states[39].add_link(0, "GAME OVER");
}
void draw() {
  background(0);
  fill(255);
  states[state].draw();


  if ( frameCount == 1 ) {
  }
}
void mousePressed() {
  states[state].mouse();
}

The only thing I see:

The function draw() in the class always executes this line:

image(scene,  width/4, height/4);

but when you haven’t added a image for that scene, sketch will crash since scene (the image) is not defined. (Error message in processing Null Pointer Exception)

You can check if the image is there:

  void draw() {
    if (scene!=null) 
      image(scene, width/4, height/4);

to avoid the error : if (scene!=null)

Remark

use add_Image in setup() to add images to the other states. The states without image just don’t show an image then (if you have the new line above).

Otherwise the code runs here, the image does not disturb the mouse input

Chrisir

1 Like