canvas+processing实现一个牛顿重力小游戏代码

代码语言:html

所属分类:游戏

代码描述:canvas+processing实现一个牛顿重力小游戏代码,使用键盘上“向上”或“W”键跳转,左键或“A”键向左移动,右键或“D”键向右移动,您需要避免撞击重力向您投掷的炸弹、砖块和物体。您持续时间越长,获得的积分越多。开始时相当简单,但您很快就会发现它并不是这样。在游戏期间按P暂停/继续。让我知道您在评论中的高分是多少。祝你好运,小牛顿,暴风雨就要来了!

代码标签: canvas processing 牛顿 重力 游戏 代码

下面为部分代码预览,完整代码请点击下载或在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();
    
textFont(createFont("verdana"));

{
    //Key|Button stuff
    var clicked = false;
    var hover = false;
    var keys = [];
    keyPressed = function () {
        keys[keyCode] = true;
    };
    keyReleased = function () {
        keys[keyCode] = false;
    };
    mouseClicked = function () {
        clicked = true;
    };
} //Keys/Mouse

var Game = function(config) {
    this.page = "home";
    this.base = 570;
    
    this.titleFont = createFont("arial black");
    
    this.frequencyMax = 240;
    this.frequencyMin = 100;
    this.frequency = this.frequencyMax;
    this.frequencyTimer = 0;
    
    this.colors = [
        { //red
            back: color(205, 20, 54),
            front: color(237, 64, 66),
            brick: color(244, 120, 82)
        },
        { //blue
            back: color(84, 133, 229),
            front: color(94, 148, 255),
            brick: color(126, 169, 255)
        },
        { //green
            back: color(125, 181, 169),
            front: color(139, 201, 77),
            brick: color(162, 212, 113)
        },
        { //purple
            back: color(144, 101, 229),
            front: color(160, 113, 225),
            brick: color(179, 141, 255)
        }
    ];
    this.theme = 0;
    
    this.bricksRepository = [];
    this.bricks = [];
    this.brickPieces = [];
    
    this.enemiesRepository = [];
    this.enemies = [];
    
    this.bombsRepository = [];
    this.bombs = [];
    this.bombPiecesRepository = [];
    this.bombPieces = [];
    
    this.medkitsRepository = [];
    this.medkits = [];
    
    this.clouds = [];
    this.rain = [];
    this.drops = 0;
    
    this.storm = 0;
    
    this.score = 0;
    this.highscore = 0;
    this.health = 100;
    
    this.shake = 0;
    this.shakedown = 0.2;
    
    this.bombPieceCoords = [
        {
            x: 0,
            y: 0
        },
        {
            x: -0.5,
            y: 0 
        },
        {
            x: 0.5,
            y: 0 
        },
        {
            x: 0,
            y: -0.5 
        },
        {
            x: 0,
            y: 0.5 
        },
        {
            x: 0.25,
            y: 0.25 
        },
        {
            x: 0.25,
            y: -0.25 
        },
        {
            x: -0.25,
            y: 0.25 
        },
        {
            x: -0.25,
            y: -0.25 
        }
    ];
    
    this.leaderboardArr = [
        {
            user: "Could be you",
            score: 0
        },
        {
            user: "Could be you",
            score: 0
        },
        {
            user: "Could be you",
            score: 0
        },
        {
            user: "Could be you",
            score: 0
        },
        {
            user: "Could be you",
            score: 0
        },
        {
            user: "Could be you",
            score: 0
        },
        {
            user: "Could be you",
            score: 0
        },
        {
            user: "Could be you",
            score: 0
        },
        {
            user: "Could be you",
            score: 0
        },
        {
            user: "Could be you",
            score: 0
        }
    ];
    this.leaderboardArr.sort(function(a, b) {
       return b.score - a.score; 
    });
};
var game = new Game({});

