js>>>>范围之内随机数。

var 随机变量  = randFn(20,30);:产生20-30之间的随机数;;




键盘监听:用上下左右键 控制物体上下左右移动移动

//键盘监听事件
		document.onkeydown = function(e){
			switch(e.keyCode){
				case 37:
				//左
					hero.left = true;
					hero.right = false;
					hero.top = false;
					hero.bottom = false;
					break;
					case 38:
//					上
					hero.left = false;
					hero.right = false;
					hero.top = true;
					hero.bottom = false;
					break;
					case 39:
					//右
					hero.left = false;
					hero.right = true;
					hero.top = false;
					hero.bottom = false;
					break;
					case 40:
					//下
					hero.left = false;
					hero.right = false;
					hero.top = false;
					hero.bottom = true;
					break;
					default:
					break;
			}
		}
		document.onkeyup = function(){
			hero.left = false;
			hero.right = false;
			hero.top = false;
			hero.bottom = false;
		}
用在:

move: function() {
//				onkeydown
				//左
				if(this.left) {
					this.x -= this.speed;
				}
				//右
				if(this.right) {
					this.x += this.speed;
				}
				//				上
				if(this.top) {
					this.y -= this.speed;
				}
				//xia
				if(this.bottom) {
					this.y += this.speed;
				}
			}
		};


创建游戏等需要的图片方法:

//C创建游戏所用的到所有图片对象
		var background1Img = new Image();
		background1Img.src = "img/background.png";
		var background2Img = new Image();
		background2Img.src = "img/background.png";
		var heroImg = new Image();
		heroImg.src = "img/herofly.png";
		var enemy1 = new Image();
		enemy1.src = "img/enemy1.png";
		var enemy2 = new Image();
		enemy2.src = "img/enemy2.png";
		var enemy3 = new Image();
		enemy3.src = "img/enemy3.png";
		var bullet1 = new Image();
		bullet1.src = "img/bullet1.png";
		var bullet2 = new Image();
		bullet2.src = "img/bullet2.png";
		var prop = new Image();
		prop.src = "img/prop.png";



posted @   ·一库  阅读(38)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示