需要帮助制作益智游戏

时间:2014-12-07 00:25:07

标签: javascript

我使用Perlenspiel制作益智游戏并使用带有Sublime Text的Javascript。游戏是关于控制蜗牛的玩家,它留下了绿色粘液的痕迹,玩家无法通过。目标是用绿色粘液覆盖白色珠子区域。我已经设置了大部分游戏,我只需要帮助弄清楚如何结束游戏并在用绿泥覆盖该区域后加载下一级别。

我是新手使用Javascript和Perlenspiel,我不知道如何让它发挥作用。

// Put your global variables after this line
var GRIDWIDTH, GRIDHEIGHT;
GRIDWIDTH = 11;
GRIDHEIGHT = 11;
var player = new Object();
player.x = 0;
player.y = 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 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;
}





// PS.init( system, options )
// Initializes the game
PS.init = function(system, options) {
  "use strict";

  // Use PS.gridSize( x, y ) to set the grid to
  // the initial dimensions you want (32 x 32 maximum)
  // Do this FIRST to avoid problems!
  // Otherwise you will get the default 8x8 grid

  PS.gridSize(GRIDWIDTH, GRIDHEIGHT); // replace with your own x/y values

  // Add any other initialization code you need here
  PS.statusText("Move:WASD Shoot:Arrow Keys")

  // Walls that The Snail can't pass through
  PS.color(7, 1, PS.COLOR_BLACK);
  PS.data(7, 1, "wall");

  PS.color(9, 1, PS.COLOR_BLACK);
  PS.data(9, 1, "wall");

  PS.color(10, 0, PS.COLOR_BLACK);
  PS.data(10, 0, "wall");

  PS.color(10, 10, PS.COLOR_BLACK);
  PS.data(10, 10, "wall");

  PS.color(8, 10, PS.COLOR_BLACK);
  PS.data(8, 10, "wall");

  PS.color(5, 4, PS.COLOR_BLACK);
  PS.data(5, 4, "wall");

  PS.color(10, 8, PS.COLOR_BLACK);
  PS.data(10, 8, "wall");

  PS.color(8, 8, PS.COLOR_BLACK);
  PS.data(8, 8, "wall");

  PS.color(0, 2, PS.COLOR_BLACK);
  PS.data(0, 2, "wall");

  PS.color(2, 3, PS.COLOR_BLACK);
  PS.data(2, 3, "wall");

  PS.color(2, 2, PS.COLOR_BLACK);
  PS.data(2, 2, "wall");

  PS.color(1, 0, PS.COLOR_BLACK);
  PS.data(1, 0, "wall");

  PS.color(0, 0, PS.COLOR_BLACK);
  PS.data(0, 0, "wall");

  PS.color(4, 4, PS.COLOR_BLACK);
  PS.data(4, 4, "wall");

  PS.color(4, 5, PS.COLOR_BLACK);
  PS.data(4, 5, "wall");

  PS.color(5, 5, PS.COLOR_BLACK);
  PS.data(5, 5, "wall");

  PS.color(10, 3, PS.COLOR_BLACK);
  PS.data(10, 3, "wall");

  PS.color(10, 5, PS.COLOR_BLACK);
  PS.data(10, 5, "wall");

  PS.color(4, 0, PS.COLOR_BLACK);
  PS.data(4, 0, "wall");

  PS.color(8, 5, PS.COLOR_BLACK);
  PS.data(8, 5, "wall");

  PS.color(3, 7, PS.COLOR_BLACK);
  PS.data(3, 7, "wall");

  PS.color(1, 9, PS.COLOR_BLACK);
  PS.data(1, 9, "wall");

  PS.color(4, 10, PS.COLOR_BLACK);
  PS.data(4, 10, "wall");

  PS.color(0, 5, PS.COLOR_BLACK);
  PS.data(0, 5, "wall");

  PS.color(1, 7, PS.COLOR_BLACK);
  PS.data(1, 7, "wall");

  PS.color(6, 7, PS.COLOR_BLACK);
  PS.data(6, 7, "wall");

  PS.color(6, 9, PS.COLOR_BLACK);
  PS.data(6, 9, "wall");


  // display The Snail's location
  drawPlayer(5, 0);
  PS.data(5, 0, "wall");
};