{
    var Button = function (config) {
        this.pos = config.pos || new PVector(0, 0);
        this.size = config.size || 70;
        this.content = config.content || "Home";
        this.page = config.page || "home";
        this.textSize = config.textSize || this.size / 5;
        this.borderColor = color(12, 31, 3, 20);
        this.backColor = game.colors[game.theme].back;
        this.textColor = color(245, 242, 242);
        this.backColorHover = game.colors[game.theme].front;
        this.textColorHover = color(245, 242, 242);
        this.growth = 0;
    };

    Button.prototype.draw = function () {
        pushStyle();
        textAlign(CENTER, CENTER);
        textSize(this.textSize + (this.growth * 0.1));
        noStroke();

        //shadow
        fill(20, 20, 20, 30);
        ellipse(this.pos.x, this.pos.y + this.size * 0.52, (this.size + this.growth) * 0.8, (this.size + this.growth) * 0.3);

        //circles
        if (dist(mouseX, mouseY, this.pos.x, this.pos.y) <= this.size / 2) { //hover
            this.growth = constrain(this.growth + 0.5, 0, 10);
            if (clicked) {
                game.page = this.page;
                if(game.page === "home") {
                    game.player.x = 300;
                    game.player.y = game.base;
                    game.boss.x = 280;
                    game.boss.y = 54;
                }
                if(game.page === "play" || game.page === "replay") {
                    game.reset();
                }
            }

            fill(this.backColorHover);
            stroke(this.borderColor);
            ellipse(this.pos.x, this.pos.y, this.size + this.growth, this.size + this.growth);
            fill(this.textColorHover);
            switch(this.content) {
                case "Play":
                    triangle(this.pos.x + this.size*0.25, this.pos.y, this.pos.x - this.size*0.15, this.pos.y - this.size*0.25, this.pos.x - this.size*0.15, this.pos.y + this.size*0.25);
                    break;
                case "How":
                    pushStyle();
                        textSize(this.size*0.6);
                        text("?", this.pos.x, this.pos.y);
                    popStyle();
                    break;
                case "Story":
                    pushStyle();
                        noFill();
                        stroke(this.textColorHover);
                        strokeWeight(4);
                        line(this.pos.x-this.size*0.23, this.pos.y-this.size*0.2, this.pos.x+this.size*0.23, this.pos.y-this.size*0.2);
                        line(this.pos.x-this.size*0.23, this.pos.y, this.pos.x+this.size*0.23, this.pos.y);
                        line(this.pos.x-this.size*0.23, this.pos.y+this.size*0.2, this.pos.x+this.size*0.23, this.pos.y+this.size*0.2);
                    popStyle();
                    break;
                case "Themes":
                    arc(this.pos.x, this.pos.y, this.size + this.growth, this.size + this.growth, radians(271), radians(450));
                    break;
                case "Back":
                    pushStyle();
                    beginShape();
                        vertex(this.pos.x+this.size*0.25, this.pos.y); //1
                        vertex(this.pos.x+this.size*0.25, this.pos.y+this.size*0.25); //2
                        vertex(this.pos.x+this.size*0.07, this.pos.y+this.size*0.25); //3
                        vertex(this.pos.x+this.size*0.07, this.pos.y+this.size*0.12); //4
                        vertex(this.pos.x-this.size*0.07, this.pos.y+this.size*0.12); //5
                        vertex(this.pos.x-this.size*0.07, this.pos.y+this.size*0.25); //6
                        vertex(this.pos.x-this.size*0.25, this.pos.y+this.size*0.25); //7
                        vertex(this.pos.x-this.size*0.25, this.pos.y); //8
                        vertex(this.pos.x, this.pos.y-this.size*0.2); //9
                        vertex(this.pos.x+this.size*0.25, this.pos.y); //10
                    endShape();
                    noFill();
                    stroke(this.textColorHover);
                    strokeWeight(this.size*0.05);
                    line(this.pos.x-this.size*0.27, this.pos.y-this.size*0.05, this.pos.x, this.pos.y-this.size*0.27);
                    line(this.pos.x+this.size*0.27, this.pos.y-this.size*0.05, this.pos.x, this.pos.y-this.size*0.27);
                    line(this.pos.x+this.size*0.15, this.pos.y-this.size*0.19, this.pos.x+this.size*0.15, this.pos.y-this.size*0.25);
                    popStyle();
                    break;
                case "Leaderboard":
                    pushStyle();
                        noFill();
                        stroke(this.textColorHover);
                        strokeWeight(this.size * 0.14);
                        strokeCap(SQUARE);
                        
                        line(this.pos.x, this.pos.y + this.size * 0.25, this.pos.x, this.pos.y - this.size * 0.3);
                        line(this.pos.x - this.size * 0.2, this.pos.y + this.size * 0.25, this.pos.x - this.size * 0.2, this.pos.y - this.size * 0.1);
                        line(this.pos.x + this.size * 0.2, this.pos.y + this.size * 0.25, this.pos.x + this.size * 0.2, this.pos.y - this.size * 0.2);
                    popStyle();
                    break;
                case "Replay":
                    pushStyle();
                        noFill();
                        stroke(this.textColorHover);
                        strokeWeight(5);
                        pushMatrix();
                            translate(this.pos.x, this.pos.y);
                            rotate(radians(frameCount * 5));
                            arc(0, 0, this.size * 0.6, this.size * 0.6, 0, radians(275));
                            noStroke();
                            fill(this.textColorHover);
                            translate(this.size * 0.30, -this.size * 0.18);
                            rotate(radians(-70));
                            triangle(0, -this.size * 0.1, -this.size * 0.14, -this.size * 0.3, this.size * 0.14, -this.size * 0.3);
                        popMatrix();
                    popStyle();
                    break;
                default:
                    text(this.content, this.pos.x, this.pos.y);
            }
        }
        else { //not hover
            this.growth = constrain(this.growth - 0.5, 0, 10);
            fill(this.backColor);
            stroke(this.borderColor);
            strokeWeight(2);
            noStroke();
            ellipse(this.pos.x, this.pos.y, this.size + this.growth, this.size + this.growth);
            fill(this.textColor);
            switch(this.content) {
                case "Play":
                    triangle(this.pos.x + this.size*0.25, this.pos.y, this.pos.x - this.size*0.15, this.pos.y - this.size*0.25, this.pos.x - this.size*0.15, this.pos.y + this.size*0.25);
                    break;
                case "How":
                    pushStyle();
                        textSize(this.size*0.6);
                        text("?", this.pos.x, this.pos.y);
                    popStyle();
                    break;
                case "Story":
                    pushStyle();
                        noFill();
                        stroke(this.textColor);
                        strokeWeight(4);
                        line(this.pos.x-this.size*0.23, this.pos.y-this.size*0.2, this.pos.x+this.size*0.23, this.pos.y-this.size*0.2);
                        line(this.pos.x-this.size*0.23, this.pos.y, this.pos.x+this.size*0.23, this.pos.y);
                        line(this.pos.x-this.size*0.23, this.pos.y+this.size*0.2, this.pos.x+this.size*0.23, this.pos.y+this.size*0.2);
                    popStyle();
                    break;
                case "Themes":
                    arc(this.pos.x, this.pos.y, this.size, this.size, radians(271), radians(450));
                    break;
                case "Back":
                    pushStyle();
                    beginShape();
                        vertex(this.pos.x+this.size*0.25, this.pos.y); //1
                        vertex(this.pos.x+this.size*0.25, this.pos.y+this.size*0.25); //2
                        vertex(this.pos.x+this.size*0.07, this.pos.y+this.size*0.25); //3
                        vertex(this.pos.x+this.size*0.07, this.pos.y+this.size*0.12); //4
                        vertex(this.pos.x-this.size*0.07, this.pos.y+this.size*0.12); //5
                        vertex(this.pos.x-this.size*0.07, this.pos.y+this.size*0.25); //6
                        vertex(this.pos.x-this.size*0.25, this.pos.y+this.size*0.25); //7
                        vertex(this.pos.x-this.size*0.25, this.pos.y); //8
                        vertex(this.pos.x, this.pos.y-this.size*0.2); //9
                        vertex(this.pos.x+this.size*0.25, this.pos.y); //10
                    endShape();
                    noFill();
                    stroke(this.textColor);
                    strokeWeight(this.size*0.05);
                    line(this.pos.x-this.size*0.27, this.pos.y-this.size*0.05, this.pos.x, this.pos.y-this.size*0.27);
                    line(this.pos.x+this.size*0.27, this.pos.y-this.size*0.05, this.pos.x, this.pos.y-this.size*0.27);
                    line(this.pos.x+this.size*0.15, this.pos.y-this.size*0.19, this.pos.x+this.size*0.15, this.pos.y-this.size*0.25);
                    popStyle();
                    break;
                case "Leaderboard":
                    pushStyle();
                        noFill();
                        stroke(this.textColor);
                        strokeWeight(this.size * 0.14);
                        strokeCap(SQUARE);
                        
                        line(this.pos.x, this.pos.y + this.size * 0.25, this.pos.x, this.pos.y - this.size * 0.3);
                        line(this.pos.x - this.size * 0.2, this.pos.y + this.size * 0.25, this.pos.x - this.size * 0.2, this.pos.y - this.size * 0.1);
                        line(this.pos.x + this.size * 0.2, this.pos.y + this.size * 0.25, this.pos.x + this.size * 0.2, this.pos.y - this.size * 0.2);
                    popStyle();
                    break;
                case "Replay":
                    pushStyle();
                        noFill();
                        stroke(this.textColor);
                        strokeWeight(5);
                        pushMatrix();
                            translate(this.pos.x, this.pos.y);
                            rotate(radians(sin(radians(frameCount * 5)) * 20));
                            arc(0, 0, this.size * 0.6, this.size * 0.6, 0, radians(275));
                            noStroke();
                            fill(this.textColor);
                            translate(this.size * 0.30, -this.size * 0.18);
                            rotate(radians(-70));
                            triangle(0, -this.size * 0.1, -this.size * 0.14, -this.size * 0.3, this.size * 0.14, -this.size * 0.3);
                        popMatrix();
                    popStyle();
                    break;
                default:
                    text(this.content, this.pos.x, this.pos.y);
            }
        }

        popStyle();
    };
} //Buttons

