My Program is Lagging

Hello All,

I am working on a video game project. Each level of the game is a top down view of the game world. My recent addition to the game was to have an "unexplored map" and "fog of war" functionality, similar to what games like the Age of Empires and Civilization series of games use. However, my program now chugs along when I run it.

Here is the code I have for my Level 0. Is there a better way to create this effect that won't cause the program to lag? Is there a way to boost the Processing 3 IDE to give it enough capacity to handle the amount of objects I have on the screen? I have already tried increasing the amount of memory in preferences up from 256MB to 1000MB and saw no change to the program.

Thanks for the help.

//All Levels will have their own UnexploredMap class and FogOfWar class
UnexploredMap[][] level0map;
FogOfWar[][] level0fog;
String gamestate = "level 0";

void setup() {
  size(1600, 900);
  background(125);

  playerlevel0 = new Player(300, 700, 25, 25);

  level0map = new UnexploredMap[310][160];
  for (int i = 0; i < 310; i++) {
    for (int j = 0; j < 160; j++){
      level0map[i][j] = new UnexploredMap(25 + (10 * i), 50 + (10 * j));
    }
  }

  level0fog = new FogOfWar[310][160];
  for (int i = 0; i < 310; i++) {
    for (int j = 0; j < 160; j++){
      level0fog[i][j] = new FogOfWar(25 + (10 * i), 50 + (10 * j));
    }
  }
}

void draw(){
  if(gamestate == "level 0"){
     Level0();
  }
}

void Level0(){

  for (int i = 0; i < 310; i++) {
    for (int j = 0; j < 160; j++) {
      level0fog[i][j].display();
      level0fog[i][j].update();
    }
  }

  for (int i = 0; i < 310; i++) {
    for (int j = 0; j < 160; j++) {
      level0map[i][j].display();
      level0map[i][j].update();
    }
  }

  for (int i = 0; i < 310; i++) {
    for (int j = 0; j < 160; j++) {
      areaexplored(playerlevel0, level0map[i][j]);
    }
  }

  for (int i = 0; i < 310; i++) {
    for (int j = 0; j < 160; j++) {
      areavisible(playerlevel0, level0fog[i][j]);
    }
  }

  playerlevel0.display();
  playerlevel0.update();
}

class Player {
  float inputX, inputY, z, v;
  float inputW, inputH;
  float SPD = 1.15, ROT = .03;

  Player(float posX, float posY, float posW, float posH) {
    inputX = posX;
    inputY = posY;
    inputW = posW;
    inputH = posH;
  }

  void update() {
    v = (up? SPD : 0);
    z += (right?  ROT : 0) - (left?  ROT : 0);

    inputX +=  cos(z)*v + mapleftright;
    inputY +=  sin(z)*v + mapupdown;
  }

  void display() {
    pushMatrix();
    translate(inputX, inputY);
    rotate(z);

    noStroke();
    fill(#008000); 
    rect(inputW/-2, inputH/-2, inputW, inputH);

    stroke(0);
    noFill();
    ellipse(10, 0, 5, 5);
    popMatrix();
  }
}
Tagged:

Answers

  • please don't post duplicates. add the code to the previous question.

  • My understanding is that if I have a question specifically dealing with code it goes in this forum where as if I have a more general question it goes in the "Programming Questions" forum. Sorry, I won't post duplicates in the future

  • Answer ✓

    you can change the category of a question.

  • i have deleted the other.

  • FogOfWar not found

    UnexploredMap not found

  • Since this is a thread about fog and lagging, consider renaming it to something e.g. "Fog of war causing severe lag"

    This recent fog thread might be relevant to you:

  • Hello Everyone,

    I forgot the Fog of War and Unexplored Area classes in my original post. Here they are.

    class FogOfWar{
      float x, y;
      boolean visible = true;
    
      FogOfWar(float posX, float posY){
        x = posX;
        y = posY;
      }
    
      void update(){
        x += mapleftright;
        y += mapupdown;
      }
    
      void display(){
        pushMatrix();
        if (visible){
          noStroke();
          fill(0, 255, 0, 125);
          rect(x, y, 10, 10);
        }
        popMatrix();
      }
    }
    
    class UnexploredMap{
      float x, y;
      boolean visible = true;
    
      UnexploredMap(float posX, float posY){
        x = posX;
        y = posY;
      }
    
      void update(){
        x += mapleftright;
        y += mapupdown;
      }
    
      void display(){
        pushMatrix();
        if (visible){
          noStroke();
          fill(0);
          rect(x, y, 10, 10);
        }
        popMatrix();
      }
    }
    
Sign In or Register to comment.