I need help with upgrading a game

Hello everybody. In my country its 2:12 am so be ready to find some unlogic sentences and misspelled words in my post :slight_smile:

Java is my friend for already a year. I guess I`ve got the basic skills for programming at the moment.
But I have no experience in game development.

I began reading this forum and understood that I`m ready to ask you, guys, for help.
For the beginning I searched for open source project games and compiled them, studied them and wrote additional stuff.

Eventually the problem:
I have a little bit changed version of the game from http://cokeandcode.com/index.html?page=tutorials.

The Main idea of my topic:

  1. I want to add methods of picking a key (the yellow block) and exiting the level (by standing on the white block)

  2. It seems a shame to ask, but I dont know where to place the right code. :slight_smile:

  3. I have the logic implemented in methods, so check out what I`m going to show you.

  4. The methods for picking the key:
    • I added and boolean variable hasKey in class Entity.java.
    • I added a getter and setter method for this variable
    • I added an boolean keyValidation() method


public boolean keyValidation(){
if(hasKey == true)
                return true;
            else 
                return false;
}

• I guess that i need to set the key true when the entities location (coordinates) are the same as the keys ???

if(???player???[x][y] == map[13][13])
setKey(true);

and if at this moment I`ll reach an true state of the hasKey variable it would be already a good step for me.
2. Method for opening the door.


public void openDoor(){
  if(keyValidation() == true)
  System.out.println("GameOver");   
        }

Really the problem doesn`t sound very hard, especially for u, madskilled java man)
But I stopped at this point and I would be very pleased if somebody looks through the project or code and helps me.
Thank you.

The project source:
https://docs.google.com/open?id=0B8BBSI9JDFc4YXpST2hQOXNpdHc

Sincerely yours, tdchk

Don’t you have somewhere that updates the player on every move? The most logical place to put the code is at the end of the move method.

Concerning your code, there is no need to compare a boolean with another boolean to get a boolean. You could change all your code blocks to:


public boolean keyValidation() {
    return hasKey;
}

//put this after the player moves
setKey(player.x == 13 && player.y == 13);

//put this also after the player moves
public void openDoor() {
   if(keyValidation())
       SOP("GameOver");
}

As I remember that tutorial uses interpolated movement, so I doubt your ==map[13][13] would work.

I supposed to place the open door method at the end of the move(). And changed the setKey() because unfortunatelly and dont know “whom i need to ask for my coordinates”, so I just did like this:

public boolean keyValidation(){
                return hasKey;
        }
public void openDoor(){
            if(keyValidation())
            System.out.println("GameOver");
        }

And the method that should manage all this stuff:

public boolean move(float dx, float dy) {
		float nx = x + dx;
		float ny = y + dy;
		if (validLocation(nx, ny)) {
			x = nx;
			y = ny;
			ang = (float) (Math.atan2(dy, dx) - (Math.PI / 2));
			return true;
		}
    setKey(dx, dy);
    openDoor();
		return false;
	}

Till this stage everything is clear, the problem is with the setKey().
Here are several ways i`ve tried handling this problem, but still no success.
1.

public void setKey(float dx, float dy){
            if(dx == 13 && dy == 13){
System.out.println("The key has been picked");               
hasKey = true;
            }
            else
               hasKey = false;
        }

  1. I guess this is the best one, but i need to change map.key(nx,ny) to some sort of player(nx,ny), but i dont have such stuff in this app :frowning:
public void setKey(float nx, float ny){
            if (map.key(nx,ny) == map.key(13,13))
               System.out.println("The key has been picked");
                hasKey = true;
        }

//public boolean key(float x, float y) {
//return map[(int) x][(int) y] == KEY;
//}

Interpolated move doesn’t guarantee you that player will stand exactly on 13,13, which result false on the check. Use rectangle’s contains().