前端简易小demo-弹球游戏

实现简易弹球游戏:
有得分项 有速度设置项
先点击速度项再点击开始游戏(若不选择速度则默认为x1.0)

在做这个东西时,有个logo漂浮的遇到边界反弹的小玩意可以让你参考一下了解计时器的用法,也让你对做这个游戏有点小feel。
https://blog.csdn.net/qq_42664066/article/details/108529828

先上效果图:

在这里插入图片描述
点击结束游戏:
在这里插入图片描述
接不到结束:
在这里插入图片描述

涉及主要知识点:计时器的设置、键盘监听
css代码如下:

		#score{
			width: 40%;
			height: 40px;
			margin: 5px auto;
			font-size: 40px;
			position: absolute;
			left: 470px;
		}
		#num{
			width: 15%;
			height: 40px;
			text-align: center;
			line-height: 40px;
			float: left;
			position: absolute;
			left: 150px;
			font-size: 40px;
			top: 5px;
			border: none;
		}
		#container{
			position: absolute;
			width: 1000px;
			height: 480px;
			border:1px solid red;
			top: 80px;
			left: 110px;
		}
		#ball{
			border-radius: 50%;
			width: 100px;
			height: 100px;
			position: absolute;
			left: 0px;
			top: 0px;
			background-color: skyblue;
		}
		#show{
			width: 60%;
			height: 100px;
			position: absolute;
			top: 30%;
			left: 20%;
			font-size: 60px;
			text-align: center;
			line-height: 100px;
		}
		#ban{
			/*border: 1px solid brown;*/
			width: 180px;
			height: 20px;
			background-color: brown;
			position: absolute;
			left: 0px;
			bottom: 0px;
		}
		button,#start,#over{
			width: 120px;
			height: 30px;
			margin: 0px 10px 0 10px;
			position: relative;
			top: 570px;
			left: 220px;
		}

html代码如下:

	<div id="score">Score:<input id="num" value="0" type="text" /></div>
	<div id="container">
		<div id="ball"></div>
		<div id="show">Game Over!</div>
		<div id="ban"></div>
	</div>
	<input type="button" id="start" name="" value="开始游戏" onclick="start();">
	<button value="10">x1.0</button>
	<button value="20">x2.0</button>
	<button value="30">x3.0</button>
	<input type="button" id="over" name="" value="结束游戏" onclick="over();">

jquery代码如下:(jquery3.0)
虽说我用的jquery但对初学javascript的伙伴来说主要理解内部逻辑即可,然后再敲一遍也是一样的。代码这种东西主要是理解想法和借鉴思路。

//相关参数设置
	$("#show").hide();//初始设置gameover隐藏
	var score=0;//设置初始化分数
	var t=0;
	var speed=10;//number

	var ballwidth = $("#ball").width();//获取球的宽度
	var ballheight = $("#ball").height();//获取球的高度
	var ballgox=true;//设置球的行动:true表示沿x正方向走即右走,flase反之
	var ballgoy=true;//y轴同上

	var banx;//板在x轴的起始点
	var bany = $("#ban").offset().top;//板在y轴的起始点
	var banwidth = $("#ban").width();//板的宽度
	var banheight = $("#ban").height();//板的高度
	var bangox=true;//设置板的行动

	var winwidth = $("#container").width();//游戏界面的宽度
	var winheight = $("#container").height();//游戏界面的高度
	var winx = $("#container").offset().left;//游戏界面的左上角x位置
	var winy = $("#container").offset().top;//游戏界面左上角y位置

	//设置得分
	function set_score(score){
		$("#num").val(score);
	}
	//获取游戏速度
	function getspeed(){
		$("button").click(function(){
			speed = $(this).val();//string类型
			speed = parseInt(speed);//转换为numberl类型
		})
		return speed;	
	}
	speed = getspeed();

	//游戏结束函数
	function over(){
		$("#show").show();
		clearTimeout(t);//使得球和板暂停移动
	}
	//游戏开始
	function start(){
		score=0;
		set_score(score);
		//每次开始游戏板和球的初始化位置
		$("#ball").css('left',0);
		$("#ban").css('left',0);
		$("#ball").css('top',0);
		ballx = $("#ball").offset().left;
		bally = $("#ball").offset().top;
		banx = $("#ban").offset().left;

		//球的移动
		$("#show").hide();//设置gameover隐藏
		t=setInterval(function(){
			if(ballgox==true){
				$("#ball").css('left',ballx=ballx+speed);//球向右移动
			}else{
				$("#ball").css('left',ballx=ballx-speed);
			}
			if(ballx>=(winwidth-ballwidth)){//当球撞击游戏右边界
				ballgox=false;
			}if(ballx<=0){//球撞击左边界
				ballgox=true;
			}

			if(ballgoy==true){
				$("#ball").css('top',bally=bally+speed);//球向下移动
			}else{
				$("#ball").css('top',bally=bally-speed);
			}
			
			if((bally+ballheight)>=(winheight-banheight)){//当球到达板的位置
				//没接到
				if(ballx<banx||ballx>(banx+banwidth)){
					over();
				}
				//接到
				else{
					ballgoy=false;//向上弹
					score=score+speed;//计分
					set_score(score);
				}
			}
			if(bally<=0){     //球弹到游戏上边界
				ballgoy=true;
			}
			//
			
		},100)
		controleban();
	}
	//控制板:键盘监听
	function controleban(){
		$(document).keydown(function(event){
			if(event.keyCode == 39){//键盘右键
				if(banx>=(winwidth-banwidth-1)){
		 			banx=winwidth-banwidth-5;
		 			$("#ban").css('left',banx);
		 			//当板到达右边界时,设置位置默认在右边界处避免出边界
		 		}
				$("#ban").css('left',banx=banx+40);
		 	}
		 	if(event.keyCode == 37){//键盘左键
				if(banx<=0){
					banx=0;
					$("#ban").css('left',banx);
					//当板到达左边界时,设置位置默认在左边界处避免出边界		
				}
		 		$("#ban").css('left',banx=banx-40);
		 	}
		});
	}

