无法在Javascript中阻止播放器的路径

时间:2014-12-07 22:30:22

标签: javascript

我无法阻挡他们已经通过的玩家路径。我让玩家留下了一条没有想过要经历的粘液。我使用了崇高的文字



// Put your global variables after this line
var GRIDWIDTH, GRIDHEIGHT;
GRIDWIDTH = 11;
GRIDHEIGHT = 13;
var player = new Object();
player.x = 0;
player.y = 0;
var reset = 0x0052;
var GAME_WON = false;
var foodValue = 1;

var PlayerInventory = {
  key: 0,
  food: 0,
  foodLeft: 0,

}



// Put your function definitions after this line
function drawPlayer(x, y) {
  PS.color(x, y, PS.COLOR_GREEN);
  PS.glyphColor(x, y, PS.COLOR_WHITE);
  PS.glyph(x, y, "ඬ");
  player.x = x;
  player.y = y;
}

function drawSlime(x, y, dir) {
  PS.color(x, y, PS.COLOR_GREEN);
  PS.data(x, y, dir);
  PS.data(x, y, "wall")
}

function PlaceKey(x, y) {
  PS.color(x, y, PS.COLOR_YELLOW);
  PS.glyphColor(x, y, PS.COLOR_ORANGE);
  PS.glyph(x, y, "K")
  PS.data(x, y, "key");
}

function removePlayer(x, y) {

  PS.glyph(x, y, 0);
}


function isInGrid(x, y) {
  if (x < 0) return false;
  else if (x >= GRIDWIDTH) return false;
  else if (y < 0) return false;
  else if (y >= GRIDHEIGHT) return false;
  else return true;
}

function CreateButtons() {
  PS.glyph(5, 12, reset);


  PS.data(5, 12, "resetbutton");

}

function DrawInventory() {
  PS.statusText(" key: " + PlayerInventory.key + "food: " + PlayerInventory.food);
}

function PlaceFood(x, y) {
    PS.glyph(x, y, 0x25CE);
    PS.glyphColor(x, y, 0xBA9A19);
    PS.data(x, y, "food");
    PlayerInventory.foodLeft += foodValue;


  }
  

// PS.keyDown ( key, shift, ctrl, options )
  // Called when a key on the keyboard is pressed
PS.keyDown = function(key, shift, ctrl, options) {
  "use strict";

  // Uncomment the following line to inspect parameters
  // PS.debug( "PS.keyDown(): key = " + key + ", shift = " + shift + ", ctrl = " + ctrl + "\n" );

  // Add code here for when a key is pressed

  
  if (GAME_WON) {
    GameSetup();
  } else {
    var oldX = player.x;
    var oldY = player.y;

    // Moves the player in the game with the key arrow
    if (key == PS.KEY_ARROW_RIGHT && (player.x < (GRIDWIDTH - 1)))
      player.x += 1;
    else if (key == PS.KEY_ARROW_LEFT && (player.x > 0))
      player.x -= 1;
    else if (key == PS.KEY_ARROW_UP && (player.y > 0))
      player.y -= 1;
    else if (key == PS.KEY_ARROW_DOWN && (player.y < (GRIDHEIGHT - 1)))
      player.y += 1;

    var dataAtPlayer = PS.data(player.x, player.y);

    if (dataAtPlayer == "wall") {
      player.x = oldX;
      player.y = oldY;
    }

    if (dataAtPlayer == "drawSlime") {
      player.x = oldX;
      player.y = oldY;
    }

    if (dataAtPlayer == "leaf") {
      if (PlayerInventory.key > -0) {
        PS.audioPlay("fx_blast4");
        DrawInventory();

      } else {
        player.x = oldX;
        player.y = oldY;
      }
    }


    if (dataAtPlayer == "key") {
      PlayerInventory.key += 1;
      DrawInventory();
      PS.audioPlay("fx_pop");
    }
    if (dataAtPlayer == "food") {
      PlayerInventory.foodLeft -= foodValue;
      PlayerInventory.foods += foodValue;
      DrawInventory();
      PS.audioPlay("fx_tada");
      if (PlayerInventory.foodLeft == 0) {
        GAME_WON = true;
        PS.statusText("Click R to restar!");
      }
    }


    drawPlayer(player.x, player.y);

  }
};
&#13;
&#13;
&#13;

我尝试通过将代码更改为此来解决此问题:

&#13;
&#13;
// WASD keys to move The Snail
if (key == 119) {
  //Check that up isn’t a wall
  //Check that up isn’t off the screen
  if (player.y - 1 >= 0) {
    if (PS.data(player.x, player.y - 1) != "wall") {
      //If both are true, remove player from current position
      //If both are true, draw player in new position
      removePlayer(player.x, player.y);
      drawPlayer(player.x, player.y - 1);
      drawSlime(player.x, player.y);
    }
  }
}
if (key == 115) {
  //Check that down isn’t a wall
  //Check that down isn’t off the screen
  if (player.y + 1 < GRIDHEIGHT) {
    if (PS.data(player.x, player.y + 1) != "wall") {
      //If both are true, remove player from current position
      //If both are true, draw player in new position
      removePlayer(player.x, player.y);
      drawPlayer(player.x, player.y + 1);
      drawSlime(player.x, player.y);
    }
  }
}
if (key == 97) {
  //Check that left isn’t a wall
  //Check that left isn’t off the screen
  if (player.x - 1 >= 0) {
    if (PS.data(player.x - 1, player.y) != "wall") {
      //If both are true, remove player from current position
      //If both are true, draw player in new position
      removePlayer(player.x, player.y);
      drawPlayer(player.x - 1, player.y);
      drawSlime(player.x, player.y);

    }
  }
}
if (key == 100) {
  //Check that right isn’t a wall
  //Check that right isn’t off the screen
  if (player.x + 1 < GRIDWIDTH) {
    if (PS.data(player.x + 1, player.y) != "wall") {
      //If both are true, remove player from current position
      //If both are true, draw player in new position
      removePlayer(player.x, player.y);
      drawPlayer(player.x + 1, player.y);
      drawSlime(player.x, player.y);
    }
  }
}
&#13;
&#13;
&#13;

虽然这有效,但我无法与游戏中的项目进行互动,

0 个答案:

没有答案