用Html+js自己写了一个小游戏

刚接触HTML,感觉挺好玩儿的,自己做了一个小游戏,我要放上来,哈哈!!
<!DOCTYPEHTML>
<html>
	<head>
		<meta charset="utf-8">
		<title>贪吃球</title>
		<style type="text/css"><!------调用css样式列表------->
		</style>
		
	</head>

	<body οnkeydοwn="doKeyDown(event)">
		<div align="center">
			<canvas id="Mycanvas" width="600" height="500" style="border-top:2px solid #f00;border-bottom:2px solid green;border-left:2px solid blue;border-right:2px solid #f90;">你的浏览器不支持该功能</canvas><br>
			<div id="msg"></div>
			<input type="button" value="重新开始" onClick="rest()">
			<input type="button" value="游戏说明" onClick="explain()">
		</div>
		<!-- <script src="animation.js"></script> -->
		<script type="text/JavaScript">
			var canva=document.getElementById("Mycanvas");
			var msg = document.getElementById("msg");
			var context=canva.getContext("2d");
			var img=new Image();
			img.src = "ground.jpg";  
			img.onload = function() {  
				context.drawImage(img, 0, 0);  
			} 
			
			var i=1;
			var j=1;
			var animation={	
				//为该对象添加属性
				//添加x,y方向的偏移量
				"x":100,
				"y":50,
				//圆的半径
				"radius":15,
				//颜色
				"color":"red",
			}
			var Bean={
				"x":300,
				"y":200,
				"color":"blue",
				"radius":5,
			}
			var Rect={
				"x":200,
				"y":100,
				"color":"red",
				"width":10,
				"height":10,				
			}
			function rest(){
				animation.radius=15;
				Draw();
				Drawfood();
			}
			/*****************************************************************
			function:draw()
			use:首先清空画布,再调用animation对象的uodata函数,更新动画属性(在清空画布之前,先对状态进行保存;绘制完成后,及时恢复绘图状态)
			*****************************************************************/
			function Draw(){				
				var width =600;
				var height = 500;		
				//清空画布
				context.clearRect(0,0,width,height);

				context.drawImage(img, 0, 0);
				//填充颜色
				context.fillStyle=animation.color;
				//重新绘制
				context.beginPath();
				context.arc(animation.x,animation.y,animation.radius,0,Math.PI*2,true);
				context.fill();
			};
			window.addEventListener("load",Draw,true);
			function doKeyDown(event){
				switch (event.keyCode)
				{
				case 37://左键头
					go("left");
					break;
				case 38://上键头
					go("up");
					break;
				case 39://右箭头
					go("right");
					break;
				case 40://下箭头
					go("down");
					break;
				}
			}
			
			function go(dir){
				
				switch(dir)
				{
					case "up":
					animation.y-=2;
					Draw();
					break;
					case "down":
					animation.y+=2;
					Draw();
					break;
					case "left":
					animation.x-=2;
					Draw();
					break;
					case "right":
					animation.x+=2;
					Draw();
					break;
				}
				if((animation.x-Bean.x)*(animation.x-Bean.x)+(animation.y-Bean.y)*(animation.y-Bean.y)<=(animation.radius+Bean.radius)*(animation.radius+Bean.radius))
				{
					i++;
					if(i%2==0)
					{
						Beanrandom();
					}
					if(i%2!=0)
					{
						Beanrandom();
					}
					animation.radius += 5;
					if(animation.radius>100)
					{
						animation.radius=100;
					}
					Draw();
					
				}
				if((animation.x-Rect.x)*(animation.x-Rect.x)+(animation.y-Rect.y)*(animation.y-Rect.y)<=(animation.radius+8)*(animation.radius+8))
				{
					j++;
					if(j%2==0)
					{
						Rectrandom();
					}
					if(j%2!=0)
					{
						Rectrandom();
					}
					animation.radius += 5;
					if(animation.radius>100)
					{
						animation.radius=100;
					}
					Draw();			
				}
				Drawfood();
			}
			function Drawfood(){
				DrawBean();
				DrawRect();
			}
			function DrawBean(){
				context.fillStyle=Bean.color;
				context.beginPath();
				context.arc(Bean.x,Bean.y,Bean.radius,0,Math.PI*2,true);
				context.fill();
			}
			window.addEventListener("load",DrawBean,true);
			function Beanrandom(){
				Bean.x=Math.random();
				Bean.x=Math.ceil(Bean.x * 600);
				Bean.y=Math.random();
				Bean.y=Math.ceil(Bean.y * 500);
			}
			<!-- alert(animation.delay); -->
			function DrawRect(){
				context.fillStyle=Rect.color;
				context.beginPath();
				context.rect(Rect.x,Rect.y,Rect.width,Rect.height);
				context.strokeStyle="red";
				context.lineWidth=1;
				context.fillRect(Rect.x,Rect.y,Rect.width,Rect.height);
			}
			window.addEventListener("load",DrawRect,true);
			function Rectrandom(){
				Rect.x=Math.random();
				Rect.x=Math.ceil(Rect.x * 600);
				Rect.y=Math.random();
				Rect.y=Math.ceil(Rect.y * 500);
			}
			var showhelp = false;
			function explain(){
				showhelp = !showhelp;
				if(showhelp)
				{
					msg.innerHTML = "用键盘上的上、下、左、右键移动贪吃球,吃掉蓝色的小球或者红色的方块,可以让贪吃球变大,当然贪吃球不会一直变大的。";
				}else{
					msg.innerHTML = "小游戏:贪吃球";
				};
			}
		</script>
	</body>
</html>

  • 13
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是一个使用HTML、CSS和JS小游戏示例,名为“打地鼠”: HTML代码: ``` <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>打地鼠</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div class="game"> <div class="hole hole1"></div> <div class="hole hole2"></div> <div class="hole hole3"></div> <div class="score">得分:<span id="score">0</span></div> </div> <script type="text/javascript" src="game.js"></script> </body> </html> ``` CSS代码: ``` body { background-color: #F8F8F8; font-family: Arial, sans-serif; } .game { margin: 50px auto; position: relative; width: 860px; height: 440px; background-image: url(images/bg.jpg); background-repeat: no-repeat; background-position: center; background-size: cover; box-shadow: 0 0 20px rgba(0,0,0,0.5); } .hole { position: absolute; width: 200px; height: 200px; background-image: url(images/mole.png); background-repeat: no-repeat; background-position: center bottom; cursor: pointer; opacity: 0; transition: opacity 0.2s; } .hole1 { top: 0; left: 0; } .hole2 { top: 0; left: 330px; } .hole3 { top: 0; left: 660px; } .hole.show { opacity: 1; } .score { position: absolute; top: 20px; right: 20px; font-size: 24px; font-weight: bold; color: #FFF; text-shadow: 0 0 10px rgba(0,0,0,0.5); } ``` JS代码: ``` var score = 0; // 得分 var holes = document.querySelectorAll('.hole'); // 地鼠洞 var scoreBoard = document.querySelector('#score'); // 得分面板 var lastHole; // 上一个出现的地鼠洞 var timeUp = false; // 时间结束标志 function randomTime(min, max) { return Math.round(Math.random() * (max - min) + min); } function randomHole(holes) { var idx = Math.floor(Math.random() * holes.length); var hole = holes[idx]; if (hole === lastHole) { return randomHole(holes); } lastHole = hole; return hole; } function peep() { var time = randomTime(500, 1000); var hole = randomHole(holes); hole.classList.add('show'); setTimeout(function() { hole.classList.remove('show'); if (!timeUp) { peep(); } }, time); } function startGame() { score = 0; scoreBoard.textContent = score; timeUp = false; peep(); setTimeout(function() { timeUp = true; }, 10000); } function bonk(event) { if (!event.isTrusted) { // 防止作弊 return; } score++; this.classList.remove('show'); scoreBoard.textContent = score; } holes.forEach(function(hole) { hole.addEventListener('click', bonk); }); startGame(); ``` 这个小游戏的原理是,在一个背景图像中随机显示三个地鼠洞,每隔一段时间,随机从其中一个洞里冒出一个地鼠,玩家需要尽快点击它,每点击一次得一分。游戏时间为10秒,时间结束后得分最高者胜利。 希望这个示例对你有所帮助!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值