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 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.
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 esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM 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.
<head>
<link href="https://fonts.googleapis.com/css?family=Bree+Serif|VT323" rel="stylesheet">
</head>
<body>
<!-- audio elements for each button -->
<audio id="audio-button-tl">
<source src="https://s3.amazonaws.com/freecodecamp/simonSound1.mp3" type="audio/mpeg" />
</audio>
<audio id="audio-button-tr">
<source src="https://s3.amazonaws.com/freecodecamp/simonSound2.mp3" type="audio/mpeg" />
</audio>
<audio id="audio-button-bl">
<source src="https://s3.amazonaws.com/freecodecamp/simonSound3.mp3" type="audio/mpeg" />
</audio>
<audio id="audio-button-br">
<source src="https://s3.amazonaws.com/freecodecamp/simonSound4.mp3" type="audio/mpeg" />
</audio>
<div class="game-container">
<div class="button-container">
<!-- tl = top-left, br = bottom-right, etc -->
<div class="row">
<button id="button-tl" value="1" class="game-btn">
</button>
<button id="button-tr" value="2" class="game-btn">
</button>
</div>
<div class="row">
<button id="button-bl" value="4" class="game-btn">
</button>
<button id="button-br" value="3" class="game-btn">
</button>
</div>
<div class="overlay-container">
<div class="interface-container">
<h1 class="game-title">simon</h1>
<div class="row">
<label class="label" for="button-start">START</label>
<button class="button-small" id="button-start"></button>
<label id="strict-label" class="label" for="button-strict">STRICT</label>
<button class="button-small" id="button-strict"></button>
<label class="label" for="button-reset">RESET</label>
<button class="button-small" id="button-reset"></button>
</div>
<div class="row">
<input class="digital-screen" placeholder="--" disabled>
<button class="strict-led" disabled></button>
</div>
</div>
</div>
</div>
</div>
<h1 class="win-lose-text">
You won!
</h1>
</body>
/* Remove button outline after it is clicked in chrome*/
button:focus {
outline: none;
}
body {
background-image: url("http://cdn.backgroundhost.com/backgrounds/subtlepatterns/45degreee_fabric.png");
background-repeat: repeat;
background-size: auto auto;
/* Make text unselectable */
-webkit-touch-callout: none; /* iOS Safari */
-webkit-user-select: none; /* Chrome/Safari/Opera */
-khtml-user-select: none; /* Konqueror */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* Internet Explorer/Edge */
user-select: none;
}
.win-lose-text {
font-family: Verdana;
display: none;
height: 80px;
line-height: 80px;
margin: auto;
margin-top: 50px;
text-align: center;
vertical-align: baseline;
}
.game-container {
position: relative;
margin: 2% auto;
width: 500px;
height: 500px;
border-radius: 100%;
background: #343838;
box-shadow: 4px 4px 5px #888888;
}
.button-container {
width: 540px;
height: 540px;
margin: auto;
padding: 30px;
padding-left: 40px;
position: relative;
}
.game-btn {
border: none;
height: 200px;
width: 200px;
margin: 10px;
}
.overlay-container {
position: absolute;
background: #343838;
border-radius: 100%;
top: 26%;
left: 26%;
height: 40%;
width: 40%;
z-index: 5;
}
.interface-container {
background: #FEFEFE;
margin: 10%;
height: 80%;
width: 80%;
border-radius: 100%;
text-align: center;
}
.game-title {
padding-top: 24px;
font-family: 'Bree Serif', serif;
font-size: 40px;
font-weight: 600;
}
.digital-screen {
position: relative;
margin-left: 7%;
font-family: 'VT323', mono;
color: rgba(0,0,0, .5);
font-size: 36px;
line-height: 44px;
text-align: center;
background: #B6C2A1;
border: 2px inset rgba(0,0,0,0.2);
border-radius: .1em;
box-shadow: 0 0 1.5em .1em rgba(0,0,0,0.25) inset;
width: 1.2em;
height: 1em;
}
.label {
position: absolute;
padding: 4px;
color: black;
}
.button-small {
margin: 14px;
width: 18px;
height: 18px;
border-radius: 100%;
border: 2px solid #343838;
box-shadow: 0 1px rgba(0, 0, 0, 0.4);
background: black;
}
.strict-led {
vertical-align: top;
padding: 4px;
width: 4px;
height: 4px;
background-color: #460000;
border-radius: 100%;
border: 1px solid #343838;
}
.strict-led-enabled {
background-color: red;
}
#button-tl {
border-radius: 100% 0px 0px 0px;
background: #FFD026;
}
#button-tr {
border-radius: 0px 100% 0px 0px;
background: #2659A9;
}
#button-bl {
border-radius: 0px 0px 0px 100%;
background: #D62626;
}
#button-br {
border-radius: 0px 0px 100% 0px;
background: #26B644;
}
#button-reset {
margin-top: 20px;
background: red;
}
#button-start {
background: green;
}
#button-strict {
background: yellow;
}
// The javascript for this game is heavily based on the Simon Says project by Kelly King which can be found on GitHub, adapted to work within my HTML structure for the game (https://github.com/kellyk/javascript-simon)
// Globals to minimize magic numbers
var GAME_LOST = -1;
var GAME_WON = 1;
var YEL_BTN = 1;
var BLU_BTN = 2;
var GRN_BTN = 3;
var RED_BTN = 4;
// The audio elements for each button, will be bound on DOM load
var YEL_BTNSOUND, BLU_BTNSOUND, GRN_BTNSOUND, RED_BTNSOUND;
// global strict mode
var strictMode = false;
// define a Simon object, that will represent the game
var Simon = {
sequence: [],
sequence_copy: [],
steps: 0,
active: true,
// Initialize a new game state, and start the game
startGame: function() {
this.sequence = [];
this.sequence_copy = [];
this.steps = 0;
this.active = true;
this.nextRound(true);
},
// Resets member variables to their initial values
reset: function() {
this.sequence = [];
this.sequence_copy = [];
this.steps = 0;
this.active = true;
strictMode = false;
// Reset the css of the screen and led
$(".strict-led").toggleClass("strict-led-enabled", strictMode);
$(".digital-screen").attr("value", "");
},
// Runs a round of the game, by adding a new
// button to the sequence, updating the steps counter,
// and animating the new sequence
// The addToSequence boolean value enables non-strict mode
nextRound: function(addToSequence) {
if (addToSequence) {
this.sequence.push(this.getRandomButton());
this.steps++;
$(".digital-screen").attr("value", this.steps.toString());
}
this.sequence_copy = this.sequence.slice(0);
this.animateSequence();
},
// Checks whether the player has won, lost,
// or if the game needs to continue
updateState: function() {
// If the sequence_copy is empty, the user input the correct click
// and it is round 20, the game is over and the player won
// If it's not round 20, the game continues
if (this.sequence_copy.length === 0 && this.active) {
if (this.steps >= 20) {
this.endGame(GAME_WON);
}
else {
this.deactivateBoard();
this.nextRound(true);
}
}
// If the player clicked the wrong button, we replay the round
// unless strictMode is enabled, in which case the player loses
else if (!this.active && strictMode) {
this.endGame(GAME_LOST);
}
else if(!this.active) {
this.deactivateBoard();
this.nextRound(false);
}
},
endGame: function(winOrLose) {
if (winOrLose === GAME_WON) {
$(".win-lose-text").text("Congratulations, you won!")
.fadeIn("fast").fadeOut(3200);
}
else {
$(".win-lose-text").text("Oh no, you lost!")
.fadeIn("fast").fadeOut(3200);
}
this.reset();
},
// Returns a random number from 1 to 4,
// representing a game button
getRandomButton: function() {
return Math.floor(Math.random() * 4 + 1);
},
// Animates the current sequence of buttons
// Reactivates interactivity with the board
// once it has completed the animation
animateSequence: function() {
var i = 0;
var localThis = this;
var interval = setInterval(function() {
localThis.playSound(localThis.sequence[i]);
localThis.lightButton(localThis.sequence[i]);
i++;
if (i >= localThis.sequence.length) {
clearInterval(interval);
localThis.activateBoard();
}
}, 800);
},
// Plays the designated sound for the button at buttonIdx
// 1-4 map to buttons, any other number will play the error sound
playSound: function(btnIdx) {
switch (btnIdx) {
case YEL_BTN:
YEL_BTNSOUND.play();
break;
case BLU_BTN:
BLU_BTNSOUND.play();
break;
case GRN_BTN:
GRN_BTNSOUND.play();
break;
case RED_BTN:
RED_BTNSOUND.play();
break;
default:
break;
}
},
// "Lights" up the button at the specified index by changing its color
lightButton: function(btnIdx) {
switch (btnIdx) {
case YEL_BTN:
$("#button-tl").css("background-color", "yellow");
window.setTimeout(function() {
$("#button-tl").css("background-color", "#FFD026");
}, 400);
break;
case BLU_BTN:
$("#button-tr").css("background-color", "blue");
window.setTimeout(function() {
$("#button-tr").css("background-color", "#2659A9");
}, 400);
break;
case GRN_BTN:
$("#button-br").css("background-color", "green");
window.setTimeout(function() {
$("#button-br").css("background-color", "#26B644");
}, 400);
break;
case RED_BTN:
$("#button-bl").css("background-color", "red");
window.setTimeout(function() {
$("#button-bl").css("background-color", "#D62626");
}, 400);
break;
default:
break;
}
},
// Function for enabling and disabling user input
activateBoard: function() {
$(".game-btn").click(checkClick);
$(".button-small").prop("disabled", false);
},
deactivateBoard: function() {
$(".game-btn").off("click");
$(".button-small").prop("disabled", true);
},
// Toggles strictMode on and off,
// as well as changing the css of the strict-led
changeMode: function() {
strictMode = !strictMode;
$(".strict-led").toggleClass("strict-led-enabled", strictMode);
}
};
// Checks whether the click matched the next one in the sequence
function checkClick() {
var buttonClicked = parseInt($(this).prop("value"));
var desiredClicked = Simon.sequence_copy.shift();
// Play sound/light the button we clicked regardless of whether it is correct
Simon.playSound(buttonClicked);
Simon.lightButton(buttonClicked);
Simon.active = (desiredClicked === buttonClicked);
Simon.updateState();
}
// Bind the necessary buttons to their respective functions
$(document).ready(function() {
// Bind the audio elements
YEL_BTNSOUND = document.getElementById("audio-button-tl");
BLU_BTNSOUND = document.getElementById("audio-button-tr");
GRN_BTNSOUND = document.getElementById("audio-button-br");
RED_BTNSOUND = document.getElementById("audio-button-bl");
$("#button-start").click(function() {
Simon.startGame();
});
$("#button-strict").click(Simon.changeMode);
$("#button-reset").click(Simon.reset);
});
Also see: Tab Triggers