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 @ 2022-06-23 18:11  ·一库  阅读(43)  评论(0)    收藏  举报