1 function Fun4() { 2 ctx.clearRect(0, 0, canvas.width, canvas.height); 3 ctx.beginPath(); 4 ctx.moveTo(550, 0); 5 ctx.lineTo(550, 550); 6 ctx.lineTo(0, 550); 7 ctx.lineWidth = 1; 8 ctx.stroke(); 9 //头移动 10 arr_B[0] = arr_A[0][0]; 11 arr_B[1] = arr_A[0][1]; 12 arr_A[0][0] = arr_A[0][0] + 11 * gx; 13 arr_A[0][1] = arr_A[0][1] + 11 * gy; 14 isFail(); 15 //身体移动 16 for (j = 1; j < l; j++) { 17 arr_C[0] = arr_A[j][0]; 18 arr_C[1] = arr_A[j][1]; 19 arr_A[j][0] = arr_B[0]; 20 arr_A[j][1] = arr_B[1]; 21 arr_B[0] = arr_C[0]; 22 arr_B[1] = arr_C[1]; 23 } 24 //吃食物 25 eat(); 26 //画蛇 27 28 for (i = 0; i < l; i++) { 29 // alert(arr_A); 30 if (i == 0) { ctx.fillStyle = "rgb(63, 46, 219)"; } 31 ctx.fillRect(arr_A[i][0], arr_A[i][1], 10, 10); 32 ctx.fillStyle = "rgb(154, 210, 244)"; 33 } 34 //画食物 ,得分 35 ctx.save(); 36 ctx.fillStyle = "red"; 37 ctx.fillRect(arr_D[0], arr_D[1], 10, 10); 38 ctx.fillStyle = "black"; 39 ctx.clearRect(600, 50, 200, 200); 40 ctx.font = "bolder 20px 宋体"; 41 ctx.fillText("得分:" + (l - 6) * 10, 600, 50); 42 ctx.restore(); 43 }