나는 사용자가 비행기를 선택하고 주위를 움직일 간단한 게임 시뮬레이션을 만들려고합니다. 하나의 비행기를 그릴 수 있고 4 개의 버튼을 추가하여 주변을 이동할 수 있습니다. 그러나 임의의 위치에 6 개의 똑같은 비행기를 만들고 그릴 수있는 방법을 모르겠습니다. 또한 사용자는 평면 중 하나를 선택하고 주위를 이동할 수 있어야합니다.자바 스크립트 : 여러 위치에서 동일한 폴리곤을 그리는 방법은 무엇입니까?
Jsfiddle : https://jsfiddle.net/fvtjzLhr/
HTML 코드 :
<canvas id="canvas" width="500" height="500"></canvas>
<br>
<button id="Left">Left</button>
<button id="Up">Up</button>
<button id="Down">Down</button>
<button id="Right">Right</button>
자바 스크립트 코드 :
이var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var poly=[ 20,0, 40,0, 50,15, 100,10, 130,30, 100,50, 50,45, 40,60, 20,60, 30,45, 20,40, 10,40, 0,45, 0,15, 10,20, 20,20, 30,15];
var spaceship1 = {
x: 0,
y: 0,
speed: 50,
altitude: 360,
id: 68,
direction: 150
}
document.getElementById("Up").addEventListener("click", function(){
spaceship1.y -= 30;
});
document.getElementById("Down").addEventListener("click", function(){
spaceship1.y += 30;
});
document.getElementById("Left").addEventListener("click", function(){
spaceship1.x -= 30;
});
document.getElementById("Right").addEventListener("click", function(){
spaceship1.x += 30;
});
function renderSpaceship(){
ctx.clearRect(0, 0, canvas.width, canvas.height);
//ctx.fillStyle = '#D3D3D3';
ctx.beginPath();
ctx.moveTo(poly[0]+spaceship1.x, poly[1]+spaceship1.y);
for(item=2 ; item < poly.length-1 ; item+=2){ctx.lineTo(poly[item]+spaceship1.x , poly[item+1]+spaceship1.y)}
ctx.closePath();
ctx.fill();
ctx.font="17px Georgia";
ctx.fillText("ID: "+spaceship1.id, spaceship1.x, 120+spaceship1.y);
ctx.fillText("Altitude: "+spaceship1.altitude, spaceship1.x, 105+spaceship1.y);
ctx.fillText("Speed: "+spaceship1.speed, spaceship1.x, 90+spaceship1.y);
ctx.fillText("Direction: "+spaceship1.direction, spaceship1.x, 75+spaceship1.y);
}
function renderAll(){
renderSpaceship();
}
setInterval(renderAll, 10);
이 배경을 무시합니다. 청색은 선택된 평면을 나타냅니다.