Loading...
Logo
Processing Forum

Adventure Game.

in Programming Questions  •  3 years ago  
I am a newbie programmer and am  Trying to make a game in processing much like zelda, using classes as each room. but whenever i use the code, i get NullPointerException.
 

//works for screen being 500 i think
//note this is a work in progress
// a zelda esque adventure type thing

final static int up = 1;
final static int right = 2;
final static int down = 4;
final static int left = 8;
int bushcol=color(50,160,20);
int landcol=color(255,197,130);//a orangeish brown
//following four variables work with health
//if the healthxdist becomes 0, there will be no more health on the health bar.
//this causes you to die.
//bummer.
int healthx=0;
int healthy=0;
int healthxdist=30;
int healthydist=10;
 

class Hero {
  int hx;//hero x
  int hy;//hero y
  int hrad;//hero radius
 
 
  Hero(){
    hrad=10;
    hx=0;
    hy=0;
  }
 
  void setLocation(int temphx, int temphy){
    hx=temphx;
    hy=temphy;
  }
  void display(){
    stroke(0,255,50);
    fill(0,255,50);
    ellipse(hx,hy,hrad*2,hrad*2);
  }
}
class Area1{
  int monster1x=0;//x of monster
  int monster1y=250;//y of monster
  int monsterspeed=7;
  int monster_rad=25;//speed of monster
  int monstercol=color(255,0,0);
  int bush1x=100;//bush 1 x
  int bush2x=400;//bush 2 x
  int bush1y=100;//bush 1 y
  int bush2y=400;//bush 2 y
  int bushrad=50;//radius of square bush
  int pitx=25;
  int pity=475;
  int pitrad=30;
 
 
  void display(){
    fill(bushcol);//fills bush with a dark green
    rect(bush1x,bush1y,bushrad,bushrad);//creates bush numero uno
    rect(bush2x,bush2y,bushrad,bushrad);//here comes bush number 2!
    fill(monstercol);
    ellipse(monster1x,monster1y,monster_rad*2,monster_rad*2);//a monster draws near...
    background(landcol);
  }
  void move(){
    monster1x=monster1x+monsterspeed;//moving the monster
    if(monster_rad+monster1x>500||monster1x<0){
      monsterspeed=-1*monsterspeed;//lets the monster go back and forth...
    }
  }
}
class Dungeon1{
  int lightx=10;//x of mystical light
  int lighty=475;//y of mystical light
  int lightrad=32;//radius of mystical light
  int lightcol=color(200,200,0);//color of mystical light(lello)
  int dungeoncol=color(0);//color of dungeon floor=black
  void display(){
    stroke(lightcol);
    fill(lightcol);
    ellipse(lightx,lighty,lightrad,lightrad);
    background(dungeoncol);
  }
}
class Mainmenu{
  String txt1;//first string in menu
  String txt2;//second string in menu
  String txt3;//third string in menu
  String txt4;// fourth string in menu
    void mainmenu(){
       txt1=("welcome to the Adventure");
       txt2=("controlls are j,k,i,and l to move");
       txt3=("you can shoot your gun with Q");
       txt4=("to start your noble quest, press 'b'");
    }
 
 
  void display(){//this displays the main menu
    text(txt1,100,100);
    text(txt2,100,200);
    text(txt3,100,300);
    text(txt4,100,400);
  }
}

void setup(){
  size(500,500);
  Mainmenu mainmenu;
  mainmenu= new Mainmenu();
  mainmenu.display();
}
 
 
i realize there might be some other errors but could someone please help me with this?
Thanks in Advance!

Replies(6)

Re: Adventure Game.

3 years ago
the class Mainmenu does not have a constructor or you've misnamed the constructor the code should be:

Copy code
  1. class Mainmenu{
      String txt1;//first string in menu
      String txt2;//second string in menu
      String txt3;//third string in menu
      String txt4;// fourth string in menu
        Mainmenu(){
           txt1=("welcome to the Adventure");
           txt2=("controlls are j,k,i,and l to move");
           txt3=("you can shoot your gun with Q");
           txt4=("to start your noble quest, press 'b'");
        }
     
     
      void display(){//this displays the main menu
        text(txt1,100,100);
        text(txt2,100,200);
        text(txt3,100,300);
        text(txt4,100,400);
      }
The constructor shares the same name as the class

Re: Adventure Game.

3 years ago
Yep, Bjordal is right. A class constructor must have NO return type and be named same as class, including capitalization.

Many things in this code are irrational.. well, just go on CREATING, experience comes only as a result of experiencing things. You are making a good start anyway! More points that I can add from myself:

1. Your healthxdist is not used yet anywhere, and I advice you to get rid of it. Why determining a players health with his health bar length? It is like doing eye surgery through the black hole ) Avoid such things. So it would better be:

Copy code
  1. class Hero
  2. {
  3. ...
  4. int health; //instead of all your healths things
  5. ...
  6. }

then create a class called GUI and place all you gui variables and drawing functions there

