JS微信打飞机游戏(二)

这个版本实现飞机发射子弹, 但是子弹飞机界面外没有移走.


/**
 * put script at the bottom of body document.
 * or you should add code as
 * <code>
 *	window.addEventListener("load",function(){
 *	 //todo put your code
 *	});
 *
 *</code>
 */
var mycanvas=document.getElementById("mycanvas");
var ctx=mycanvas.getContext("2d");	

var canWidth=mycanvas.width;
var canHeight=mycanvas.height;

//bullets collection
var bulletList = new Array();


/*
 * We will only have one instance player,so create a player object directly.
 *
 */
var player={
	width:20,
	height:20,
	step:5,//when press key,the player will move.
	//here just use Rectangle as Plane,if you have Plane image...
	// then maybe color properties can be removed.
	color:"red",
	position:{
		x:0,
		y:0
	}
};

/*
 * The player display method, and it will show at (x,y).
 */
player.display=function(x,y){
	this.position.x=x;
	this.position.y=y;
	//save the Context status. then restore it,as "ctx.restore" shows
	ctx.save();
	ctx.fillStyle=this.color;
	//here just use Rectangle as Plane,if you have Plane image...
	ctx.fillRect(x,y,this.width,this.height);
	ctx.restore();
}
//The player move method.
player.move=function(dir){
	//Firstly remove it.Then show the new one.
	this.miss();
	switch(dir){
		case "up":
			this.position.y-=this.step;
		break;
		case "down":
			this.position.y+=this.step;
		break;
		case "left":
			this.position.x-=this.step;
		break;
		case "right":
			this.position.x+=this.step;
		break;
		
		
	}
	this.display(this.position.x,this.position.y);
}
//The player remove
player.miss=function(){
	ctx.clearRect(this.position.x,this.position.y,this.width,this.height);
}

//Bullet Class
function Bullet(ctx){
	this.r=2;
	this.color="yellow";
	this.ctx= ctx;
	//a bullet will have it own position
	this.x=0;
	this.y=0;
	//should be set as Bullet Class variable
	this.step=15;
}
/*
 *Every bullet instance will have it own property.
 *But share the prototype function and property. 
 */

//show the Bullet
Bullet.prototype.display = function(x,y){

	this.x=x;
	this.y=y;
	
	this.ctx.save();
	this.ctx.fillStyle=this.color;
	this.ctx.beginPath();
	this.ctx.arc(x,y,this.r,0,2*Math.PI);
	this.ctx.fill();
	this.ctx.closePath();
	this.ctx.restore();
}

//remove the Bullet
Bullet.prototype.miss=function(){
	
	this.ctx.clearRect(this.x-this.r,this.y-this.r,2*this.r,2*this.r);
}
Bullet.prototype.move=function(){
	//firstly remove it
	this.miss();
	this.display(this.x,this.y-this.step);
}


player.fire=function(){
	var b = new Bullet(ctx);
	//let the new bullet will the center of player
	b.display(this.position.x+this.width/2-b.r/2,this.position.y-b.r);
	bulletList.push(b);
}


	
	//display player
	player.display((canWidth-player.width)/2,canHeight-player.height);

	//add window keypress event listener
	//keypress event will trigger the function every 33 millisecond.
	//I do not know, whether it is 33 millisecond in other browser and Java Swing.
	window.addEventListener("keypress",function(e){
		var keyCode=e.keyCode;
		if(keyCode===119){
			//w
			dir="up";
		}else if(keyCode===100){
			//d
			dir="right";
		}else if(keyCode===115){
			//s
			dir="down";
		}else if(keyCode===97){
			//a
			dir="left";

		}
		//pass the dir to move the player.
		player.move(dir);
	});

	
	 
	//player will always fire, and have enough bullet the same as WeChat
	 window.setInterval(function(){
		 //player fire
		 player.fire();
		 //bullets move
		 for(var i=0;i<bulletList.length;i++){
			bulletList[i].move();
		}
		 
	 },100);
	


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Cocos Creator是一种基于JavaScript游戏开发引擎,可用于开发简单的手机游戏应用程序,包括微信游戏。目前,Cocos Creator平台上有120多套微信游戏源代码可供游戏开发者使用,这些源代码包含了各种类型的游戏,如益智游戏、动作游戏、运动游戏、角色扮演游戏等等。这些源代码可大大加速游戏开发进程,并使游戏开发者能够专注于游戏的设计和创意。此外,通过Cocos Creator平台,游戏开发者可以使用丰富的可视化编辑工具和开发工具,轻松创建游戏精灵、动画和场景等元素,进而快速实现游戏的构建。总的来说,Cocos Creator平台上的120多套微信游戏源代码为游戏开发者提供了可靠的资源和支持,使他们可以更快、更便捷地创建出高质量的微信游戏,从而实现更大的商业成功。 ### 回答2: cocos creator是一款非常受欢迎的游戏开发引擎,其集成了丰富的功能和工具,可以很方便地构建2D和3D游戏微信游戏则是目前非常流行的一种轻量级移动游戏,因为与微信平台无缝集成,所以被越来越多的开发者采用。 在这样的背景下,120多套cocos creator微信游戏源码的出现,可以让开发者更加便捷地开展游戏开发工作。这些源码涵盖了各种类型的游戏,如飞行射击、跑酷、休闲益智、卡牌战斗等等,开发者可以根据自己的需要选择适合自己的源码进行次开发与修改。 使用这些源码,不仅可以节省大量的开发时间和精力,而且还可以获得很好的学习机会,了解其他优秀游戏的设计思路和开发技巧,从而提高自己的开发水平。当然,开发者也可以将这些源码用于商业项目,加速产品的上线和推广。 总之,cocos creator微信游戏120多套源码的出现,为广大的游戏开发者带来了极大的便利和启示,也让我们更加期待未来cocos creator和微信游戏的发展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值