贪吃蛇
const WIDTH = 1000; const HEIGHT = 700; const SNACK_WIDTH = 20; const SNACK_HEIGHT = 20; //移动时间间隔 const TIME_MOVE = 300; //食物刷新时间 const TIME_FOOD = 5000; //食物总量 const TOTAL_FOOD = 200; //石头刷新时间 const TIME_STONE = 8000; //石头总量 const TOTAL_STONE = 300; var switchStatus = 0; var changeMove = "right"; var currentMove = "right"; //所有被用的位置 var points = new Array(); var snacks = new Array(); var foods = new Array(); var stones = new Array(); var moveing = false; var timeMoveId, timeFoodId, timeStoneId; window.onload = function () { createGround(); }; //捕获按的哪个键 function keyDown(event) { if (event.keyCode == "87") { //按下W键 changeMove = "up"; } else if (event.keyCode == "68") { //按下D键 changeMove = "right"; } else if (event.keyCode == "83") { //按下S键 changeMove = "down"; } else if (event.keyCode == "65") { //按下A键 changeMove = "left"; } } function handlePoints(x, y, type) { if (0 == type) { points.push(x+y*WIDTH); } else { var index = points.indexOf(x+y*WIDTH); if (index != -1) { points.splice(index, 1); } } } //贪吃蛇 function snack(x, y) { this.x = x; this.y = y; } //食物 function food(x, y) { this.x = x; this.y = y; } //石头 function stone(x, y) { this.x = x; this.y = y; } //开关操作 function clickSwitch() { if (switchStatus == 0 || switchStatus == 2) { document.getElementById("switch").value = "暂停"; if (switchStatus == 0) { //开始 play(); } switchStatus = 1; } else if (switchStatus == 1) { document.getElementById("switch").value = "继续"; switchStatus = 2; } } //碰撞判定并操作 function judge(x, y) { /*** 判断是否撞上边界 ***/ if (x