# How do I optimize my program?

**URL:** <https://discourse.processing.org/t/how-do-i-optimize-my-program/1465>\
**Category:** Project Guidance\
**Created:** [July 3, 2018, 3:41pm UTC](https://discourse.processing.org/t/how-do-i-optimize-my-program/1465 "2018-07-03T15:41:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Teen\_Coder](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Teen\_Coder](https://discourse.processing.org/u/Teen_Coder)\
**Post date:** [July 3, 2018, 3:41pm UTC](https://discourse.processing.org/t/how-do-i-optimize-my-program/1465/1 "2018-07-03T15:41:10Z")

</div>

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](http://www.mediafire.com/folder/jb67p75mad4tz/application.windows64)

---

<div class="post-metadata">

**Author:** ![colin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/colin/32/230_2.png) [@colin](https://discourse.processing.org/u/colin)\
**Post date:** [July 4, 2018, 8:56am UTC](https://discourse.processing.org/t/how-do-i-optimize-my-program/1465/2 "2018-07-04T08:56:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Teen\_Coder](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Teen\_Coder](https://discourse.processing.org/u/Teen_Coder)\
**Post date:** [July 4, 2018, 2:48pm UTC](https://discourse.processing.org/t/how-do-i-optimize-my-program/1465/3 "2018-07-04T14:48:59Z")

</div>

Here is the class for the player:

```auto
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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![colin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/colin/32/230_2.png) [@colin](https://discourse.processing.org/u/colin)\
**Post date:** [July 5, 2018, 3:28pm UTC](https://discourse.processing.org/t/how-do-i-optimize-my-program/1465/4 "2018-07-05T15:28:17Z")

</div>

It seems to be really well done ! 😃  
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.

---

<div class="post-metadata">

**Author:** ![Teen\_Coder](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Teen\_Coder](https://discourse.processing.org/u/Teen_Coder)\
**Post date:** [July 5, 2018, 3:49pm UTC](https://discourse.processing.org/t/how-do-i-optimize-my-program/1465/5 "2018-07-05T15:49:00Z")

</div>

Thanks I’ll give your suggestions a go!

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [July 5, 2018, 4:34pm UTC](https://discourse.processing.org/t/how-do-i-optimize-my-program/1465/6 "2018-07-05T16:34:27Z")

</div>

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:

```auto
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:

```auto
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']`!

---

<div class="post-metadata">

**Author:** ![Teen\_Coder](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Teen\_Coder](https://discourse.processing.org/u/Teen_Coder)\
**Post date:** [July 5, 2018, 5:17pm UTC](https://discourse.processing.org/t/how-do-i-optimize-my-program/1465/7 "2018-07-05T17:17:03Z")

</div>

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