// PS.touch ( x, y, data, options )
// Called when the mouse button is clicked on a bead, or when a bead is touched
PS.touch = function(x, y, data, options) {
  "use strict";

  // Uncomment the following line to inspect parameters
  //PS.debug( "PS.touch() @ " + x + ", " + y + "\n" );

  // Add code here for mouse clicks/touches over a bead

};

// PS.release ( x, y, data, options )
// Called when the mouse button is released over a bead, or when a touch is lifted off a bead
PS.release = function(x, y, data, options) {
  "use strict";

  // Uncomment the following line to inspect parameters
  // PS.debug( "PS.release() @ " + x + ", " + y + "\n" );

  // Add code here for when the mouse button/touch is released over a bead
  //PS.color(x, y, PS.COLOR_GREEN);
  //PS.debug("You clicked on " + x + ", " + y + "\n");
};

// PS.enter ( x, y, button, data, options )
// Called when the mouse/touch enters a bead
PS.enter = function(x, y, data, options) {
  "use strict";

  // Uncomment the following line to inspect parameters
  // PS.debug( "PS.enter() @ " + x + ", " + y + "\n" );

  // Add code here for when the mouse cursor/touch enters a bead
};

// PS.exit ( x, y, data, options )
// Called when the mouse cursor/touch exits a bead
PS.exit = function(x, y, data, options) {
  "use strict";

  // Uncomment the following line to inspect parameters
  // PS.debug( "PS.exit() @ " + x + ", " + y + "\n" );

  // Add code here for when the mouse cursor/touch exits a bead
};

// PS.exitGrid ( options )
// Called when the mouse cursor/touch exits the grid perimeter
PS.exitGrid = function(options) {
  "use strict";

  // Uncomment the following line to verify operation
  // PS.debug( "PS.exitGrid() called\n" );

  // Add code here for when the mouse cursor/touch moves off the grid
};

// 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

  // 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 left isn’t a wall
    //Check that left 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);
      }
    }
  }

  // Keys to shoot the slime from the snail
  if (key == PS.KEY_ARROW_UP) // shoot up
  {
    if (isInGrid(player.x, player.y - 1)) drawSlime(player.x, player.y - 1, "up");

  }
  if (key == PS.KEY_ARROW_LEFT) // shoot left
  {
    if (isInGrid(player.x - 1, player.y)) drawSlime(player.x - 1, player.y, "left");
  }
  if (key == PS.KEY_ARROW_DOWN) // shoot down
  {
    if (isInGrid(player.x, player.y + 1)) drawSlime(player.x, player.y + 1, "down");
  }
  if (key == PS.KEY_ARROW_RIGHT) // shoot right
  {
    if (isInGrid(player.x + 1, player.y)) drawSlime(player.x + 1, player.y, "right");
  }

};

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

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

  // Add code here for when a key is released
};

// PS.input ( sensors, options )
// Called when an input device event (other than mouse/touch/keyboard) is detected
PS.input = function(sensors, options) {
  "use strict";

  // Uncomment the following block to inspect parameters
  /*
    PS.debug( "PS.input() called\n" );
    var device = sensors.wheel; // check for scroll wheel
    if ( device )
    {
        PS.debug( "sensors.wheel = " + device + "\n" );
    }
    */

  // Add code here for when an input event is detected
};

1 个答案:

答案 0 :(得分:0)

虽然出于多种原因这是错误的方法,但一个简单的解决方案是遍历x内的yPS.data的所有值,并检查除了蜗牛的当前位置已设置为"wall"

function boardCovered() {
  for(var x = 0; x < 10; x++) {
    for(var y = 0; y < 10; y++) {
      if(PS.data(x, y) != "wall")  //This is not the correct check - just an example. You need to figure this part out
        return false;
    }
  }
  return true;
}

我对Perienspiel一无所知,但这将迭代您的数据结构并为您提供正确的结果。