Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

Save Automatically?

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

              
                <div id="app">

        <div class="main">
            <div>
                <div @click="click(0)" @mousedown="paddleDown(0)" @mouseup="paddleUp(0)" :style="{opacity: litState[0] ? 1 : 0.7}" class="paddle green t-l"></div>
                <div @click="click(1)" @mousedown="paddleDown(1)" @mouseup="paddleUp(1)" :style="{opacity: litState[1] ? 1 : 0.7}" class="paddle red t-r"></div>
            </div>
            <div>
                <div @click="click(2)" @mousedown="paddleDown(2)" @mouseup="paddleUp(2)" :style="{opacity: litState[2] ? 1 : 0.7}" class="paddle yellow b-l"></div>
                <div @click="click(3)" @mousedown="paddleDown(3)" @mouseup="paddleUp(3)" :style="{opacity: litState[3] ? 1 : 0.7}" class="paddle blue b-r"></div>
            </div>
        </div>

        <div class="controls">

            <h1>Simon</h1>
            <div>
                <span class="display">
                    <span id="screen">{{ screen }}</span>
                </span>
                <button id="start" @click="gameStart">re<br>set</button>
            </div>

            <section class="slider-checkbox">
                <input v-model="strict" type="checkbox" id="strict" />
                <label class="label" for="strict">Strict mode</label>
            </section>

            <section class="slider-checkbox">
                <input v-model="on" type="checkbox" id="onoff" />
                <label class="label" for="onoff">On/Off</label>
            </section>

        </div>

    </div>

    <audio id="audio-0" src="https://s3.amazonaws.com/freecodecamp/simonSound1.mp3"></audio>
    <audio id="audio-1" src="https://s3.amazonaws.com/freecodecamp/simonSound2.mp3"></audio>
    <audio id="audio-2" src="https://s3.amazonaws.com/freecodecamp/simonSound3.mp3"></audio>
    <audio id="audio-3" src="https://s3.amazonaws.com/freecodecamp/simonSound4.mp3"></audio>
              
            
!

CSS

              
                $bg-grey: #333;
body {
    font-family: monospace;
    color: #FFF;
}

h1 {
    text-transform: uppercase;
    font-size: 36px;
    margin: 20px 0 10px;
}

.main {
    margin: 50px auto 0;
    max-width: 460px;
    font-size: 0;
    border-radius: 100%;
    border: 15px solid $bg-grey;
    background-color: $bg-grey;
}

.paddle {
    height: 200px;
    width: 200px;
    border: 15px solid $bg-grey;
    display: inline-block;
}

.controls {
    margin: 0 auto;
    margin-top: -356px;
    z-index: 1;
    position: relative;
    width: 200px;
    text-align: center;
    height: 200px;
    background-color: $bg-grey;
    border-radius: 100%;
    border: 10px solid $bg-grey;
}

.display {
    font-size: 32px;
    color: orange;
    background-color: black;
    padding: 2px 7px;
    border-radius: 5px;
}

#start {
    border-radius: 100%;
    height: 40px;
    width: 40px;
    display: inline-block;
    background-color: red;
    opacity: 0.9;
    vertical-align: bottom;
    border: none;
    outline: none;
    margin-left: 12px;
}

.pressed {
    box-shadow: inset 0px 0px 20px 5px rgba(0, 0, 0, 0.75);
}

.green {
    background-color: green;
}

.red {
    background-color: red;
}

.yellow {
    background-color: yellow;
}

.blue {
    background-color: blue;
}

.t-l {
    border-top-left-radius: 100%
}

.t-r {
    border-top-right-radius: 100%
}

.b-l {
    border-bottom-left-radius: 100%
}

.b-r {
    border-bottom-right-radius: 100%
}

.slider-checkbox {
    margin-top: 15px;
}

