Me and a friend are working on a game where the player is in space, and we decided to add stars to the background. It worked well but the more stars there are the more the game lags, is there anyway to make them draw once and then stop lagging game so much? With only one star the game has over 4000 fps, with 60 stars the game has only 70 fps.
Here’s our code
public class Star extends Entity {
private int tex, rot;
private boolean rotBool;
public Star(float x, float y, int tex, int rot, boolean rotBool) {
super(x, y, 5, 5);
this.tex = tex;
this.rot = rot;
this.rotBool = rotBool;
setLocation(x,y);
}
@Override
public void update(GameContainer container) {
}
@Override
public void draw(Graphics g) throws SlickException {
Image img = new Image(Core.RES + "Star" + tex + ".png");
if (rotBool){
img.rotate(rot);
}
img.draw(getX(), getY());
}
}
@Override
public void render(GameContainer container, Graphics g) throws SlickException {
g.setBackground(new Color(71, 126, 255));
for (Star s : stars) {
s.draw(g);
}
for (Entity e : entities) {
e.draw(g);
}
}
while (stars.size() < 60) {
int randX = new Random().nextInt(container.getWidth() - 10);
int randY = new Random().nextInt(container.getHeight() - 10);
int randFile = new Random().nextInt(5) + 1;
stars.add(new Star(randX, randY, randFile, new Random().nextInt(359), new Random().nextBoolean()));
}