var Block = function(config) {
    this.x = config.x || 0;
    this.y = config.y || 0;
    this.w = config.w || 40;
    this.h = config.h || 40;
    this.vx = random(-2, 2);
    this.vy = random(-5, -3);
    this.gravity = 0.3;
    this.used = config.used || false;
    this.timer = 300;
    this.opacity = 255;
    this.shaking = false;
    
    this.draw = function() {
        //override this function
    };
    
    this.run = function() {
        this.update();
        this.draw();
    };
};
Block.prototype.init = function() {
    this.x = game.boss.x + game.boss.w * 0.5 - this.w * 0.5;
    this.y = game.boss.y + game.boss.h * 0.5 - this.h * 0.5;
    this.vy = random(-5, -3);
    this.vx = random(-2, 2);
    this.opacity = 255;
    this.y += this.vy;
    this.timer = 300;
};
Block.prototype.update = function() {
    this.x += this.vx;
    this.vy += this.gravity;
    this.y = constrain(this.y + this.vy, -100, game.base - this.h);
};

var BrickPiece = function (config) {
    Block.call(this, config);
    
    this.image = config.image;

    this.draw = function() {
        noStroke();
        
        pushMatrix();
            translate(this.x, this.y);
        
            image(this.image, 0, 0, this.w, this.h);
        
        popMatrix();
    };
    
    this.run = function() {
        this.update();
        this.draw();
    };
};
BrickPiece.prototype = Object.create(Block.prototype);
BrickPiece.prototype.init = function() {
    Block.prototype.init.call(this);
};
BrickPiece.prototype.update = function() {
    Block.prototype.update.call(this);
};

