使用js制作简单的农场游戏

div{
	font-size:12px;
	border:#999 1px solid;
	padding:5px;
}

#bg{		
	width:456px;
	height:266px;
	background-image:url(images/plowland.jpg);
}
img{		
	position:absolute;
	top:2000px;
	left:2000px;
}
#seed{		
	background-image:url(images/btn_seed.png);
	width:56px;
	height:56px;
	position:absolute;
	top:229px;
	left:49px;
	cursor:hand;
}
#grow{		
	background-image:url(images/btn_grow.png);
	width:56px;
	height:56px;
	position:absolute;
	top:229px;
	left:154px;
	cursor:hand;
}
#bloom{		
	background-image:url(images/btn_bloom.png);
	width:56px;
	height:56px;
	position:absolute;
	top:229px;
	left:259px;
	cursor:hand;
}
#fruit{		
	background-image:url(images/btn_fruit.png);
	width:56px;
	height:56px;
	position:absolute;
	top:229px;
	left:368px;
	cursor:hand;
	.border{
		border:opx ;
	}
}

首先是CSS样式,四个按钮,播种,生长,开花结果。

<div id="bg">
    <span id="seed"></span>
    <span id="grow"></span>
    <span id="bloom"></span>
    <span id="fruit"></span>
</div>

然后是HTML代码

$(document).ready(function(){
		$("#bg").bind("click",function(e){			
			$("<img src='images/"+img+"'/>").prependTo("#bg").css("top",e.pageY-60)	.css("left",e.pageX-40);				
		
		});	
		$("#grow").bind("click",function(){			
			$(this).addClass("border");
			img="grow.png";
			//$("#bg").append("<img src='images/grow.png' />");
		});	
		$("#bloom").bind("click",function(){	
			
			$(this).addClass("border");
			img="bloom.png";
		//	$("img").replaceWith("<img src='images/bloom.png' />");
		});
		$("#fruit").bind("click",function(){
			
			img="fruit.png"
		//	$("<img src='images/fruit.png' />").replaceAll("img");
		});								
	});

然后是js代码绑定四个事件

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值