js实现跑酷小游戏
代码语言:html
所属分类:游戏
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Kandi Runner</title> <style> body { font-family: arial,sans-serif; font-size: 16px } .wrapper { width: 600px; height: 360px; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; position: absolute; left: 20px; margin-left: auto; margin-right: auto } canvas { position: absolute; top: 0; left: 0; border: 1px solid #000; z-index: 1; width: 600px; height: 360px; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box } #game-over { display: none; text-align: center; padding-top: 92px; z-index: 7; width: 600px; height: 360px; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; overflow: auto; margin: auto; position: absolute } </style> </head> <body> <h3>按空格键跳跃</h3> <div class="wrapper"> <canvas id="canvas" width="800" height="480"> <p> You're browser does not support the required functionality to play this game. </p> <p> Please update to a modern browser such as <a href="www.google.com/chrome/">Google Chrome</a> to play. </p> </canvas> <div id="game-over"> <a href="javascript:void(0);" class="restart" id="restart">再试一次?</a> </div> </div> <script> (function() { var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); var player, score, stop, ticker; var ground = [], water = [], enemies = [], environment = []; var platformHeight, platformLength, gapLength; var platformWidth = 32; var platformBase = canvas.height-platformWidth; var platformSpacer = 64; function rand(low, high) { return Math.floor(Math.random()*(high-low+1)+low); } function bound(num, low, high) { return Math.max(Math.min(num, high), low); } var assetLoader = (function() { this.imgs = { 'bg': '/bfwrepo/image/paoku/bg.png', 'sky': '/bfwrepo/image/paoku/sky.png', 'backdrop': '/bfwrepo/image/paoku/backdrop.png', 'backdrop2': '/bfwrepo/image/paoku/backdrop_ground.png', 'grass': '/bfwrepo/image/paoku/grass.png', 'avatar_normal': '/bfwrepo/image/paoku/normal_walk.png', 'water': '/bfwrepo/image/paoku/water.png', 'grass1': '/bfwrepo/image/paoku/grassMid1.png', 'grass2': '/bfwrepo/image/paoku/grassMid2.png', 'bridge': '/bfwrepo/image/paoku/bridge.png', 'plant': '/bfwrepo/image/paoku/plant.png', 'bush1': '/bfwrepo/image/paoku/bush1.png', 'bush2': '/bfwrepo/image/paoku/bush2.png', 'cliff': '/bfwrepo/image/paoku/grassCliffRight.png', 'spikes': '/bfwrepo/image/paoku/spikes.png', 'box': '/bfwrepo/image/paoku/boxCoin.png', 'slime': '/bfwrepo/image/paoku/slime.png' }; var assetsLoaded = 0; var numImgs = Object.keys(this.imgs).length; this.totalAssest = numImgs; function assetLoaded(dic, name) { if (this[dic][name].status !== 'loading') { return; } this[dic][name].status = 'loaded'; assetsLoaded++; if (assetsLoaded === this.totalAssest && typeof this.finished === 'function') { this.finished(); }} this.downloadAll = function() { var _this = this; var src; for (var img in this.imgs) { if (this.imgs.hasOwnProperty(img)) { src = this.imgs[img]; (function(_this, img) { _this.imgs[img] = new Image(); _this.imgs[img].status = 'loading'; _this.imgs[img].name = img; _this.imgs[img].onload = function() { assetLoaded.call(_this, 'imgs', img)}; _this.imgs[img].src = src; })(_this, img); }}} return { imgs: this.imgs, totalAssest: this.totalAssest, downloadAll: this.downloadAll }; })(); assetLoader.finished = function() { startGame(); } function SpriteSheet(path, frameWidth, frameHeight) { this.image = new Image(); this.frameWidth = frameWidth; this.frameHeight = frameHeight; var self = this; this.image.onload = function() { self.framesPerRow = Math.floor(self.image.width/self.frameWidth); }; this.image.src = path; } function Animation(spritesheet, frameSpeed, startFrame, endFrame) { var animationSequence = []; var currentFrame = 0; var counter = 0; for (var frameNumber = startFrame; frameNumber <= endFrame; frameNumber++) animationSequence.push(frameNumber); this.update = function() { if (counter == (frameSpeed-1)) currentFrame = (currentFrame+1)%animationSequence.length; counter = (counter+1)%frameSpeed; }; this.draw = function(x, y) { var row = Math.floor(animationSequence[currentFrame]/spritesheet.framesPerRow); var col = Math.floor(animationSequence[currentFrame]%spritesheet.framesPerRow); ctx.drawImage(spritesheet.image, col*spritesheet.frameWidth, row*spritesheet.frameHeight, spritesheet.frameWidth, spritesheet.frameHeight, x, y, spritesheet.frameWidth, spritesheet.frameHeight); }; } var background = (function() { var sky = {}; var backdrop = {}; var backdrop2 = {}; this.draw = function() { ctx.drawImage(assetLoader.imgs.bg, 0, 0); sky.x -= sky.speed; backdrop.x -= backdrop.speed; backdrop2.x -= backdrop2.speed; ctx.drawImage(assetLoader.imgs.sky, sky.x, sky.y); ctx.drawImage(assetLoader.imgs.sky, sky.x+canvas.width, sky.y); ctx.drawImage(assetLoader.imgs.backdrop, backdrop.x, backdrop.y); ctx.drawImage(assetLoader.imgs.backdrop, backdrop.x+canvas.width, backdrop.y); ctx.drawImage(assetLoader.imgs.backdrop2, backdrop2.x, backdrop2.y); ctx.drawImage(assetLoader.imgs.backdrop2, backdrop2.x+canvas.width, backdrop2.y); if (sky.x+assetLoader.imgs.sky.width <= 0) sky.x = 0; if (backdrop.x+assetLoader.imgs.backdrop.width <= 0) backdrop.x = 0; if (backdrop2.x+assetLoader.imgs.backdrop2.width <= 0) backdrop2.x = 0; }; this.reset = function() { sky.x = 0; sky.y = 0; sky.speed = 0.2; backdrop.x = 0; backdrop.y = 0; backdrop.speed = 0.4; backdrop2.x = 0; backdrop2.y = 0; backdrop2.speed = 0.6; } return { draw: this.draw, reset: this.reset }; })(); function Vector(x, y, dx, dy) { this.x = x || 0; this.y = y || 0; this.dx = dx || 0; this.dy = dy || 0; } Vector.prototype.advance = function() { this.x += this.dx; this.y += this.dy; }; Vector.prototype.minDist = function(vec) { var minDist = Infinity; var max = Math.max(Math.abs(this.dx), Math.abs(this.dy), Math.abs(vec.dx), Math.abs(vec.dy)); var slice = 1/max; var x, y, distSquared; var vec1 = {}, vec2 = {}; vec1.x = this.x+this.width/2; vec1.y = this.y+this.height/2; vec2.x = vec.x+vec.width/2; vec2.y = vec.y+vec.height/2; for (var percent = 0; percent < 1; percent += slice) { x = (vec1.x+this.dx*percent)-(vec2.x+vec.dx*percent); y = (vec1.y+this.dy*percent)-(vec2.y+vec.dy*percent); distSquared = x*x+y*y; minDist = Math.min(minDist, distSquared); } return Math.sqrt(minDist); }; var player = (function(player) { player.width = 60; player.height = 96; player.speed = 6; player.gravity = 1; player.dy = 0; player.jumpDy=-10; player.isFalling = false; player.isJumping = false; player.sheet = new SpriteSheet('/bfwrepo/image/paoku/normal_walk.png', player.width, player.height); player.walkAnim = new Animation(player.sheet, 4, 0, 15); player.jumpAnim = new Animation(player.sheet, 4, 15, 15); player.fallAnim = new Animation(player.sheet, 4, 11, 11); player.anim = player.walkAnim; Vector.call(player, 0, 0, 0, player.dy); var jumpCounter = 0; player.update = function() { if (KEY_STATUS.space &&.........完整代码请登录后点击上方下载按钮下载查看
网友评论0