使用面向对象思想实现贪吃蛇食物的功能

<style type="text/css">
		*{
			margin:0;
			padding:0;
		}
		#map{
			width:800px;
			height:600px;
			position:relative;
			border:1px solid #ccc;
		}
	</style>
<body>
	<div id="map"></div>
</body>
<script type="text/javascript">
		window.οnlοad=function(){
			var oMap=document.getElementById("map");
			function Random(){}
			//在原型中添加方法:获取随机数用来表示食物的坐标
			Random.prototype.getrandom=function(min,max){
				return Math.floor(Math.random()*(max-min)+min);
			};
			window.random=new Random();
			function food(width,height,color){//食物进行初始化			
				this.width=width;
				this.height=height;
				this.color=color;
			}
			food.prototype.show=function(map){//食物在画布中如何显示
				this.element=document.createElement("div");
				var oDiv=this.element;
				oMap.appendChild(oDiv);
				oDiv.style.width=this.width+"px";
				oDiv.style.height=this.height+"px";
				oDiv.style.backgroundColor=this.color;
				oDiv.style.position="absolute";
				fd.where(oMap);
			};
			food.prototype.where=function(map){
				var x=random.getrandom(0,(map.offsetWidth/this.width))*this.width;
				var y=random.getrandom(0,(map.offsetHeight/this.height))*this.height;
				var oDiv=this.element;
				oDiv.style.left=x+"px";
				oDiv.style.top=y+"px";
			};
			var fd=new food(20,20,"red");
			fd.show(oMap);
		};
	</script>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值