How do I optimize my program?

Hi, I have a quick question. I’m currently working on a clone of Google Chrome’s dinosaur game, but my version of the game will stutter at seemingly random times. I know my code isn’t well optimized, but I have no idea on how one would go about optimizing code.
Here is the source code. (it isn’t the finished product but I need to know how to optimize it before continuing so any comments or suggestions are appreciated)
Here’s the link to the .exe and source code.
http://www.mediafire.com/folder/jb67p75mad4tz/application.windows64

Hi, @Teen_Coder. Could you post some parts of your code ? I’m curious about the part that display graphics in your game.

Here is the class for the player:

boolean duckDino, jumpDino, dabDino;
float xDino, yDino;

class Dino {
  //Members
  PVector jump;
  PImage [] [] movement;
  PImage spriteSheet;
  int currentState;
  float currentFrame, numofFrames, floorLevel;

  //Constructors
  Dino() {
    currentState = 0;
    currentFrame = 0;
    duckDino = false;
    jumpDino = false;
    dabDino = false;
    numofFrames = 4;
    floorLevel = 883;
    xDino = 75;
    yDino = floorLevel;
    jump = new PVector();
    setupSprites();
    jump.y = 14;
  }

  //Methods
  void setupSprites() {
    movement = new PImage[5][6];
    spriteSheet = loadImage("DinoSpritesGreen.png");
    //dino walking sprites
    for (int i = 0; i < 5; i++) {
      movement[0][i] = spriteSheet.get(4 + 85 * i, 4, 80, 80);
    }
    //dino ducking sprites
    for (int i = 0; i < 5; i++) {
      movement[1][i] = spriteSheet.get(4 + 117 * i, 133, 112, 80);
    }
    //dino jumping sprite
    movement[2][1] = spriteSheet.get(4, 4, 80, 80);

    //dino falling sprite
    movement[3][1] = spriteSheet.get(4, 4, 80, 80);

    //dino dabing sprite
    for (int i = 0; i < 5; i++) {
      movement[4][i] = spriteSheet.get(4 + 88 * i, 217, 84, 80);
    }
  }
  void drawDino() {
    image(movement[currentState][1 + int(currentFrame)], xDino, yDino);
  }
  void moveDino() {
    if (jumpDino) {
      numofFrames = 1;
      currentState = 2;
      yDino = yDino - jump.y;
      jump.y = jump.y - 0.525;
    } else {
      numofFrames = 4;
      currentState = 0;
    }
    if (yDino > floorLevel) {
      jumpKeys['w'] = false;
      jump.y = 14;
      yDino = floorLevel;
    }
    if (duckDino) {
      numofFrames = 4;
      currentState = 1;
    }
    if (dabDino) {
      numofFrames = 4;
      currentState = 4;
    }
    currentFrame = (currentFrame + 0.25) % numofFrames;
  }
}

The player is then drawn here:

Dino player;
Cactus enemy;
Cloud entity;
Path ground;
boolean [] keys = new boolean[128];
boolean [] jumpKeys = new boolean[128];



void setup() {
  size(1920, 1080);
  //fullScreen(0);
  frameRate(60);
  player = new Dino();
  enemy = new Cactus();
  entity = new Cloud();
  ground = new Path();
}

void draw() {
  background(255);
  ground.drawPath();
  ground.movePath();
  player.drawDino();
  player.moveDino();
  move();
  enemy.drawCactus();
  enemy.moveCactus();
  entity.drawCloud();
  entity.moveCloud();
}

void move() {
  if (jumpKeys['w']) {
    jumpDino = true;
  } else {
    jumpDino = false;
  }
  if (keys['s']) {
    duckDino = true;
  } else {
    duckDino = false;
  }
  if (keys['d']) {
    dabDino = true;
  } else {
    dabDino = false;
  }
}
void keyPressed() {
  keys[key] = true;
}

void keyTyped() {
  jumpKeys[key] = true;
}

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

Hope this helps!

It seems to be really well done ! :smiley:
To optimize my games I usually check if there is some graphics that are calculated while not being actually visible by the user. You can also try to reduce the sprites files size.

Thanks I’ll give your suggestions a go!

Looks fine to me! There’s only one thing that jumps out at me as being something that could obviously be improved. It’s this move() function:

void move() {
  if (jumpKeys['w']) {
    jumpDino = true;
  } else {
    jumpDino = false;
  }
  if (keys['s']) {
    duckDino = true;
  } else {
    duckDino = false;
  }
  if (keys['d']) {
    dabDino = true;
  } else {
    dabDino = false;
  }
}

It could just be this:

void move() {
    jumpDino = keys['w'];
    duckDino = keys['s'];
    dabDino = keys['d'];
}

Actually, you could just get rid of the jumpDino, duckDino, and dabDino variables! You can replace them directly with keys['w'], keys['s'], and keys['d']!

Thanks for the suggestion @TfGuy44. I’ll implement that right away!