This game uses HTML5 canvas, and the browser needs to support HTML5 to run the game.
This article explains in detail how to use html5 to develop a shooting game. Thunderbolt can be said to be a classic among shooting games. Below Let’s imitate it.
Let’s take a look at the screenshots of the game first

##Game development requires the use of Open source engine: lufylegend.js
The game is expected to use the following Several files
index.html
js folder|---Main .js
and
images folder|--picturesI will briefly talk about the production process, the source code is at the bottom
First create the index.html file,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <!DOCTYPE html>
<html>
<head>
<meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" >
<title>弹幕</title>
<!--
<meta name= "viewport" content= "width=480,initial-scale=0.5, minimum-scale=0.5, maximum-scale=1.0,user-scalable=no" />
-->
<meta name= "viewport" content= "width=480,initial-scale=0.6" />
<script type= "text/javascript" src= "../legend/legend.js" ></script>
<script type= "text/javascript" src= "./js/Global.js" ></script>
<script type= "text/javascript" src= "./js/Bullet.js" ></script>
<script type= "text/javascript" src= "./js/Plain.js" ></script>
<script type= "text/javascript" src= "./js/Main.js" ></script>
</head>
<body>
<p id= "mylegend" >loading……</p>
</body>
</html>
|
Copy after login
Open Main.js
Add code inside, first read all the images, and display the progress bar
And add some variables that may be used Go in
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
init(50, "mylegend" ,480,800,main);
var loadingLayer;
var backLayer;
var ctrlLayer;
var loadIndex = 0;
var frames = 0;
var boosstart = false;
var gameover = false;
var gameclear = false;
var point = 0;
var player;
var pointText;
var imgData = new Array();
var imglist = {};
var barrage = new Array();
var barrageSpeed = [5,10];
var enemys = new Array();
function main(){
imgData.push({name: "back" ,path: "./images/back.jpg" });
imgData.push({name: "enemy" ,path: "./images/e.png" });
imgData.push({name: "player" ,path: "./images/player.png" });
imgData.push({name: "boss" ,path: "./images/boss.png" });
imgData.push({name: "ctrl" ,path: "./images/ctrl.png" });
imgData.push({name: "item1" ,path: "./images/1.png" });
loadingLayer = new LSprite();
loadingLayer.graphics.drawRect(1, "black" ,[50, 200, 200, 20],true, "#ffffff" );
addChild(loadingLayer);
loadImage();
}
function loadImage(){
if (loadIndex >= imgData.length){
removeChild(loadingLayer);
legendLoadOver();
gameInit();
return ;
}
loader = new LLoader();
loader.addEventListener(LEvent.COMPLETE,loadComplete);
loader.load(imgData[loadIndex].path, "bitmapData" );
}
function loadComplete(event){
loadingLayer.graphics.clear();
loadingLayer.graphics.drawRect(1, "black" ,[50, 200, 200, 20],true, "#ffffff" );
loadingLayer.graphics.drawRect(1, "black" ,[50, 203, 200*(loadIndex/imgData.length), 14],true, "#000000" );
imglist[imgData[loadIndex].name] = loader.content;
loadIndex++;
loadImage();
}
|
Copy after login
Now, all the pictures used have been loaded. First add a background and display a picture
It is very simple to use the legend library to display pictures
1 2 3 4 5 6 7 8 | function gameInit(event){
backLayer = new LSprite();
addChild(backLayer);
bitmapdata = new LBitmapData(imglist[ "back" ]);
bitmap = new LBitmap(bitmapdata);
backLayer.addChild(bitmap);}
|
Copy after login
The effect is as follows
##Shooting game, bullets are the highlight, how to add a variety of bullets is The key to the game
To make the bullet change, you must set the corresponding angle, acceleration, and other variables
In order to realize these changes, let's create a bullet class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
function Bullet(belong,x,y,angle,xspeed,yspeed,aspeed,speed){
base(this,LSprite,[]);
var self = this;
self.belong = belong;
self.x = x;
self.y = y;
self.angle = angle;
self.speed = speed;
self.xspeed = xspeed;
self.yspeed = yspeed;
self.aspeed = aspeed;
var bitmapdata,bitmap;
bitmapdata = new LBitmapData(imglist[ "item1" ]);
bitmap = new LBitmap(bitmapdata);
self.bitmap = bitmap;
self.addChild(bitmap);
}
|
Copy after login
Then, during the movement of the bullet, various transformations are implemented based on these variables
In the common class, add a bullet array to distinguish various Bullet
1 2 3 4 5 6 7 |
Global.bulletList = new Array(
{startAngle:0,angle:20,speed:5,aspeed:0, count :1,shootspeed:10,sspeed:0},
);
|
Copy after login
The most basic bullet in the game is, of course, one bullet each time.
Build a function that fires bullets in the common class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Global.setBullet = function (plainObject){
var i,j,obj,xspeed,yspeed,kaku;
var bullet = Global.bulletList[0];
for (i=0;i<bullet. count ;i++){
kaku = i*bullet.angle + bullet.startAngle;
xspeed = bullet.speed*Math.sin(kaku * Math.PI / 180);
yspeed = barrageSpeed[0]*Math. cos (kaku * Math.PI / 180);
obj = new Bullet(0,210,300,kaku,xspeed,yspeed,bullet.aspeed,bullet.speed);
backLayer.addChild(obj);
barrage.push(obj);
}
};
|
Copy after login
Here, ultimately it needs to be fired according to The aircraft are different, so I added the parameter aircraft
Now create the aircraft class, as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
function Plain(name,belong,x,y,bullets){
base(this,LSprite,[]);
var self = this;
self.name = name;
self.x = x;
self.y = y;
self.belong = belong;
self.bullets = bullets;
self.bullet = self.bullets[Math. floor (Math.random()*self.bullets.length)];
self.shootspeed = Global.bulletList[self.bullet].shootspeed;
self.sspeed = 0;
self.shootctrl = 0;
self.list = Global.getPlainStatus(self);
self.bitmap = self.list[0];
self.addChild(self.bitmap);
self.shootx = self.list[1];
self.shooty = self.list[2];
self.speed = self.list[3];
self.hp = self.list[4];
self.move = [0,0];
self.shootcount = 0;
self.canshoot = true;
if (name== "player" )self.canshoot = false;
}
Plain.prototype.onframe = function (){
var self = this;
self.x += self.move[0]*self.speed;
self.y += self.move[1]*self.speed;
switch (self.name){
case "player" :
if (self.x < 0)self.x = 0;
else if (self.x + self.bitmap.getWidth() > LGlobal.width)self.x = LGlobal.width-self.bitmap.getWidth();
if (self.y < 0)self.y = 0;
else if (self.y + self.bitmap.getHeight() > LGlobal.height)self.y = LGlobal.height-self.bitmap.getHeight();
break ;
case "boss" :
if (self.y < 0){
self.y = 0;
self.move[1] = 1;
} else if (self.y + self.bitmap.getHeight() > LGlobal.height){
self.y = LGlobal.height-self.bitmap.getHeight();
self.move[1] = -1;
}
self.hitTest();
break ;
case "enemy" :
default :
self.hitTest();
}
if (self.canshoot)self.shoot();
};
Plain.prototype.hitTest = function (){
var self = this;
var disx,disy,sw,ew;
sw = (self.bitmap.getWidth() + self.bitmap.getHeight())/4;
ew = (player.bitmap.getWidth() + player.bitmap.getHeight())/4;
disx = self.x+sw - (player.x + ew);
disy = self.y+self.bitmap.getHeight()/2 - (player.y + player.bitmap.getHeight()/2);
if (disx*disx + disy*disy < (sw+ew)*(sw+ew)){
player.visible = false;
gameover = true;
}
};
Plain.prototype.shoot = function (){
var self = this;
if (self.shootctrl++ < self.shootspeed) return ;
self.shootctrl = 0;
if (self.name == "boss" ){
if (self.shootcount++ % 20 > 5) return ;
} else {
if (self.shootcount++ % 10 > 5) return ;
}
Global.setBullet(self);
if (self.name == "boss" ){
if (self.shootcount % 20 < 5) return ;
} else {
if (self.shootcount % 10 < 5) return ;
}
if (self.bullets.length <= 1) return ;
self.bullet = self.bullets[Math. floor (Math.random()*self.bullets.length)];
self.shootspeed = Global.bulletList[self.bullet].shootspeed;
};
|
Copy after login
The code has been added with detailed comments, it is not difficult to understand
Improve the bullet class as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
function Bullet(belong,x,y,angle,xspeed,yspeed,aspeed,speed){
base(this,LSprite,[]);
var self = this;
self.belong = belong;
self.x = x;
self.y = y;
self.angle = angle;
self.speed = speed;
self.xspeed = xspeed;
self.yspeed = yspeed;
self.aspeed = aspeed;
var bitmapdata,bitmap;
bitmapdata = new LBitmapData(imglist[ "item1" ]);
bitmap = new LBitmap(bitmapdata);
self.bitmap = bitmap;
self.addChild(bitmap);
}
Bullet.prototype.onframe = function (index){
var self = this;
self.x += self.xspeed;
self.y += self.yspeed;
if (self.aspeed != 0){
self.angle += self.aspeed;
self.xspeed = self.speed*Math.sin(self.angle * Math.PI / 180);
self.yspeed = self.speed*Math. cos (self.angle * Math.PI / 180);
}
if (self.x < 0 || self.x > LGlobal.width || self.y < 0 || self.y > LGlobal.height){
backLayer.removeChild(self);
barrage.splice(index,1);
} else {
self.hitTest(index);
}
};
Bullet.prototype.hitTest = function (index){
var self = this;
var disx,disy,sw,ew,obj,i;
if (self.belong == player.belong){
for (i=0;i<enemys.length;i++){ obj= "enemys[i];" sw= "self.bitmap.getWidth()/2;" ew= "obj.bitmap.getWidth()/2;" disx= "self.x+sw" -= "" (obj.x= "" += "" ew);= "" disy= "self.y+self.bitmap.getHeight()/2" (obj.y= "" obj.bitmap.getheight()= "" 2);= "" 距离检测= "" if (disx*disx= "" disy*disy= "" <= "" ew*ew){= "" obj.hp--;= "" if (obj.hp= "=" 0){= "" point= "" pointtext.text= "point;" 从屏幕移除= "" backlayer.removechild(obj);= "" 从敌机数组移除= "" enemys.splice(i,1);= "" if (obj.name= "=" "boss" ){= "" gameclear= "true;" }= "" backlayer.removechild(self);= "" 从子弹数组移除= "" barrage.splice(index,1);= "" } else {= "" 敌机子弹= "" ew*ew= "" 10){= "" obj.visible= "false;" gameover= "true;" };<= "" pre= "" ><div class = "contentsignin" > Copy after login</div></enemys.length;i++){>
|
The bullet launch function is modified as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Global.setBullet = function (plainObject){
var i,j,obj,xspeed,yspeed,kaku;
var bullet = Global.bulletList[plainObject.bullet];
plainObject.sspeed += bullet.sspeed;
for (i=0;i<bullet. count ;i++){
kaku = i*bullet.angle + bullet.startAngle + plainObject.sspeed;
xspeed = bullet.speed*Math.sin(kaku * Math.PI / 180);
yspeed = barrageSpeed[0]*Math. cos (kaku * Math.PI / 180);
obj = new Bullet(plainObject.belong,plainObject.x+plainObject.shootx,plainObject.y+plainObject.
shooty,kaku,xspeed,yspeed,bullet.aspeed,bullet.speed);
backLayer.addChild(obj);
barrage.push(obj);
}
};
|
Copy after login
Add a loop in the Main file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function onframe(){
var i;
for (i=0;i<barrage.length;i++){
barrage[i].onframe(i);
}
for (i=0;i<enemys.length;i++){
enemys[i].onframe();
}
}
|
Copy after login
Now, I only need to add the aircraft, and that’s it Fire the bullet
1 | plain = new Plain( "enemy" ,1,200,300,[0]);
|
Copy after login
See the effect
Modify, the corresponding parameters of the bullet are as follows

1 2 3 4 5 6 7 8 9 10 11 12 13 |
Global.bulletList = new Array(
{startAngle:0,angle:20,speed:5,aspeed:0, count :1,shootspeed:10,sspeed:0},
{startAngle:-20,angle:20,speed:5,aspeed:0, count :3,shootspeed:10,sspeed:0},
{startAngle:0,angle:20,speed:5,aspeed:0, count :1,shootspeed:1,sspeed:20},
{startAngle:0,angle:20,speed:5,aspeed:0, count :18,shootspeed:3,sspeed:0},
{startAngle:0,angle:20,speed:5,aspeed:1, count :18,shootspeed:3,sspeed:0},
{startAngle:180,angle:20,speed:5,aspeed:0, count :1,shootspeed:5,sspeed:0},
{startAngle:160,angle:20,speed:5,aspeed:0, count :3,shootspeed:5,sspeed:0}
);
|
Copy after login
The effects are



#lufylegend.js engine package contains this demo, please download the lufylegend.js engine directly and view the source code in the engine package
The above is the content of HTML5 game development-barrage + lightning-like game demo. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!