[英]Need help making a puzzle game
我正在使用Perlenspiel制作益智游戲,並將Javascript與Sublime Text結合使用。 該游戲是關於玩家控制蝸牛的,它留下了玩家無法通過的綠色泥渣。 目標是用綠色粘液覆蓋白色珠子區域。 我已經完成了大部分游戲設置,我只需要幫助弄清楚如何用綠色粘液覆蓋該區域后如何結束游戲並加載下一個關卡。
我是使用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
};
盡管由於多種原因這是錯誤的方法,但一個簡單的解決方案是遍歷PS.data
的x
和y
所有值,並檢查是否已將蝸牛的當前位置以外的每個坐標設置為"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一無所知,但這會迭代您的數據結構並為您提供正確的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.