最近比较长一段时间都在学习js,也不知道自己学的怎么样,借这个游戏练练手,第一次写,虽然非常简陋,但还算能玩,小小满足一下继续努力,hoho!
<head>
<meta content-type="text/html;charset=utf-8"/>
<script >
Game = {
BlockWidth : 20,
GameState :"pause",
Map : {
init : function(left,top,width,height,foodNum){
this.food = [];
this.foodNum = foodNum;
this.left = left;
this.top = top;
this.width = width;
this.height = height;
this.instance = document.createElement("div");
this.instance.setAttribute("id","map");
this.instance.style.offsetLeft = left;
this.instance.style.offsetTop = top;
//this.instance.style.offsetWidth = width;
//this.instance.style.offsetHeight = height;
this.instance.style.width = width;
this.instance.style.height = height;
this.instance.style.backgroundColor = "gray";
document.getElementById("adiv").appendChild(this.instance);
},
createOrRefreshFood: function(){
var foodPosLeft,foodPosTop;
for(var i=0;i<this.foodNum;i++){
if(!this.food[i]){
this.food[i] = Game.Food.create(Game.BlockWidth,Game.BlockWidth,"red");
this.instance.appendChild(this.food[i]);
}
foodPosLeft = 0.5*(0.75+Math.random())*this.width + this.left;
foodPosTop = 0.5*(0.75+Math.random())*this.height + this.top;
this.food[i].style.left = foodPosLeft;
this.food[i].style.top = foodPosTop;
}
}
},
Food : {
create : function(width,height,color){
this.width = width;
this.height = height;
this.color = color;
this.instance = document.createElement("div");
this.instance.style.position = "absolute";
this.instance.style.zIndex = "9";
this.instance.style.width = width;
this.instance.style.height = height;
this.instance.style.backgroundColor = color;
return this.instance;
}
},
Snack : {
init: function(foodNum){
this.foodNum = foodNum;
this.foodNumOfEat = 0;
this.instance = [];
this.derect = -1; // -1 = left,1=right,-2=up,2=down
this.instance[0] = document.createElement("div");
this.instance[0].style.position = "absolute";
this.instance[0].style.backgroundColor = "black";
this.instance[0].style.zIndex = "9";
this.instance[0].style.width = Game.BlockWidth;
this.instance[0].style.height = Game.BlockWidth;
this.instance[0].style.left = 200;
this.instance[0].style.top = 200;
this.tail = null;
this.instance[0].style.border = "2px solid white";
Game.Map.instance.appendChild(this.instance[0]);
for(var i=1;i<foodNum;i++){
this.instance[i] = this.instance[0].cloneNode(true);
this.instance[i].style.width = Game.BlockWidth;
this.instance[i].style.height = Game.BlockWidth;
this.instance[i].style.left = (parseInt(this.instance[i-1].style.left)+ Game.BlockWidth);
this.instance[i].style.top = 200;
this.instance[i].style.backgroundColor = "black";
this.instance[i].style.border = "2px solid white";
//this.instance[i].style.borderColor = "red";
Game.Map.instance.appendChild(this.instance[i]);
}
},
move:function(derect){
if(derect + this.derect!=0){
this.derect = derect;
}
var head = {};
head.left = parseInt(this.instance[0].style.left);
head.top = parseInt(this.instance[0].style.top);
switch (this.derect){
case -1:{
this.instance[0].style.left = parseInt(this.instance[0].style.left)-Game.BlockWidth;
break;
}
case 1:{
this.instance[0].style.left = parseInt(this.instance[0].style.left)+Game.BlockWidth;
break;
}
case -2:{
this.instance[0].style.top = parseInt(this.instance[0].style.top)-Game.BlockWidth;
break;
}
case 2:{
this.instance[0].style.top = parseInt(this.instance[0].style.top)+Game.BlockWidth;
break;
}
}
if(this.tail!=null){
this.foodNum++;
}
if(this.foodNum>1){
for(var i=this.foodNum-1;i>1;i--){
this.instance[i].style.left = this.instance[i-1].style.left;
this.instance[i].style.top = this.instance[i-1].style.top;
}
if(this.tail!=null){
Game.Map.instance.appendChild(this.tail);
this.tail = null;
}
this.instance[1].style.left = head.left;
this.instance[1].style.top = head.top;
}
if(this.isFull()){
clearInterval(Game.timing);
alert("you win!");
}else if(this.isDead()){
clearInterval(Game.timing);
window.onkeydown = null;
alert("you fail");
}else{
this.eat();
}
},
eat : function(){
var map = Game.Map;
var head = this.instance[0];
for(var i=0;i<map.food.length;i++){
if(Math.abs(parseInt(this.instance[0].style.left)-parseInt(map.food[i].style.left))<=15
&& Math.abs(parseInt(this.instance[0].style.top)-parseInt(map.food[i].style.top))<=15) {
//alert("eat!");
this.tail = this.instance[this.instance.length] = this.instance[0].cloneNode(true);
var foodEated = map.food[i];
for(var j=i+1;j<map.food.length;j++){
map.food[j-1] = map.food[j];
}
map.food.length--;
foodEated.parentNode.removeChild(foodEated);
this.foodNumOfEat++;
break;
}
}
if(map.food.length<=0){
map.createOrRefreshFood(map.foodNum);
}
},
isFull : function(){
//alert(this.foodNumOfEat);
return this.foodNumOfEat >= 15;
},
isDead : function(){
var map = Game.Map;
if(
parseInt(this.instance[0].style.left) <= map.left
|| parseInt(this.instance[0].style.left) >= (map.left+map.width)
|| parseInt(this.instance[0].style.top) <= map.top
|| parseInt(this.instance[0].style.top) >= (map.top + map.height) ) {
return true;
}
for(var i=1;i<this.foodNum;i++){
if(this.instance[0].style.left == this.instance[i].style.left
&& this.instance[0].style.top == this.instance[i].style.top){
return true;
}
}
}
},
GameStart :function(){
this.Map.init(0,0,1000,600,1);
this.Map.createOrRefreshFood();
this.Snack.init(6);
var gameItsSelf = this;
//if(!window.onkeydown){
// window.onkeydown = document.body.onkeydown;
//}
//alert(document.body.onkeydown);
window.onkeydown = document.body.onkeydown = function(event){
event = window.event||event;
var keyCode = event.keyCode;
//alert(keyCode);
if(keyCode==32){
if(Game.GameState=="pause"){
Game.GameState = "alive";
window.clearInterval(Game.timing);
Game.timing = window.setInterval(bindParamsToFun(Game.Snack,Game.Snack.move,Game.Snack.derect),100);
}else{
Game.GameState = "pause";
window.clearInterval(Game.timing);
}
}else if(Game.GameState =="alive"){
var tempDirect = Game.rederect;
switch (keyCode) {
case 37:{
tempDirect = -1;
window.clearInterval(Game.timing);
Game.timing = window.setInterval(bindParamsToFun(Game.Snack,Game.Snack.move,tempDirect),100)
break;
}
case 38:{
tempDirect = -2;
window.clearInterval(Game.timing);
Game.timing = window.setInterval(bindParamsToFun(Game.Snack,Game.Snack.move,tempDirect),100)
break;
}
case 39:{
tempDirect = 1;
window.clearInterval(Game.timing);
Game.timing = window.setInterval(bindParamsToFun(Game.Snack,Game.Snack.move,tempDirect),100)
break;
}
case 40: {
tempDirect = 2;
window.clearInterval(Game.timing);
Game.timing = window.setInterval(bindParamsToFun(Game.Snack,Game.Snack.move,tempDirect),100)
break;
}
default :
break;
}
}
}
}
}
function bindParamsToFun(obj,fun){
var args = Array.prototype.slice.call(arguments).slice(2);
return function(){
fun.apply(obj,args);
};
}
window.onload = function(){
Game.GameStart();
}
//window.onload = function(){
// alert(document.getElementById("theBody"));
//}
</script>
</head>
<body id="theBody">
<div id="adiv" width="1000" height="1000"></div>
</body>
分享到:
相关推荐
Win32简陋版贪吃蛇 Win32简陋版贪吃蛇Win32简陋版贪吃蛇
使用WPF技术,实现了一个简陋版的贪吃蛇小游戏。
贪吃蛇代码,没写注释,但是程序的函数名可以很容易知道是什么意思
自己用java写的一个贪吃蛇小程序,100来行代码,简单易懂~~~学习java的可以用来做个很好的参考~~~
jQuery贪吃蛇网页版游戏代码,贪吃蛇网页版游戏代码基于jquery.1.11.3.min.js制作,贪吃蛇游戏是一款经典的益智游戏,有PC和手机等多平台版本,既简单又耐玩.Query网页版贪吃蛇游戏,带得分排行榜,交互的贪吃蛇游戏...
根据本网站一些其他高手做的贪吃蛇游戏,自己整理学习,取了高手们做的贪吃蛇游戏的优点,自己做了一下。
Java版贪吃蛇游戏.zipJava版贪吃蛇游戏.zipJava版贪吃蛇游戏.zip Java版贪吃蛇游戏.zipJava版贪吃蛇游戏.zipJava版贪吃蛇游戏.zip Java版贪吃蛇游戏.zipJava版贪吃蛇游戏.zipJava版贪吃蛇游戏.zip Java版贪吃蛇游戏....
很久以前自己用c++在vs2008下实现的一个贪吃蛇游戏, 框架用的是win32, 绘图用的是简单的windows GDI, 用到了双画面缓冲, 很适合学游戏开发的初学者, 代码风格一般般, 如果有什么地方写得不好的话, 欢迎大家...
贪吃蛇贪吃蛇贪吃蛇贪吃蛇贪吃蛇贪吃蛇贪吃蛇
通过网页版贪吃蛇实例可以很好地让你掌握jquery的基本用法,熟悉常用方法。
用python写一个贪吃蛇小游戏,只需要短短十几行代码,就可以写出一个贪吃蛇小游戏
C语言写的贪吃蛇练习,linux平台下写的,不是很完善
用java多线程写的一个简单的贪吃蛇,使用graphics2d类绘图,总体分为窗口类,蛇类,食物类,有注释,很容易看懂
【精品】如何用Python写一个贪吃蛇AI.pdf【精品】如何用Python写一个贪吃蛇AI.pdf【精品】如何用Python写一个贪吃蛇AI.pdf【精品】如何用Python写一个贪吃蛇AI.pdf【精品】如何用Python写一个贪吃蛇AI.pdf【精品】...
双人版对战贪吃蛇,可输入获胜所需积分
vb写的贪吃蛇vb写的贪吃蛇vb写的贪吃蛇vb写的贪吃蛇vb写的贪吃蛇vb写的贪吃蛇vb写的贪吃蛇vb写的贪吃蛇vb写的贪吃蛇vb写的贪吃蛇vb写的贪吃蛇vb写的贪吃蛇vb写的贪吃蛇vb写的贪吃蛇vb写的贪吃蛇vb写的贪吃蛇vb写的...
html写的贪吃蛇JS,通过html编写实现html小游戏的功能
自己写的贪吃蛇,写得不怎么好希望大家不要喷
简易版贪吃蛇代码,重思想
汇编贪吃蛇汇编贪吃蛇汇编贪吃蛇汇编贪吃蛇汇编贪吃蛇汇编贪吃蛇