贪吃蛇源码(免费,直接复制粘贴,不墨迹)
思路之后有时间再总结,想学的可以看注释,带入面向对象思想即可
好用给个赞
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
div#map{
width: 200px;
height: 200px;
border: 1px black solid;
margin: 50px auto;
position: relative;
}
div#top{
height: 50px;
width: 205px;
margin: 0px auto;
}
button{
font-size: 30px;
width: 100px;
}
div#info{
font-size: 40px;
color: pink;
font-family: "楷体";
text-align: center;
}
div#size{
margin: 0px auto;
text-align: center;
}
input{
width: 50px;
}
</style>
</head>
<body>
<div id="info">Q暂停,E开始,你想鼠标点按钮随便你</div>
<div id="top">
<button id="start">开始</button>
<button id="pause">暂停</button>
</div>
<div id="size">
游戏界面宽:<input id="width" type="number" value="20"/><br />
游戏界面高:<input id="height" type="number" value="20"/><br />
<font color="red">(建议最小20X20)</font>
</div>
<div id="map"></div>
</body>
<script>
function Map(width,height){
this.canva = document.getElementById("map");
this.cellLength = 20;
this.width = width;
this.height = height;
this.getMap = function (judge){
this.canva.style.width = this.width*this.cellLength+"px";
this.canva.style.height = this.height*this.cellLength+"px";
if(judge == true){
for(var i=0 ; i<this.height ; i++){
for(var j=0 ; j<this.width ; j++){
var s = document.createElement("div");
s.style.width = this.cellLength+"px";
s.style.height = this.cellLength+"px";
s.style.backgroundColor = "green";
s.style.border = "1px black solid";
s.style.position = "absolute";
s.style.left = j*this.cellLength+"px";
s.style.top = i*this.cellLength+"px";
this.canva.appendChild(s);
}
}
}
}
this.setSize = function(nwidth,nheight){
this.width = nwidth;
this.height = nheight;
this.canva.style.width = this.width*this.cellLength+"px";
this.canva.style.height = this.height*this.cellLength+"px";
}
}
function Food(map){
this.cellLength = map.cellLength;
this.x = Math.floor(Math.random()*map.width);
this.y = Math.floor(Math.random()*map.height);
this.color = "rgb("+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+")";
this.flag;
this.getFood = function(){
var s = document.createElement("div");
this.flag = s;
s.className = "food";
s.style.width = this.cellLength+"px";
s.style.height = this.cellLength+"px";
s.style.backgroundColor = this.color;
s.style.position = "absolute";
s.style.left = this.x*this.cellLength+"px";
s.style.top = this.y*this.cellLength+"px";
map.canva.appendChild(s);
}
}
function Snack(map){
this.body = [{x:2,y:0},{x:1,y:0},{x:0,y:0}];
this.cellLength = map.cellLength;
this.point = "right";
this.getBody = function(){
for(var i=0 ; i<this.body.length ; i++){
var s = document.createElement("div");
this.body[i].flag = s;
s.style.width = this.cellLength+"px";
s.style.height = this.cellLength+"px";
s.style.backgroundColor = "rgb("+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+")";
s.style.position = "absolute";
s.style.left = this.body[i].x*this.cellLength+"px";
s.style.top = this.body[i].y*this.cellLength+"px";
if(i == 0){
s.style.borderRadius = "50%";
}
map.canva.appendChild(s);
}
}
this.getFood = function(food){
if(((this.body[0].x == food.x)&&(this.body[0].y == food.y))==false){
return -1;
}
var newx = this.body[this.body.length-1].x;
var newy = this.body[this.body.length-1].y;
this.body[this.body.length] = {x:newx,y:newy};
var s = document.createElement("div");
this.body[this.body.length-1].flag = s;
s.style.width = this.cellLength+"px";
s.style.height = this.cellLength+"px";
s.style.backgroundColor = "rgb("+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+")";
s.style.position = "absolute";
s.style.left = newx*this.cellLength+"px";
s.style.top = newy*this.cellLength+"px";
map.canva.appendChild(s);
return 1;
}
this.run = function(){
for(var i=this.body.length-1 ; i>0 ; i--){
if(i>0){
this.body[i].x = this.body[i-1].x;
this.body[i].y = this.body[i-1].y;
}
this.body[i].flag.style.left = this.body[i].x*this.cellLength+"px";
this.body[i].flag.style.top = this.body[i].y*this.cellLength+"px";
}
if(this.point=="right"){
this.body[0].x++;
}
if(this.point=="left"){
this.body[0].x--;
}
if(this.point=="top"){
this.body[0].y--;
}
if(this.point=="down"){
this.body[0].y++;
}
this.body[0].flag.style.left = this.body[0].x*this.cellLength+"px";
this.body[0].flag.style.top = this.body[0].y*this.cellLength+"px";
}
this.isDie = function(score){
if(this.body[0].x==map.width||this.body[0].x==-1||this.body[0].y==map.height||this.body[0].y==-1){
if(confirm("差一筹,本次共得到"+score+"分,是否开始新的一局?")){
window.location.reload();
}else{
for(;;){
if(confirm("取消没有用,老老实实点确定吧...")){
break;
}
}
window.location.reload();
}
}
for(var i=4 ; i<this.body.length ; i++){
if((this.body[0].x==this.body[i].x)&&(this.body[0].y==this.body[i].y)){
if(confirm("你咋这么笨,还能撞到自己-_-,本次共得到"+score+"分,是否开始新的一局?")){
window.location.reload();
}else{
for(;;){
if(confirm("取消没有用,老老实实点确定吧...")){
break;
}
}
window.location.reload();
}
}
}
}
}
function getLevel(){
var level = prompt("请输入你要挑战的级别(1-10:易-难),11级稍微难,12地狱级");
if(isNaN(level)){
return 500;
}
if(level==11){
return 50
}
if(level==12){
return 1
}
for(var i=1,j=1000 ; i<=10 ; i++){
if(level == i){
level = j;
break;
}
j-=100;
}
return level;
}
var nwidth = document.getElementById("width");
var nheigt = document.getElementById("height");
var width = 20;
var height = 20;
var map = new Map(width,height);
map.getMap(false);
nwidth.onblur = function(){
map.setSize(nwidth.value,nheigt.value);
}
nheigt.onblur = function(){
map.setSize(nwidth.value,nheigt.value);
}
var food = new Food(map);
food.getFood();
var snack = new Snack(map);
snack.getBody();
var start = document.getElementById("start");
var level = 1;
level = getLevel();
var interval;
var score=0;
start.onclick = function(){
if(interval == null){
interval = setInterval(function(){
snack.run();
snack.onkeydown;
snack.isDie(score);
var result = snack.getFood(food);
if(result==1){
score++;
map.canva.removeChild(food.flag);
food = new Food(map);
food.getFood();
}
},level);
}
}
onkeydown = function(e){
if(e.which==81){
clearInterval(interval);
interval = null;
return
}
if(e.which==69){
if(interval == null){
interval = setInterval(function(){
snack.run();
snack.onkeydown;
snack.isDie(score);
var result = snack.getFood(food);
if(result==1){
score++;
map.canva.removeChild(food.flag);
food = new Food(map);
food.getFood();
}
},level);
}
}
if((e.which==37||e.which==65)&&snack.point!="right"){
snack.point = "left";
return
}
if((e.which == 39||e.which==68)&&snack.point!="left"){
snack.point = "right";
return
}
if((e.which == 38||e.which == 87)&&snack.point!="down"){
snack.point = "top";
return
}
if((e.which == 40||e.which==83)&&snack.point!="top"){
snack.point = "down";
return
}
}
var pause = document.getElementById("pause");
pause.onclick = function(){
clearInterval(interval);
interval = null;
}
</script>
</html>