// Set the initial position of the pinball, x and y coordinates setPosition: function(x, y) {
this.dom.style.left = x "px"; this.dom.style.top = y "px" ;
},
// Pinball animation means movement. The incoming parameters are the width and height of the game background. animation: function(gameWidth, gameHeight, stick) {
var _this = this;
// Actual lateral movement speed, left or right var _movepx = this.dom.offsetLeft > gameWidth/2 ? -1*this.movepx : this.movepx; var _movepy = this.dom.offsetTop > gameHeight/2 ? this.movepy : -1*this.movepy;
// Process move function var process = function () {
// x, y coordinates of the pinball var left = _this.dom.offsetLeft; var top = _this.dom.offsetTop;
// Whether to reverse direction if (left <= 0 || left >= gameWidth - _this.dom.clientWidth) {
_movepx *= -1;
}
var isCrashStick = _this.OnCheckCrashStick(); var isCrashBall = _this.OnCheckCrashBrick();
// Determine whether you want to change the direction up if (top < 0 || isCrashStick || isCrashBall) {
_movepy *= -1;
}
// Move down top = top _movepy; left = left _movepx;
//Set pinball position _this.dom.style.top = top "px"; _this.dom.style.left = left "px";
if(top > gameHeight) {
_this.onend(); alert("You Lose");
} else {
setTimeout( process, _this.movesp);
}
// Determine the moving direction of the pinball if (_movepx > 0 && _movepy < 0) {
_this .direction = "RightUp";
return;
}
if (_movepx > 0 && _movepy > 0) {
_this.direction = "RightDown";
return;
}
if (_movepx < 0 && _movepy < 0) {
_this.direction = " LeftUp";
return;
}
if (_movepx < 0 && _movepy > 0) {
_this.direction = "LeftDown" ;
return;
}
};
// Start moving process();
},
// External interface, detect whether it hits the magic wand OnCheckCrashStick: function() {},
// External interface, detect whether it hits the brick OnCheckCrashBrick: function () {},
// Pinball end event onend : function() {},
// Game over gameover : function() {}
}
The brick code is as follows brick.js source file:
//Set the position setPosition: function(gamePanel, width, height) {
// Add the magic wand to the game background this.gamePanel = gamePanel; this .gamePanel.appendChild(this.dom);
// Set the initial position of the aircraft this.dom.style.left = (width - this.dom.clientWidth)/2 "px"; this.dom.style.top = height - this.dom.clientHeight "px";
// Get the width and height of the game background this.gameWidth = width; this .gameHeight = height;
},
// Keyboard press event keydown: function(e) {
var keyCode = e.keyCode;
if (!this.isMove) {
this.move(keyCode);
}
},
// Keyboard release Event keyup: function(e) {
// Determine whether the keyboard is released if (this.keyCodeAndDirection[e.keyCode]) {
// Stop moving this.stopMove();
} else if (e.keyCode == 32) {
// Set to non-shooting this.isSend = false;
}
},
// Move move: function(keyCode) {
// Set to moving this. isMove = true; var _this = this;
// Determine the movement direction switch(this.keyCodeAndDirection[keyCode]) {
var left = this.dom[ "offsetLeft"]; left = left - this.movepx >= 0 ? left - this.movepx : 0; this.dom.style["left"] = left "px";
if (left == 0) {
this.stopMove();
}
},
// Move right moveRight : function() {
var left = this.dom["offsetLeft"]; var maxDistance = this.gameWidth - this.dom.clientWidth; left = left this. movepx <= maxDistance ? left this.movepx: maxDistance; this.dom.style["left"] = left "px";
var left = this.dom["offsetLeft"]; left = left - this.movepx >= 0 ? left - this.movepx : 0; this.dom.style["left"] = left "px";
if (left == 0) {
this.stopMove();
}
},
// Move right moveRight : function( ) {
var left = this.dom["offsetLeft"]; var maxDistance = this.gameWidth - this.dom.clientWidth; left = left this.movepx <= maxDistance ? left this.movepx: maxDistance; this.dom.style["left"] = left "px";
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn