3

Very basic java knowledge at the moment, in need of some assistance in trying to create a game of Pacman.

Currently I have three classes, the player class, the dot class and the game class, all interacting to create a basic game of Pacman.

The issue I am having is this:

I need the 'initialX and initialY fields as show below (these will be user inputted coordinates);

public class Player
{
    private int x, y, collectedDots;

    public Player(int initialX, int initialY)
    {
        x = initialX;
        y = initialY;

        collectedDots = 0;
    }

    public int getX()
    {
        return x;
    }

    public int getY()
    {
        return y;
        }

} 

to pass through as my parameters within the new 'player' object within the Game Class.

public class Game
{


    public Game()
    {
        Player player = new Player();
        Dot dot1 = new Dot();
        Dot dot2 = new Dot();
        Dot dot3 = new Dot();
    }


}

It has me stumped, and I'm assuming I've either gone about this the wrong way, or I'm completely missing something.

5
  • 1
    The constructor takes two ints, are you talking about something like: Player player = new Player(0, 1); or something else entirely? Commented Mar 30, 2014 at 7:24
  • The player will take to integers yes, say (1,1) initially. But it has to take those initial values from the parameters passed to the constructor sitting in the Player class.. if that makes sense? Commented Mar 30, 2014 at 7:26
  • You can define int variables in the Player class. Assign those to x and y through the constructor. And then access those values to the Game class by doing something like player.getX(); or player.getY(); given that player is an instance of the Player class created in the Game class. If that isn't what you're asking then I'm afraid I am having a hard time understanding what you want. Commented Mar 30, 2014 at 7:35
  • So, something along the lines of: Player player = new Player(player.getX(), player.getY());? Commented Mar 30, 2014 at 7:43
  • No. Let me write this as an answer so you understand what I'm saying better. Commented Mar 30, 2014 at 7:44

2 Answers 2

1

Your declared constructor take two int parameters, and you call it with out sending any parameters.

When use the below line, the compiler will look for a default constructor that takes no parameters, but it will fail since you haven't declared a default one, but you declared a constructor with two int parameters.

Player player = new Player();

With this line:

Player player = new Player(1, 2);// pass two int parameters t your defined constructor.
Sign up to request clarification or add additional context in comments.

2 Comments

But if those two integers are defined within the Player constructor in the Player class, how would I pass across these to the Game class?
Simply you can use the getter methods to access the declared variable within the Player class.
0

You can have variables defined in your Player class and then assign those variables to other variables in the constructor of the same class like so:

public class Player
{
    private int x, y, collectedDots;

    int initialX = 15;
    int initialY = 20;

    public Player()
    {
        x = initialX;
        y = initialY;

        collectedDots = 0;
    }

    public int getX()
    {
        return x;
    }

    public int getY()
    {
        return y;
    }

}

Now your x is equal to 15 and y is equal to 20. But these values are all part of the Player class. However, you create an object of the Player class in the Game class and through that object you can access x and y like so:

public class Game
{


    public Game()
    {
        Player player = new Player();

        System.out.println(player.getX()); // prints out 15 in the console
        System.out.println(player.getY()); // prints out 20 in the console

        Dot dot1 = new Dot();
        Dot dot2 = new Dot();
        Dot dot3 = new Dot();
    }
}

You can of course do whatever you want with these values (like assign them to new variables in the Game class and manipulate them there). I am just printing them out so you can see that its the values from the Player class.

UPDATE

You can take user input before you create the Player object and then pass those values to the constructor of the Player class like so:

public class Game
{
    public Game()
    {
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        int j = sc.nextInt();

        Player player = new Player(i,j);

        System.out.println(player.getX()); // prints out 15 in the console
        System.out.println(player.getY()); // prints out 20 in the console

        Dot dot1 = new Dot();
        Dot dot2 = new Dot();
        Dot dot3 = new Dot();
    }
}

This is, of course, provided you keep your Player class how you posted it originally. But if you want to continue being able to change the values of x and y you can always provide Setter methods. Your class will then become:

public class Player
{
    private int x, y, collectedDots;

    public Player(int initialX, int initialY)
    {
        x = initialX;
        y = initialY;

        collectedDots = 0;
    }

    public int getX()
    {
        return x;
    }

    public int getY()
    {
        return y;
    }

    public void setX(int newX)
    {
        x = newX;
    }

    public void setY(int newY)
    {
        y = newY;
    }
}

Whenever you want to then change the values of x and y from the Game class then you'd just have to do the following (just an example):

public class Game
{
    public Game()
    {
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        int j = sc.nextInt();

        Player player = new Player(i,j);

        player.setX(35);
        player.setY(48);
    }
}

Again, this is just an example. I wouldn't really be doing all of this in the constructor but just using this to explain how you can do what you want.

2 Comments

@Wombat Let me know if this is or isn't what you were looking for. It was hard to explain all of this via the comments.
But without the parameters, after I have compiled the code I am unable to enter different x and y values?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.