javascript - 如何创建程序数组地图(使用数组作为平铺地图)
问题描述
我有一个游戏,(顺便说一句,它在https://juniorcpc.itch.io/dungeon-game上发布 ,代码的下载文件也在那里),它使用数组来创建平铺地图。(0-4 个数字代表不同的图块)问题是我只能创建地图并发布它们,我想知道如何让代码为我创建这些数组地图。我不能使用其他教程,因为它们只展示了如何使用平铺地图、迷宫或其他东西,但我使用的是数组而不是其他系统。如果有一种方法可以使用很棒的数组制作程序地图,谢谢!
顺便说一句,阵列是 10x10 网格,1 = 墙壁,0 = 空白空间,3 = 退出。
数组示例:
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 2, 4, 1,
1, 2, 2, 0, 0, 2, 2, 2, 2, 1,
1, 1, 1, 2, 0, 2, 2, 2, 2, 1,
1, 2, 2, 0, 0, 2, 2, 2, 4, 1,
1, 0, 0, 0, 2, 2, 2, 3, 2, 1,
1, 0, 2, 2, 2, 2, 2, 2, 2, 1,
1, 0, 2, 1, 1, 2, 0, 0, 0, 1,
1, 0, 2, 2, 2, 2, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
],
以下是它如何绘制它们:(我的 HTML 中有一个宽度为 300 像素、高度为 300 像素的画布)
function mapM() {
var ctx = document.getElementById("canv").getContext('2d');
ctx.fillStyle = "red";
var mapOnX = -1;
var mapOnY = 0;
var l = map[0].length + 1;
for (i = 0; i < l; i++) {
if (i % 10 == 0) {
var mapOnX = 0;
mapOnY++;
} else {
mapOnX++;
}
if (map[level][i] == 1) {
ctx.fillStyle = "red";
} else {
if (map[level][i] == 0) {
ctx.fillStyle = 'blue';
} else {
if (map[level][i] == 3) {
ctx.fillStyle = 'gold';
} else {
if (map[level][i] == 2) {
ctx.fillStyle = 'black';
} else {
ctx.fillStyle = 'lightBlue';
}
}
}
}
ctx.fillRect(mapOnX * 25, mapOnY * 25, 25, 25);
ctx.fillStyle = 'purple';
ctx.fillRect(mapX * 25, mapY * 25, 25, 25);
}
}
不要担心我能做到的其他数字。谢谢!
解决方案
推荐阅读
- c# - 使用 MSDASC.DLL 时,我应该如何解决“不能封送 '...' 的至少一个参数”错误
- matlab - 找出不同长度的键控向量之间的差异(在matlab中)
- flask - 如何正确配置路由以仅显示子目录?
- emacs - Emacs“没有到主机的路由”:名称“elpa.gnu.org”
- authentication - 最新的 ejabberd 社区版是否支持基于客户端证书的身份验证?
- css - 试图抓取除前 5 个之外的所有锚标签
- c - 为什么分配指针后程序崩溃
- kubernetes - 如何修复 PipelineParam 丢弃 Kubeflow Pipeline 中除名称以外的所有信息
- oracle - 将多个 clob 行合并为一个
- clojure - 如何在 Clojure 中将 CLI 参数向量拆分成对?