Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <canvas></canvas>
<div class="hint">Mouse over here to start</div>

<h2 id="gameStatus"></h2>

<div class="stats">
    <div class="lives">Lives: <span id="lifeCount">5</span></div>
    <div class="wins">Wins: <span id="winCount">0</span></div>
</div>

<div class="stats record">Record: <span id="record">0</span></div>

<audio id="blip" autostart="false" src="https://zachsaucier.com/pen-stuff/blip.mp3"></audio>
<audio id="die" autostart="false" src="https://zachsaucier.com/pen-stuff/die.mp3"></audio>
<audio id="win" autostart="false" src="https://zachsaucier.com/pen-stuff/win.mp3"></audio>

<audio id="music" autostart="false" loop src="https://zachsaucier.com/pen-stuff/Guile%20Theme%20(SNES).mp3"></audio>

<img id="guile" src="https://zachsaucier.com/pen-stuff/guile-hdstance.gif"/>
              
            
!

CSS

              
                html, body {
  margin:0; 
  padding:0;
}
body { 
    background: rgb(38, 38, 38); 
    color:white;
    font-size:26px;
    font-family: Helvetica, serif;
}
canvas { z-index:1; cursor:crosshair; }
.hidden { display:none; }

div, h2 { pointer-events:none; }
.hint {
    position:absolute;
    top:5px;
    left:35px;
}
.stats {
    position:absolute;
    bottom:20px;
    right:20px;
    font-size:50px;
}
.record { top:20px; }
#gameStatus {
    position:absolute;
    top:20px;
    left:50%;
    transform:translateX(-50%);
}

#guile {
    position:absolute;
    bottom:20px;
    left:20px;
    width:50px;
    cursor:pointer;
}
              
            
!