Copy code
  1. Hero player = new Hero(everythingsetupplayerhere);

  2. class GUI
  3. {
  4.       ...
  5.       void Draw()
  6.       {
  7.             rect (0, 0, player.health * 5, 10); //so that each healthpoint will be                                                 //exactly 5 pixels of the healtbar.
  8.       }
  9. }
Of course there is a better way by making bars also a class, so you could hold all the bars you need there and just run through them on drawing your gui.

2. Use arrays! Arrays are great! Instead of having tonns of variables like monster1x/monster2x just do this:

Copy code
  1. final int X = 0;
  2. final int Y = 1;

  3. class Monster
  4. {
  5.   int[] position;

  6.    Monster(int x, int y)
  7.       {
  8.             int position = new int[2];
  9.             position[X] = x;
  10.             position[Y] = y;
  11.       }
  12. }
So later you can just use things like monster.position[X]....

3. Arrays are even better than you think! Organize class objects into arrays! Say:

Copy code
  1. class Room
  2. {
  3.       ArrayList monsters;

  4.       Room()
  5.       {
  6.             ArrayList monsters = new ArrayList();
  7.       }      
  8.       
  9.       void AddMonster(x,y,kind, health..anythingyouwishelsehere)
  10.       {
  11.             monsters.add(new Monster(x,y, anythingyouwant); //THAT'S IT! It is power of OOP.
  12.       }

  13.       void Update()
  14.       {
  15.             for (int i = 0; i < monsters.size(); i++)
  16.             {
  17.                    Monster tempMonster = (Monster) monsters.get(i);
  18.                   tempMonster.update();
  19.              }
  20.       }
  21. }
Here I used ArrayList which is similar to a simple array except that it works better with class objects.

4. Escape sequences...

instead of
Copy code
  1.        txt1=("welcome to the Adventure");
  2.        txt2=("controlls are j,k,i,and l to move");
  3.        txt3=("you can shoot your gun with Q");
  4.        txt4=("to start your noble quest, press 'b'");
you can do 
Copy code
  1.       menutext = "welcome to the Adventure\ncontrolls are j,k,i,and l to move\nyou can shoot your gun with Q\nto start your noble quest, press 'b'";
which is more logical, although, may seem less comfortable... so:
Copy code
  1. String[] menuStrings = new String[4];
  2. menustrings[0] = "welcome to the Adventure";
  3. ...
  4. for (int i = 0; i< menuStrings.length; i++)
  5. {
  6.       text (menuStrings[i], 100, i*100);
  7. }
5. Needless to say... you better organize everything in classes since you were given the ability:

Copy code
  1. class Game
  2. {
  3.       ArrayList Dungeons;
  4. }

  5. class Dungeon
  6. {
  7.       ArrayList Rooms;
  8.       Tileset tileset;
  9. }

  10. class Room
  11. {     
  12.       ArrayList Monsters;
  13.       ArrayList Items;
  14.       ArrayList Doors;
  15.       Tileset tileset;
  16.       int[] RoomTileMap;
  17.       ...
  18. }
So you see... everything can be highly stucturized thus helping you to organize your code in a better way. You may consider drawing a concept map, with descriptions, which classes you have and what is the hierarchy, relations and interactions between them. After you have a more or less clear picture of the structure of your game, coding is much easier, believe me.. although, on newbie steps, while starting (and to be true, non-newbies get into this also), it is obviously that you possibly cannot foresee everything you will want to implement so create only a draft structure for basic concepts, later it is easily extendable.

I am fond of creating games and love roguelikes so feel free to ask, I will be very glad to help!

Re: Adventure Game.

3 years ago
To quote the Processing Overview in the learning section:
Don't start by trying to build a cathedral
A "game ... much like zelda" is not a good choice for a first project: it's the sort of thing that takes a team of developers years to make!  Get to grips with the basics first, perhaps using elements of a game as targets for your experiments (e.g. keyboard controls, interface elements, room layout etc...), but breaking them down into separate sketches.  Don't try and build the whole thing in one go: you'll almost certainly doom yourself to failure.

Good luck!

Re: Adventure Game.

3 years ago
2Blindfish: actually you are right. But I think that it is possible for a newbie to write a game much like rogue, especially that by reading Zeldaguru's code I can assume he has quite a potential. Even something like a rogue, working, though, simple enough, may be a proof of ones abilities. I was using module approach when I was learning - first I wrote a very draft thing, with most of the placeholders I could think of. Then I began creating all the 'subsystems' treating them as separate tasks. This way I was gradually implementing gameworld logic, input, graphics... many mistakes were made, but when all these things got finnaly assembled into something more or less solid.. well.. I was happy )

Re: Adventure Game.

3 years ago
Thanks for replying to my post, everyone. i understand what you are trying to say. but i see that it would take a lot of time to fully make a game. what im trying to do with this is use it as sort of a demo for now. Thanks alot!

Re: Adventure Game.

3 years ago
So you post your demo, when ready so we can see it! )