My code.org Game Lab project is running really slowly, how can I optimize the code more?

160 Views Asked by At

I am working on my final project, and it is a game made with Java Script in code.org's Game Lab. The code worked fine and it was not slow at all, but as I worked on it, it became much slower! The character starts lagging and it eventually stops moving at all.

I have tried optimizing it by combining the tasks of a few functions into one function that does it all (the motion of the character) and it seemed to help, but not for long. It worked at first with the createPlatform() function, but now it is just making the code slower as well. The goal of the createPlatform() function is for its parameter to be the number of screens that I want the platform to extend to. It did work, but now it seems to be making the game unplayable.

Here is the code:

var max_X = 200;
var min_X = 50;
var min_Y = 330;
var platforms = [];
var centered = false;
var sprite = createSprite(200, 250);
sprite.setAnimation("alienBlue_2");
//sprite.scale = 0.15;


function draw() {
 startGame(2, "gray", "groundBrickPlain.png_1", 4);
 drawSprites();
}

function startGame(levelGravity, backG, platformType, platSize) {
  //var Currentlevel = level;
  // Set the initial velocity and gravity
  sprite.velocityY = 0;
  var gravity = levelGravity;
  // Clear the screen
  background(backG);
  createPlatform(platSize, platformType);

  // Apply gravity
  sprite.velocityY = sprite.velocityY + gravity;

  
  move();
  
  if ((keyDown("tab") && keyDown("right")) && centered){
    chageWorldSpeed(7);         
  }
  
  if (sprite.y >= min_Y) {
    console.log("You died.");
  }
  // Prevent the sprite from falling through the platform
  //dontFall(312, 0);

  // Make the sprite jump
  //jump(-12, "alienBlue_jump_1");
  
  var brickN1 = createSprite(200, 200);
  brickN1.setAnimation("brickNormal");
}

function chageWorldSpeed(speed) {
  for (var p = 0; p < platforms.length; p++) {
      platforms[p].x -= speed;
    }
}
function createPlatform(numScreens, platType) {
  var startX = 0;
  var startY = 380;
  var spacing = 40;
  var numBricks = numScreens* 400;
  
  for (var i = 0; i < numBricks; i += spacing) {
    var platform = createSprite(startX + i, startY);
    platform.setAnimation(platType);
    platform.scale = 0.0333333333333333333333333333;
    platforms.push(platform);
  }
}
function move() {
  // Make the sprite move
  if (keyDown("left")) {
    sprite.velocityX = -5;
    sprite.setAnimation("alienBlue_walk_1_copy_1");
    for (var i = 0; i < platforms.length; i++) {
      platforms[i].x += 5;
    }
  } else if (keyDown("right")) {
    sprite.velocityX = 5;
    sprite.setAnimation("alienBlue_walk_1");
    for (var t = 0; t < platforms.length; t++) {
      platforms[t].x -= 5;
    }
  }else if(keyDown("down")) {
    squat("alienBlue_duck_1");
  }else {
    sprite.velocityX = 0;
    sprite.setAnimation("alienBlue_2");
  }
  if(sprite.x == max_X && keyDown("right")) {
    sprite.velocityX = 0;
    centered = true;
  }
  if (sprite.x == min_X && keyDown("left")) {
    var spriteX = 5;
    sprite.velocityX = 0;
    for (var h = 0; h < platforms.length; h++) {
      platforms[h].x -= spriteX;
  }
  }
  
  var jumped = false;
  for (var m = 0; m < platforms.length; m++) {
    if (keyDown("space")|| (keyDown("up")) && sprite.isTouching(platforms[m])) {
      sprite.setAnimation("alienBlue_jump_1");
      sprite.velocityY = -12;
      jumped = true;
      break;
    }
  }
  if (!jumped) {
        sprite.setAnimation("alienBlue_2"); // Reset animation if no jump occurs
    }
    
  for (var v = 0; v < platforms.length; v++) {
    if (sprite.velocityY > 0 && sprite.isTouching(platforms[v])){
      sprite.velocityY = 0;
      sprite.y = 312;
    }
  }
}
function squat(anim) {
  sprite.setAnimation(anim);
}
function dontFall(yVal, yVelocity) {
  for (var v = 0; v < platforms.length; v++) {
    if (sprite.velocityY > 0 && sprite.isTouching(platforms[v])){
      sprite.velocityY = yVelocity;
      sprite.y = yVal;
    }
  }
}
function blockLeft(xVal) {
  var spriteX = xVal;
  sprite.velocityX = 0;
  for (var h = 0; h < platforms.length; h++) {
    platforms[h].x -= spriteX;
  }
}

And here is the link to try it out!

Any help would be greatly appreciated!

1

There are 1 best solutions below

2
On

Your platforms array is going to infinity …

createPlatform(platSize, platformType); Keeps getting called every draw frame.

I would look into that.