我仅使用Javascript创建游戏“吃豆人”。
我需要解决这个卡住的问题,希望你们中的一些人可以帮助我检查我在哪里错了。请注意,我只能使用Javascript。
我将其组合到一个html文件中以显示JS代码的作用,但我们将重点放在javascript代码上,当然首先我对游戏的工作感兴趣。因此,如果需要,请忽略html部分。
请注意,我是新的编程Java脚本,可能会使用一些奇怪的想法。
游戏板
我需要在30x30的游戏板阵列上创建“吃豆子游戏”。
游戏板位于称为“板”的2D阵列上,其长度为30。 “ board”数组中的元素仅是“ 0”和“ 1”元素。元素“ 1”表示墙,元素“ 0”表示路径。
玩家
播放器(也是数组)可以通过“ 0”路径在地图上移动。它在地图内随机生成。生成时,它需要随机地具有一个有效的方向才能移动,并能够通过按last_keyboard_direction(向上,向下,向左,向右)进行控制。
此播放器数组将按顺序包含4个元素:[行,列,方向,last_keyboard_direction]。
鬼魂
幽灵随机产生,并自动直接移动到有效路径(此处为0),直到它们与墙碰撞,然后随机更改为另一个有效方向(上,下,左或右)。
每个重影数组元素为:[行,列,方向]
到目前为止我做了什么?
我现在要做什么?
我被困在哪里?
我陷入了功能:checkPosition()。当鬼魂产生并需要第一次移动然后设置有效方向时,将使用此功能。当幽灵继续直行到它已经设置的方向并碰撞,然后设置新的有效方向时,也会使用它。
我需要检查其方向的下一个位置是否有效,如果无效,则搜索一个新的有效位置并随机选择一个。搜索到的有效个人将被临时保存到变量中:有效= [];
当函数找到有效的下一个位置时,将其推入临时数组“ valid”。例如,如果幽灵直行碰撞到数字“ 1”,它将检查新的有效方向直行直到再次发生碰撞。
示例:如果它可以上下移动,则将其保存在有效[]上,最终填充为:有效[上,下]。
然后,一个名为“ direction”的新变量将在“ valid”数组内的所有元素中随机选择它:var direccio = valid [Math.floor(Math.random()* valid.length)];最后为幽灵设置新的有效方向。
请注意:
代码已被注释,请特别检查checkPosition()数组以及如何使用其新的有效位置更新ghosts数组。
所有代码:
<html>
<head>
<title>PACMAN</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<table id="table" cellpadding=4 border="0" style="width:50%;text-align:center;vertical-align:middle" style="align:center">
<tr>
<th> 0</th>
<th> 1</th>
<th> 2</th>
<th> 3</th>
<th> 4</th>
<th> 5</th>
<th> 6</th>
<th> 7</th>
<th> 8</th>
<th> 9</th>
<th>10</th>
<th>11</th>
<th>12</th>
<th>13</th>
<th>14</th>
<th>15</th>
<th>16</th>
<th>17</th>
<th>18</th>
<th>19</th>
<th>20</th>
<th>21</th>
<th>22</th>
<th>23</th>
<th>24</th>
<th>25</th>
<th>26</th>
<th>27</th>
<th>28</th>
<th>29</th>
</tr>
</table>
<script>
var board = [];
//GHOSTS, VALUES: [row, column, direction] //set randomly
var ghost1 = [0, 0, 0];
var ghost2 = [0, 0, 0];
var ghost3 = [0, 0, 0];
//PLAYER, VALUES: [row, column, direction, last_keyboard_direction] //set randomly except last_keyboard_direction
var player = [0, 0, 0, 0];
board[0]= [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
board[1]= [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
board[2]= [1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1];
board[3]= [1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1];
board[4]= [1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1];
board[5]= [1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1];
board[28]= [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
board[29]= [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
var row_ghost = [];
var column_ghost = [];
function generateGhosts() {
var comptador = 0;
do {
var row = getRandomInt(0, 29);
var column = getRandomInt(0, 29);
var random_position = board[row][column];
if (random_position === 0) {
board[row][column] = "F" + comptador;
row_ghost.push(row);
column_ghost.push(column);
document.write("GHOST " + (comptador) + " ACTUAL POSITION:: " + "ROW: " + row + " COLUMN: " + column + " VALUE: " + random_position);
document.write(" </br> ");
comptador++;
}
} while (comptador < 3) //creates 3 ghosts on valid positions
}
function generatePlayer() {
player = 0;
do {
var row = getRandomInt(0, 29);
var column = getRandomInt(0, 29);
var random_position = board[row][column];
if (random_position === 0) {
player++;
board[row][column] = "JG";
document.write("PLAYER " + (player) + " ACTUAL POSITION:: " + "ROW: " + row + " COLUMN: " + column + " VALUE: " + random_position);
document.write(" </br> ");
}
} while (player < 1) //creates one player
}
//This next function will need to set a new random valid direction in case the ghost can go straight anymore to it's actual set direction
function checkPosition() {
//Check new next positions
for (i = 0; i < row_ghost.length; i++) {
var valid = [];
//CHECKING NEW VALID POSITIONS IF !== 1 then is valid for a new.
//MY MAIN PROBLEM IS HERE INSIDE THE "IF" CONDITIONALS. YOU CAN TEST document.write("valid");, it just enters all if's all pushes all positions as valid.
//I know I'm comparing the wrong way. Need to take the element inside this row_ghost and column_ghost position but I dont know how.
if (((row_ghost[i]) - 1) && (column_ghost[i]) !== 1){
//It can go up ^
valid.push("up");
}
if (((row_ghost[i]) + 1) && (column_ghost[i]) !== 1){
//It can go down
valid.push("down");
}
if ((row_ghost[i] && ((column_ghost[i]) - 1)) !== 1){
//It can go left <
valid.push("left");
}
if ((row_ghost[i] && ((column_ghost[i]) + 1)) !== 1){
//It can go right >
valid.push("right");
}
//Once we checked the possible new valid positions, with the array direction we choose only ONE ELEMENT RANDOMLY
var direction = valid[Math.floor(Math.random() * valid.length)];
rowValid = []; //Set a new array to save the next row valid position for each player
columnValid = []; //Set a new array to save the next column valid position for each player
//ONLY ONE "direction" WILL BE SENDED BELOW, chosed randomly
if (direction === "up") {
//up ^
rowValid = (row_ghost[i] - 1);
columnValid = column_ghost[i];
}
if (direction === "down"){
//down v
rowValid = (row_ghost[i] + 1);
columnValid = column_ghost[i];
}
if (direction === "left"){
//left <
rowValid = row_ghost[i];
columnValid = (column_ghost[i] - 1);
}
if (direction === "right"){
//right >
rowValid = row_ghost[i];
columnValid = (column_ghost[i] + 1);
}
document.write("NEXT POSITION OF GHOST:::: " + i + ":::: ROW " + rowValid + " - COLUMN " + columnValid);document.write(" Next direction is: " +direction + " ///");
document.write("/// Next valid positions taken from this gost were: " + valid + ")"); //As you can see it pushes all positions as valid
document.write("</br>");
valid = []; //We empty the array for future checkings
}
}
generateGhosts(); //Generate 3 ghosts on valid positions.
generatePlayer(); //Generate one player on a valid position.
document.write("</br>");
document.write("FUTURE PATHS:");
document.write("</br></br>");
checkPosition();
//==============PRINT TABLE HTML==============//
table = document.getElementById("table");
for(var i = 0; i < board.length; i++)
{
// create a new row
var newRow = table.insertRow(table.length);
for(var j = 0; j < board[i].length; j++)
{
// create a new cell
var cell = newRow.insertCell(j);
// add value to the cell
cell.innerHTML = board[i][j];
}
}
//==============TABLE PRINTED==============//
</script>
主要问题代码:
请注意:row_ghost.push(row);和column_ghost.push(column); 它将用于其他函数checkPosition();
function generateGhosts() {
var comptador = 0;
do {
var row = getRandomInt(0, 29);
var column = getRandomInt(0, 29);
var random_position = board[row][column];
if (random_position === 0) {
board[row][column] = "F" + comptador;
row_ghost.push(row);
column_ghost.push(column);
comptador++;
}
} while (comptador < 3) //creates 3 ghosts on valid positions
}
//This next function will need to set a new random valid direction in case the ghost can go straight anymore to it's actual set direction
function checkPosition() {
//Check new next positions
for (i = 0; i < row_ghost.length; i++) {
var valid = [];
if (((row_ghost[i]) - 1) && (column_ghost[i]) !== 1){
//It can go up ^
valid.push("up");
}
if (((row_ghost[i]) + 1) && (column_ghost[i]) !== 1){
//It can go down
valid.push("down");
}
if ((row_ghost[i] && ((column_ghost[i]) - 1)) !== 1){
//It can go left <
valid.push("left");
}
if ((row_ghost[i] && ((column_ghost[i]) + 1)) !== 1){
//It can go right >
valid.push("right");
}
//Once we checked the possible new valid positions, with the array direction we choose only ONE ELEMENT RANDOMLY
var direction = valid[Math.floor(Math.random() * valid.length)];
rowValid = []; //Set a new array to save the next row valid position for each player
columnValid = []; //Set a new array to save the next column valid position for each player
//ONLY ONE "direction" WILL BE SENDED BELOW, chosed randomly
if (direction === "up") {
//up ^
rowValid = (row_ghost[i] - 1);
columnValid = column_ghost[i];
}
if (direction === "down"){
//down v
rowValid = (row_ghost[i] + 1);
columnValid = column_ghost[i];
}
if (direction === "left"){
//left <
rowValid = row_ghost[i];
columnValid = (column_ghost[i] - 1);
}
if (direction === "right"){
//right >
rowValid = row_ghost[i];
columnValid = (column_ghost[i] + 1);
}
valid = []; //We empty the array for future checkings
}
}