JS

              
                window.onload = function() {
    window.requestAnimationFrame = function() {
        return window.requestAnimationFrame ||
            window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame ||
            window.msRequestAnimationFrame ||
            window.oRequestAnimationFrame ||
            function(f) {
            window.setTimeout(f, 1e3 / 60);
        }
    }();

    // Ring functions
    var canvas = document.querySelector('canvas'),
        ctx = canvas.getContext('2d'),
        W = canvas.width = window.innerWidth,
        H = canvas.height = window.innerHeight;

    var Options = function() {
        this.ringCount = 10;  // WARNING: 30+ makes even good computers lag or freeze
        this.isPlaying = false;
        this.difficulty = .03;
    };
    
    var myOptions = new Options();

    var SpaceShip = function() {
        this.x = 200;
        this.y = 100;
        this.radius = 40;
        this.wing_count = 3;
        this.steps = Math.PI * 2 / this.wing_count;
        this.color = 'hsl(0,100%,50%)';	
        this.hue = 0; 
        this.hue_target = parseInt(Math.random() * 360);
        this.angle = 0;	
        this.rotation_speed = 0.03;	
        
        this.draw = function(ctx) {	
            if (this.angle > Math.PI * 2)
                this.angle -= Math.PI * 2;
            this.hue += (this.hue_target - this.hue) * 0.05;
            this.color = 'hsl(' + this.hue + ', 100%, 50%)';	
            ctx.strokeStyle = this.color;	

            ctx.lineWidth = 15;
            ctx.save();	
            ctx.translate(this.x, this.y);
            
            this.angle += this.rotation_speed;
            ctx.rotate(this.angle);

            for (var i = 0; i < this.wing_count; i++) {
                ctx.beginPath();
                ctx.arc(
                    0,
                    0,
                    this.radius,
                    i * this.steps,
                    i * this.steps + this.steps / 2,
                    false
                );
                ctx.stroke();
                ctx.closePath();
            }
            ctx.restore();
        };
    };

    var Circle = function() {
        this.draw = function(ctx) {
            ctx.beginPath();
            ctx.fillStyle = 'rgb(0, 0, 0)';
            ctx.arc(W / 2, H / 2, 20, 0, Math.PI * 2, false);
            ctx.fill();
            ctx.closePath();
        };
    };
    var innerCircle = new Circle();
    
    var Square = function() {
        this.draw = function(ctx) {
            ctx.beginPath();
            ctx.fillStyle = 'rgb(255, 255, 255)';
            ctx.rect(0, 0, 20, 20);
            ctx.fill();
            ctx.closePath();
        };
    };
    var startSquare = new Square();

    var ships = [];

    function createShips() {
        blipSound();
        
        if(ships.length > 0)
            [].forEach.call(ships, function(elem) {
                elem = undefined;
            });
        
        for(var i = 0; i < myOptions.ringCount; i++) {
            var ship = new SpaceShip();
            ship.x = W / 2;
            ship.y = H / 2;
            ship.radius = 20 * (i + 2);

            ship.rotation_speed = Math.random() * myOptions.difficulty - myOptions.difficulty / 2;

            ships[i] = ship;
        }
    }
    
    
    // Game functions
    var lifeCount = document.getElementById("lifeCount"),
        winCount = document.getElementById("winCount"),
        gameStatus = document.getElementById("gameStatus"),
        recordCount = document.getElementById("record"),
        hint = document.querySelector(".hint");
    
    var numLives = 5,
        numWins = 0,
        record = 0;
    
    canvas.onmousemove = checkColor;
    
    document.addEventListener('contextmenu', function(e) { // Prevent right click menu
        e.preventDefault();
    }, false);
    
    function findPos(obj) {
        var curleft = 0, 
            curtop = 0;
        
        if (obj.offsetParent) {
            do {
                curleft += obj.offsetLeft;
                curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
            return { x: curleft, y: curtop };
        }
        return undefined;
    }
  
    function checkColor(e) {
        var pos = findPos(this),
            x = e.pageX - pos.x,
            y = e.pageY - pos.y,
            myCtx = this.getContext('2d'); 
        
        var imgData = myCtx.getImageData(x, y, 1, 1);
        red = imgData.data[0];
        green = imgData.data[1];
        blue = imgData.data[2];
        // alpha = imgData.data[3];
        
        if(red === 255 && green === 255 && blue === 255) { // Start
            myOptions.isPlaying = true;
            createShips();
            hint.classList.add("hidden");
            gameStatus.classList.add("hidden");
        } else if(red < 38 && green < 38 && blue < 38) { // Beat the level
            gameStatus.classList.remove("hidden");
            if(myOptions.isPlaying) {
                nextLevel();
            }
        } else if(red != 38 && green != 38 && blue != 38) { // Lost
            gameStatus.classList.remove("hidden");
            die();
        }
    }
    
    function nextLevel() {
        winSound();
        winCount.innerText = ++numWins;
        gameStatus.innerText = "Level " + numWins + " won!";
        myOptions.isPlaying = false;
        if(myOptions.difficulty < 0.1) // Max at 0.1
            myOptions.difficulty += 0.01;
    }
    
    function die() {
        dieSound();
        if(--numLives > 0) {
            lifeCount.innerText = numLives;
            gameStatus.innerText = "You died.";
        } else { // Game over, dude
            gameStatus.innerText = "You lost on level " + (numWins + 1) + ".";
            myOptions.difficulty = 0.03;
            if(numWins > record) {
                record = numWins;
                recordCount.innerText = record;
            }
            
            numLives = 5;
            lifeCount.innerText = numLives;

            numWins = 0;
            winCount.innerText = numWins;
        }
        
        myOptions.isPlaying = false;
    }
  
    var winAudio = document.getElementById("win"),
        dieAudio = document.getElementById("die"),
        blipAudio = document.getElementById("blip"),
        music = document.getElementById("music"),
        guile = document.getElementById("guile");
    
    guile.onclick = toggleMusic;
    music.volume = 0.4;
    
    function dieSound() {
        dieAudio.currentTime = 0;
        dieAudio.play();
    }
    
    function winSound() {
        winAudio.currentTime = 0;
        winAudio.play();
    }
    
    function blipSound() {
        blipAudio.currentTime = 0;
        blipAudio.play();
    }
    
    function toggleMusic() {
        if(music.paused || music.stopped)
            music.play();
        else
            music.pause();
    }
    
    
    
    // Drawing function
    (function renderFrame() {
        window.requestAnimationFrame(renderFrame);
        ctx.fillStyle = "rgb(38,38,38)";
        ctx.fillRect(0, 0, W, H);
        
        if(myOptions.isPlaying) {
            innerCircle.draw(ctx);
            ships.forEach(function(ship){
                ship.draw(ctx); 
            });
        } else {
            startSquare.draw(ctx);
        }
        
    }());
}
              
            
!
999px

Console