$checked-color: #1877c1;
$unchecked-color: #888;
.slider-checkbox {
    position: relative;
    input {
        margin: 0px;
        margin-top: 1px;
        cursor: pointer;
        opacity: 0;
        -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
        filter: alpha(opacity=0);
        -moz-opacity: 0;
        -khtml-opacity: 0;
        opacity: 0;
        position: absolute;
        z-index: 1;
        top: 0px;
        left: 0px;
        background: red;
        width: 40px;
        height: 20px;
        &:checked+.label {
            &:before {
                background-color: $checked-color;
                content: " ";
                padding-left: 6px;
            }
            &:after {
                left: 21px;
            }
        }
    }
    .label {
        position: relative;
        padding-left: 46px;
        &:before,
        &:after {
            position: absolute;
            -webkit-border-radius: 10px;
            -moz-border-radius: 10px;
            border-radius: 10px;
            transition: background-color 0.3s, left 0.3s;
        }
        &:before {
            content: " ";
            color: #fff;
            box-sizing: border-box;
            font-family: 'FontAwesome', sans-serif;
            padding-left: 23px;
            font-size: 12px;
            line-height: 20px;
            background-color: $unchecked-color;
            left: 0px;
            top: 0px;
            height: 20px;
            width: 40px;
            -webkit-border-radius: 10px;
            -moz-border-radius: 10px;
            border-radius: 10px;
        }
        &:after {
            content: "";
            letter-spacing: 20px;
            background: #fff;
            left: 1px;
            top: 1px;
            height: 18px;
            width: 18px;
        }
    }
}
              
            
!

JS

              
                Number.prototype.pad = function(size) {
    var s = String(this);
    while (s.length < (size || 2)) { s = "0" + s; }
    return s;
}

var app = new Vue({
    el: '#app',
    data: {
        litState: [false, false, false, false],
        sequence: [],
        userSequence: [],
        on: false,
        gameInProgress: false,
        showPhase: false,
        screenWarn: false,
        strict: false,
    },
    computed: {
        level() { return this.sequence.length },
        screen() {
            if (!this.on) { return '__' }
            if (this.screenWarn) { return '!!' }
            if (this.gameInProgress) { return this.level.pad(2) }
            return '--'
        },
        pressable() {
            if (this.on && this.gameInProgress && !this.showPhase) {
                return true
            }
            return false
        }
    },
    watch: {
        on() {
            this.gameStart()
        }
    },
    methods: {
        lightOn(n) { Vue.set(this.litState, n, true) },
        lightOff(n) { Vue.set(this.litState, n, false) },

        paddleDown(n) { if (this.pressable) { this.lightOn(n) } },
        paddleUp(n) { if (this.pressable) { this.lightOff(n) } },

        click(paddle) {
            if (this.pressable) {
                document.getElementById(`audio-${paddle}`).play()
                this.userSequence.push(paddle)
                this.checkUserSequence()
            }
        },
        checkUserSequence() {
            for (var i in this.userSequence) {
                if (this.userSequence[i] !== this.sequence[i]) {
                    this.levelFailedAnimation()
                    return
                }
            }
            if (this.sequence.length === this.userSequence.length) {
                if (this.sequence.length >= 20) {
                    this.victory()
                } else {
                    setTimeout(() => {
                        this.levelUp()
                    }, 1100)
                }
            }
        },
        levelUp() {
            const randPaddle = Math.floor(Math.random() * 4)
            this.sequence.push(randPaddle)
            this.userSequence = []
            this.showSequence()
        },
        levelFailedAnimation() {
            this.showPhase = true
            setTimeout(_ => this.screenWarn = true, 0)
            setTimeout(_ => this.screenWarn = false, 500)
            setTimeout(_ => this.screenWarn = true, 1000)
            setTimeout(_ => {
                this.screenWarn = false
                this.showPhase = false
                this.levelFailed()
            }, 1500)
        },
        levelFailed() {
            if (this.strict) {
                this.gameStart()
            } else {
                this.userSequence = []
                this.showSequence()
            }

        },
        gameStart() {
            if (this.on) {
                this.sequence = []
                this.userSequence = []
                this.gameInProgress = false
                setTimeout(_ => {
                    this.gameInProgress = true
                    this.levelUp()
                }, 500)
            }
        },
        victory() {
            alert('You made it, you won, all 20. Can you do it again?')
            this.gameStart()
        },
        showSequence() {
            this.showPhase = true

            // create timeouts for the sequence, then a timeout to turn showPhase off
            var timeOn = 500;
            var timeBetween = 1000;
            for (var i in this.sequence) {
                // this timeout is wrapped in a self invoking function so it gets a new copy of each loop
                ((i) => {
                    setTimeout(() => {
                        this.lightPaddle(this.sequence[i], timeOn)
                    }, timeBetween * i)
                })(i)
            }
            // the sequence has been played after timeBetween * max(i) + timeOn
            setTimeout(() => {
                this.showPhase = false
            }, (timeBetween * (this.level - 1)) + timeOn)
        },
        lightPaddle(paddle, timeOn) {
            document.getElementById(`audio-${paddle}`).play()
                // turn on then set timeout to switch off
            this.lightOn(paddle)
            setTimeout(() => {
                this.lightOff(paddle)
            }, timeOn)
        }
    },
})
              
            
!
999px

Console