var Brick = function (config) {
    Block.call(this, config);

    this.draw = function() {
        noStroke();
        
        pushMatrix();
            translate(this.x, this.y);
        
            image(game.images.brick, 0, 0, this.w, this.h);
        
        popMatrix();
    };
    
    this.run = function() {
        this.update();
        this.draw();
    };
    
};
Brick.prototype = Object.create(Block.prototype);
Brick.prototype.init = function() {
    Block.prototype.init.call(this);
};
Brick.prototype.update = function() {
    Block.prototype.update.call(this);
};
Brick.prototype.getImage = function() {
    noStroke();
    
    pushMatrix();
        translate(this.x, this.y);
    
        fill(game.colors[game.theme].back, this.opacity);
        rect(0, 0, this.w, this.h);
        
        fill(game.colors[game.theme].front, this.opacity);
        rect(this.w * 0.1, this.h * 0.1, this.w * 0.8, this.h * 0.8);
        
        fill(game.colors[game.theme].brick, this.opacity);
        rect(this.w * 0.25, this.h * 0.25, this.w * 0.3, this.h * 0.15);
        rect(this.w * 0.45, this.h * 0.65, this.w * 0.3, this.h * 0.15);
        
        if(this.timer < 300) {
            pushStyle();
                fill(0);
                textAlign(CENTER, CENTER);
                textSize(12);
                text(this.timer, this.w * 0.5, this.h * 0.5);
            popStyle();
        }
    
    popMatrix();
};

