面向对象之贪吃蛇




	
   
   
	Document
	 rel="stylesheet" type="text/css" href="global.css">
	


   
   
↑:上 ↓:下 ←:左 →:右
得分: 0
<script type="text/javascript"> var longness =25; document.writeln("
"); for (var y = 0; y < longness; y++) { document.writeln("
"); for (var x = 0; x < longness; x++) { document.writeln("
"); } document.writeln(" "); } document.writeln(" "); </script>
<script src="https://code.jquery.com/jquery-1.8.3.js"></script> <script type="text/javascript"> var Snake = function() { this.snakes = [ [0, 0], [1, 0], [2, 0] ]; this.x = 3; this.y = 0; this.longness = 25; this.scores = 0; this.foods = []; this.direction = 'right'; this.start(); } Snake.prototype = { //启动 start: function() { this.init(); this.run(); }, //初始化 init: function() { this.food(); $("#x" + this.foods[0] + "y" + this.foods[1] + "").css("background", "blue"); for (var i = 0; i < this.snakes.length; i++) { $("#x" + this.snakes[i][0] + "y" + this.snakes[i][1] + "").css("background", "red"); } }, //移动 move: function() { this.snakes.push([this.x, this.y]); this.snakes.shift(); this.key(); }, //创造食物坐标 food: function() { var ary1 = []; var ary2 = []; for (var i = 0; i < this.snakes.length; i++) { ary1.push([this.snakes[i][0], this.snakes[i][1]]); } for (var j = 0; j < this.longness; j++) { for (var l = 0; l < this.longness; l++) { ary2.push([l, j]); } } var sum = ary2.concat(ary1); sum.sort(); var s = []; for (var k = 0; k < sum.length - 1; k++) { if (sum[k][0] == sum[k + 1][0] && sum[k][1] == sum[k + 1][1]) { k++; } else { s.push([(sum[k][0]), (sum[k][1])]); } } s.push([(sum[sum.length - 1][0]), (sum[sum.length - 1][1])]); var yui = Math.floor(Math.random() * s.length); this.foods[0] = s[yui][0]; this.foods[1] = s[yui][1]; }, //吃到食物 eatfood: function() { this.snakes.unshift([this.x, this.y]); this.food(); this.scores += 1; }, //吃到自己 eatself: function() { this.death(); }, //碰到墙壁 collisionwall: function() { this.death(); }, //死亡 death: function() { alert("you die!!!"); window.location.reload(); }, //碰撞判定 determine: function() { //碰到食物 if (this.foods[0] == this.snakes[this.snakes.length - 1][0] && this.foods[1] == this.snakes[this.snakes.length - 1][1]) { this.eatfood(); } //碰到墙壁 else if (this.snakes[this.snakes.length - 1][0] > this.longness - 1 || this.snakes[this.snakes.length - 1][0] < 0 || this.snakes[this.snakes.length - 1][1] > this.longness - 1 || this.snakes[this.snakes.length - 1][1] < 0) { this.collisionwall(); } //什么都没碰到 else { this.move(); } //碰到身体 for (var j = 0; j < this.snakes.length - 1; j++) { if (this.snakes[this.snakes.length - 1][0] == this.snakes[j][0] && this.snakes[this.snakes.length - 1][1] == this.snakes[j][1]) { this.eatself(); } } }, //键盘事件 key: function() { var self = this; $(document).keydown(function(event) { var key = event.keyCode; switch (key) { case 38: if (self.direction == 'bottom') { return; } self.direction = 'top'; break; case 40: if (self.direction == 'top') { return; } self.direction = 'bottom'; break; case 37: if (self.direction == 'right') { return; } self.direction = 'left'; break; case 39: if (self.direction == 'left') { return; } self.direction = 'right'; break; } }); if (self.direction == 'top') { this.y -= 1; } else if (self.direction == 'bottom') { this.y += 1; } else if (self.direction == 'left') { this.x -= 1; } else if (self.direction == 'right') { this.x += 1; } }, //得分显示 score: function() { $("#score").html(this.scores); }, //遍历 ergodic: function() { this.determine(); this.score(); $("td").css("background", "none"); $("#x" + this.foods[0] + "y" + this.foods[1] + "").css("background", "blue"); for (var i = 0; i < this.snakes.length; i++) { $("#x" + this.snakes[i][0] + "y" + this.snakes[i][1] + "").css("background", "red"); } }, //跑起来 run: function() { var self = this; setInterval(function() { self.ergodic(); }, 60); } } $(function() { var a = new Snake(); }); </script>

利用面向对象的方法,实现贪吃。 1. 利用面向对象的思想实现——一个食物对象、一个对象、一个游戏总控对象。 2. 在使用××.prototype= {}重写原型对象的时候,一定要加上一句constructor:该对象。不然会造成实例化出来的实例的constructor为object。 3. 在underscore中,使用_.random(a,b)即可获得a-b中的一个随机数。 4. 在求食物的随机位置的时候,用到了panel.clientHeight/this.height - 1) * this.height。 原理是使用盒子的高度/小球的高度,可以算得最多放多少个小球。因为要控制小球不能超过边界,所以总数量要减去1,数量×高度即为随机位置的最大值。 5. 在对象中,用body数组存放身体每一个部分对象的绘制过程就是遍历body,在面板上绘制。 6. 的移动分为两部分。 ① 节移动到前一个节的位置。直到头后一个节移动到头的位置。 ② 根据direction判断头如何移动。 注意:在游戏绘制的过程中,界面的每一次绘制都要**删除**之前的绘制,不然会叠加到一起。 7. 在的闭包中建一个局部数组,存储对象,可以更加方便的删除操作。 8. 只有在原型对象中的方法和属性,外界是可以调用的。 9. 的移动(动画)必然需要定时器协助。定时器的时间,即象征着刷新速度,也就是难度。 10. this所在的函数在哪一个对象中,this就指向谁。单独写一个函数的时候,如果调用之前对象的this,需要备份指针(将对象的this赋值给另一个变量)。 11. JavaScript原生的键盘按下事件(keydown) 中,事件有一个keyCode属性,其值代表按下的键。其中:37—left、38—top、39—right、40—bottom。 12. 边界控制。通过判断头与最大X和Y的关系,判断是否碰到边界。 13. confirm()方法用于显示一个带有指定消息和确认及取消按钮的对话框。 14. window.location.reload(); 重新加载当前文档 15. window.close() 方法用于关闭浏览器窗口。 16. 与食物的碰撞检测:如果头和食物坐标重叠,将尾添加到body中。并重新绘制一个食物点,将之前的食物删掉。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值