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.
<h1>Simon Game</h1>
<div id="circle-container">
<div class="quarter" id="top-left" onclick="quarterClicked(this.id)"></div>
<div class="quarter" id="top-right" onclick="quarterClicked(this.id)"></div>
<div class="quarter" id="bottom-left" onclick="quarterClicked(this.id)"></div>
<div class="quarter" id="bottom-right" onclick="quarterClicked(this.id)"></div>
<div id="inner">
<div id="button-container">
<button class="round-button" id="go" onclick="goButtonClicked()" title="Click to start or restart the game">GO!</button>
<div id="display">--</div>
</div>
</div>
</div>
body {
font-family: Arial, sans-serif;
background-color: black;
vertical-align: middle;
}
h1 {
text-align: center;
line-height: 0.8em;
color: white;
margin: 3em;
}
.glow {
box-shadow: 0 0 8px rgba(255, 255, 255, 0.8);
transition: box-shadow linear 0.05s;
}
#circle-container {
background-color: black;
position: relative;
top: -30px;
margin-left: auto;
margin-right: auto;
width: 300px;
height: 300px;
border-radius: 50%;
border: 2px solid black;
/* box-shadow: 2px 4px 1px #888888; */
}
.quarter {
width: 148px;
height: 148px;
border: 1px solid black;
cursor: default;
}
#top-left {
border-top-left-radius: 50px;
background: darkgreen;
float: left;
}
#top-right {
border-top-right-radius: 50px;
background: darkred;
float: right;
}
#bottom-left {
border-bottom-left-radius: 50px;
background: goldenrod;
float: left;
}
#bottom-right {
border-bottom-right-radius: 50px;
background: midnightblue;
float: right;
}
#inner {
position: relative;
height: 160px;
width: 160px;
border-radius: 50%;
left: 70px;
top: 70px;
background-color: #101010;
border: 1px solid black;
color: antiquewhite;
}
#display {
border: 1px solid #f5f5f5;
border-radius: 4px;
width: 30px;
height: 24px;
margin: 0 auto;
font-family: VT323, monospace;
font-size: 22px;
font-weight: bold;
color: red;
background-color: RGB(50, 5, 12);
text-shadow: 1px 1px 2px #F00;
position: relative;
top: 10px;
}
#button-container {
position: relative;
top: -250px;
left: 0;
height: 160px;
width: 160px;
border-radius: 50%;
color: antiquewhite;
text-align: center;
}
.round-button {
cursor: pointer;
width: 60px;
height: 60px;
line-height: 40px;
border: 1px solid #f5f5f5;
border-radius: 50%;
color: #f5f5f5;
text-align: center;
text-decoration: none;
background: blue;
box-shadow: 0 0 3px gray;
font-size: 15px;
font-weight: bold;
}
.round-button:hover {
background: skyblue;
}
.onoffswitch {
position: relative;
margin: 0 auto;
width: 73px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
}
.onoffswitch-checkbox {
display: none;
}
.onoffswitch-label {
display: block;
overflow: hidden;
cursor: pointer;
border: 2px solid #F5F5F5;
border-radius: 18px;
}
.onoffswitch-inner {
display: block;
width: 200%;
margin-left: -100%;
transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before,
.onoffswitch-inner:after {
display: block;
float: left;
width: 50%;
height: 20px;
padding: 0;
line-height: 20px;
font-size: 10px;
color: white;
font-family: Trebuchet, Arial, sans-serif;
font-weight: bold;
box-sizing: border-box;
}
.onoffswitch-inner:before {
content: "STRICT";
padding-left: 10px;
background-color: #6495ED;
color: #FFFFFF;
}
.onoffswitch-inner:after {
content: "SLACK";
padding-right: 10px;
background-color: #EEEEEE;
color: #999999;
text-align: right;
}
.onoffswitch-switch {
display: block;
width: 18px;
margin: 1px;
background: #FFFFFF;
position: absolute;
top: 0;
bottom: 0;
right: 49px;
border: 2px solid #F5F5F5;
border-radius: 18px;
transition: all 0.3s ease-in 0s;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
right: 0;
}
"use strict";
/***********************************************************************
GLOBAL VARIABLES
***********************************************************************/
var quarters = ["top-left", "top-right", "bottom-left", "bottom-right"],
context = new AudioContext(),
repetitions = 0, // Number of intended repetitions
computerList = [], // This array is filled with random values later on
playerList = [], // What the player actually clicked
user = 0, // The variables user and computer are just intuitive aliases for the numbers
computer = 1,
activePlayer = computer,
loopCounter = 0,
clickCounter = 0,
computerLoopTimer = null,
maxRuns = 20, // Player wins after maxRuns
strictMode = false,
looped = false;
/***********************************************************************
HELPER FUNCTIONS
***********************************************************************/
function zeroPad(n) {
if (n < 10) {
return "0" + n;
} else {
return n;
}
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function playSound(frequency) {
var oscillator = context.createOscillator();
oscillator.type = "sine";
oscillator.frequency.value = frequency;
oscillator.connect(context.destination);
oscillator.start(context.currentTime);
oscillator.stop(context.currentTime + 0.45);
}
function setBackgroundColor(id, color) {
document.getElementById(id).style.backgroundColor = color;
}
function removeClass(id, cssClass) {
var str = " " + cssClass + "$",
re = new RegExp(str, "g");
document.getElementById(id).className = document.getElementById(id).className.replace(re, '');
}
function setCursorForQuarters(cursor) {
var index = 0,
id = "";
for (index in quarters) {
id = quarters[index];
document.getElementById(id).style.cursor = cursor;
}
}
/***********************************************************************
CORE GAME FUNCTIONS
***********************************************************************/
// This function makes an individual quarter (identified by the id parameter)
// glow and plays its corresponding sound
function flashQuarter(id) {
document.getElementById(id).className += " glow";
if (id === "top-left") {
setBackgroundColor(id, "lawngreen");
playSound(500);
setTimeout(setBackgroundColor, 450, id, "darkgreen");
} else if (id === "top-right") {
document.getElementById(id).style.backgroundColor = "tomato";
playSound(450);
setTimeout(setBackgroundColor, 450, id, "darkred");
} else if (id === "bottom-left") {
document.getElementById(id).style.backgroundColor = "#ffff66";
playSound(400);
setTimeout(setBackgroundColor, 450, id, "goldenrod");
} else if (id === "bottom-right") {
document.getElementById(id).style.backgroundColor = "deepskyblue";
playSound(350);
setTimeout(setBackgroundColor, 450, id, "midnightblue");
}
setTimeout(removeClass, 450, id, "glow");
}
// This function flashes all quarters of the circle twice when the page loads
function flashAll() {
var index = 0,
id = "";
playSound(500);
setBackgroundColor("go", "deepskyblue");
setTimeout(setBackgroundColor, 700, "go", "blue");
for (index in quarters) {
id = quarters[index];
if (id === "top-left") {
document.getElementById(id).className += " glow";
setBackgroundColor(id, "lawngreen");
setTimeout(setBackgroundColor, 700, id, "darkgreen");
setTimeout(removeClass, 700, id, "glow");
continue; // we found the right one, no need to test the other cases
} else if (id === "top-right") {
document.getElementById(id).className += " glow";
setBackgroundColor(id, "tomato");
setTimeout(setBackgroundColor, 700, id, "darkred");
setTimeout(removeClass, 700, id, "glow");
continue;
} else if (id === "bottom-left") {
document.getElementById(id).className += " glow";
setBackgroundColor(id, "#ffff66");
setTimeout(setBackgroundColor, 700, id, "goldenrod");
setTimeout(removeClass, 700, id, "glow");
continue;
} else if (id === "bottom-right") {
document.getElementById(id).className += " glow";
setBackgroundColor(id, "deepskyblue");
setTimeout(setBackgroundColor, 700, id, "midnightblue");
setTimeout(removeClass, 700, id, "glow");
}
}
// Play it a second time after 1000 milliseconds:
if (!looped) {
setTimeout(flashAll, 1000);
}
looped = true;
activePlayer = user;
}
// Called from computerMoves; flashes the quarters stored in computerList
function loopQuarters() {
flashQuarter(quarters[computerList[loopCounter]]);
loopCounter++;
// Clear interval after given number of repetitions
if (loopCounter >= repetitions) {
clearInterval(computerLoopTimer);
loopCounter = 0;
return;
}
}
function computerMoves() {
activePlayer = computer;
loopCounter = 0;
computerLoopTimer = setInterval(loopQuarters, 750);
repetitions++;
document.getElementById("display").innerHTML = zeroPad(repetitions).toString();
activePlayer = user;
setCursorForQuarters("pointer");
}
function initializeGame() {
repetitions = 0;
playerList = [];
activePlayer = computer;
loopCounter = 0;
clickCounter = 0;
document.getElementById("display").innerHTML = "--";
}
// Fill the computerList array with maxRuns (20) random integers from 0 to 3
function initializeComputerList() {
computerList = [];
for (var i = 0; i < maxRuns; i++) {
computerList.push(getRandomInt(0, 3));
}
}
function goButtonClicked() {
// If it's not the user's turn, they have no business pressing the go button
if (activePlayer === user) {
// Ask for confirmation if a game is already running
if (repetitions > 0) {
var answer = confirm("Do you want to start over?");
if (answer === true) {
initializeGame();
initializeComputerList();
computerMoves();
} else {
return;
}
} else {
initializeComputerList();
initializeGame();
// Set button to darker color
document.getElementById("go").style.backgroundColor = "CornflowerBlue";
computerMoves();
}
}
}
// This function is called whenever the player clicks one of the quarters
function quarterClicked(id) {
// return early if it's not the player's turn
if (activePlayer !== user) {
// console.log("returning");
return;
}
flashQuarter(id);
clickCounter++;
playerList.push(id);
// Player made a mistake
if (id !== quarters[computerList[clickCounter - 1]]) {
document.getElementById("display").innerHTML = "!!";
playSound(200);
if (strictMode === true) {
initializeComputerList();
setTimeout(initializeGame, 500);
setTimeout(computerMoves, 900);
return;
}
// repeat the old sequence if the user made a mistake
repetitions--; // repetitions is incremented in computerMoves.
activePlayer = computer;
setTimeout(setCursorForQuarters, 500, "default");
setTimeout(computerMoves, 1000);
clickCounter = 0;
return; // return early so the following isn't executed
}
// Player wins
if (clickCounter == maxRuns) {
document.getElementById("display").innerHTML = "WIN";
looped = false;
setTimeout(flashAll, 800);
setTimeout(initializeGame, 4500);
initializeComputerList();
setTimeout(computerMoves, 7000);
return;
}
// No mistake. Next round with one more flashing quarter.
if (clickCounter >= repetitions) {
activePlayer = computer;
setTimeout(setCursorForQuarters, 500, "default");
setTimeout(computerMoves, 1000);
clickCounter = 0;
}
}
window.onload = flashAll();
Also see: Tab Triggers