Multiple objects/player objects for my game?

Started by
2 comments, last by null; 8 years, 1 month ago

Okay, so I've been doing Java for a few months or so, and I've learned a lot. I'm teaching myself since I'm quite young and don't think real classes are COMPLETELY necessary - yet, plus, learning Java was kind of just an extemporaneous decision. Anyways, I didn't dare touch GUIs until I learned everything I possibly could about logic, functions, etc. Recently, I decided I should move up a level and try out shapes and controls and movement, GUI in general, I guess.

Back to the main topic, so I created a Player class with the parameters (int x, int y, Color color, int width, int height). I'm using rectangles as players because collision isn't very easy - especially at the level I'm at right now, and by using Rectangles, I could use the 'intersects' function.

My only question is - how could I create multiple player objects and render them without initializing each individual variable for the parameters?

Again, I'm still a beginner. Criticism is welcome, but maybe not TOO harsh XD? I'm not sharing the source code at the moment because I don't have a lot of time and it's not completely necessary - I'm just asking for the main idea/concept of how I would go about doing it. I know I should probably use an ArrayList, but can I have an example of some sort?

Advertisement

how could I create multiple player objects and render them without initializing each individual variable for the parameters?
Not sure how you see this as a problem, you can just copy the values several times?

public static List<player> makePlayers(int numPlayers, int x, int y, Color color, int width, int height) {
    List<Player> players = new ArrayList<>();
    for (int i = 0; i < numPlayers; i++) players.add(new Player(x + i * (width + 10), y, color, width, height));
    return players;
}

but now you get several players all with the same color, you may want to provide several different colors here, or so.

You are not quite there yet with your fundamental knowledge so my advice is to continue learning more basics. About your specific question, you should use a datastructure to hold multiple objects of the same kind. There are many data structures that all act differently. If you have done serious work in the months you learned Java then you should have read about arrays. Arrays are data structures but offer the least of functionality and can only be of a fixed size. Lists are probably the next you will get to know. They have variable size and offer a lot more functionality. You also have Sets and Maps, sets don't allow equal objects in it and maps map objects together like a dictionary. These are the most important but you need to know them all in order to pick the right one for the right job.

Now more specific to your question, since you have a player with a position my guess is you have a draw and logic loop somewhere where you would calculate and draw your stuff. Here you can iterate these data structures and perform actions on each of it's contents.


public class Player {
	private int x, y, width, height, health;
	private Color color;

	public Player(int x, int y, int width, int height, int health, Color color) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.health = health;
		this.color = color;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public int getHealth() {
		return health;
	}

	public void draw()
	{
		//However you draw this player.
	}
}

Now you can specify a datastructure to hold <Type Player>, a ArrayList is most probably your best option here. You can add players as you like and do all kind of queries on the list.


                List<Player> myPlayerList = new ArrayList<Player>();

		myPlayerList.add(new Player(0, 0, 10, 10, 100, Color.GREEN));
		myPlayerList.add(new Player(20, 20, 10, 10, 150, Color.RED));

		//Call the draw method on all players in the list
		for (Player player : myPlayerList)
		{
			player.draw();
		}

		//Find a player with certain properties
		Player pickedPlayer = null;
		for (int i = 0; i < myPlayerList.size(); i++)
		{
			//Check conditions
			if (myPlayerList.get(i).getX() == 0 && myPlayerList.get(i).getY() == 0)
			{
				//Set picked player to the current iteration
				pickedPlayer = myPlayerList.get(i);
				//break out of loop
				break;
			}
		}

		//You can remove objects from a ArrayList by index or if you know the object
		myPlayerList.remove(pickedPlayer);

		//If you want to change the list while iterating you need to use a iterator.
		for (Iterator<Player> iterator = myPlayerList.iterator(); iterator.hasNext();)
		{
			Player currentPlayer = iterator.next();

			if (currentPlayer.getHealth() <= 0) iterator.remove();
		}

Try to find a good use for each type of data structure and understand how each one works.

You are not quite there yet with your fundamental knowledge so my advice is to continue learning more basics. About your specific question, you should use a datastructure to hold multiple objects of the same kind. There are many data structures that all act differently. If you have done serious work in the months you learned Java then you should have read about arrays. Arrays are data structures but offer the least of functionality and can only be of a fixed size. Lists are probably the next you will get to know. They have variable size and offer a lot more functionality. You also have Sets and Maps, sets don't allow equal objects in it and maps map objects together like a dictionary. These are the most important but you need to know them all in order to pick the right one for the right job.

Now more specific to your question, since you have a player with a position my guess is you have a draw and logic loop somewhere where you would calculate and draw your stuff. Here you can iterate these data structures and perform actions on each of it's contents.


public class Player {
	private int x, y, width, height, health;
	private Color color;

	public Player(int x, int y, int width, int height, int health, Color color) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.health = health;
		this.color = color;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public int getHealth() {
		return health;
	}

	public void draw()
	{
		//However you draw this player.
	}
}

Now you can specify a datastructure to hold <Type Player>, a ArrayList is most probably your best option here. You can add players as you like and do all kind of queries on the list.


                List<Player> myPlayerList = new ArrayList<Player>();

		myPlayerList.add(new Player(0, 0, 10, 10, 100, Color.GREEN));
		myPlayerList.add(new Player(20, 20, 10, 10, 150, Color.RED));

		//Call the draw method on all players in the list
		for (Player player : myPlayerList)
		{
			player.draw();
		}

		//Find a player with certain properties
		Player pickedPlayer = null;
		for (int i = 0; i < myPlayerList.size(); i++)
		{
			//Check conditions
			if (myPlayerList.get(i).getX() == 0 && myPlayerList.get(i).getY() == 0)
			{
				//Set picked player to the current iteration
				pickedPlayer = myPlayerList.get(i);
				//break out of loop
				break;
			}
		}

		//You can remove objects from a ArrayList by index or if you know the object
		myPlayerList.remove(pickedPlayer);

		//If you want to change the list while iterating you need to use a iterator.
		for (Iterator<Player> iterator = myPlayerList.iterator(); iterator.hasNext();)
		{
			Player currentPlayer = iterator.next();

			if (currentPlayer.getHealth() <= 0) iterator.remove();
		}

Try to find a good use for each type of data structure and understand how each one works.

I already had the basic draw method and data loop. And yes, I know how arrays work. Just because I was a little confused about how I would go about holding the player object in a data structure doesn't necessarily mean I'm completely clueless, considering the fact that I'm still learning as I go along. I considered the idea, but I figured it would save me a lot of time to at least have a permanent solution rather than trying different things through trial and error.

Thanks for the answer, by the way! Now I have the right idea.

This topic is closed to new replies.

Advertisement