Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Why is my game lagging so much? HELP!

$
0
0

Hi! I have been working on an RTS game lately and it has been lagging A LOT. I only started development 3 days ago. (It can't be my PC, because it can play Overwatch) Anyways, here is my code (It is objective oriented so there will be lots of classes before the main code):

class SelectionMenu {
  PImage buildingSprite;
  boolean placing;
  boolean bought;
  boolean setMouseCoordinateToXY;
  int buildingX;
  int buildingY;

  void display() {
    //Background
    stroke(155, 0, 0);
    strokeWeight(4);
    fill(0, 0, 0, 175);

    rect(0, 500, 1280, 220, 20);

    //Tower Selection
  }

  void update() {
    if (mousePressed == true && bought == false && mouseY < 500) {
      placing = true;
    }
    if (placing == true) {
      image(loadImage("bank.png"), mouseX, mouseY);
      if (mousePressed == true) {
        if (setMouseCoordinateToXY == false) {
          buildingX = mouseX;
          buildingY = mouseY;
        }
        gameBuildings.add(new Building(1, mouseX, mouseY));
      }
    }
  }
}



class Troop extends GameObject {
  PImage troopSprite;

  Troop(int type_) {
    type_ = type;
  }
}



class Building extends GameObject {
  PImage buildingSprite;

  Building(int type_, int x_, int y_) {
    type = type_;
    x = x_;
    y = y_;
  }

  void display() {
    image(buildingSprite, x + cameraX, y + cameraY);
  }

  void update() {
    //Selects buildingSprite
    if (type == 1) {
      buildingSprite = loadImage("bank.png");
    }

    //BANK
    if (type == 1) {
      money += .25;
    }
  }
}



class MusicHandler extends GameObject {
  boolean bgm1playing;
  boolean bgm2playing;
  boolean bgm3playing;
  int timer;

  void update() {
    text(timer, 100, 100);

    //Updates timer
    timer++;

    //Does stuff
    if (bgm1playing == false && bgm2playing == false && bgm3playing == false) {
      bgm1.play();
      bgm1playing = true;
      if (timer > 300) {
        bgm2playing = true;
        bgm1playing = false;
      }
    }
    if (bgm2playing == true) {
      bgm2.play();
      if (timer > 500) {
        bgm3playing = true;
        bgm2playing = false;
      }
    }
    if (bgm3playing == true) {
      bgm3.play();
      if (bgm3.duration() > 10) {
        bgm1playing = false;
        bgm2playing = false;
        bgm3playing = false;
      }
    }
  }
}



class CameraController {
  void update() {
    if ((keys['w']) || (keys['W'])) {
      cameraY += 15;
    }
    if ((keys['a']) || (keys['A'])) {
      cameraX += 15;
    }
    if ((keys['s']) || (keys['S'])) {
      cameraY -= 15;
    }
    if ((keys['d']) || (keys['D'])) {
      cameraX -= 15;
    }
  }
}



class GameObject {
  boolean grounded;
  float x;
  float y;
  float w;
  float h;
  int type;

  void display() {}
  void update() {}
}



import processing.sound.*;

final static ArrayList<Building> gameBuildings = new ArrayList();
final static ArrayList<Troop> gameTroops = new ArrayList();

boolean keys[] = new boolean[128];

SoundFile bgm1;
SoundFile bgm2;
SoundFile bgm3;

SelectionMenu selectionMenu = new SelectionMenu();

MusicHandler mHandler = new MusicHandler();

CameraController camera = new CameraController();

PImage background;

double money;

int cameraX;
int cameraY;

void setup() {
  fullScreen();

  bgm1 = new SoundFile(this, "bgm1.mp3");
  bgm2 = new SoundFile(this, "bgm2.mp3");
  bgm3 = new SoundFile(this, "bgm3.mp3");

  background = loadImage("background.png");
}

void draw() {
  background(0, 120, 0);

  camera.update();

  for (int i = -5120; i < 5120; i += 1280) {
    for (int a = -2880; a < 2880; a += 720) {
      image(background, i + cameraX, a + cameraY);
    }
  }

  for (int i = 0; i < gameBuildings.size(); i++) {
    Building bObj = gameBuildings.get(i);
    bObj.update();
    bObj.display();
  }

  selectionMenu.display();
  selectionMenu.update();

  fill(0, 255, 0);
  text("$" + money, 10, 10);
}

void keyPressed() {
  keys[key] = true;
}

void keyReleased() {
  keys[key] = false;
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles