Pacman游戏:检查和更新2d数组有效位置-Javascript
我只使用Javascript创建游戏“Pacman” 我需要解决这个我陷入困境的问题,我希望你们中的一些人能帮助我检查我错在哪里。请注意,我只能使用Javascript 我在一个html文件中结合了这一点来展示JS代码的作用,但我们将重点关注javascript代码,首先我对游戏的运行感兴趣。因此,如果需要,可以忽略html部分 请注意,我是一个新的Javascript编程人员,我可能会使用一些奇怪的想法 游戏板 我需要在30x30的游戏板阵列上创建“吃豆人游戏” 游戏板位于一个称为“板”的2D阵列上,其长度为30。“board”数组中的元素仅为“0”和“1”元素。元素“1”表示墙,“0”表示路径 玩家 玩家也是一个数组,可以通过“0”路径在地图上移动。它在地图中随机生成。当它繁殖时,它需要随机有一个有效的移动方向,并且能够由最后按下的键盘方向(上、下、左、右)控制 此播放器数组将按顺序包含4个元素:[行、列、方向、最后一个键盘方向] 幽灵 重影随机生成并自动直接移动到有效路径(0所在位置),直到它们与墙碰撞,然后随机更改到另一个有效方向(上、下、左或右) 每个重影数组元素为:[行、列、方向] 到目前为止我做了什么?Pacman游戏:检查和更新2d数组有效位置-Javascript,javascript,html,arrays,Javascript,Html,Arrays,我只使用Javascript创建游戏“Pacman” 我需要解决这个我陷入困境的问题,我希望你们中的一些人能帮助我检查我错在哪里。请注意,我只能使用Javascript 我在一个html文件中结合了这一点来展示JS代码的作用,但我们将重点关注javascript代码,首先我对游戏的运行感兴趣。因此,如果需要,可以忽略html部分 请注意,我是一个新的Javascript编程人员,我可能会使用一些奇怪的想法 游戏板 我需要在30x30的游戏板阵列上创建“吃豆人游戏” 游戏板位于一个称为“板”的2D
- 创建2D阵列boardgame:board[30][30]
- 在正确的位置随机产生玩家“JG”和3个幽灵“F0”、“F1”、“F2”
- 创建一个函数来检查(墙碰撞后)重影旁边的位置是否可以有效移动
- 然后,使用这个有效的下一个位置,用这个有效的位置填充一个临时数组,并在其中随机选择(上、下、左或右)
- 如果有效位置为“查找”,则设置此新值并保持直线移动,直到再次碰撞
- 当函数找到有效的下一个位置时,将其推送到临时数组“valid”。例如,如果重影笔直与数字“1”碰撞,它将检查是否有新的有效方向笔直,直到再次碰撞
- 示例:如果它可以上下移动,则会将其保存在valid[]上,最后填充如下:valid[up,down]
- 然后一个名为“direction”的新变量将在“valid”数组中的所有元素中随机选择它:var direccio=valid[Math.floor(Math.random()*valid.length)];最后将新的有效方向设置为重影
<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,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, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1];
board[25]= [1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1];
board[26]= [1, 0, 1, 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[27]= [1, 0, 1, 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>
- 游戏速度移动将是每1秒一次。(此代码中尚未设置速度)
- 代码不尝试记录最后按下的箭头键
- 路径编号为“0”,墙壁编号为“1”
- 在任意位置产生鬼魂和玩家,中心不需要
- HTML代码并不重要,只是为了您对代码的理解
<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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1];
board[24]= [1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1];
board[25]= [1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1];
board[26]= [1, 0, 1, 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[27]= [1, 0, 1, 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>
吃豆人
0
1.
2.
3.
4.
5.
6.
7.
8.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var board=[];
//重影,值:[行、列、方向]//随机设置
var ghost1=[0,0,0];
var ghost2=[0,0,0];
var ghost3=[0,0,0];
//播放器,值:[行,列,方向,最后一个键盘方向]//随机设置,最后一个键盘方向除外
var-player=[0,0,0,0];
电路板[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,1,1,1,1,1,1];
电路板[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,1];
电路板[2]=[1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,0,1];
电路板[3]=[1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,0,1];
电路板[4]=[1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,0,1];
电路板[5]=[1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,0,1];
电路板[6]=[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1];
电路板[7]=[1,0,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,1,0,1];
电路板[8]=[1,0,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,1,0,1];
板[9]=[1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,