<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body {
text-align: center;
}
canvas {
border: 1px solid;
}
</style>
</head>
<body>
<canvas id="canvas" height="700px" width="400">
</canvas>
<script>
var canvas = document.getElementById("canvas");
var game = canvas.getContext("2d");
const START = 0,
LOADING = 1,
RUNNING = 2,
PASUE = 3,
GAMEOVER = 4;
var score = 0;
var life = 10;
var start_up = START;
var w = 400,
h = 700;
var Img = new Image();
Img.src = "../images/06.png";
var obj = {
height: 771,
width: 400,
imgs: Img,
}
function Bg(ppt) {
this.imgs = ppt.imgs;
this.width = ppt.width;
this.height = ppt.height
this.x1 = 0;
this.x2 = 0;
this.y1 = 0;
this.y2 = -this.height
this.draw = function() {
game.drawImage(ppt.imgs, this.x1, this.y1);
game.drawImage(ppt.imgs, this.x2, this.y2);
}
this.sport = function() {
this.y1++;
this.y2++;
if (this.y1 == this.height) {
this.y1 = -(this.height)
}
if (this.y2 == this.height) {
this.y2 = -this.height;
}
}
}
var _bg = new Bg(obj);
var logo = new Image();
logo.src = "../images/07.png"
var lodings = [];
lodings[0] = new Image();
lodings[0].src = "../images/12.png";
lodings[1] = new Image();
lodings[1].src = "../images/13.png";
lodings[2] = new Image();
lodings[2].src = "../images/14.png";
lodings[3] = new Image();
lodings[3].src = "../images/15.png";
var enter = {
imgs: lodings,
width: 196,
height: 176,
length: lodings.length
}
function ENTER(ppd) {
this.startIndex = 0;
this.paint = function() {
game.drawImage(ppd.imgs[this.startIndex], (w - ppd.width) / 2, (h - ppd.height) / 2);
}
this.time = 0;
this.step = function() {
this.time++
if (this.time % 25 == 0) {
this.startIndex++;
}
if (this.startIndex == ppd.length) {
start_up = RUNNING;
}
}
}
var loding = new ENTER(enter);
var plane = [];
plane[0] = new Image();
plane[0].src = "../images/2.png";
plane[1] = new Image();
plane[1].src = "../images/boom1.png";
plane[2] = new Image();
plane[2].src = "../images/boom2.png";
plane[3] = new Image();
plane[3].src = "../images/boom3.png";
plane[4] = new Image();
plane[4].src = "../images/boom4.png";
var PLANE = {
imgs: plane,
length: plane.length,
width: 84,
height: 104,
frame: 2
}
function PLANES(pdd) {
this.width = pdd.width;
this.length = pdd.length;
this.height = pdd.height;
this.x = (w - pdd.width) / 2
this.y = h - pdd.height;
this.frame = pdd.frame;
this.imgs = pdd.imgs;
this.down = false;
this.candel = false;
this.startIndex = 0;
this.paint = function() {
game.drawImage(this.imgs[this.startIndex], this.x, this.y);
}
this.step = function() {
if (!this.down) {
this.startIndex = 0;
} else {
this.startIndex++;
this.LIFE = 0;
if (this.startIndex == this.length) {
this.LIET++;
if (this.LIFE % 6 == 0) {
life--;
if (life == 0) {
start_up = GAMEOVER;
this.startIndex = this.length - 1;
} else {
PLANES1 = new PLANES(PLANE);
}
}
}
}
}
this.bang = function() {
this.down = true;
}
this.time = 0;
this.shoot = function() {
this.time++;
if (this.time % 7 == 0) {
bullets.push(new BULLETS(BULLET));
}
}
}
var PLANES1 = new PLANES(PLANE);
canvas.onmousemove = function(event) {
if (start_up == RUNNING) {
var x = event.offsetX;
var y = event.offsetY;
PLANES1.x = x - PLANES1.width / 2;
PLANES1.y = y - PLANES1.height / 2;
}
}
var bullet = new Image();
bullet.src = "../images/011.png";
var BULLET = {
imgs: bullet,
width: 127,
height: 127
}
function BULLETS(pdd) {
this.imgs = pdd.imgs;
this.width = pdd.width;
this.height = pdd.height;
this.x = PLANES1.x + PLANES1.width * 0.5 - this.width * 0.5;
this.y = PLANES1.y * 1.1 - this.height * 1.1;
this.paint = function() {
game.drawImage(this.imgs, this.x, this.y);
game.drawImage(this.imgs, this.x + 40, this.y + 20);
game.drawImage(this.imgs, this.x - 40, this.y + 20);
}
this.step = function() {
this.y -= 10;
}
this.candel = false;
this.bang = function() {
this.candel = true;
}
}
var bullets = [];
function bulletpaint() {
for (var i = 0; i < bullets.length; i++) {
bullets[i].paint();
}
}
function bulletsStep() {
for (var i = 0; i < bullets.length; i++) {
bullets[i].step();
}
}
function bulletsDel() {
for (var i = 0; i < bullets.length; i++) {
if (bullets[i].y < -bullets[i].height) {
bullets.splice(i, 1);
}
}
}
var enemy1 = [];
enemy1[0] = new Image();
enemy1[0].src = "../images/enemy1.png";
enemy1[1] = new Image();
enemy1[1].src = "../images/enemy1_down1.png";
enemy1[2] = new Image();
enemy1[2].src = "../images/enemy1_down2.png";
enemy1[3] = new Image();
enemy1[3].src = "../images/enemy1_down3.png";
enemy1[4] = new Image();
enemy1[4].src = "../images/enemy1_down4.png";
var enemy1s = {
imgs: enemy1,
width: 57,
height: 51,
length: enemy1.length,
type: 1,
frame: 1,
life: 50,
score: 1
}
var enemy2 = [];
enemy2[0] = new Image();
enemy2[0].src = "../images/enemy2.png";
enemy2[1] = new Image();
enemy2[1].src = "../images/enemy2_down1.png";
enemy2[2] = new Image();
enemy2[2].src = "../images/enemy2_down2.png";
enemy2[3] = new Image();
enemy2[3].src = "../images/enemy2_down3.png";
enemy2[4] = new Image();
enemy2[4].src = "../images/enemy2_down4.png";
var enemy2s = {
imgs: enemy2,
width: 69,
height: 95,
length: enemy2.length,
type: 2,
frame: 1,
life: 200,
score: 5
}
var enemy3 = [];
enemy3[0] = new Image();
enemy3[0].src = "../images/enemy3_hit.png";
enemy3[1] = new Image();
enemy3[1].src = "../images/enemy3_n1.png";
enemy3[2] = new Image();
enemy3[2].src = "../images/enemy3_n2.png";
enemy3[3] = new Image();
enemy3[3].src = "../images/enemy3_down1.png";
enemy3[4] = new Image();
enemy3[4].src = "../images/enemy3_down2.png";
enemy3[5] = new Image();
enemy3[5].src = "../images/enemy3_down3.png";
enemy3[6] = new Image();
enemy3[6].src = "../images/enemy3_down4.png";
var enemy3s = {
imgs: enemy3,
width: 169,
height: 260,
length: enemy3.length,
type: 3,
frame: 2,
life: 300,
score: 20
}
function Enemy(config) {
this.imgs = config.imgs;
this.length = config.length;
this.width = config.width;
this.height = config.height;
this.type = config.type;
this.frame = config.frame;
this.life = config.life;
this.score = config.score;
this.x = Math.random() * (w - this.width);
this.y = -this.height;
this.startIndex = 0;
this.down = false;
this.candel = false;
this.paint = function() {
game.drawImage(this.imgs[this.startIndex], this.x, this.y);
}
this.step = function() {
if (!this.down) {
this.startIndex++;
this.startIndex = this.startIndex % this.frame;
this.y += 2;
} else {
this.startIndex++;
if (this.startIndex == this.length) {
this.candel = true;
this.startIndex = this.length - 1;
}
}
}
this.checkHit = function(wo) {
return wo.y + wo.height > this.y &&
wo.x + wo.width > this.x &&
wo.y < this.y + this.height &&
wo.x < this.x + this.width;
}
this.bang = function() {
this.LIET = 0;
this.LIET++;
if (this.LIET % 1 == 0) {
console.log(123);
this.life--;
if (this.life == 0) {
this.down = true;
score += this.score;
}
}
}
}
var enemise = [];
function enterEnemise() {
var rand = Math.floor(Math.random() * 100)
if (rand < 10) {
enemise.push(new Enemy(enemy1s));
} else if (rand < 55 && rand > 50) {
enemise.push(new Enemy(enemy2s));
} else if (rand == 88) {
if (enemise[0].type != 3 && enemise.length > 0) {
enemise.splice(0, 0, new Enemy(enemy3s));
}
}
}
function enemyPaint() {
for (var i = 0; i < enemise.length; i++) {
enemise[i].paint();
}
}
function enemyStep() {
for (var i = 0; i < enemise.length; i++) {
enemise[i].step();
}
}
function delenemy() {
for (var i = 0; i < enemise.length; i++) {
if (enemise[i].y > h || enemise[i].candel) {
enemise.splice(i, 1)
}
}
}
function hitEnemise() {
for (var i = 0; i < enemise.length; i++) {
if (enemise[i].checkHit(PLANES1)) {
enemise[i].bang();
PLANES1.bang()
}
for (var j = 0; j < bullets.length; j++) {
if (enemise[i].checkHit(bullets[j])) {
enemise[i].bang();
bullets[j].bang();
}
}
}
}
function scoreText() {
game.font = "bold 24px 微软雅黑"
game.fillText("score:" + score, 10, 30)
game.fillText("life:" + life, 300, 30)
var ziti = game.createLinearGradient(150, 0, 0, 150)
ziti.addColorStop(0, "blue");
ziti.addColorStop(0.5, "pink");
ziti.addColorStop(1, "red");
game.fillStyle = ziti;
}
canvas.onmouseout = function(event) {
if (start_up == RUNNING) {
start_up = PASUE;
}
}
canvas.onmouseover = function(event) {
if (start_up == PASUE) {
start_up = RUNNING;
}
}
var pauseimg = new Image();
pauseimg.src = "../images/16.png";
var suspend = {
imgs: pauseimg,
width: 187,
height: 107
}
function paintGO() {
game.font = "bold 48px 微软雅黑";
game.fillText("GAME OVER", (h / 2 * 0.15), (w / 2 * 1.6));
var ziti = game.createLinearGradient(150, 0, 0, 150)
ziti.addColorStop(0, "blue");
ziti.addColorStop(0.5, "pink");
ziti.addColorStop(1, "red");
game.fillStyle = ziti;
}
function SUSPEND(pdd) {
this.width = pdd.width;
this.height = pdd.height;
this.imgs = pdd.imgs;
this.paint = function() {
game.drawImage(pdd.imgs, (w - pdd.width * 1.5), (h - pdd.height) / 2);
}
}
var SUSPEND = new SUSPEND(suspend);
var imgS = new Image();
imgS.src = "../images/17.png";
imgS.onclick = function() {
console.log(999)
}
canvas.addEventListener("click",function(e=e||window.e){
console.log(e.offsetX,e.offsetY);
if(start_up === START){
if(e.offsetX>137 && e.offsetX <261 && e.offsetY >304 && e.offsetY<334){
start_up = LOADING;
}
}
if(start_up === GAMEOVER){
if(e.offsetX>137 && e.offsetX <205 && e.offsetY >356 && e.offsetY<438){
start_up = LOADING;
score = 0;
life = 10;
}
}
})
setInterval(function() {
_bg.draw();
_bg.sport();
if (start_up == START) {
game.drawImage(logo, 125, 300)
} else if (start_up == LOADING) {
loding.paint();
loding.step();
} else if (start_up == RUNNING) {
PLANES1.paint();
PLANES1.step();
PLANES1.shoot();
bulletpaint();
bulletsStep();
bulletsDel();
enterEnemise()
enemyPaint();
enemyStep();
delenemy();
hitEnemise();
scoreText()
} else if (start_up == PASUE) {
PLANES1.paint();
bulletpaint();
enemyPaint();
PLANES1.shoot();
SUSPEND.paint();
} else if (start_up == GAMEOVER) {
PLANES1.paint();
bulletpaint();
enemyPaint();
paintGO();
scoreText();
game.drawImage(imgS, 125, 350);
loding.startIndex = 0;
}
}, 20)
</script>
</body>
</html>