HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URL's 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 it's URL and the proper URL extention.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Simon-Free Code Camp</title>
<link rel="stylesheet" type="text/css" href="css/app.css">
<link href="https://fonts.googleapis.com/css?family=Teko:700" rel="stylesheet">
</head>
<body>
<div id="wrap">
<div id="game">
<div id="buttons">
<button id="btn_green" class="btn_gameplay" value="1" disabled></button>
<button id="btn_red" class="btn_gameplay" value="2" disabled></button>
<button id="btn_yellow" class="btn_gameplay" value="3" disabled></button>
<button id="btn_blue" class="btn_gameplay" value="4" disabled></button>
</div>
<div id="center">
<div id="name"><h1>Simon</h1></div>
<div id="main_controls">
<div id="count">
<h2>--</h2>
<h3 id="count">count</h3>
</div>
<div id="start">
<button id="btn_start" disabled></button>
<h3>start</h3>
</div>
<div id="strict">
<button id="btn_strict" disabled></button>
<div class="strict_indicator"></div>
<h3>strict</h3>
</div>
</div>
<div id="power">
<h3>off
<span id="off-on">
<label class="switch">
<input type="checkbox">
<span class="slider"></span>
</label>
</span>
on</h3>
</div>
</div>
<div id="background"></div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="js/app.js"></script>
</body>
</html>
/* Global */
* {
padding: 0;
margin: 0;
border: none;
text-align: center;
}
html {
box-sizing: border-box;
overflow: hidden; /* Eliminates scroll bar */
}
#wrap {
display: flex;
height: 100vh; /* Need height or won't vertically center */
justify-content: center;
align-items: center;
/* Photo by artbaggage from Pixabay.com */
background: center no-repeat url('https://raw.githubusercontent.com/Feldbot/fCCassets/master/project-10-build-a-simon-game/circular-837510_1920.jpg');
}
#game {
position: relative;
width: 310px;
height: 310px;
}
#buttons {
position: absolute;
top: 50%;
left: 50%;
margin: -139px 0 0 -139px;
display: grid;
grid-template-columns: repeat(2, 135px);
grid-template-rows: repeat(2, 135px);
grid-gap: .5em; /* 8px */
grid-template-areas:
'green red'
'yellow blue';
}
.btn_gameplay {
width: 135px;
height: 135px;
opacity: .5;
}
#btn_green {
grid-area: green;
background-color: rgb(62,222,75);
border-radius: 100% 0 0 0;
}
#btn_red {
grid-area: red;
background-color: rgb(222,75,62);
border-radius: 0 100% 0 0;
}
#btn_blue {
grid-area: blue;
background-color: rgb(75,62,222);
border-radius: 0 0 100% 0;
}
#btn_yellow {
grid-area: yellow;
background-color: rgb(255,234,55);
border-radius: 0 0 0 100%;
}
/* Center UI controls */
#center {
z-index: 10;
position: absolute;
top: 50%;
left: 50%;
margin: -80px 0 0 -80px;
width: 160px;
height: 160px;
background-color: rgb(255,255,255);
border: 1px solid rgb(0,0,0);
border-radius: 50%;
}
#name {
margin-top: -5px;
}
#main_controls {
display: flex;
justify-content: space-around;
height: 50px;
margin: 0px 5px;
position: relative;
}
div#count{
background-color: rgb(25,25,25);
border-radius: 6px;
width: 30px;
height: 25px;
position: relative;
top: -2px;
}
#btn_start, #btn_strict {
width: 20px;
height: 20px;
border: 1px solid grey;
border-radius: 50%;
}
#btn_start {
background-color: rgb(255,0,0);
}
#btn_strict {
background-color: rgb(255,255,0);
}
#strict {
position: relative;
}
.strict_indicator {
width: 10px;
height: 10px;
background-color: rgb(25,25,25);
border-radius: 50%;
position: absolute;
top: -10px;
right: 0;
}
.red_indicator {
background-color: rgb(255,0,0);
}
/* Toggle switch code: https://www.w3schools.com/howto/howto_css_switch.asp */
/* The switch box around On/Off slider */
.switch {
position: relative;
display: inline-block;
width: 35px;
height: 21px;
}
/* Hide default HTML checkbox */
.switch input {display: none;}
/* The slider */
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .2s;
transition: .2s;
}
.slider:before {
position: absolute;
content: "";
height: 15px;
width: 15px;
left: 4px;
bottom: 3px;
background-color: white;
-webkit-transition: .2s;
transition: .2s;
}
input:checked + .slider { /* + is adjacent sibling combinator */
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(13px);
-ms-transform: translateX(13px);
transform: translateX(13px);
}
#background {
background-color: rgb(22,22,22);
z-index: -10;
width: 310px;
height: 310px;
border-radius: 50%;
box-shadow: 10px 10px 22px rgb(72,72,72);
}
/* Typography */
h1, h2, h3 {
font-family: sans-serif;
text-align: center;
}
h1 {
font-family: "Teko";
font-weight: 700;
font-size: 2.5em;
margin-top: 15px;
}
h2 {
font-size: 1.2em;
color: rgb(90,25,25);
}
h3 {
font-size: .8em;
margin-top: 5px;
}
h3#count {
padding-top: 4px;
}
/* Responsive (tablet and above ) */
@media screen and (min-width: 600px) {
#game {
position: relative;
width: 510px;
height: 510px;
}
#buttons {
margin: -228px 0 0 -228px;
grid-template-columns: repeat(2, 220px);
grid-template-rows: repeat(2, 220px);
grid-gap: 1em;
}
.btn_gameplay {
width: 220px;
height: 220px;
}
#center {
margin: -130px 0 0 -130px;
width: 260px;
height: 260px;
}
#main_controls {
justify-content: space-around;
height: 75px;
margin: 10px;
}
div#count{
border-radius: 6px;
width: 45px;
height: 40px;
top: -10px;
}
#btn_start, #btn_strict {
width: 25px;
height: 25px;
}
.strict_indicator {
width: 12px;
height: 12px;
top: -15px;
right: 0;
}
.switch {
margin: -5px 3px;
width: 45px;
height: 26px;
}
.slider:before {
height: 18px;
width: 18px;
left: 6px;
bottom: 4px;
background-color: white;
-webkit-transition: .2s;
transition: .2s;
}
input:checked + .slider:before {
-webkit-transform: translateX(15px);
-ms-transform: translateX(15px);
transform: translateX(15px);
}
#background {
width: 510px;
height: 510px;
box-shadow: 20px 20px 42px rgb(72,72,72);
}
h1 {
font-size: 4em;
margin-top: 30px;
}
h2 {
font-size: 2em;
margin-top: 2px;
}
h3 {
font-size: 1em;
margin-top: 13px;
}
h3#count {
padding-top: 0;
margin-top: 12px;
}
}
var simon = {
init: function() {
this.count = 1; // player move count
this.round = 1; // game round
this.sequence = [];
this.player_move = null;
this.player_turn = false;
},
count: 1,
round: 1,
strict: false,
power_on: false,
player_turn: false,
sequence: [],
nextSequence: function() {
return this.sequence.slice(0, this.round + 1);
},
currentSequence: function() {
return this.sequence.slice(0, this.round);
},
player_sequence: [],
current_move: null,
player_move: null,
keypress: 0,
error: false,
mapFeedback: function(num) {
var sound, light;
switch (num) {
case 1: // Green
sound = new Audio ('https://raw.githubusercontent.com/Feldbot/fCCassets/master/project-10-build-a-simon-game/1_green.mp3'),
light = $("#btn_green").css("opacity", "1");
if (simon.player_turn) { simon.player_move = 1; }
if (!simon.player_turn) { simon.current_move = 1; }
break;
case 2: // Red
sound = new Audio ('https://raw.githubusercontent.com/Feldbot/fCCassets/master/project-10-build-a-simon-game/2_red.mp3'),
light = $("#btn_red").css("opacity", "1");
if (simon.player_turn) { simon.player_move = 2; }
if (!simon.player_turn) { simon.current_move = 2; }
break;
case 3: // Yellow
sound = new Audio ('https://raw.githubusercontent.com/Feldbot/fCCassets/master/project-10-build-a-simon-game/3_yellow.mp3'),
light = $("#btn_yellow").css("opacity", "1");
if (simon.player_turn) { simon.player_move = 3; }
if (!simon.player_turn) { simon.current_move = 3; }
break;
case 4: // Blue
sound = new Audio ('https://raw.githubusercontent.com/Feldbot/fCCassets/master/project-10-build-a-simon-game/4_blue.mp3'),
light = $("#btn_blue").css("opacity", "1");
if (simon.player_turn) { simon.player_move = 4; }
if (!simon.player_turn) { simon.current_move = 4; }
break;
}
renderFeedback(sound, light);
}
}
// Timers
var playAlertID,
playCurrentSequenceID;
simon.init(); // Start game
// Power off/on button
$("label.switch input").on("click", function() {
if (simon.power_on === false) {
simon.power_on = true;
$("h2").css("color", "rgb(242,72,72)");
// Turn on Start/Strict buttons
$("#btn_start, #btn_strict")
.prop("disabled", false).css("cursor", "pointer");
console.log('power on');
} else {
simon.power_on = false;
$("h2").css("color", "rgb(90,25,25)");
// Turn off buttons and behavior
$("#btn_start, #btn_strict, .btn_gameplay")
.prop("disabled", true).css("cursor", "default");
$("h2").text("--");
$(".strict_indicator").removeClass("red_indicator");
clearTimeout(playAlertID);
clearTimeout(playCurrentSequenceID);
console.log('power off');
}
})
// Start/strict event handlers
$("#btn_start").on("click", function() { startGame(); });
$("#btn_strict").on("click", function() {
simon.strict = !simon.strict;
$(".strict_indicator").toggleClass("red_indicator");
});
// Capture player move, update board, and check input
$(".btn_gameplay").on("click", function() {
simon.player_move = parseInt(this.value);
// TODO Limit keypresses to number of rounds
// simon.keypress++;
// if (simon.keypress > simon.round) {
// $(".btn_gameplay").prop("disabled", true).css("cursor", "default");
// $(".btn_gameplay").off("click");
// console.log('lay off da keys!');
// }
// playTimer();
checkMove();
});
// Colored buttons UI availability
function toggleButtons() {
if (simon.player_turn) {
$(".btn_gameplay").prop("disabled", false).css("cursor", "pointer");
} else {
$(".btn_gameplay").prop("disabled", true).css("cursor", "default");
$(".btn_gameplay").off("click");
}
}
function startGame() {
simon.init();
alert("--");
createSequence();
setTimeout(playSequence, 1000, [simon.sequence[0]]);
}
function createSequence() {
simon.sequence = [];
for (var i = 0; i < 20; i++) {
simon.sequence.push(Math.ceil(Math.random() * (4)));
}
}
// Iterates over sequence array and plays individual element
function playSequence(sequence) {
simon.player_turn = false;
$("h2").text(simon.round);
for (let i = 0; i < sequence.length; i++) {
setTimeout(function() {
simon.mapFeedback(sequence[i]);
// When length is met queue player turn and start timer
if (i === sequence.length - 1) {
setTimeout(function() {
takeTurn();
}, 1500);
}
}, i * 1000); // increase time with each round
}
}
function renderFeedback(sound, light) {
// If no error play sound (otherwise error buzz plays)
if (!simon.error) {
sound.play();
}
light;
setTimeout(function() {
light.css("opacity", ".5");
}, 250, light);
}
function takeTurn() {
simon.player_turn = true;
toggleButtons();
// playTimer(); // Starts timer input or not
}
// Alerts and resets game if player times out
// TODO: Get this function working!
// function playTimer() {
// playAlertID = setTimeout(function() {
// alert('!!', 'buzz');
// console.log('timeout!')
// // Replays current sequence
// playCurrentSequenceID = setTimeout(playSequence, 1000, simon.currentSequence());
// // If input entered clear timer
// if (simon.player_move !== null) {
// console.log('player moved')
// clearTimeout(playAlertID);
// clearTimeout(playCurrentSequenceID);
// }
// }, 3000);
// }
function alert(text, sound) {
var win, buzz;
// Blink text and display current round
$("h2").text(text).fadeOut(250).fadeIn(250).fadeOut(250).fadeIn(250);
if (sound === 'win') {
// Courtesy https://freesound.org/s/342218/
win = new Audio ('https://raw.githubusercontent.com/Feldbot/fCCassets/master/project-10-build-a-simon-game/win.mp3'),
setTimeout(function() {
win.play();
}, 500);
setTimeout(startGame, 3500);
} else if (sound === 'buzz') {
// Courtesy RICHERlandTV, freesound.org
// https://freesound.org/s/216090/
// https://creativecommons.org/licenses/by/3.0/
// Changed filename from:
// 216090__richerlandtv__bad-beep-incorrect
buzz = new Audio ('https://raw.githubusercontent.com/Feldbot/fCCassets/master/project-10-build-a-simon-game/buzz.mp3'),
buzz.play();
}
}
function checkMove() {
// Check if player move matches the current sequence
if (simon.player_move === simon.currentSequence()[simon.count - 1]) {
clearTimeout(playAlertID);
clearTimeout(playCurrentSequenceID);
// Generate player sequence array from player moves
simon.player_sequence[simon.count - 1] = simon.player_move;
simon.mapFeedback(simon.player_move);
// Advance sequence when last match is made
if (simon.count === simon.currentSequence().length) {
simon.count = 1; // Check at beginning on next round
// Game not won - play next sequence
if (simon.round !== 9) {
setTimeout(playSequence, 1000, simon.nextSequence());
simon.player_move = null;
// Game won - alert victory and restart
} else if (simon.round === 9) {
alert('**', 'win');
simon.init();
}
simon.round++;
// Keep checking sequence until last match
} else {
simon.count++;
// playTimer();
}
// If player move doesn't match current sequence
} else {
simon.error = true;
simon.mapFeedback(simon.player_move);
simon.error = false;
alert('!!', 'buzz');
simon.player_move = null;
simon.count = 1;
playCurrentSequenceID = setTimeout(playSequence, 1000, simon.currentSequence());
// Start over again in non-strict mode
if (simon.strict) {
clearTimeout(playCurrentSequenceID);
setTimeout(startGame, 1300);
}
}
}
/***** Notes *****
// Clear all timeouts strategy (create simon.timeouts array)
simon.timeouts.push(playAlertID, playCurrentSequenceID);
function clearAllTimeouts() {
console.log('simon.timeouts ', simon.timeouts);
for (var i = 0; i < simon.timeouts.length; i++) {
clearTimeout(simon.timeouts[i]);
}
simon.timeouts = [];
}
*/
Also see: Tab Triggers