var Enemy = function (config) {
    Block.call(this, config);
    
    this.moving = false;
    this.dir = 0;
    this.flip = -1;
    this.scale = 1;
    
    this.hit = false;
    this.angle = 0;
    
    this.image = config.image;

    this.draw = function() {
        noStroke();
        
        pushMatrix();
            translate(this.x + this.w * 0.5, this.y + this.h * 0.5);
            scale(this.flip * this.scale, this.scale);
            if(this.hit) {
                rotate(radians(this.angle++));
            }
            translate(-this.w * 0.5, -this.h * 0.5);
            
            if(this.vx < 0) {
                this.flip = 1;
                image(this.image, 0, 0, this.w, this.h);
            }
            else {
                this.flip = -1;
                image(this.image, 0, 0, this.w, this.h);
            }
        popMatrix();
    };
    
    this.run = function() {
        this.update();
        this.draw();
    };
    
};
Enemy.prototype = Object.create(Block.prototype);
Enemy.prototype.init = function() {
    Block.prototype.init.call(this);
    this.moving = false;
    this.hit = false;
    this.angle = 0;
};
Enemy.prototype.update = function() {
    //Block.prototype.update.call(this);
    this.x += this.vx;
    this.vy += this.gravity;
    if(this.hit === false) {
        this.y = constrain(this.y + this.vy, -100, game.base - this.h);
    }
    else {
        this.y += this.vy;
    }
};

var Bomb = function (config) {
    Block.call(this, config);
    
    this.corner = 50;
    this.wick = this.h * 0.5;
    this.hitRadius = 200;
    
    this.draw = function() {
        noStroke();
        
        pushMatrix();
            translate(this.x + this.w * 0.5, this.y + this.h * 0.5);
            
            pushStyle();
                noFill();
                stroke(240, 240, 240);
                strokeWeight(2);
                line(0, -this.h * 0.5 - this.wick, 0, -this.h * 0.5);
                
                if(this.wick > 0) {
                    noStroke();
                    fill(242, 49, 24);
                    ellipse(0, -this.h * 0.5 - this.wick, random(6, 12), random(6, 12));
                    fill(250, 136, 15);
                    ellipse(0, -this.h * 0.5 - this.wick, random(4, 8), random(4, 8));
                }
                
                noStroke();
                fill(46, 44, 43);

                rect(-this.w * 0.1, -this.h * 0.6, this.w * 0.2, this.h * 0.2);
                ellipse(0, 0, this.w, this.h);

                fill(100, 100, 100, 20);
                ellipse(0, 0, this.hitRadius, this.hitRadius);
            popStyle();
        popMatrix();
    };
    
    this.run = function() {
        this.update();
        this.draw();
    };
    
};
Bomb.prototype = Object.create(Block.prototype);
Bomb.prototype.init = function() {
    Block.prototype.init.call(this);
    this.wick = this.h * 0.5;
};
Bomb.prototype.update = function() {
    Block.prototype.update.call(this);
};

var BombPiece = function (config) {
    Block.call(this, config);
    
    this.rotation = true;

    this.draw = function() {
        noStroke();
        
        pushMatrix();
            translate(this.x + this.w * 0.5, this.y + this.h * 0.5);
            if(this.rotation) {
                rotate(radians(frameCount));
            }
            translate(-this.w * 0.5, -this.h * 0.5);
            
            pushStyle();
                fill(46, 44, 43, this.opacity);
                rect(0, 0, this.w, this.h);
            popStyle();
        popMatrix();
    };
    
    this.run = function() {
        this.update();
        this.draw();
    };
    
};
BombPiece.prototype = Object.create(Block.prototype);
BombPiece.prototype.init = function(x, y) {
    this.x = x;
    this.y = y;
    this.vy = random(-10, -5);
    this.vx = random(-5, 5);
    this.opacity = 255;
    this.y += this.vy;
    this.rotation = true;
};
BombPiece.prototype.update = function() {
    Block.prototype.update.call(this);
};

