我想知道如何创建当被球击中时消失的砖块,而不是手动绘制每一块砖块。手动,我的意思是创建每一块砖,并为每一块砖使用 if 语句来检查球是否击中。我已经完成了所有其他事情,这是我到目前为止的代码:提前谢谢你......
<body>
<canvas height="400" width="400" id="myCanvas"></canvas>
<script>
"use strict"
var a=document.getElementById("myCanvas");
var c=a.getContext("2d");
var platformX=170;
var speed=0;
var ballX=200
var ballY=50;
var ballSpeed=0;
var ballBounce=0;
var ballRadius=10;
var interval=window.setInterval(createBall,17);
var bullets=[];
c.fillStyle="white";
c.beginPath();
c.rect(platformX,350,40,30);
c.fill();
c.closePath();
c.fillStyle="green";
c.beginPath();
c.arc(200,ballY,10,0,2*Math.PI);
c.fill();
c.closePath();
window.onkeydown=function(e){
if(e.keyCode==37){
speed=-6;
}
if(e.keyCode==39){
speed=6;
}
if(e.keyCode==32){
ballSpeed=2;
}
}
window.onkeyup=function(e){
if(e.keyCode==37){
speed=0;
}
if(e.keyCode==39){
speed=0;
}
}
function movePlatform(){
platformX+=speed;
c.clearRect(0,350,400,50);
c.fillStyle="white";
c.beginPath();
c.rect(platformX,350,80,20);
c.fill();
c.closePath();
}
window.setInterval(movePlatform,17);
function createBall(){
ballY+=ballSpeed;
ballX+=ballBounce;
c.clearRect(0,0,400,350);
c.fillStyle="green";
c.beginPath();
c.arc(ballX,ballY,ballRadius,0,2*Math.PI);
c.fill();
</script>
</body>
子衿沉夜
相关分类