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.
ints, are you talking about something like:Player player = new Player(0, 1);or something else entirely?intvariables in thePlayerclass. Assign those toxandythrough the constructor. And then access those values to theGameclass by doing something likeplayer.getX();orplayer.getY();given thatplayeris an instance of thePlayerclass created in theGameclass. If that isn't what you're asking then I'm afraid I am having a hard time understanding what you want.