var MedKit = function (config) {
    Block.call(this, config);
    
    this.draw = function() {
        noStroke();
        
        pushMatrix();
            translate(this.x, this.y);
            
            pushStyle();
                fill(255, 255, 255);
                rect(0, 0, this.w, this.h);
                fill(240, 65, 65);
                ellipse(this.w * 0.5, this.h * 0.5, this.w * 0.8, this.h * 0.8);
                fill(255, 255, 255);
                rectMode(CENTER);
                rect(this.w * 0.5, this.h * 0.5, this.w * 0.5, this.h * 0.2);
                rect(this.w * 0.5, this.h * 0.5, this.w * 0.2, this.h * 0.5);

                if(this.timer < 300) {
                    pushStyle();
                        fill(255);
                        textAlign(CENTER, CENTER);
                        textSize(12);
                        text(this.timer, 0, 0);
                    popStyle();
                }
            popStyle();
        
        popMatrix();
    };
    
    this.run = function() {
        this.update();
        this.draw();
    };
    
};
MedKit.prototype = Object.create(Block.prototype);
MedKit.prototype.init = function() {
    Block.prototype.init.call(this);
    this.x = random(0, width - this.w);
    this.y = -this.h;
    this.vx = 0;
};
MedKit.prototype.update = function() {
    Block.prototype.update.call(this);
};

