processing实现canvas万圣节鬼屋跳跃射击闯关类游戏代码
代码语言:html
所属分类:游戏
代码描述:processing实现canvas万圣节鬼屋跳跃射击闯关类游戏代码,使用箭头键向左、向右和跳跃,按A和D键射击,将敌人从左、右、上方和下方击退,您只有有限数量的子弹,要重新装弹,请确保收集更多的弹药,每个级别都有一定数量的怪物需要摧毁,然后才能移动到下一级别,如果您想玩特定级别,您始终可以直接转到级别页面中的每个级别。
代码标签: processing canvas 万圣节 鬼屋 跳跃 射击 游戏 闯关 代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> * { margin: 0; box-sizing: border-box; overflow: hidden; } body { background: #222; width: 100%; height: 100vh; display: flex; justify-content: center; align-items: center; } body canvas { box-shadow: 0.2em 0.2em 2em #0008; border: none; outline: none; } </style> </head> <body > <canvas id="canvas"></canvas> <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/processing.min.js"></script> <script > var sketchProc = function(processingInstance) { with (processingInstance) { size(600, 600); frameRate(60); smooth(); { /**/// // Do not remove this line angleMode = "radians"; textAlign(CENTER, CENTER); textFont(createFont("Verdana")); var game, player; } //Globals { //Key|Button stuff var clicked = false, hover = false; var keys = []; keyPressed = function(){ keys[keyCode] = true; }; keyReleased = function(){ keys[keyCode] = false; }; mouseClicked = function(){ clicked = true; }; } //Keys/Mouse { var Button = function(config) { this.x = config.x || 0; this.y = config.y || 0; this.size = config.size || 100; this.content = config.content || "Home"; this.page = config.page || "home"; this.level = config.level || 0; this.textSize = config.textSize || this.size * 0.2; this.borderColor = color(119, 22, 22, 30); this.backColor = color(119, 22, 22, 200); this.textColor = color(245, 242, 242, 150); this.backColorHover = color(50, 50, 50, 200); this.textColorHover = color(200, 200, 200, 200); this.growth = 0; }; Button.prototype.draw = function () { textSize(this.textSize + (this.growth * 0.1)); noStroke(); //circles if (dist(mouseX, mouseY, this.x, this.y) <= this.size / 2) { //hover this.growth = constrain(this.growth + 0.5, 0, 10); if(clicked) { game.page = this.page; if(this.page === "level") { game.level = this.level; } game.reset(); } pushStyle(); textAlign(CENTER, CENTER); fill(this.backColorHover); stroke(this.borderColor); strokeWeight(this.size * 0.1); ellipse(this.x, this.y, this.size + this.growth, this.size + this.growth); fill(this.textColorHover); text(this.content, this.x, this.y); popStyle(); } else { //not hover this.growth = constrain(this.growth - 0.5, 0, 10); pushStyle(); textAlign(CENTER, CENTER); fill(this.backColor); stroke(this.borderColor); strokeWeight(2); noStroke(); ellipse(this.x, this.y, this.size + this.growth, this.size + this.growth); fill(this.textColor); text(this.content, this.x, this.y); popStyle(); } }; } //Buttons { //Coin object var Coin = function(x, y, w, h, timeToLive) { this.pos = new PVector(x, y); this.w = w || 5; this.h = h || 5; this.timeToLive = timeToLive || 200; }; Coin.prototype.update = function() { this.timeToLive--; }; //Bone Object - Inherits from Coin var Bone = function(x, y) { this.w = 15; this.h = 3; this.timeToLive = 200; Coin.call(this, x, y, this.w, this.h, this.timeToLive); }; Bone.prototype = Object.create(Coin.prototype); Bone.prototype.display = function() { fill(200, 200, 200, 200); rect(this.pos.x, this.pos.y, this.w, this.h, 5); ellipse(this.pos.x, this.pos.y+this.h/2, 5, 6); ellipse(this.pos.x, this.pos.y-this.h/2, 6, 5); ellipse(this.pos.x + this.w, this.pos.y+this.h/2, 6, 5); ellipse(this.pos.x + this.w, this.pos.y-this.h/2, 5, 6); }; Bone.prototype.run = function() { this.update(); this.display(); }; } //Coins { //Ammo Object var Ammo = function(x, y) { this.pos = new PVector(x, y); this.w = 36; this.h = 30; this.timeToLive = 200; }; Ammo.prototype.update = function() { this.pos.y = constrain(this.pos.y + 3, -50, 470); if(this.pos.y === 470) { this.timeToLive--; } }; Ammo.prototype.display = function() { noStroke(); strokeWeight(1); stroke(222, 222, 222, 30); fill(20, 19, 19, 200); rect(this.pos.x, this.pos.y, this.w, this.h, 2); fill(240, 240, 240); textSize(8); textAlign(CENTER, CENTER); text("AMMO", this.pos.x + this.w/2, this.pos.y + this.h/2); }; Ammo.prototype.run = function() { this.update(); this.display(); }; } //Ammo { var Cross = function(config) { this.pos = config.pos || new PVector(0, 0); this.scale = config.scale || new PVector(1, 1); this.angle = config.angle || 0; this.lightColor = color(92, 94, 92); this.darkColor = color(87, 89, 87); }; Cross.prototype.display = function() { noStroke(); fill(this.lightColor); pushMatrix(); translate(this.pos.x, this.pos.y); scale(this.scale.x, this.scale.y); rotate(radians(this.angle)); rect(-10, 0, 20, 100); rect(-30, 20, 60, 20); fill(this.darkColor); rect(0, 0, 10, 100); rect(0, 20, 30, 20); popMatrix(); }; } //Cross { var GraveStone = function(config) { this.pos = config.pos || new PVector(0, 0); this.scale = config.scale || new PVector(1, 1); this.angle = config.angle || 0; this.lightColor = color(132, 135, 132); this.darkColor = color(123, 128, 123); }; GraveStone.prototype.display = function() { noStroke(); fill(this.lightColor); pushMatrix(); translate(this.pos.x, this.pos.y); scale(this.scale.x, this.scale.y); rotate(radians(this.angle)); arc(0, 0, 150, 120, 181, 360); rect(-75, 0, 150, 100); fill(this.darkColor); arc(0, 0, 150, 120, 271, 360); rect(0, 0, 75, 100); pushStyle(); textSize(40); fill(59, 57, 57); textAlign(CENTER, CENTER); text("R.I.P", 0, 0); popStyle(); popMatrix(); }; } //Gravestone { //Spider Web Object var SpiderWeb = function(config) { this.pos = config.pos || new PVector(0, 0); this.scale = config.scale || new PVector(1, 1); this.webColor = color(255, 255, 255, 50); }; SpiderWeb.prototype.display = function() { noFill(); strokeWeight(2); stroke(this.webColor); pushMatrix(); translate(this.pos.x, this.pos.y); scale(this.scale.x, this.scale.y); beginShape(); vertex(320, 80); bezierVertex(280, 53, 270, 53, 220, 0); endShape(); beginShape(); vertex(320, 80); bezierVertex(320, 63, 300, 53, 290, 0); endShape(); beginShape(); vertex(320, 80); bezierVertex(330, 63, 320, 55, 350, 0); endShape(); beginShape(); vertex(320, 80); bezierVertex(340, 78, 345, 72, 400, 42); endShape(); beginShape(); vertex(320, 80); bezierVertex(345, 98, 355, 98, 400, 110); endShape(); beginShape(); vertex(320, 80); bezierVertex(335, 118, 345, 128, 400, 190); endShape(); //first row beginShape(); vertex(287, 58); bezierVertex(298, 56, 300, 56, 305, 46); endShape(); beginShape(); vertex(305, 46); bezierVertex(312, 56, 320, 56, 329, 50); endShape(); beginShape(); vertex(329, 50); bezierVertex(335, 66, 335, 66, 344, 73); endShape(); beginShape(); vertex(344, 73); bezierVertex(340, 76, 340, 80, 346, 95); endShape(); beginShape(); vertex(346, 95); bezierVertex(338, 104, 338, 97, 339, 116); endShape(); //second row beginShape(); vertex(260, 38); bezierVertex(280, 37, 285, 35, 296, 23); endShape(); beginShape(); vertex(296, 23); bezierVertex(310, 35, 320, 33, 338, 26); endShape(); beginShape(); vertex(338, 26); bezierVertex(340, 45, 350, 52, 368, 61); endShape(); beginShape(); vertex(368, 61); bezierVertex(355, 80, 360, 82, 371, 102); endShape(); beginShape(); vertex(371, 102); bezierVertex(355, 120, 360, 122, 357, 139); endShape(); //third row beginShape(); vertex(235, 16); bezierVertex(270, 17, 280, 15, 290, 0); endShape(); beginShape(); vertex(290, 0); bezierVertex(315, 17, 330, 15, 347, 5); endShape(); beginShape(); vertex(347, 5); bezierVertex(360, 37, 350, 35, 392, 47); endShape(); beginShape(); vertex(392, 47); bezierVertex(380, 67, 370, 90, 395, 108); endShape(); beginShape(); vertex(395, 108); bezierVertex(380, 117, 370, 140, 378, 165); endShape(); //forth row beginShape(); vertex(372, 0); bezierVertex(380, 27, 385, 25, 400, 26); endShape(); beginShape(); vertex(400, 137); bezierVertex(384, 163, 392, 175, 393, 182); endShape(); popMatrix(); }; } //Spiderweb { var Tree = function(config) { this.x = config.x || width/2; this.y = config.y || height; this.length = config.length || 60; this.depth = config.depth || 4; this.weight = config.weight || 6; this.baseColor = config.baseColor || color(20); }; Tree.prototype.branch = function(length, depth, weight) { strokeWeight(weight); stroke(this.baseColor); line(0, 0, 0, -length); translate(0, -length); if (depth > 0) { depth--; for(var i = 0; i < random(2, 4); i++) { var dir = random() < 0.5 ? 1 : -1; pushMatrix(); rotate(radians(random(10, 40) * dir)); this.branch(length * random(0.65, 0.75), depth, weight * 0.65); popMatrix(); } } }; Tree.prototype.display = function() { pushMatrix(); translate(this.x, this.y); this.branch(this.length, this.depth, this.weight); popMatrix(); }; } //Tree { //Lightning object var Lightning = function(config) { this.minx = config.minx || width/3; this.maxx = config.maxx || width-width/3; this.y = config.y || 0; this.length = config.length || 30; this.depth = config.depth || 18; this.weight = config.weight || 3; this.angleRange = config.angleRange || 60; //should always be less than 90 so forks always move down this.forkColor = config.forkColor || color(255); this.flashColor = config.flashColor || color(75); }; //Recursive function for displaying the lightning forks Lightning.prototype.fork = function(length, depth, weight, maxAngle) { var angle = 0; var valid = false; while(!valid) { angle = random(-30, 30); if(maxAngle + angle > -this.angleRange && maxAngle + angle < this.angleRange) { //ensure forks only ever within n degree area (moving downwards) valid = true; } } maxAngle+= angle; //Store the total angle depth--; strokeWeight(weight); stroke(this.forkColor); line(0, 0, 0, length); translate(0, length); //Only generate more forks for given depth if (depth >= 0) { pushMatrix(); if(angleMode === "radians") {rotate(radians(angle));} else {rotate(angle);} //rotate based on current angleMode this.fork(length, depth, weight*0.75, maxAngle); popMatrix(); //Randomly add a new fork if(random() < 0.1) { angle = random(-30, 30); pushMatrix(); if(angleMode === "radians") {rotate(radians(angle));} else {rotate(angle);} //rotate based on current angleMode this.fork(length*0.85, depth, weight*0.5, angle); popMatrix(); } } }; //Display the lightning Lightning.prototype.display = function() { background(this.flashColor); pushMatrix(); translate(random(this.minx, this.maxx), this.y); this.fork(this.length, this.depth, this.weight, 0); popMatrix(); }; } //Lightening //Lightning object (fractal) var Lightning = (function() { Lightning = function(args) { this.min = args.min || width/6; this.max = args.max || width-width/6; this.y = args.y || 0; this.length = args.length || ~~(height / 11); this.depth = constrain(args.depth || ~~(height / 32), 5, 25); this.weight = constrain(args.weight || 4, 2, 8); this.forkColor = args.forkColor || color(255); this.flashColor = args.flashColor || color(200); this.angleRange = constrain(args.angleRange || 60, 40, 80); this.arr = []; }; Lightning.prototype = { //recursive function generate the random lightning fork: function(length, depth, weight, px, py) { var angle = random(-this.angleRange, this.angleRange); //decrease the depth so can exit the recursive function depth--; //calculate the next x/y coordinate for the fork var x = px + cos(radians(angle - 90)) * length; var y = py + sin(radians(angle + 90)) * length; //add fork to the start of the array this.arr.unshift({ weight: weight, stroke: this.forkColor, px: px, py: py, x: x, y: y, length: length, opacity: 255, angle: angle }); //Only generate more forks for given depth if (depth >= 0) { this.fork(length, depth, weight*0.8, x, y); //Randomly add a new fork if(random() < 0.15) { angle = random(-this.angleRange, this.angleRange); this.fork(length*0.85, depth, weight*0.5, x, y); } } }, draw: function() { //draw the lightning pushStyle(); for(var i = this.arr.length - 1; i >= 0; i--) { strokeWeight(this.arr[i].weight); stroke(this.arr[i].stroke, this.arr[i].opacity); line(this.arr[i].px, this.arr[i].py+this.arr[i].weight, this.arr[i].x, this.arr[i].y); this.arr[i].opacity-=10; if(this.arr[i].opacity <= 0) { this.arr.splice(i, 1); } } popStyle(); }, generate: function() { //initiates a new lightning strike this.fork( this.length, //length of each strike this.depth, //how deep you want to recurse this.weight, //starting weight of the strike random(this.min, this.max), //starting (previous) x pos this.y //starting (previous) y pos ); } }; return Lightning; })(); { var RedBack = function(config) { this.pos = config.pos || new PVector(0, 0); this.webY = config.webY || this.pos.y - 50; this.w = 55; this.h = 70; this.scale = config.scale || 1; this.bodyColor = config.bakColor || color(0); this.stripeColor = config.stripeColor || color(201, 26, 26, 200); this.webColor = config.webColor || color(255, 255, 255, 50); this.yOffset = config.yOffset || 0; this.yDir = config.yDir || 1; this.img = this.getImage(); }; RedBack.prototype.getImage = function() { background(0, 0, 0, 0); pushMatrix(); translate(0, 0); noStroke(); fill(this.bodyColor); //body ellipse(350, 220, 20, 25); //head ellipse(350, 237, 10, 10); //stripe noFill(); stroke(this.stripeColor); strokeWeight(3); ellipse(350, 216, 3, 8); ellipse(350, 223, 2, 2); noFill(); stroke(this.bodyColor); strokeWeight(2); //eyes line(348, 240, 348, 242); line(352, 240, 352, 242); strokeWeight(1); //back legs beginShape(); vertex(340, 220); bezierVertex(325, 210, 326, 206, 326, 199); endShape(); beginShape(); vertex(360, 220); bezierVertex(375, 210, 374, 206, 374, 199); endShape(); beginShape(); vertex(341, 212); bezierVertex(327, 202, 334, 196, 333, 190); endShape(); beginShape(); vertex(359, 212); bezierVertex(373, 202, 366, 196, 367, 190); endShape(); //front legs beginShape(); vertex(340, 223); bezierVertex(336, 225, 336, 228, 328, 230); endShape(); beginShape(); vertex(328, 230); bezierVertex(332, 250, 332, 248, 334, 250); endShape(); beginShape(); vertex(360, 223); bezierVertex(364, 225, 364, 228, 372, 230); endShape(); beginShape(); vertex(372, 230); bezierVertex(368, 250, 368, 248, 366, 250); endShape(); beginShape(); vertex(342, 227); bezierVertex(340, 229, 340, 232, 334, 234); endShape(); beginShape(); vertex(334, 234); bezierVertex(338, 254, 343, 252, 342, 255); endShape(); beginShape(); vertex(358, 227); bezierVertex(360, 229, 360, 232, 366, 234); endShape(); beginShape(); vertex(366, 234); bezierVertex(362, 254, 357, 252, 358, 255); endShape(); popMatrix(); return get(321, 185, this.w, this.h); }; RedBack.prototype.display = function() { pushMatrix(); scale(this.scale); strokeWeight(1); stroke(this.webColor); line(this.pos.x + this.w * 0.5, this.webY, this.pos.x + this.w * 0.5, this.pos.y + this.yOffset + this.h * 0.5); image(this.img, this.pos.x, this.pos.y + this.yOffset); popMatrix(); }; RedBack.prototype.update = function() { this.yOffset+= 0.6 * this.yDir; if(this.yOffset > 60 || this.yOffset < 0) { this.yDir*= -1; } }; RedBack.prototype.run = function() { this.update(); this.display(); }; } //Red Back { { //Enemy Object var Enemy = function(config) { this.pos = config.pos || new PVector(0, 470); //default location is on the ground this.w = config.w || 30; this.h = config.h || 30; this.speed = config.speed || 3; this.dir = config.dir || random() < 0.5 ? 1 : -1; if(config.pos === null) { if(this.dir === 1) { this.pos.x = floor(random(-250, -50)); } else { this.pos.x = floor(random(650, 850)); } } }; } //Enemy { //Mummy Object var Mummy = function(config) { Enemy.call(this, config); this.backColor = color(218, 224, 195); this.bandageColor = color(66, 63, 63, 100); this.eyeBandColor = color(71, 65, 65); this.eyeColor = color(184, 171, 170); }; Mummy.prototype = Object.create(Enemy.prototype); Mummy.prototype.display = function() { pushMatrix(); translate(this.pos.x, this.pos.y); if(this.dir === -1) { scale(-1, 1); translate(-this.w, 0); } //body noStroke(); fill(this.backColor); rect(0, 0, this.w, this.h, 3); //bandages stroke(this.bandageColor); strokeWeight(1); line(0, 0, this.w, this.h * 0.1); line(0, this.h * 0.1, this.w, this.h * 0.15); line(0, this.h * 0.3, this.w, this.h * 0.2); line(0, this.h * 0.2, this.w, this.h * 0.4); line(0, this.h * 0.5, this.w, this.h * 0.3); line(0, this.h * 0.6, this.w, this.h * 0.35); line(0, this.h * 0.45, this.w, this.h * 0.5); line(0, this.h * 0.7, this.w, this.h * 0.6); line(0, this.h * 0.75, this.w, this.h * 0.7); line(0, this.h * 0.6, this.w, this.h * 0.7); line(0, this.h * 0.8, this.w, this.h * 0.9); line(0, this.h * 0.9, this.w, this.h * 0.8); noStroke(); fill(this.eyeBandColor); beginShape(); vertex(0, this.h * 0.2); vertex(this.w, this.h * 0.25); vertex(this.w, this.h * 0.4); vertex(0, this.h * 0.4); endShape(CLOSE); //eyes fill(this.eyeColor); ellipse(this.w/3 + this.w/6, this.h/3.5, 4, 4); ellipse(this.w - this.w/3 + this.w/6, this.h/3.5, 4, 4); popMatrix(); }; Mummy.prototype.update = function() { this.pos.x += this.speed * this.dir; if(random() < 0.005) { game.coins.push(new Bone(this.pos.x, 495)); } }; Mummy.prototype.run = function() { this.update(); this.display(); }; } //Mummies { //Skeleton Object - Inhertis from Enemy var Skeleton = function(config) { Enemy.call(this, config); this.headColor = color(31, 29, 28); this.bodyColor = color(3, 3, 3); this.eyeColor = color(180, 180, 180); this.legColor = color(8, 8, 8); }; Skeleton.prototype = Object.create(Enemy.prototype); Skeleton.prototype.display = function() { pushMatrix(); translate(this.pos.x, this.pos.y); if(this.dir === -1) { scale(-1, 1); translate(-this.w, 0); } //body noStroke(); fill(222, 222, 222); rect(0, 0, this.w, this.h/2, 3); stroke(222, 222, 222); strokeWeight(1); noFill(); //spine line(this.w/2, this.h/2, this.w/2, this.h); //ribs line(this.w/8, this.h * 0.65, this.w - this.w/8, this.h * 0.65); line(this.w/8, this.h * 0.8, this.w - this.w/8, this.h * 0.8); line(this.w/8, this.h * 0.95, this.w - this.w/8, this.h * 0.95); //arms line(this.w/8, this.h * 0.65, -this.w/75, this.h * 0.9); line(this.w - this.w/8, this.h * 0.65, this.w + this.w/75, this.h * 0.9); noStroke(); //eyes fill(38, 38, 38); var dir = -0; ellipse(this.w/3 + this.speed, this.h/3.5, 4, 4); ellipse(this.w - this.w/3 + this.speed, this.h/3.5, 4, 4); popMatrix(); }; Skeleton.prototype.update = function() { this.pos.x += this.speed * this.dir; if(random() < 0.005) { game.coins.push(new Bone(this.pos.x, 495)); } }; Skeleton.prototype.run = function() { this.update(); this.display(); }; //ZamikazeSkeleton - Inhertis from Skeleton > Enemy var KamikazeSkeleton = function(config) { Skeleton.call(this, config); this.timeToFall = config.timeToFall || random(200, 700); this.dropSpeed = config.dropSpeed || 1; this.isOnGround = false; }; KamikazeSkeleton.prototype = Object.create(Skeleton.prototype); KamikazeSkeleton.prototype.display = function() { if(this.pos.y < 500 + this.h) { this.h = 500 - this.pos.y; } else { this.h = 0; } Skeleton.prototype.display.call(this); }; KamikazeSkeleton.prototype.drop = function() { if(!this.isOnGround) { this.timeToFall--; if(this.timeToFall < 0) { this.pos.y -= this.dropSpeed; if(this.pos.y <= 470) { this.isOnGround = true; } } } this.display(); }; } //Skeleton { //Vampire Object - Inhertis from Enemy var Vampire = function(config) { Enemy.call(this, config); this.headColor = color(214, 213, 171); this.bodyColor = color(12, 12, 12); this.hairColor = color(22, 22, 22); this.eyeColor = color(38, 38, 38); this.fangColor = color(245, 242, 242); this.capeColor = random() < 0.5 ? color(115, 13, 13) : color(12, 12, 12); this.batColor = color(0); this.isBat = false; this.theta = 0.0; this.amplitude = 20.0; this.dy = 0.0; this.ybase = this.pos.y; }; Vampire.prototype = Object.create(Enemy.prototype); Vampire.prototype.display = function() { pushMatrix(); translate(this.pos.x, this.pos.y); if(this.dir === -1) { scale(-1, 1); translate(-this.w, 0); } if(this.isBat) { fill(this.batColor); //wings noStroke(); pushMatrix(); translate(0, 0); rotate(radians(this.dy*1.5)); beginShape(); vertex(0, 3); vertex(-30, 3); bezierVertex(-29, 6, -26, 9, -27, 12); bezierVertex(-24, 8, -19, 8, -13, 10); bezierVertex(-10, 7, 5, 7, 0, 6); vertex(0, 3); endShape(); popMatrix(); pushMatrix(); translate(15, 0); rotate(radians(-this.dy*1.5)); beginShape(); vertex(0, 3); vertex(30, 3); bezierVertex(29, 6, 26, 9, 27, 12); bezierVertex(24, 8, 19, 8, 13, 10); bezierVertex(10, 7, -5, 7, 0, 6); vertex(0, 3); endShape(); popMatrix(); //body noStroke(); fill(this.batColor); rect(0, 0, 15, 15, 3); //ears triangle(2, 0, 5, -6, 8, 0); triangle(this.w-2, 0, this.w-5, -6, this.w-8, 0); //eyes fill(255); ellipse(5, 4, 3, 3); ellipse(this.w-5, 4, 3, 3); } else { //vampire //body noStroke(); fill(this.headColor); rect(0, 0, this.w, this.h, 3); fill(this.bodyColor); rect(0, this.h/2, this.w, this.h/2); //cape fill(this.capeColor); triangle(0, this.h/4, 0, this.h*0.6, -this.w/6, this.h/4); triangle(this.w, this.h/4, this.w, this.h*0.6, this.w + this.w/6, this.h/4); //fangs fill(this.fangColor); triangle(this.w * 0.3, this.h/2, this.w * 0.4, this.h * 0.75, this.w * 0.45, this.h/2); triangle(this.w * 0.75, this.h/2, this.w * 0.8, this.h * 0.75, this.w * 0.9, this.h/2); triangle(this.w * 0.45, this.h/2, this.w * 0.5, this.h * 0.65, this.w * 0.55, this.h/2); triangle(this.w * 0.65, this.h/2, this.w * 0.7, this.h * 0.65, this.w * 0.75, this.h/2); triangle(this.w * 0.55, this.h/2, this.w * 0.6, this.h * 0.65, this.w * 0.65, this.h/2); //eyes fill(this.eyeColor); ellipse(this.w/3 + this.w/6, this.h/3.5, 4, 4); ellipse(this.w - this.w/3 + this.w/6, this.h/3.5, 4, 4); //hair stroke(this.hairColor); strokeWeight(4); line(0, 0, this.w-1, 0); } popMatrix(); noStroke(); }; Vampire.prototype.update = function() { this.pos.x += this.speed * this.dir; if(random() < 0.005) { game.coins.push(new Bone(this.pos.x, 495)); } }; Vampire.prototype.run = function() { this.update(); this.display(); }; //ZamikazeVampire - Inhertis from Vampire > Enemy var KamikazeVampire = function(config) { Vampire.call(this, config); this.timeToFall = random(200, 700); this.dropSpeed = 5; this.isOnGround = false; this.amplitude = 20.0; this.ybase = this.pos.y; this.isBat = true; this.w = 15; this.h = 15; this.flyUp = config.flyUp || random(100, 100); }; KamikazeVampire.prototype = Object.create(Vampire.prototype); KamikazeVampire.prototype.drop = function() { if(!this.isOnGround) { this.timeToFall--; if(this.timeToFall < 0) { this.pos.y += this.dropSpeed; if(this.pos.y >= 470) { this.isOnGround = true; } } } this.display(); }; } //Vampire { //Spider Object - Inherits from Enemy var Spider = function(config) { Enemy.call(this, config); this.headColor = color(31, 29, 28); this.bodyColor = random() < 0.5 ? color(115, 60, 11) : color(3, 3, 3); this.eyeColor = color(180, 180, 180); this.legColor = color(8, 8, 8); }; Spider.prototype = Object.create(Enemy.prototype); Spider.prototype.display = function() { pushMatrix(); translate(this.pos.x, this.pos.y); if(this.dir === -1) { scale(-1, 1); translate(-this.w, 0); } //legs - eight of them :) strokeWeight(1); stroke(this.legColor); for(var i = 0; i < 4; i++) { //left leg line(0, this.h * 0.2 + this.h/7*i, -this.w/8, this.h * 0.2 + this.h/7*i); line(-this.w/8, this.h * 0.2 + this.h/7*i, -this.w/4, this.h * 0.5 + this.h/6*i); //right leg line(this.w, this.h * 0.2 + this.h/7*i, this.w + this.w/8, this.h * 0.2 + this.h/7*i); line(this.w + this.w/8, this.h * 0.2 + this.h/7*i, this.w + this.w/4, this.h * 0.5 + this.h/6*i); } //body noStroke(); fill(this.headColor); rect(0, 0, this.w, this.h/2, 8, 8, 0, 0); fill(this.bodyColor); rect(0, this.h/2, this.w, this.h/2.5, 0, 0, 8, 8); //eyes - eight of them :) fill(this.eyeColor); for(var i = 1; i <= 4; i++) { ellipse(this.w/5 * i + this.speed/2, this.h/6, 3, 3); ellipse(this.w/5 * i + this.speed/2, this.h/2.7, 3, 3); } //fangs triangle(this.w * 0.4, this.h/2, this.w * 0.5, this.h * 0.75, this.w * 0.55, this.h/2); triangle(this.w * 0.65, this.h/2, this.w * 0.7, this.h * 0.75, this.w * 0.8, this.h/2); popMatrix(); noStroke(); }; Spider.prototype.update = function() { this.pos.x += this.speed * this.dir; if(random() < 0.005) { game.coins.push(new Bone(this.pos.x, 495)); } }; Spider.prototype.run = function() { this.update(); this.display(); }; //ZamikazeSpider - Inhertis from Spider > Enemy var KamikazeSpider = function(config) { Spider.call(this, config); this.timeToFall = random(200, 700); this.dropSpeed = 7; this.yDir = 1; this.isOnGround = false; }; KamikazeSpider.prototype = Object.create(Spider.prototype); KamikazeSpider.prototype.drop = function() { if(!this.isOnGround) { this.timeToFall--; if(this.timeToFall < 0) { this.pos.y += this.dropSpeed; if(this.pos.y >= 470) { this.isOnGround = true; } } } this.display(); }; } //Spider { //Zombie Object - Inherits from Enemy var Zombie = function(config) { Enemy.call(this, config); this.skinColor = random() < 0.5 ? color(60, 99, 36) : color(79, 53, 110); this.shirtColor = random() < 0.5 ? color(10, 10, 9) : color(18, 33, 38); this.hairColor = color(33, 30, 29); }; Zombie.prototype = Object.create(Enemy.prototype); Zombie.prototype.display = function() { pushMatrix(); translate(this.pos.x, this.pos.y); if(this.dir === -1) { scale(-1, 1); translate(-this.w, 0); } //back arm stroke(this.skinColor); strokeWeight(3); line(this.w, this.h/1.5, this.w+this.w/3, this.h/1.8); //fingers strokeWeight(2); line(this.w+this.w/3, this.h/1.8, this.w+this.w/2, this.h/1.4); line(this.w+this.w/3, this.h/1.8, this.w+this.w/2.5, this.h/1.3); line(this.w+this.w/3, this.h/1.8, this.w+this.w/3.5, this.h/1.3); //body noStroke(); fill(this.skinColor); rect(0, 0, this.w, this.h, 3); //shirt fill(this.shirtColor); rect(0, this.h/2, this.w, this.h/2); //front arm stroke(this.skinColor); strokeWeight(3); line(this.w/2, this.h/1.5, this.w, this.h/1.4); //fingers strokeWeight(2); line(this.w, this.h/1.4, this.w+this.w/8, this.h/1.2); line(this.w, this.h/1.4, this.w+this.w/20, this.h/1.1); line(this.w, this.h/1.4, this.w-this.w/10, this.h/1.1); //hair stroke(this.hairColor); strokeWeight(4); line(2, 0, this.w-2, 0); noStroke(); //eyes fill(251, 249, 166); ellipse(this.w/3 + 5, this.h/3.5, 10, 10); ellipse(this.w - this.w/3 + 5, this.h/3.5, 10, 10); fill(43, 40, 40); ellipse(this.w/3 + 7, this.h/3.1, 2, 2); ellipse(this.w - this.w/3 + 7, this.h/3.9, 2, 2); popMatrix(); noStroke(); }; Zombie.prototype.update = function() { this.pos.x += this.speed * this.dir; if(random() < 0.005) { game.coins.push(new Bone(this.pos.x, 495)); } }; Zombie.prototype.run = function() { this.update(); this.display(); }; //ZamikazeZombie - Inhertis from Zombie > Enemy var KamikazeZombie = function(config) { Zombie.call(this, config); this.timeToFall = random(200, 500); this.dropSpeed = 7; this.isOnGround = false; }; KamikazeZombie.prototype = Object.create(Zombie.prototype); KamikazeZombie.prototype.drop = function() { if(!this.isOnGround) { this.timeToFall--; if(this.timeToFall < 0) { this.pos.y += this.dropSpeed; if(this.pos.y >= 470) { this.isOnGround = true; } } } this.display(); }; } //Zombie { //Grim Object - Inherits from Enemy var Grim = function(config) { Enemy.call(this, config); this.bodyColor = config.bodyColor || random() < 0.5 ? color(32, 35, 36) : color(87, 7, 4); this.hairColor = config.hairColor || color(33, 30, 29); this.eyeColor = config.eyeColor || color(38, 38, 38); }; Grim.prototype = Object.create(Enemy.prototype); Grim.prototype.display = function() { pushMatrix(); translate(this.pos.x, this.pos.y); if(this.dir === -1) { scale(-1, 1); translate(-this.w, 0); } //body/head noStroke(); fill(this.bodyColor); rect(0, 0, this.w, this.h); fill(74, 13, 5); ellipse(this.w * 0.47, this.h * 0.3, this.w * 0.85, this.h * 0.5); fill(252, 252, 249); ellipse(this.w * 0.5, this.h * 0.3, this.w * 0.78, this.h * 0.5); //hoody fill(this.bodyColor); beginShape(); vertex(0, this.h * 0.3); vertex(-this.w * 0.1, this.h * 0.1); vertex(0, -this.h * 0.1); vertex(this.w * 0.7, -this.h * 0.3); vertex(this.w * 1.3, 0); vertex(this.w, 0); vertex(this.w * 1.2, this.h * 0.2); vertex(this.w, this.h * 0.3); vertex(this.w * 0.8, this.h * 0.2); vertex(this.w * 0.4, 0); vertex(0, 0); endShape(CLOSE); //teeth fill(245, 242, 242); rect(this.w * 0.29, this.w * 0.45, this.w * 0.02, this.h * 0.15, 8); rect(this.w * 0.43, this.w * 0.45, this.w * 0.02, this.h * 0.15, 8); rect(this.w * 0.56, this.w * 0.45, this.w * 0.02, this.h * 0.15, 8); rect(this.w * 0.7, this.w * 0.45, this.w * 0.02, this.h * 0.15, 8); //eyes fill(38, 38, 38); var dir = 0.2; ellipse(this.w/3 + dir*this.w/6, this.h/3.5, 10, 10); ellipse(this.w - this.w/3 + dir*this.w/6, this.h/3.5, 9, 9); //scythe noStroke(); fill(130, 125, 125); beginShape(); vertex(-this.w * 0.2, -this.h * 0.2); bezierVertex(-this.w * 0.35, 0, -this.w * 0.7, 0, -this.w * 0.7, this.h * 0.5); bezierVertex(-this.w * 0.5, this.h * 0.2, -this.w * 0.3, 0, -this.w * 0.2, -this.h * 0.1); endShape(CLOSE); noFill(); stroke(77, 51, 7); strokeWeight(3); line(this.w * 0.2, this.h * 0.6, -this.w * 0.2, -this.h * 0.2); strokeWeight(1); noStroke(); popMatrix(); }; Grim.prototype.update = function() { this.pos.x += this.speed * this.dir; if(random() < 0.005) { game.coins.push(new Bone(this.pos.x, 495)); } }; Grim.prototype.run = function() { this.update(); this.display(); }; //ZamikazeGrim - Inhertis from Grim > Enemy var KamikazeGrim = function(config) { Grim.call(this, config); this.timeToFall = config.timeToFall || random(200, 500); this.dropSpeed = config.dropSpeed || 7; this.isOnGround = false; }; KamikazeGrim.prototype = Object.create(Grim.prototype); KamikazeGrim.prototype.drop = function() { if(!this.isOnGround) { this.timeToFall--; if(this.timeToFall < 0) { this.pos.y += this.dropSpeed; if(this.pos.y >= 470) { this.isOnGround = true; } } } this.display(); }; } //Grim { var Witch = function(config) { this.startpos = config.pos || new PVector(0, 0); this.pos = config.pos || new PVector(0, 0); this.w = config.w || 30; this.h = config.h || 30; this.dir = random() < 0.5 ? 1 : -1; this.speed = random(2, 3); this.skinColor = random() < 0.5 ? color(137, 209, 123) : color(201, 123, 54); this.broom = this.getBroom(); }; Witch.prototype.update = function() { this.pos.x -= this.speed * this.dir; }; Witch.prototype.display = function() { pushMatrix(); translate(this.pos.x + this.w * this.dir * -1.4, this.pos.y); //adjust the position so the cat is at the back of the broom with the witch if(this.dir === -1) { scale(-1, 1); translate(-this.w, 0); } //body noStroke(); fill(this.skinColor); rect(0, 0, this.w, this.h, 3); //clothes fill(12, 13, 12); rect(0, this.h/2, this.w, this.h/2); //eyes fill(38, 38, 38); //var dir = -0; ellipse(this.w/3 + this.dir*this.w/6, this.h/3.5, 4, 4); ellipse(this.w - this.w/3 + this.dir*this.w/6, this.h/3.5, 4, 4); //hair stroke(12, 13, 12); strokeWeight(2); line(this.w * 0.05, 0, -this.w * 0.05, this.h * 0.3); line(this.w * 0.05, 0, -this.w * 0.1, this.h * 0.2); line(this.w * 0.95, 0, this.w * 1.05, this.h * 0.3); line(this.w * 0.95, 0, this.w * 1.1, this.h * 0.2); //hat stroke(38, 38, 38); strokeWeight(4); line(-this.w * 0.2, 0, this.w * 1.2, 0); noStroke(); beginShape(); vertex(this.w * 0.1, 0); vertex(this.w * 0.4, -this.h * 0.4); vertex(this.w * 0.8, -this.h * 0.6); vertex(this.w * 0.7, -this.h * 0.3); vertex(this.w * 0.9, 0); endShape(CLOSE); image(this.broom, -this.w * 2, this.h * 0.65); popMatrix(); }; Witch.prototype.getBroom = function(w, h) { background(0, 0, 0, 0); var w = 140; var h = 3; noStroke(); fill(0); rect(50, 10, w, h, 10); stroke(61, 40, 11); strokeWeight(1); noFill(); for(var i = 0; i < 20; i++) { var xOffset = random(10, 25); var yOffset = random(-15, 15); line(55, 11.5, 50-xOffset, 10 + yOffset); } return get(0, 0, w + 50, 20); }; } //Witch { //Cat Object - Inherits from Enemy var Cat = function(config) { Enemy.call(this, config); this.headColor = config.headColor || color(31, 29, 28); this.bodyColor = config.bodyColor || color(3, 3, 3); this.eyeColor = config.eyeColor || color(163, 230, 108); }; Cat.prototype = Object.create(Enemy.prototype); Cat.prototype.display = function() { pushMatrix(); translate(this.pos.x, this.pos.y); if(this.dir === -1) { scale(-1, 1); translate(-this.w, 0); } //body noStroke(); fill(this.headColor); rect(0, 0, this.w, this.h/2); fill(this.bodyColor); rect(0, this.h/2, this.w, this.h/2); //ears triangle(this.w * 0.1, 0, this.w * 0.4, 0, this.w * 0.2, -this.h * 0.3); triangle(this.w * 0.9, 0, this.w * 0.6, 0, this.w * 0.8, -this.h * 0.3); //eyes fill(this.eyeColor); //left beginShape(); vertex( this.w * 0.15 + 5, this.h * 0.25); bezierVertex( this.w * 0.2 + 5, this.h * 0.15, this.w * 0.3 + 5, this.h * 0.15, this.w * 0.4 + 5, this.h * 0.25); bezierVertex( this.w * 0.3 + 5, this.h * 0.35, this.w * 0.2 + 5, this.h * 0.35, this.w * 0.15 + 5, this.h * 0.25); endShape(CLOSE); //right beginShape(); vertex( this.w * 0.5 + 5, this.h * 0.25); bezierVertex( this.w * 0.55 + 5, this.h * 0.15, this.w * 0.65 + 5, this.h * 0.15, this.w * 0.75 + 5, this.h * 0.25); bezierVertex( this.w * 0.65 + 5, this.h * 0.35, this.w * 0.55 + 5, this.h * 0.35, this.w * 0.5 + 5, this.h * 0.25); endShape(CLOSE); //eye balls stroke(0); strokeWeight(1); line(this.w * 0.27 + 5, this.h * 0.15, this.w * 0.27 + 5, this.h * 0.35); line(this.w * 0.62 + 5, this.h * 0.15, this.w * 0.62 + 5, this.h * 0.35); //fangs noStroke(); fill(245, 242, 242); triangle(this.w * 0.35 + 4, this.h/2, this.w * 0.4 + 4, this.h * 0.65, this.w * 0.45 + 4, this.h/2); triangle(this.w * 0.55 + 4, this.h/2, this.w * 0.6 + 4, this.h * 0.65, this.w * 0.65 + 4, this.h/2); triangle(this.w * 0.45 + 4, this.h/2, this.w * 0.5 + 4, this.h * 0.65, this.w * 0.55 + 4, this.h/2); noStroke(); popMatrix(); }; Cat.prototype.update = function() { this.pos.x += this.speed * this.dir; if(random() < 0.005) { game.coins.push(new Bone(this.pos.x, 495)); } }; Cat.prototype.run = function() { this.update(); this.display(); }; //ZamikazeCat - Inhertis from Cat > Enemy var KamikazeCat = function(config) { Cat.call(this, config); this.timeToFall = config.timeToFall || random(200, 700); this.dropSpeed = config.dropSpeed || 7; this.yDir = 1; this.isOnGround = false; }; KamikazeCat.prototype = Object.create(Cat.prototype); KamikazeCat.prototype.drop = function() { if(!this.isOnGround) { this.timeToFall--; if(this.timeToFall < 0) { this.pos.y += this.dropSpeed; if(this.pos.y >= 470) { this.isOnGround = true; } } } this.display(); }; } //Cat { //Ghost Object - Inhertis from Enemy var Ghost = function(config) { Enemy.call(this, config); //this.bodyColor = color(240, 237, 235, 200); this.bodyR = 240; this.bodyG = 237; this.bodyB = 235; this.opacity = config.opacity || 150; this.eyeColor = config.eyeColor || color(38, 38, 38); this.theta = 0.0; this.amplitude = 20.0; this.dy = 0.0; this.ybase = this.pos.y; this.scale = config.scale || 1; }; Ghost.prototype = Object.create(Enemy.prototype); Ghost.prototype.display = function() { pushMatrix(); translate(this.pos.x, this.pos.y); if(this.dir === -1) { scale(-this.scale, this.scale); //scale(-1, 1); translate(-this.w, 0); } //new body noStroke(); fill(this.bodyR, this.bodyG, this.bodyB, this.opacity); beginShape(); vertex(this.w * 0.5, 0); bezierVertex(this.w * 0.9, 0, this.w, this.h * 0.25, this.w, this.h * 0.5); bezierVertex(this.w, this.h, this.w * 0.8, this.h * 1.2, this.w * 0.66, this.h * 0.66); bezierVertex(this.w * 0.66, this.h * 1.1, this.w * 0.33, this.h * 1.1, this.w * 0.33, this.h * 0.66); bezierVertex(this.w * 0.33, this.h, 0, this.h * 1.2, 0, this.h * 0.5); bezierVertex(0, this.h * 0.25, this.w * 0.1, 0, this.w * 0.5, 0); endShape(CLOSE); //eyes fill(this.eyeColor); ellipse(this.w/3 + this.speed, this.h/3.5, 4, 4); ellipse(this.w - this.w/3 + this.speed, this.h/3.5, 4, 4); popMatrix(); }; Ghost.prototype.update = function() { this.theta += 3; this.dy = sin(radians(this.theta)) * this.amplitude; this.pos.x += this.speed * this.dir; this.pos.y = this.ybase + this.dy; if(random() < 0.005) { game.coins.push(new Bone(this.pos.x, 495)); } }; Ghost.prototype.run = function() { this.update(); this.display(); }; //ZamikazeGhost - Inhertis from Ghost > Enemy var KamikazeGhost = function(config) { Ghost.call(this, config); this.timeToFall = config.timeToFall || random(200, 700); this.dropSpeed = config.dropSpeed || 5; this.isOnGround = false; this.amplitude = 30.0; this.ybase = this.pos.y; }; KamikazeGhost.prototype = Object.create(Ghost.prototype); KamikazeGhost.prototype.drop = function() { if(!this.isOnGround) { this.timeToFall--; if(this.timeToFall < 0) { this.pos.y += this.dropSpeed; if(this.pos.y >= 470) { this.isOnGround = true; } } } this.display(); }; } //Ghost { //Werewolf Object var Werewolf = function(config) { Enemy.call(this, config); this.isWolf = config.isWolf || random() < 0.5 ? true : false; this.manTimer = 0; this.wolfTimer = 0; this.transitionTime = random(100, 200); this.manSpeed = 2; this.wolfSpeed = 5; this.skinColor = color(215, 177, 112); this.wolfSkinColor = color(92, 48, 10); this.shirtColor = random() < 0.5 ? color(32, 49, 82) : color(59, 34, 5); this.hairColor = color(54, 31, 14); this.eyeColor = color(150, 23, 14); this.wolfEyeColor = color(245, 35, 8); }; Werewolf.prototype = Object.create(Enemy.prototype); Werewolf.prototype.display = function() { pushMatrix(); translate(this.pos.x, this.pos.y); if(this.dir === -1) { scale(-1, 1); translate(-this.w, 0); } if(this.isWolf) { //body noStroke(); fill(this.wolfSkinColor); rect(0, 0, this.w, this.h, 3); fill(this.hairColor); rect(0, this.h/2, this.w, this.h/2); //eyes fill(this.wolfEyeColor); ellipse(this.w/3 + this.speed, this.h/3.5, 4, 4); ellipse(this.w - this.w/3 + this.speed, this.h/3.5, 4, 4); //ears fill(this.hairColor); //right beginShape(); vertex(0, this.h * 0.1); vertex(-this.w * 0.2, -this.h * 0.1); bezierVertex(-this.w * 0.2, this.h * 0.15, -this.w * 0.2, this.h * 0.25, 0, this.h * 0.4); endShape(CLOSE); //left beginShape(); vertex(this.w, this.h * 0.1); vertex(this.w * 1.2, -this.h * 0.1); bezierVertex(this.w * 1.2, this.h * 0.15, this.w * 1.2, this.h * 0.25, this.w, this.h * 0.4); endShape(CLOSE); //fangs noStroke(); fill(245, 242, 242); triangle(this.w * 0.35 + 4, this.h/2, this.w * 0.4 + 4, this.h * 0.65, this.w * 0.45 + 4, this.h/2); triangle(this.w * 0.55 + 4, this.h/2, this.w * 0.6 + 4, this.h * 0.65, this.w * 0.65 + 4, this.h/2); //triangle(this.w * 0.45 + 4, this.h/2, this.w * 0.5 + 4, this.h * 0.65, this.w * 0.55 + 4, this.h/2); } else { //body noStroke(); fill(this.skinColor); rect(0, 0, this.w, this.h, 3); fill(this.shirtColor); rect(0, this.h/2, this.w, this.h/2); fill(this.hairColor); rect(-2, -2, this.w + 4, 3); //eyes fill(this.eyeColor); ellipse(this.w/3 + this.speed, this.h/3.5, 4, 4); ellipse(this.w - this.w/3 + this.speed, this.h/3.5, 4, 4); } popMatrix(); }; Werewolf.prototype.update = function() { if(this.isWolf) { this.pos.x += this.wolfSpeed * this.dir; if(this.wolfTimer++ >= this.transitionTime) { this.isWolf = false; this.wolfTimer = 0; } } else { this.pos.x += this.manSpeed * this.dir; if(this.manTimer++ >= this.transitionTime) { this.isWolf = true; this.manTimer = 0; } } if(random() < 0.005) { game.coins.push(new Bone(this.pos.x, 495)); } }; Werewolf.prototype.run = function() { this.update(); this.display(); }; } //Werewolf } //Enemies (Inherited) { //Player Object var Player = function(x, y, w, h) { this.startpos = new PVector(x, y); this.pos = new PVector(x, y); this.w = w; this.h = h; this.xs = 0; this.ys = 0; this.canJump = false; this.gravity = 0.5; this.jumpPower = 10; this.acceleration = 0.5; this.maxSpeed = 5; this.momentum = 0.5; this.armed = 0; //angle of the guns - 0 (loaded) or 70 (unloaded) this.fired = false; this.bulletsInit = 2; this.bullets = game.levels[game.level].bullets || this.bulletsInit; this.bullet = { pos: new PVector(0, 0), w: 5, h: 5, dir: 0, speed: 10 }; }; Player.prototype.display = function() { noStroke(); //body fill(215, 177, 112); rect(this.pos.x, this.pos.y, this.w, this.h, 3); fill(68, 112, 53); rect(this.pos.x, this.pos.y+this.h/2, this.w, this.h/2); //dark green spots (camo) fill(37, 78, 36); ellipse(this.pos.x + this.w/8, this.pos.y + this.h / 1.4, 7, 7); ellipse(this.pos.x + this.w/5, this.pos.y + this.h / 1.5, 5, 5); ellipse(this.pos.x + this.w/3, this.pos.y + this.h / 1.2, 9, 7); ellipse(this.pos.x + this.w/2, this.pos.y + this.h / 1.5, 9, 7); ellipse(this.pos.x + this.w/1.8, this.pos.y + this.h / 1.2, 5, 5); ellipse(this.pos.x + this.w/1.4, this.pos.y + this.h / 1.3, 5, 5); ellipse(this.pos.x + this.w/1.2, this.pos.y + this.h / 1.4, 9, 9); //eyes fill(28, 28, 28); //moving eyes in direction of player ellipse(this.pos.x + this.w/3 + this.xs/this.maxSpeed*this.w/6, this.pos.y + this.h/3.5, 5, 5); ellipse(this.pos.x + this.w - this.w/3 + this.xs/this.maxSpeed*this.w/6, this.pos.y + this.h/3.5, 5, 5); //helmet stroke(68, 112, 53); strokeWeight(4); line(this.pos.x, this.pos.y, this.pos.x + this.w-1, this.pos.y); noStroke(); //hands and guns... pushMatrix(); translate(this.pos.x + this.w/2, this.pos.y + this.h/1.8); //hands fill(215, 177, 112); ellipse(-this.w/2, this.h/6, 8, 8); ellipse(this.w/2, this.h/6, 8, 8); //guns // stroke(38, 38, 38); stroke(102, 102, 102); strokeWeight(4); //left gun pushMatrix(); translate(-this.w/2, this.h/10); rotate(radians(this.armed)); line(0, 0, -this.w/4, 0); popMatrix(); //right gun pushMatrix(); translate(this.w/2, this.h/10); rotate(radians(-this.armed)); line(0, 0, this.w/4, 0); popMatrix(); popMatrix(); noStroke(); if(this.fired) { this.bullet.pos.x+= this.bullet.speed * this.bullet.dir; // fill(8, 8, 8); fill(200, 200, 200); ellipse(this.bullet.pos.x, this.bullet.pos.y, this.bullet.w, this.bullet.h); } }; Player.prototype.setMovement = function() { var speed = 0.1; if(keys[RIGHT]) { this.xs = constrain(this.xs + this.acceleration, -this.maxSpeed, this.maxSpeed); } else if(keys[LEFT]) { this.xs = constrain(this.xs - this.acceleration, -this.maxSpeed, this.maxSpeed); } else { this.xs *= this.momentum; } if(this.canJump && keys[UP]) { this.ys = -this.jumpPower; } }; Player.prototype.update = function() { this.setMovement(); if(!this.canJump) { this.ys += this.gravity; } this.canJump = false; this.pos.y = constrain(this.pos.y + this.ys, 0, 550); this.pos.x = constrain(this.pos.x + this.xs, 0, 600 - this.w); if(this.pos.y + this.h > 500){ this.ys = 0; this.pos.y = 500 - this.h; this.canJump = true; } if(this.bullets > 0) { this.armed = 0; } else { this.armed = 70; } }; Player.prototype.shoot = function() { //Check if already fired - can only fire one bullet at a time if(this.fired === false) { //If have bullets then fire if(this.bullets > 0) { if(keyPressed && keyCode === 68) { //D - shoot right this.bullet.pos.x = this.pos.x + this.w + this.w/3; this.bullet.pos.y = this.pos.y + this.h/1.6; this.bullet.dir = 1; this.fired = true; this.bullets--; keyCode = 0; } else if(keyPressed && keyCode === 65) { //A - shoot left this.bullet.pos.x = this.pos.x - this.w/3; this.bullet.pos.y = this.pos.y + this.h/1.6; this.bullet.dir = -1; this.fired = true; this.bullets--; keyCode = 0; } } } else { //Check if killed enemy or gone off the screen for(var i = game.enemies.length - 1; i >= 0; i--) { var enemy = game.enemies[i]; if( this.bullet.pos.x + this.bullet.w > enemy.pos.x && this.bullet.pos.x < enemy.pos.x + enemy.w && this.bullet.pos.y + this.bullet.h > enemy.pos.y && this.bullet.pos.y < enemy.pos.y + enemy.h) { game.enemies.splice(i, 1); game.score+= game.levels[game.level].enemyPoints; game.enemiesKilled++; this.fired = false; break; } } //If bullet goes off the screen then able to fire again if(this.bullet.pos.x < 0 || this.bullet.pos.x > width) { this.fired = false; } } }; Player.prototype.run = function() { this.update(); this.shoot(); this.display(); }; } //Player { //Game Object var Game = function() { this.page = "home"; //Start on the home page this.level = 0; //Start on level 0 (home) this.levels = [ { //Home | Levels | How | Scores groundColors: [color(119, 22, 22), color(29, 29, 29), color(38, 38, 38), color(50, 50, 50), color(38, 38, 38.........完整代码请登录后点击上方下载按钮下载查看
网友评论0