前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HTML5---day3---之简单游戏(1)

HTML5---day3---之简单游戏(1)

作者头像
西门呀在吹雪
发布2020-11-09 09:49:43
3080
发布2020-11-09 09:49:43
举报
文章被收录于专栏:架构之巅
代码语言:javascript
复制
<!DOCTYPE HTML>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<script type="text/javascript" src="./js/jquery-1.4.2.js"></script>
	<script type="text/javascript">
		var backgroundForestImg=new Image();//forest background;
		var mushroomImg=new Image();// mushroom
		var ctx;
		var screenWidth;//画布宽度
		var screenHeight;
		
		function GameObject(){
			this.x=0;
			this.y=0;
			this.image=null;
		}
		function Mushroom(){};
		Mushroom.prototype=new GameObject();
		var mushroom=new Mushroom();
		
		function gameLoop(){
		ctx.clearRect(0,0,screenWidth,screenHeight);
		ctx.save();
		ctx.drawImage(backgroundForestImg,0,0);
		ctx.drawImage(mushroom.image,mushroom.x,mushroom.y);
		ctx.restore();
		}
		function loadImages(){
			mushroomImg.src="images/mushroom.png";
			backgroundForestImg.src="images/forest1.jpg";
		}
		function AddEventHandlers(){
			$("#container").mousemove(function(e){
			mushroom.x=e.pageX-(mushroom.image.width/2);
		});
		}
		$(window).ready(function(){
			AddEventHandlers();
			loadImages();
			ctx=document.getElementById('canvas').getContext('2d');
			
			screenWidth=parseInt($("#canvas").attr("width"));
			screenHeight = parseInt($("#canvas").attr("height"));   
			mushroom.image = mushroomImg;   
			mushroom.x = parseInt(screenWidth/2);// 蘑菇X坐标  
			mushroom.y = screenHeight - 40;//蘑菇Y坐标    
			setInterval(gameLoop, 10); 
		});
	</script>
</head>
<body>
	<div id="container" style="border:1px solid;cursor:none;width: 480px;height:320px;">
	<canvas id="canvas" width="480px" height="320px"></canvas>
	</div>
</body>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2012/03/31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档