processing+canvas实现形状入侵射击闯关类小游戏代码

代码语言:html

所属分类:游戏

代码描述:processing+canvas实现形状入侵射击闯关类小游戏代码,按住键盘左右方向键控制飞船左右移动,按住键盘d键发射子弹攻击目标,按键盘p键暂停游戏,每一关卡有三次复活机会,最后一关还有大boss。

代码标签: 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: #111;
          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();
            
        /*  ADD YOUR OWN LEVELS HERE (Boss level must be the last one)
            Must be 10 columns x 9 rows
            X = Blank space
            Y = Barrier (blocks)
            1, 2, 3, 4 = Enemies
            P = Portal (random portal enemies emerge from portals)
            B = Boss (random boss enemies emerge from the boss)
        */
        var levels = [
            {}, //start screen (Home | Levels | How | Scores | etc.)
            {
                grid: [
                    "XXXXXXXXXX",
                    "XXXXXXXXXX",
                    "X11111111X",
                    "X11111111X",
                    "X11111111X",
                    "XXXXXXXXXX",
                    "XXXXXXXXXX",
                    "XXXXXXXXXX",
                    "XYYXYYXYYX"
                ],
                enemyPoints: 10,
                story: ("Your mission is to destroy the enemy")
            }, //Level 1
            {
                grid: [
                    "XXXXXXXXXX",
                    "X2121P12XX",
                    "XX1212121X",
                    "X2121212XX",
                    "XX1212121X",
                    "XXXXXXXXXX",
                    "YXXXXXXXXY",
                    "XXXXXXXXXX",
                    "YXXXYYXXXY"
                ],
                enemyPoints: 15,
                story: ("Well done!!\nYour next mission is to destroy more enemies")
            }, //Level 2
            {
                grid: [
                    "XXXXXXXXXX",
                    "XXXPXXPXXX",
                    "XX222222XX",
                    "XXX1111XXX",
                    "XX333333XX",
                    "X33211233X",
                    "XXXXXXXXXX",
                    "XYYXXXXYYX",
                    "YXXXYYXXXY"
                ],
                enemyPoints: 20,
                story: ("Awesome work!!\nYour next mission is to destroy even more enemies")
            }, //Level 3
            {
                grid: [
                    "XXXXXXXXXX",
                    "XPXXPXXPXX",
                    "XX222222XX",
                    "XXX1111XXX",
                    "XX333333XX",
                    "X33211233X",
                    "XXXXXXXXXX",
                    "XYYXXXXYYX",
                    "YXXXYYXXXY"
                ],
                enemyPoints: 30,
                story: ("Wicked!!\nNow you get to destroy even more enemies")
            }, //Level 4
            {
                grid: [
                    "XXXXXXXXXX",
                    "X4P4444P4X",
                    "X31231231X",
                    "X44444444X",
                    "X21212121X",
                    "YXXXXXXXXY",
                    "XXXYXXYXXX",
                    "YYYXYYXYYY",
                    "XXXXXXXXXX"
                ],
                enemyPoints: 100,
                story: ("Almost there!!\nYour next mission is to destroy something")
            }, //Level 5
            {
                grid: [
                    "XXXXXXXXXX",
                    "XXXXBPXXXX",
                    "XX13XX31XX",
                    "XXXX33XXXX",
                    "XXX1221XXX",
                    "YXXXXXXXXY",
                    "XXXYXXYXXX",
                    "YXYXXXXYXY",
                    "XXXXXXXXXX"
                ],
                enemyPoints: 150,
                story: ("Youza!!\nGet ready to meet the BOSS (lite)"),
                bossLives: 5
            }, //Level 6 - Boss (lite)
            {
                grid: [
                    "XXXXXXXXXX",
                    "XXXXBPXXXX",
                    "XX13XX31XX",
                    "XXX4334XXX",
                    "XXX2121XXX",
                    "YXXXXXXXXY",
                    "XXXYXXYXXX",
                    "YXYXYYXYXY",
                    "XXXXXXXXXX"
                ],
                enemyPoints: 150,
                story: ("Sick!!\nTime to destroy the BOSS (medium)"),
                bossLives: 15
            }, //Level 7 - Boss (medium)
            {
                grid: [
                    "XXXXXXXXXX",
                    "YYXXPBPXYY",
                    "XXX3333XXX",
                    "YYXX44XXYY",
                    "XYY4444YYX",
                    "YXYY44YYXY",
                    "XYYX33XYYX",
                    "XYYYYYYYYX",
                    "XXXXXXXXXX"
                ],
                enemyPoints: 250,
                story: ("You da bomb!\nFinal showdown with the real BOSS"),
                bossLives: 25
            }  //Level 8 - Boss (finale)
        ];
        
        /*  ADD YOUR OWN COLOR THEMES HERE (Each theme must include a name and exactly 5 colors)
            Try to use light to dark, or dark to light so you get a better contrast in the game
        */
        var colorThemes = [
            {
                name: "vibes",
                colors: [
                    color(0, 48, 73),
                    color(232, 226, 136),
                    color(214, 40, 40),
                    color(247, 127, 0),
                    color(252, 191, 73)
                ]
            },
                {
                name: "chalkboard", //by RandomProgrammer24
                colors: [
                    color(64),
                    color(232, 213, 93),
                    color(162, 219, 216),
                    color(209, 182, 192),
                    color(209, 230, 207),
                ]
            },
            {
                name:"fire", //by Sreenjoy Modak
                colors:[
                color(255),
                color(255,98,0),
                color(255,187,0),
                color(240, 140, 0),
                color(255, 181, 71)
                ]
            },
            {
                name: "desert", //by Benji Doerr
                colors: [
                    color(0, 0, 0),
                    color(235, 215, 0),
                    color(137, 148, 92),
                    color(186, 89, 4),
                    color(255, 0, 0)
                ]
            },
            {
                name: "birthday party", //by Benji Doerr
                colors: [
                    color(112, 17, 109),
                    color(202, 5, 77),
                    color(240, 82, 216),
                    color(127, 204, 255),
                    color(244, 252, 0)
                ]
            },
            {
                name: "contemporary",
                colors: [
                    color(26, 26, 28),
                    color(78, 78, 80),
                    color(111, 34, 50),
                    color(149, 7, 65),
                    color(195, 8, 63)
                ]
            },
            {
                name: "futuristic",
                colors: [
                    color(44, 53, 50),
                    color(16, 100, 102),
                    color(216, 176, 140),
                    color(255, 203, 155),
                    color(210, 233, 227)
                ]
            },
            {
                name: "audacious",
                colors: [
                    color(39, 39, 39),
                    color(116, 116, 116),
                    color(255, 101, 47),
                    color(255, 228, 1),
                    color(19, 167, 107)
                ]
            },
            {
                name: "citrus",
                colors: [
                    color(31, 38, 5),
                    color(31, 100, 33),
                    color(83, 144, 15),
                    color(164, 166, 30),
                    color(214, 206, 20)
                ]
            },
            {
                name: "playing arts",
                colors: [
                    color(0, 127, 112),
                    color(69, 69, 64),
                    color(139, 42, 49),
                    color(177, 125, 89),
                    color(255, 255, 255)
                ]
            },
            {
                name: "civic",
                colors: [
                    color(64, 61, 62),
                    color(61, 158, 204),
                    color(238, 71, 49),
                    color(248, 249, 251),
                    color(54, 174, 91)
                ]
            },
            {
                name: "unnamed",
                colors: [
                    color(46, 53, 50),
                    color(220, 220, 221),
                    color(148, 149, 139),
                    color(175, 28, 28),
                    color(183, 182, 193)
                ]
            },
            {
                name: "sunset",
                colors: [
                    color(114, 17, 33),
                    color(202, 5, 77),
                    color(241, 81, 86),
                    color(255, 192, 127),
                    color(255, 207, 153)
                ]
            },
            {
                name: "black & white",
                colors: [
                    color(0, 0, 0),
                    color(51, 51, 51),
                    color(102, 102, 102),
                    color(153, 153, 153),
                    color(255, 255, 255)
                ]
            }
        ];
        
        {
            angleMode = "degrees";
            textFont(createFont("Courier New"));
            noStroke();
        
            //Grid Coordinates
            var w = width / 10;
            var h = height / 10;
        
            var highQuality = true;
            var player;
            var game;
        
            //Used for the Menu Screens
            var p1, e1, e2, e3, e4, boss, bossEnemy, portal, portalEnemy;
        
        } //Global Variables
        
        {
            //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.width = config.width || 100;
                this.height = config.height || 100;
                this.content = config.content || "Home";
                this.page = config.page || "home";
                this.level = config.level || 0;
                this.textSize = config.textSize || this.width/5;
            
                //Normal
                this.borderColor = config.borderColor || color(130, 135, 135, 100);
                this.backColor = config.backColor || color(10, 10, 10, 200);
                this.contentColor = config.contentColor || color(222, 222, 222);
                //Hover
                this.borderColorHover = config.borderColorHover || color(130, 135, 135, 50);
                this.backColorHover = config.bakColorHover || color(29, 29, 29, 200);
                this.contentColorHover = config.contentColorHover || color(222, 222, 222, 200);
            };
            
            //Draw the button
            Button.prototype.draw = function() {
                pushStyle();
                strokeWeight(2);
                if(this.isMouseInside()) {
                    hover = true;
            
                    if(clicked) {
                        game.page = this.page;
                        game.reset();
                    }
            
                    fill(this.backColorHover);
                    stroke(this.borderColor);
                    rect(this.x, this.y, this.width, this.height, 8);
                
                    fill(this.contentColorHover);
                    textSize(this.textSize);
                    textAlign(CENTER, CENTER);
                    text(this.content, this.x + this.width/2, this.y + this.height/2);
                }
                else {
                    fill(this.backColor);
                    stroke(this.borderColor);
                    rect(this.x, this.y, this.width, this.height, 8);
                
                    //fill(this.contentColor);
                    fill(game.textColor);
                    textSize(this.textSize);
                    textAlign(CENTER, CENTER);
                    text(this.content, this.x + this.width/2, this.y + this.height/2);
                }
                popStyle();
            };
            
            //Checks if the mouse it over the button
            Button.prototype.isMouseInside = function() {
                return  mouseX > this.x &&
                        mouseX < this.x + this.width &&
                        mouseY > this.y &&
                        mouseY < this.y + this.height;
            };
            
            //Handles the hover animation
            Button.prototype.hover = function(){
                if(this.isMouseInside())
                {
                    fill(this.backColorHover);
                    rect(this.x-5, this.y-5, this.width + 10, this.height + 10, 8);
                }
            };
        } //Buttons
        
        {
            var Explosion = function(config){
                this.pos = config.pos || new PVector(width/2, height/2);
                this.w = config.w || 10;
                this.h = config.h || 10;
                this.rotation = random(360);
                this.rotationDelta = config.rotationDelta || random(-5, 5);
                this.acceleration = new PVector(random(-2, 2), random(-2, 2));
                this.timeToLive = 255;
                this.red = config.red || random(255);
                this.green = config.green || random(255);
                this.blue = config.blue || random(255);
            };
            
            Explosion.prototype.run = function() {
                this.update();
                this.display();
            };
            
            Explosion.prototype.update = function() {
                this.pos.add(this.acceleration);
                this.rotation += this.rotationDelta;
            };
        
            Explosion.prototype.display = function() {
                pushMatrix();
                    translate(this.pos.x, this.pos.y);
                    rotate(radians(this.rotation));
                    noStroke();
                    fill(color(this.red, this.green, this.blue, this.timeToLive));
                    rect(-this.w / 2, -this.h / 2, this.w, this.h);
                popMatrix();    
            };
        
        } //Explosion
        
        {
            var Particle = function(config){
                this.pos = config.pos || new PVector(width/2, height/2);
                this.w = config.w || 10;
                this.h = config.h || 10;
                this.acceleration = config.acceleration || new PVector(random(-2, 2), random(1, 3));
                this.timeToLive = 255;
                this.backColor = config.backColor || color(0);
                this.red = red(this.backColor) || random(255);
                this.green = green(this.backColor) || random(255);
                this.blue = blue(this.backColor) || random(255);
            };
            
            Particle.prototype.update = function() {
                this.pos.add(this.acceleration);
            };
        
            Particle.prototype.display = function() {
                pushMatrix();
                    translate(this.pos.x, this.pos.y);
                    noStroke();
                    fill(color(this.red, this.green, this.blue, this.timeToLive));
                    ellipse(0, 0, this.w, this.h);
                popMatrix();    
            };
        
            Particle.prototype.run = function() {
                this.update();
                this.display();
            };
        
        } //Particles
        
        {
            var Missile = function (config) {
                this.pos = config.pos || new PVector(0, 0);
                this.acceleration = config.acceleration || new PVector(0, 3);
                this.w = config.w || 8;
                this.h = config.h || 12;
                this.backColor = config.backColor || color(100, 100, 100);
            };
        
            Missile.prototype.update = function () {
                this.pos.add(this.acceleration);
            };
        
            Missile.prototype.display = function () {
                fill(this.backColor);
                pushStyle();
                rectMode(CENTER);
                rect(this.pos.x, this.pos.y, this.w, this.h);
                popStyle();
            };
        
            Missile.prototype.collision = function (player) {
                //Check if killed player
                if (this.pos.x + this.w > player.pos.x &&
                    this.pos.x < player.pos.x + player.w &&
                    this.pos.y + this.h > player.pos.y &&
                    this.pos.y < player.pos.y + player.h) {
                    return true;
                }
        
                return false;
            };
        
        } //Missiles
        
        {
            //Block Object
            var Block = function(config) {
                this.pos = config.pos || new PVector(0, 0);
                this.w = config.w || 10;
                this.h = config.h || 10;
                this.backColor = game.colorTheme.colors[floor(random(1, game.colorTheme.colors.length))];
            };
        
            Block.prototype.display = function() {
                fill(this.backColor);
                stroke(255, 255, 255, 50);
                strokeWeight(1);
                rect(this.pos.x, this.pos.y, this.w, this.h);
            };
        
        } //Blocks
        
        {
            var Barrier = function(config) {
                this.pos = config.pos || new PVector(0, 0);
                this.hBlocks = config.hBlocks || 3;
                this.vBlocks = config.vBlocks || 2;
                this.blocks = [];
                this.blockWidth = config.blockWidth || w/this.hBlocks;
                this.blockHeight = config.blockHeight || h/this.vBlocks*0.75;
            };
        
            Barrier.prototype.init = function() {
                this.blocks = [];
        
                for(var v = 0; v < this.vBlocks; v++) {
                    for(var h = 0; h < this.hBlocks; h++) {
                        this.blocks.push(new Block({
                            pos: new PVector(this.pos.x + h * this.blockWidth, this.pos.y + v * this.blockHeight),
                            w: this.blockWidth,
                            h: this.blockHeight
                        }));
                    }
                }
            };
        
            Barrier.prototype.display = function() {
                for(var i = 0; i < this.blocks.length; i++) {
                    var block = this.blocks[i];
                    block.display();
                }
            };
        
        } //Barrier
        
        {
            //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.acceleration = 0.5;
                this.maxSpeed = 5;
                this.momentum = 0.5;
        
                this.showParticles = highQuality;
                this.particles = [];
                this.maxParticles = 10; //reduced from 20 to 10 for better performance
        
                this.fired = false;
                this.bulletsInit = 20;
                this.bullets = 15 || this.bulletsInit;
                this.bullet = {
                    pos: new PVector(0, 0),
                    w: 12,
                    h: 15,
                    dir: 0,
                    speed: 10
                };
            };
        
            Player.prototype.runParticles = function() {        
                for(var i = 0; i < this.particles.length; i++) {
                    var particle = this.particles[i];
                    particle.run();
        
                    particle.timeToLive-= 5;
                    
                    if(particle.pos.y > height) {
                        particle.pos = new PVector(this.pos.x + this.w * 0.5, this.pos.y + this.h * 0.8);
                        particle.timeToLive = 255;
                    }
                }
            };
        
            Player.prototype.update = function () {
                if(this.showParticles) { //only show particles if high quality
                    this.runParticles();
                }
        
                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;
                }
        
                this.pos.x = constrain(this.pos.x + this.xs, 0, 600 - this.w);
            };
        
            Player.prototype.display = function () {
                noStroke();
        
                if (this.fired) {                
                    this.bullet.pos.y -= this.bullet.speed * this.bullet.dir;
                    fill(game.colorTheme.colors[game.colorTheme.colors.length-1]);
                    ellipse(this.bullet.pos.x, this.bullet.pos.y, this.bullet.w, this.bullet.h);
                }
                
                pushStyle();
                    //fins
                    fill(game.colorTheme.colors[1]);
                    triangle(
                        this.pos.x + this.w / 2, 
                        this.pos.y + 5, 
                        this.pos.x + this.w + 10, 
                        this.pos.y + this.h, 
                        this.pos.x - 10, 
                        this.pos.y + this.h);
                    
                    stroke(game.colorTheme.colors[1]);
        
                    strokeWeight(2);
                    line(this.pos.x - 5, this.pos.y + this.h - 5, this.pos.x - 5, this.pos.y + this.h - 10);      
                    line(this.pos.x + this.w + 5, this.pos.y + this.h - 5, this.pos.x + this.w + 5, this.pos.y + this.h - 10);
                    
                    //turret
                    strokeWeight(4);
                    line(this.pos.x + this.w / 2, this.pos.y, this.pos.x + this.w / 2, this.pos.y - 2);
                    
                    //body
                    fill(game.colorTheme.colors[2]);
                    noStroke(); 
                    
                    beginShape();
                        vertex(this.pos.x + this.w / 2, this.pos.y);
                        bezierVertex(this.pos.x + this.w, this.pos.y + this.h / 2, this.pos.x + this.w, this.pos.y + this.h, this.pos.x + this.w, this.pos.y + this.h);
                        vertex(this.pos.x, this.pos.y + this.h);
                        bezierVertex(this.pos.x, this.pos.y + this.h, this.pos.x, this.pos.y + this.h / 2, this.pos.x + this.w / 2, this.pos.y);
                    endShape();
                    
                    //window
                    fill(game.colorTheme.colors[1]);
                    triangle(this.pos.x + this.w / 2, this.pos.y + 10, this.pos.x + this.w - 10, this.pos.y + this.h - 15, this.pos.x + 10, this.pos.y + this.h - 15);            
                popStyle();
            };
        
            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 up
                            this.bullet.pos.x = this.pos.x + this.w / 2;
                            this.bullet.pos.y = this.pos.y + this.h / 3;
                            this.bullet.dir = 1;
                            this.fired = true;
                            keyCode = 0;
                        }
                    }
                }
                else {//Bullet was fired and in transit
                    //Check if hit the spaceship
                    if(this.fired) {
                        for (var i = 0; i < game.spaceships.length; i++) { //Currently at most only 1 spaceship exists
                            var spaceship = game.spaceships[i];
        
                            if (this.bullet.pos.x + this.bullet.w > spaceship.pos.x - spaceship.w / 2 &&
                                this.bullet.pos.x < spaceship.pos.x + spaceship.w - spaceship.w / 2 &&
                                this.bullet.pos.y + this.bullet.h > spaceship.pos.y - spaceship.h / 2 &&
                                this.bullet.pos.y < spaceship.pos.y + spaceship.h - spaceship.h / 2) {
                                game.hitSpaceship = true;                       
                                game.addExplosion({
                                    pos: spaceship.pos,
                                    w: 40,
                                    h: 40,
                                    backColor: spaceship.backColor
                                });
                                game.spaceships.splice(i, 1); //remove spaceship                     
                                this.fired = false;
                                break;
                            }
                        }
                    }
        
                    //Check if killed enemy
                    if(this.fired) {         
                        for (var i = 0; i < game.enemies.length; i++) {
                            var enemy = game.enemies[i];
        
                            if (this.bullet.pos.x + this.bullet.w > enemy.pos.x - enemy.w / 2 &&
                                this.bullet.pos.x < enemy.pos.x + enemy.w - enemy.w / 2 &&
                                this.bullet.pos.y + this.bullet.h > enemy.pos.y - enemy.h / 2 &&
                                this.bullet.pos.y < enemy.pos.y + enemy.h - enemy.h / 2) {
                                game.hitEnemy = true;
                                enemy.lives--;
                                if(highQuality) { //display explosion every time you hit the enemy
                                    game.addExplosion(enemy);
                                }
                                else if(enemy.lives === 0) { //low quality
                                    game.addExplosion(enemy); //display explosion only when destroyed the enemy
                                }
                                if(enemy.lives === 0) {
                                    game.enemies.splice(i, 1); //remove enemy
                                }                        
                                this.fired = false;
                                break;
                            }
                        }
                    }
        
                    //Check if killed Boss/Portal Enemy
                    if(this.fired) {         
                        for (var i = 0; i < game.dynamoEnemies.length; i++) {
                            var dynamoEnemy = game.dynamoEnemies[i];
        
                            if (this.bullet.pos.x + this.bullet.w > dynamoEnemy.pos.x - dynamoEnemy.w / 2 &&
                                this.bullet.pos.x < dynamoEnemy.pos.x + dynamoEnemy.w - dynamoEnemy.w / 2 &&
                                this.bullet.pos.y + this.bullet.h > dynamoEnemy.pos.y - dynamoEnemy.h / 2 &&
                                this.bullet.pos.y < dynamoEnemy.pos.y + dynamoEnemy.h - dynamoEnemy.h / 2) {
                                game.hitEnemy = true;
                                dynamoEnemy.lives--;
                                if(highQuality) { //display explosion every time you hit the enemy
                                    game.addExplosion(dynamoEnemy);
                                }
                                else if(dynamoEnemy.lives === 0) { //low quality
                                    game.addExplosion(dynamoEnemy); //display explosion only when destroyed the enemy
                                }
                                if(dynamoEnemy.lives === 0) {
                                    game.dynamoEnemies.splice(i, 1); //remove Boss/Portal Enemy
                                }                        
                                this.fired = false;
                                break;
                            }
                        }
                    }
        
                    //Check if hit barrier
                    if(this.fired) {
                        for (var i = 0; i < game.barriers.length; i++) {
                            var barrier = game.barriers[i];
        
                            for(var j = 0; j < barrier.blocks.length; j++) {
                                var block = barrier.blocks[j];
        
                                if (this.bullet.pos.x + this.bullet.w > block.pos.x &&
                                    this.bullet.pos.x < block.pos.x + block.w &&
                                    this.bullet.pos.y + this.bullet.h > block.pos.y &&
                                    this.bullet.pos.y < block.pos.y + block.h) {
                                        game.addExplosion(block); //display explosion
                                        barrier.blocks.splice(j, 1);
                                    this.fired = false;
                                    break;
                                }
                            }
                        }
                    }
        
                    if (this.fired && this.bullet.pos.y < 0) {
                        this.fired = false;
                    }
                }
            };
        
            Player.prototype.run = function () {
                this.update();
                this.shoot();
                this.display();
            };
        } //Player
        
        {
            var Portal = function(config) {
                this.pos = config.pos || new PVector();
                this.w = config.w || w;
                this.h = config.h || h;
                this.scaleDir = config.scaleDir || 1;
                this.scale = config.scale || 1;
                this.angle = config.angle || 0;
                this.rotate = config.rotate || 9;
                this.backColor = config.backColor || game.colorTheme.colors[4];
                this.outerColor = config.outerColor || game.colorTheme.colors[3];
                this.centerColor = config.centerColor || game.colorTheme.colors[2];
                this.coreColor = config.coreColor || game.colorTheme.colors[1];
            };
        
        Portal.prototype.update = function() {
            this.angle += this.rotate;
        };
        
        Portal.prototype.display = function() {
            pushMatrix();
                translate(this.pos.x, this.pos.y);
                this.scale+= 0.01 * this.scaleDir;
                if(this.scale < 0.9 || this.scale > 1.1) {
                    this.scaleDir*= -1;
                }
                scale(this.scale);
                pushMatrix();
                fill(this.backColor);
                ellipse(0, 0, this.w, this.h);
                rotate(radians(this.angle));
                fill(this.outerColor);
                ellipse(0, 0, this.w * 0.8, this.h * 0.7);
                popMatrix();
                pushMatrix();
                rotate(radians(-this.angle));
                fill(this.centerColor);
                ellipse(0, 0, this.w * 0.5, this.h * 0.6);
                popMatrix();
                rotate(radians(this.angle));
                fill(this.coreColor);
                ellipse(0, 0, this.w * 0.3, this.h * 0.25);
            popMatrix();
        };
        
        Portal.prototype.run = function() {
            this.update();
            this.display();
        };
        
        } //Portal
        
        {
            var SpaceShip = function(config) {
                this.pos = config.pos || new PVector(0, 0);
                this.acceleration = config.acceleration || new PVector(0.75, 0);
                this.angle = config.angle || 0;
                this.rotate = config.rotate || 6;
                this.w = config.w || 50;
                this.h = config.h || 15;
                this.backColor = config.backColor || color(game.colorTheme.colors[1]);
                this.topColor = config.topColor || color(game.colorTheme.colors[2]);
                this.r = red(this.topColor);
                this.g = green(this.topColor);
                this.b = blue(this.topColor);
            };
        
            SpaceShip.prototype.update = function() {
                this.pos.add(this.acceleration);
            };
        
            SpaceShip.prototype.display = function() {
                pushMatrix();
                    translate(this.pos.x, this.pos.y);
                    noStroke();
                    fill(this.r, this.g, this.b, 200);
                    ellipse(0, this.h * 0.6, this.w / 2, this.h);
                    fill(this.backColor);
                    rect(-this.w / 2, this.h / 2, this.w, this.h, 10);
                    pushStyle();
                        noFill();
                        stroke(this.r, this.g, this.b, 150);
                        strokeWeight(1);
                        fill(this.r, this.g, this.b, 150);
                        noStroke();
        
                        this.angle+= this.rotate;
                        pushMatrix();
                            translate(0, this.h);
                            rotate(radians(-this.angle));
                            ellipse(0, 0, this.w * 0.2, this.h * 0.5); //middle window
                        popMatrix();
                        pushMatrix();
                            translate(-this.w * 0.3, this.h);
                            rotate(radians(this.angle));
                            ellipse(0, 0, this.w * 0.2, this.h * 0.5); //left window
                        popMatrix();            
                        pushMatrix();
                            translate(this.w * 0.3, this.h);
                            rotate(radians(this.angle));
                            ellipse(0, 0, this.w * 0.2, this.h * 0.5); //right window
                        popMatrix();
                    popStyle();
                popMatrix();
            };
        
            SpaceShip.prototype.run = function() {
                this.update();
                this.display();
            };
        
        } //Spaceship
        
        {
            var Enemy = function (config) {
                this.pos = config.pos || new PVector(0, 0);
                this.acceleration = config.acceleration || new PVector(0, 3);
                this.w = config.w || 30;
                this.h = config.h || 30;
                this.lives = config.lives || 1;
                this.backColor = config.backColor || game.colorTheme.colors[floor(random(1, game.colorTheme.colors.length))];
                this.missileColor = config.missileColor || game.colorTheme.colors[game.colorTheme.colors.length-2];
            };
        
            Enemy.prototype.move = function () {
                this.pos.add(this.acceleration);
            };
        
            Enemy.prototype.update = function () {
            };
        
            Enemy.prototype.shoot = function () {
                game.enemyMissiles.push(new Missile({
                    pos: new PVector(this.pos.x, this.pos.y),
                    backColor: this.missileColor
                }));
            };
        
            Enemy.prototype.display = function () {
            };
        
            Enemy.prototype.displayLives = function() {
                pushMatrix();
                    translate(this.pos.x, this.pos.y);
                    pushStyle();
                    textAlign(CENTER, CENTER);
                    textSize(18);
                    fill(game.colorTheme.colors[0]);
                    text(this.lives, 0, 0);
                    popStyle();
                popMatrix();
            };
        
            Enemy.prototype.collision = function (player) {
                if (player.pos.x + player.w > this.pos.x &&
                    player.pos.y + player.h > this.pos.y &&
                    player.pos.x < this.pos.x + this.w &&
                    player.pos.y < this.pos.y + this.h) {
                    return true;
                }
        
                return false;
            };
        
            {
                var Enemy1 = function (config) {
                    Enemy.call(this, config);
                    this.name = "Enemy 1";
                    this.state = config.state || true;
                };
        
                Enemy1.prototype = Object.create(Enemy.prototype);
        
                Enemy1.prototype.display = function () {
                    //Enemy.prototype.display.call(this);
        
                    fill(this.backColor);
                    
                    stroke(this.backColor);
                    strokeWeight(3);
                        
                    line(this.pos.x - this.w * 0.55, this.pos.y, this.pos.x + this.w * 0.55, this.pos.y);
                    line(this.pos.x, this.pos.y - this.h * 0.55, this.pos.x, this.pos.y + this.h * 0.5);
                    
                    if(this.state === true) {
                        line(this.pos.x, this.pos.y, this.pos.x - this.w * 0.5, this.pos.y + this.h * 0.3);
                        line(this.pos.x, this.pos.y, this.pos.x + this.w * 0.5, this.pos.y + this.h * 0.3);
                    }
                    else {
                        line(this.pos.x, this.pos.y, this.pos.x - this.w * 0.5, this.pos.y - this.h * 0.3);
                        line(this.pos.x, this.pos.y, this.pos.x + this.w * 0.5, this.pos.y - this.h * 0.3);
                    }
        
                    noStroke();
                    ellipse(this.pos.x, this.pos.y, this.w, this.h);
                    
                    this.displayLives();
                };
                
                Enemy1.prototype.update = function() {
                    if(frameCount % 30 === 0) {
                        this.state = !this.state;
                    }
                };
            } //Enemy 1
        
            {
                var Enemy2 = function (config) {
                    Enemy.call(this, config);
                    this.name = "Enemy 2";
                    this.angle = config.angle || 0;
                    this.rotate = config.rotate || 2;
                    this.lives = config.lives || 2;
                    this.scaleDir = config.scaleDir || 1;
                    this.scale = config.scale || 1;
                };
        
                Enemy2.prototype = Object.create(Enemy.prototype);
        
                Enemy2.prototype.move = function () {
                    Enemy.prototype.move.call(this);
                };
        
                Enemy2.prototype.update = function () {
                    this.angle += this.rotate;
                };
        
                Enemy2.prototype.display = function () {
                    //Enemy.prototype.display.call(this);
                    fill(this.backColor);
                    pushMatrix();
                    translate(this.pos.x, this.pos.y);
                    this.scale+= 0.02 * this.scaleDir;
                    if(this.scale < 0.9 || this.scale > 1.1) {
                        this.scaleDir*= -1;
                    }
                    scale(this.scale);
                    pushStyle();
                    rectMode(CENTER);
                    rect(0, 0, this.w, this.h);
                    rotate(radians(this.angle));
                    rect(0, 0, this.w, this.h);
                    popStyle();
                    popMatrix();
                    this.displayLives();
                };
            } //Enemy 2
        
            {
                var Enemy3 = function (config) {
                    Enemy.call(this, config);
                    this.name = "Enemy 3";
                    this.angle = config.angle || 0;
                    this.rotate = config.rotate || 2;
                    this.lives = config.lives || 3;
                };
        
                Enemy3.prototype = Object.create(Enemy.prototype);
        
                Enemy3.prototype.move = function () {
                    Enemy.prototype.move.call(this);
                };
        
                Enemy3.prototype.update = function () {
                    this.angle += this.rotate;
                };
        
                Enemy3.prototype.display = function () {
                    //Enemy.prototype.display.call(this);
                    fill(this.backColor);
                    pushMatrix();
                    translate(this.pos.x, this.pos.y);
                    rotate(radians(this.angle));
                    pushStyle();
                    rectMode(CENTER);
                    rect(0, 0, this.w, this.h, 10);
                    rotate(radians(this.angle));
                    rect(0, 0, this.w, this.h, 10);
                    popStyle();
                    popMatrix();
                    this.displayLives();
                };
            } //Enemy 3
        
            {
                var Enemy4 = function (config) {
                    Enemy.call(this, config);
                    this.name = "Enemy 4";
                    this.angle = config.angle || 0;
                    this.rotate = config.rotate || -2;
                    this.lives = config.lives || 4;
                };
        
                Enemy4.prototype = Object.create(Enemy.prototype);
        
                Enemy4.prototype.move = function () {
                    Enemy.prototype.move.call(this);
                };
        
                Enemy4.prototype.update = function () {
                    this.angle += this.rotate;
                };
        
                Enemy4.prototype.display = function () {
                    //Enemy.prototype.display.call(this);
                    fill(this.backColor);
                    pushMatrix();
                    translate(this.pos.x, this.pos.y);
                    rotate(radians(this.angle));
                    pushStyle();
                    rectMode(CENTER);
                    rect(0, 0, this.w, this.h);
                    rotate(radians(this.angle));
                    rect(0, 0, this.w, this.h);
                    popStyle();
                    popMatrix();
                    this.displayLives();
                };
            } //Enemy 4
        
            {
                var PortalEnemy = function (config) {
                    Enemy.call(this, config);
                    this.name = "Portal Enemy";
                    this.force = config.force || new PVector(-this.acceleration.x * 0.02, 0);
                    this.angle = config.angle || 0;
                    this.rotate = config.rotate || 6;
                    this.lives = config.lives || 2;
                    this.missileFactor = config.missileFactor || 0.02;
                };
        
                PortalEnemy.prototype = Object.create(Enemy.prototype);
        
                PortalEnemy.prototype.move = function () {
                };
        
                PortalEnemy.prototype.update = function () {
                    this.acceleration.add(this.force);
                    this.pos.add(this.acceleration);
                    this.angle += this.rotate;
                };
        
                PortalEnemy.prototype.display = function () {
                    //Enemy.prototype.display.call(this);
                    fill(this.backColor);
                    pushMatrix();
                    translate(this.pos.x, this.pos.y);
                    rotate(radians(this.angle));
                    pushStyle();
                    rectMode(CENTER);
                    rect(0, 0, this.w, this.h);
                    rotate(radians(this.angle));
                    rect(0, 0, this.w, this.h);
                    popStyle();
                    popMatrix();
                    this.displayLives();
                };
        
                PortalEnemy.prototype.shoot = function() {
                    if(random() < this.missileFactor && this.pos.y < height - h * 3) {
                        game.enemyMissiles.push(new Missile({
                            pos: new PVector(this.pos.x, this.pos.y),
                            backColor: this.missileColor,
                            w: 8,
                            h: 8,
                            acceleration: new PVector(random(-4, 4), random() < 0.5 ? -2 : 2)
                        }));
                    }
                };
        
                PortalEnemy.prototype.run = function() {
                    this.update();
                    this.shoot();
                    this.display();
                };
        
                //NEED TO CHECK THIS!!!!
                PortalEnemy.prototype.collision = function (player) {
                    if (player.pos.x + player.w > this.pos.x - this.w / 2 &&
                        player.pos.y + player.h > this.pos.y && //will never be this one
                        player.pos.x < this.pos.x + this.w / 2 &&
                        player.pos.y < this.pos.y + this.h / 2) {
                        return true;
                    }
        
                    return false;
                };
            } //Portal Enemy
        
            {
                var Boss = function (config) {
                    Enemy.call(this, config);
                    this.name = "Boss";
                    this.angle = config.angle || 0;
                    this.rotate = config.rotate || 5;
                    this.lives = config.lives || 30;
                    this.scaleDir = config.scaleDir || 1;
                    this.scale = config.scale || 1;
                    this.centerColor = config.centerColor || color(200);
                    this.centerRed = red(this.centerColor);
                    this.centerGreen = green(this.centerColor);
                    this.centerBlue = blue(this.centerColor);
                };
        
                Boss.prototype = Object.create(Enemy.prototype);
        
                Boss.prototype.move = function () {
                    Enemy.prototype.move.call(this);
                };
        
                Boss.prototype.update = function () {
                    this.angle += this.rotate;
                };
        
                Boss.prototype.display = function () {
                    //Enemy.prototype.display.call(this);
                    fill(this.backColor);
                    pushMatrix();
                    translate(this.pos.x, this.pos.y);
                    this.scale+= 0.005 * this.scaleDir;
                    if(this.scale < 0.95 || this.scale > 1.05) {
                        this.scaleDir*= -1;
                    }
                    scale(this.scale);
                    pushStyle();
                    rectMode(CENTER);
                    rotate(radians(-this.angle));
                    rect(0, 0, this.w, this.h, 5);
                    stroke(this.backColor);
                    strokeWeight(1);
                    var offset = 3;
                    line(-this.w / 2 - offset, -this.h / 2 - offset, this.w / 2 + offset, this.h / 2 + offset);
                    line(-this.w / 2 - offset, this.h / 2 + offset, this.w / 2 + offset, -this.h / 2 - offset);
                    rotate(radians(this.angle*2));
                    fill(this.backColor);
                    noStroke();
                    rect(0, 0, this.w, this.h, 5);
                    stroke(this.backColor);
                    strokeWeight(1);
                    line(-this.w / 2 - offset, -this.h / 2 - offset, this.w / 2 + offset, this.h / 2 + offset);
                    line(-this.w / 2 - offset, this.h / 2 + offset, this.w / 2 + offset, -this.h / 2 - offset);
                    //Center
                    noStroke();
                    fill(this.centerRed, this.centerGreen, this.centerBlue, 50);
                    ellipse(0, 0, this.w * 0.75, this.h * 0.75);
                    popStyle();
                    popMatrix();
                    this.displayLives();
                };
            } //Boss
        
            {
                var BossEnemy = function(config) {
                    Enemy.call(this, config);
                    this.name = "Boss Enemy";
                    this.force = config.force || new PVector(-this.acceleration.x * 0.02, 0);
                    this.angle = config.angle || 0;
                    this.rotate = config.rotate || 5;
                    this.lives = config.lives || 2;
                    this.missileFactor = config.missileFactor || 0.02;
                    this.scaleDir = config.scaleDir || 1;
                    this.scale = config.scale || 1;
                    this.centerColor = config.centerColor || color(200);
                    this.centerRed = red(this.centerColor);
                    this.centerGreen = green(this.centerColor);
                    this.centerBlue = blue(this.centerColor);
                };
        
                BossEnemy.prototype = Object.create(Enemy.prototype);
        
                BossEnemy.prototype.update = function () {
                    this.acceleration.add(this.force);
                    this.pos.add(this.acceleration);
                    this.angle += this.rotate;
                };
        
                BossEnemy.prototype.display = function () {
                    //Enemy.prototype.display.call(this);
                    fill(this.backColor);
                    pushMatrix();
                    translate(this.pos.x, this.pos.y);
                    this.scale+= 0.005 * this.scaleDir;
                    if(this.scale < 0.95 || this.scale > 1.05) {
                        this.scaleDir*= -1;
                    }
                    scale(this.scale);
                    pushStyle();
                    rectMode(CENTER);
                    rotate(radians(-this.angle));
                    rect(0, 0, this.w, this.h, 5);
                    stroke(this.backColor);
                    strokeWeight(1);
                    var offset = 3;
                    line(-this.w / 2 - offset, -this.h / 2 - offset, this.w / 2 + offset, this.h / 2 + offset);
.........完整代码请登录后点击上方下载按钮下载查看

网友评论0