私はエコロジーシミュレーションを作成しようとしてきましたが、今のところ良くなっています。以下のコードはうまくいきます。手動で行うのではなく、コードでキャンバス上にもっと多くのアイテムを描画する方が簡単なのかどうか疑問に思っています。私がそれをやっているところで、私はコードに多くのものを追加するので(例えば、移動、検出、再現、追跡、実行など)、遅れを考慮します。このより多くのスクリプトタグを作成せずにキャンバスにもっと描く
//This tag will regulate the spawning of new sheep/wolves
var totalWolves = 0;
var totalSheep = 0;
var canavs = document.getElementById("canvas");
var body = document.getElementById("body");
//styler
body.style.overflow = "hidden";
body.style.margin = "0px";
canvas.style.backgroundColor = "black";
function spawnWolves(){
\t totalWolves++;
\t var name = "wolf" + totalWolves;
\t var scrpt = document.createElement("SCRIPT");
\t document.body.appendChild(scrpt);
\t scrpt.setAttribute("id", name);
\t var script = document.getElementById(name);
\t script.innerHTML = "var rand3 = Math.floor(Math.random() * 100) + 1; var rand4 = Math.floor(Math.random() * 100) + 1; var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); context.fillStyle = 'red'; context.fillRect(rand3, rand4, 10, 10); context.fill();";
\t
}
spawnWolves();
spawnWolves();
spawnWolves();
<!DOCTYPE html>
<html>
<head>
<title>AI spawn test</title>
</head>
<body id="body">
<canvas id="canvas" width="1366px" height="768px"/>
<script>
</script>
</body>
</html>
はすでに12オオカミ –
おかげで、私が知っているよう習慣は何かを、あなたの第二のループに注意してください、私はOPのために説明を追加します。 –
キャンバスに描くためのスクリプトを作成することは冒険的な方法です.... –