var Building = function(config) {
    this.x = config.x;
    this.y = config.y;
    this.w = config.w;
    this.h = config.h;
    this.scale = config.scale || 1;

    this.theme = 0;
    
    this.back = game.colors[game.theme].back;
    this.front = game.colors[game.theme].front;
    this.brick = game.colors[game.theme].brick;
    
    this.window = function(x, y) {
        noStroke();

        pushMatrix();
            translate(x, y);
            fill(255, 255, 255);
            rect(0, 0, this.w * 0.18, this.h * 0.15);
            fill(28, 172, 172);
            rect(0, 0, this.w * 0.15, this.h * 0.135);
            
            //shading in window
            fill(160, 218, 218);
            beginShape();
                vertex(-this.w * 0.075, -this.h * 0.041);
                vertex(-this.w * 0.029, -this.h * 0.067);
                vertex(this.w * 0.075, -this.h * 0.067);
                vertex(this.w * 0.075, -this.h * 0.050);
                vertex(-this.w * 0.075, this.h * 0.040);
            endShape(CLOSE);
            beginShape();
                vertex(-this.w * 0.075, this.h * 0.055);
                vertex(this.w * 0.075, -this.h * 0.034);
                vertex(this.w * 0.075, this.h * 0.006);
                vertex(-this.w * 0.030, this.h * 0.069);
                vertex(-this.w * 0.075, this.h * 0.069);
            endShape(CLOSE);
            
            fill(255, 255, 255);
            rect(0, 0, this.w * 0.015, this.h * 0.15);
            
            //sill
            fill(this.brick);
            rect(0, this.h * 0.085, this.w * 0.2, this.h * 0.02);
            
            //shadow under sill
            fill(this.back, 150);
            rect(0, this.h * 0.12, this.w * 0.18, this.h * 0.05);
        popMatrix();
    };
    
    this.balcony = function(x, y, s) {
        noStroke();

        pushMatrix();
            translate(x, y);
            scale(s, 1);
            fill(this.front);
            rect(-this.w * 0.225, 0, this.w * 0.15, this.h * 0.025);
            fill(this.back);
            rect(-this.w * 0.275, 0, this.w * 0.075, this.h * 0.025);
            fill(182, 144, 193);
            rect(-this.w * 0.225, -this.h * 0.07, this.w * 0.15, this.h * 0.01);
            rect(-this.w * 0.168, -this.h * 0.039, this.w * 0.01, this.h * 0.052);
            rect(-this.w * 0.204, -this.h * 0.039, this.w * 0.01, this.h * 0.052);
            rect(-this.w * 0.244, -this.h * 0.039, this.w * 0.01, this.h * 0.052);
        popMatrix();
            
    };
    
    this.draw = function() {
        background(0, 0, 0, 0);
        
        pushMatrix();
            translate(this.x, this.y);
            scale(this.scale);
            pushStyle();
                rectMode(CENTER);
                noStroke();
                
                //balconies
                //right
                this.balcony(this.w * 0.8, -this.h * 0.770, 1);
                this.balcony(this.w * 0.8, -this.h * 0.520, 1);
                this.balcony(this.w * 0.8, -this.h * 0.270, 1);
                //left
                this.balcony(-this.w * 0.8, -this.h * 0.770, -1);
                this.balcony(-this.w * 0.8, -this.h * 0.520, -1);
                this.balcony(-this.w * 0.8, -this.h * 0.270, -1);
                
                //back of building
                fill(this.back);
                rect(0, -this.h * 0.5, this.w, this.h);
                
                //front of building
                fill(this.front);
                rect(0, -this.h * 0.475, this.w * 0.9, this.h * 0.94);
                
                //top of building
                fill(this.front);
                rect(0, -this.h * 1.02, this.w * 1.1, this.h * 0.04);
                
                //windows
                //top row
                this.window(-this.w * 0.25, -this.h * 0.85);
                this.window(0, -this.h * 0.85);
                this.window(this.w * 0.25, -this.h * 0.85);
                
                //middle row
                this.window(-this.w * 0.25, -this.h * 0.6);
                this.window(0, -this.h * 0.6);
                this.window(this.w * 0.25, -this.h * 0.6);
                
                //bottom row
                this.window(-this.w * 0.25, -this.h * 0.35);
                this.window(0, -this.h * 0.35);
                this.window(this.w * 0.25, -this.h * 0.35);
                
                //door
                fill(255, 255, 255);
                rect(0, -this.h * 0.08, this.w * 0.2, this.h * 0.16);
                fill(28, 172, 172);
                rect(0, -this.h * 0.08, this.w * 0.18, this.h * 0.15);
                fill(this.brick);
                rect(0, -this.h * 0.16, this.w * 0.3, this.h * 0.025);
                fill(this.front, 120);
                rect(0, -this.h * 0.135, this.w * 0.3, this.h * 0.025);
                
                //shadow on door
                fill(160, 218, 218);
                beginShape();
                    vertex(-this.w * 0.087, 0);
                    vertex(this.w * 0.089, -this.h * 0.116);
                    vertex(this.w * 0.089, -this.h * 0.095);
                    vertex(-this.w * 0.058, 0);
                endShape(CLOSE);
                triangle(-this.w * 0.038, 0, this.w * 0.089, -this.h * 0.081, this.w * 0.089, 0);
                
                //bottom of building
                fill(this.back);
                rect(0, 0, this.w * 1.1, this.h * 0.02);
                
                //sides of building
                for(var i = 0; i < 15; i++) {
                    //right
                    fill(this.front);
                    rect(this.w * 0.475, -this.h * 0.06 - i * 30, this.w * 0.05, this.h * 0.02);
                    fill(this.brick);
                    rect(this.w * 0.485, -this.h * 0.045 - i * 30, this.w * 0.07, this.h * 0.03);
                    
                    //left
                    fill(this.front);
                    rect(-this.w * 0.475, -this.h * 0.06 - i * 30, this.w * 0.05, this.h * 0.02);
                    fill(this.brick);
                    rect(-this.w * 0.485, -this.h * 0.045 - i * 30, this.w * 0.07, this.h * 0.03);
                }
                
                //top squares
                fill(this.brick);
                rect(this.w * 0.485, -this.h * 0.984, this.w * 0.07, this.h * 0.03);
                rect(-this.w * 0.485, -this.h * 0.984, this.w * 0.07, this.h * 0.03);
                
                //bricks (random)
                fill(this.brick);
                rect(this.w * 0.395, -this.h * 0.923, this.w * 0.055, this.h * 0.0175);
                rect(this.w * 0.395, -this.h * 0.702, this.w * 0.055, this.h * 0.0175);
                rect(this.w * 0.355, -this.h * 0.089, this.w * 0.055, this.h * 0.0175);
                rect(this.w * 0.313, -this.h * 0.133, this.w * 0.055, this.h * 0.0175);
                rect(this.w * 0.228, -this.h * 0.060, this.w * 0.055, this.h * 0.0175);
                rect(this.w * -0.328, -this.h * 0.073, this.w * 0.055, this.h * 0.0175);
                rect(this.w * -0.182, -this.h * 0.094, this.w * 0.055, this.h * 0.0175);
                rect(this.w * -0.354, -this.h * 0.164, this.w * 0.055, this.h * 0.0175);
                rect(this.w * 0.125, -this.h * 0.204, this.w * 0.055, this.h * 0.0175);
                rect(this.w * -0.125, -this.h * 0.416, this.w * 0.055, this.h * 0.0175);
                rect(this.w * -0.125, -this.h * 0.639, this.w * 0.055, this.h * 0.0175);
                rect(this.w * -0.125, -this.h * 0.908, this.w * 0.055, this.h * 0.0175);
                rect(this.w * -0.398, -this.h * 0.845, this.w * 0.055, this.h * 0.0175);
                rect(this.w * 0.126, -this.h * 0.721, this.w * 0.055, this.h * 0.0175);
            popStyle();
        popMatrix();
    };
};
var Boss = function(config) {
    this.x = config.x;
    this.y = config.y;
    this.w = 92;
    this.h = 98;
    this.flip = -1;
    this.dir = 1;
    this.vx = 1;
    this.scale = 1;
    
    this.image = config.image;
    
    this.getImage = function() {
        pushMatrix();
            translate(11, 10);
            scale(1.2);
            
            noStroke();
            
            //leg back
            fill(0, 23, 33);
            rect(14, 49, 13, 23);
            //arm back
            fill(0, 135, 144);
            rect(-8, 17, 15, 37, 50);
            //antenna back
            fill(95, 191, 231);
            rect(5, -8, 11, 22, 50);
            //body
            fill(15, 51, 66);
            rect(0, 0, 60, 60, 20);
            //leg front
            rect(34, 49, 13, 23);
            //eyes
            fill(206, 220, 73);
            rect(10, 10, 9, 4, 50);
            rect(28, 10, 9, 4, 50);
            //mouth
            fill(1, 176, 174);
            rect(4, 21, 43, 20, 5);
            //arm front
            rect(52, 17, 15, 37, 50);
            //antenna front
            fill(168, 210, 233);
            rect(41, -8, 11, 22, 50);
            //teeth
            fill(94, 198, 217);
            //teeth top
            for(var i = 0; i < 5; i++) {
                rect(8 + i * 7, 21, 5, 6, 0, 0, 50, 50);
                rect(8 + i * 7, 35, 5, 6, 50, 50, 0, 0);
            }
        popMatrix();
    };
    
    this.update = function() {
        this.x += this.vx * this.dir;
        
        if(this.x < 180 || this.x + this.w > 420) {
            this.dir*= -1;
            this.flip*= -1;
        }
    };
    
    this.draw = function() {
        noStroke();
        
        pushMatrix();
            translate(this.x + this.w * 0.5, this.y + this.h * 0.5);
            scale(this.flip * this.scale, this.scale);
            translate(-this.w * 0.5, -this.h * 0.5);
        
            image(this.image, 0, 0, this.w, this.h);
        popMatrix();
    };
    
    this.run = function() {
        this.update();
        this.draw();
    };
};
var Player = function(config) {
    this.x = config.x;
    this.y = config.y;
    this.w = config.w;
    this.h = config.h;
    this.vx = 0;
    this.vy = 0;
    this.vamount = 0.5;
    this.vmax = 8;
    this.friction = 10;
    this.gravity = 0.5;
    this.dir = 0;
    this.jump = true;
    
    this.flip = -1;
    this.scale = 1;
    
    this.hitboxTop = {};
    this.hitboxBottom = {};
    
    this.imageFront = config.imageFront;
    this.imageSide = config.imageSide;
    
    this.getImageFront = function() {
        pushMatrix();
            translate(7, 1);
            scale(0.70);

            //ears
            noStroke();
            fill(238, 146, 125);
            rect(-8, 25, 96, 10, 20);
            //face dark
            fill(244, 174, 138);
            rect(0, 0, 80, 50, 20);
            //face light
            fill(246, 192, 156);
            rect(0, 0, 80, 40, 20);
            //glasses
            fill(30, 64, 73);
            rect(-3, 15, 86, 10);
            ellipse(22, 20, 30, 30);
            ellipse(58, 20, 30, 30);
            //inner eyes
            fill(255, 255, 255);
            ellipse(22, 20, 15, 15);
            ellipse(58, 20, 15, 15);
            fill(164, 206, 58);
            arc(22, 20, 1.........完整代码请登录后点击上方下载按钮下载查看

网友评论0