有建议和指正,欢迎指出~表示感谢!!
此至 互勉~

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
http://blog.csdn.net/xiaoxiao108/article/details/8913616 html5 挺火,写个html5游戏玩玩吧,想起cocos2d 貌似各个平台都有,网上找了找,下载了个Cocos2d-html5引擎包。 貌似另一个开源引擎lufylegend.js也很好,下次用用lufylegend.js试试。 开发环境 chrome Cocos2d-html5 游戏地址:http://hehe108.sinaapp.com/cocos2d/ (adsw回车) 实现方法如下 1.创建好 LayerGradient的子类 (里面放坦克子弹) 2.重写 onEnter 方法添加一些基本按钮 跟一些初始坦克,子弹 3.通过schedule方法 控制 坦克 子弹的重画 4.根据键盘按键(ASWD)确定出坦克的方向,根据坦克的方向修改坦克的X,Y轴坐标,来实现坦克的移动 5.通过cc.rectIntersectsRect函数来进行碰撞检测,实现子弹打击坦克 具体代码 1.在项目里面添加方向 var Direction = { L:0, U:1, D:2, R:3, STOP:4 }; 2.添加子弹类的相关属性 SPEED:10, WIDTH:15, HEIGHT:15, x:null, y:null, dir:null, live:true, tankClient:null, //LayerGradient子类TankClient 的引用 good:null, 3.子弹初始化,重画 ctor:function (x,y,good,dir,tankClient) { cc.associateWithNative( this, cc.Sprite ); this.x=x; this.y=y; this.dir=dir; this.tankClient=tankClient; this.good=good; this.initWithFile(s_missile); this.setPosition( cc.p(this.x, this.y) ); this.tankClient.addChild(this); }, Draw:function(){ if(!this.live){ this.tankClient.removeChild(this, true); return; } this.setPosition( cc.p(this.x, this.y) ); this.Move(); }, 4.添加子弹打击坦克的方法 HitTank:function(t){ if (cc.rectIntersectsRect(this.GetRectangle(), t.GetRectangle()) && t.live && this.live && this.good != t.good){ t.live = false; this.live = false; return true; } return false; }, 5.添加坦克类相关属性 SPEED:5, WIDTH:58, HEIGHT:58, x:0, y:0, l:false, u:false, r:false, d:false, dir:Direction["STOP"], ptDir:Direction["D"], tankClient:null, good:null, step:0, live:true, 6.在tank类中 坦克初始化,重画 ctor:function (x,y,good,tankClient) { cc.associateWithNative( this, cc.Sprite ); this.x=x; this.y=y; this.tankClient=tankClient; this.good=good; if(good){ this.initWithFile(s_tank); }else{ this.initWithFile(s_enemy); } this.setPosition( cc.p(this.x, this.y) ); this.tankClient.addChild(this); }, Draw:function(){ if (!this.live){ if (!this.good){ this.tankClient.removeChild(this, true); } this.tankClient.removeChild(this, true); return; } this.setPosition( cc.p(this.x, this.y) ); switch (this.ptDir) { case Direction["D"]: this.setRotation(0); //旋转精灵控制 炮筒方向 break; case Direction["U"]: this.setRotation(180); break; case Direction["L"]: this.setRotation(270); break; case Direction["R"]: this.setRotation(90); break; } this.Move(); }, 7.tank发子弹的方法 Fire:function() { if(!this.live) return null; for(var i=0;i<this.tankClient.missiles.length;i++){ var m = this.tankClient.missiles[i]; if (m.live == false){ m.x=this.x; m.y=this.y; m.live=true; m.dir=this.ptDir; m.good=this.good; this.tankClient.addChild(m); return m; } } var missile=new Missile(this.x,this.y,this.good,this.ptDir,this.tankClient); this.tankClient.missiles.push(missile); return missile; }, 8.LayerGradient加入坦克 this.tanks = []; this.myTank = new Tank(60,20, true, this); for (var i = 0; i < 10; i++){ this.tanks.push(new Tank(50 + 70 * (i + 1), 420, false, this)); } 9.LayerGradient中调用子弹打击坦克的方法 for(var i=0;i<this.missiles.length;i++){ var m = this.missiles[i]; m.HitTank(this.myTank); m.HitTanks(this.tanks); m.Draw(); } 10.控制坦克移动射击的部分代码 onKeyUp:function(key) { this.myTank.KeyReleased(key); }, onKeyDown:function(key) { this.myTank.KeyPressed(key); } 11.用Ant和compiler合并压缩js后发布到sae 如果你发现有什么不合理的,需要改进的地方,请留言。或者可以通过 328452421@qq.com 联系我,非常感谢。 http://blog.csdn.net/xiaoxiao108/article/details/8913616

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值