Hunter Hunter = new Hunter(11,11,"H");
I did not examine everything in detail but what looked wrong was the piece above.
Can you name the variable same as the class? If you can, then it is at least bad style.
I would prefer something like
Hunter hunter = new Hunter(...);
I see a route1() and paint() method. Maybe you mixed up the terms?
I don’t completely understand what you are doing. You say that both methods are called every time the user presses one of the movement keys.
I have now looked a bit more at your code. It seems that you are creating new objects at each cycle of your game loop. And you will always give them the same starting coordinates.
If you want to save the state of player and hunters over the loop, then you should initialize your player once, create the object and use the reference to this one object without throwing it away.
public void route1(){
Board board = new Board();
Hunter Hunter = new Hunter(11,11,"H");
Scanner in = new Scanner(System.in);
Random number = new Random(2);
int random = number.nextInt(2);
if(random ==1)
Hunter.x = Hunter.x -1;
else
Hunter.y = Hunter.y -1;
}
A variable has a certain scope. If you create a variable within a method (like “Hunter” here. use hunter as name, not Hunter. Hunter is your class), it will be in your program’s memory until the method ends.
This is what you do:
Player does something. Hunters have to be updated
- create a hunter object at a position
- move the newly created hunter into a random direction
- forget that the hunter ever existed (you don’t remember the reference to it)
What you should do:
Your Hunter-class has only a constructor
public Hunter(int x, int y, String name){
this.x = x;
this.y = y;
this.name = name;
}
You should have a move() method and access to your coordinates as well.
Then you could tell the hunter to move. Then you would not create the hunter
within the route1() method but outside your game loop in your Main class. Then,
when it comes to updating the hunters’ positions, you do something like this:
for (int i = 0; i < numberofhunters; i++) {
hunter[i].move();
}
The move() method would then contain all the code to generate the new coordinates
of the hunter. It would store the coordinates within the object and would not forget
them.
There are a lot of strange things in your code. I would suggest doing some Java tutorials
first to get familiar with its basic concepts before doing a bigger project. Learn about
classes, objects and methods. Learn about the scope of variables and their visibility.
A small one class example does not provide as many chances to create bugs and will
improve the understanding. Once you have done this, go back to your project, find the
mistakes and strange things, start new and finish your project.