gpt4 book ai didi

javascript - 为什么我的游戏中没有出现任何框?

转载 作者:行者123 更新时间:2023-12-03 08:21:49 26 4
gpt4 key购买 nike

周末的作业是使用canvas和javascript制作一个吃 bean 人风格的游戏。根据我正在观看的教程,当我运行这个时,我应该看到 map 的第一行,但我只是得到了 map 边框。

这是我的 HTML

<!DOCTYPE html>
<html>
<head>
<title></title>

<meta charset="utf-8" />
</head>
<body>
<canvas id="myCanvas" width="800" height="500" style="border: 1px solid gray"></canvas>
<script src="file1.js"> </script>
</body>
</html>

这是我的 typescript

let canvas = <HTMLCanvasElement>document.getElementById('myCanvas');
let context = canvas.getContext("2d");
let img = new Image();
img.onload = function () {
context.drawImage(img, 0, 0);
}
img.src = "zombie.png";

let mapArray =
["############################",
"# # # o ##",
"# #",
"# #### ##### ## #",
"## # # ## #",
"### ## # #",
"# ### # #",
"# #### ### #",
"# ## # o #",
"# o # # o ### ### #",
"# # # #",
"############################"];

//need to add wall.scource = and grass.source =
let wall = new Image();
let grass = new Image();
grass.src = "C:\Users\Scott\Desktop\grassimg.jpg";
wall.src = "http://www.tecmobowl-vs-rbi.com/uploads/3/5/3/8/3538178/_2943519.jpg";

let posX = 0;
let posY = 0;

//for loops set images at given coordinates according to position on mapArray
for (let i = 0; i < mapArray.length; i++) {
for (let j = 0; j < mapArray.length; i++) {

if (mapArray[i][j] == "") {
context.drawImage(grass, posX, posY, 32, 32)//last two are size of image )
}
if (mapArray[i][j] == "#") {
context.drawImage(wall, posX, posY, 32, 32)//last two are size of image )
}
posX+=32
}

}

任何帮助显示其中的一些内容都会很棒!!

最佳答案

jsFiddle:https://jsfiddle.net/kvxx73f7/

JavaScript

var canvas = document.getElementById('myCanvas');
var context = canvas.getContext("2d");
var img = new Image();
img.onload = function () {
context.drawImage(img, 0, 0);
}
img.src = "zombie.png";

var mapArray =
["############################",
"# # # o ##",
"# #",
"# #### ##### ## #",
"## # # ## #",
"### ## # #",
"# ### # #",
"# #### ### #",
"# ## # o #",
"# o # # o ### ### #",
"# # # #",
"############################"];

//need to add wall.scource = and grass.source =
var wall = new Image();
var grass = new Image();
grass.src = "http://vignette2.wikia.nocookie.net/tibia/images/6/60/Grass_(Tile).gif/revision/latest?cb=20080817072800&path-prefix=en";
wall.src = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJyofLT0tMSw3QTo6LC87RD84Nzc5OjcBCgoKBQUFDgUFDisZExkrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrK//AABEIACAAIAMBIgACEQEDEQH/xAAYAAADAQEAAAAAAAAAAAAAAAACAwQFAP/EACkQAAEDAwIDCQEAAAAAAAAAAAECAxEABCESEyMxURQyM0Fhc4GT0QX/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEQMRAD8Au/oXDzT2ltZSJxAHKp27242yC5nPxQ3Qm5dVqzOMelI7vNMdYoKm718Jw4c85ApN5cXIbWvdUTtk48qBKkGcgEdaFRT2e4AIPCVQaF+1tvuawAVEQKlVBM8ulHfPoXeunfaKQqJ1ilrW0UnjNfYP2gVoCoURIpdwhoMuhKTG2ZNODiJI3miPcFddvMm1cSHGvDIEKBJoP//Z";

var posX = 0;
var posY = 0;

//for loops set images at given coordinates according to position on mapArray
for (var y = 0; y < mapArray.length; y++) {
for (var x = 0; x < mapArray[0].length; x++) {

if (mapArray[y][x] == "") {
context.drawImage(grass, (32 * x) , (32 * y), 32, 32)//last two are size of image )
}
if (mapArray[y][x] == "#") {
context.drawImage(wall, (32 * x) , (32 * y), 32, 32)//last two are size of image )
}

}

}

当您执行循环时,您在两个 for 循环中重复使用 i++ 这是错误的,其次在 j 中的 for 循环结束时您正在执行 posX+ =32 但不是 posY,但是您可以做的就是使用您的 ij (我已将其重命名为 x和 y 来帮助您理解我的代码)来计算出新的 Xposition 和 Yposition。请注意,我正在使用来自 Google 的一些图像,您可能需要刷新页面才能看到显示的图像。

关于javascript - 为什么我的游戏中没有出现任何框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33713938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com