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 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.
.infos.fadeIn.animated
h2 Project 4.
h1 Memory<br>Blocks
h3.status -
.blocks.fadeIn.animated
.row
.block.block1(onclick="game.userSendInput('1')") 1
.block.block2(onclick="game.userSendInput('2')") 2
.row
.block.block3(onclick="game.userSendInput('3')") 3
.block.block4(onclick="game.userSendInput('4')") 4
.row
.inputStatus
$colorRed: #FF5353
$colorYellow: #FFC429
$colorBlue: #5980C1
$colorWhite: #FBE9B7
@mixin size ($w,$h:$w)
width: $w
height: $h
@import url('https://fonts.googleapis.com/css?family=Roboto:700')
html,body
font-family: "roboto"
background-color: #232526
display: flex
justify-content: center
align-items: center
flex-direction: column
+size(100%)
margin: 0
padding: 0
border: solid 5px rgba(white,0.05)
transition: 0.5s
box-sizing: border-box
&:active
transition: 0s
border: solid 5px rgba(white,0.1)
.infos
color: white
position: absolute
left: 40px
top: 40px
z-index: 100
h1
margin-top: 0
margin-bottom: 0
font-size: 40px
letter-spacing: 1px
line-height: 1.2
h3
font-weight: 500
color: $colorRed
.blocks
.row
display: flex
.block
animation: comeOut 1s
width: 100px
height: 100px
border: solid 1px rgba(white,0.3)
display: flex
justify-content: center
align-items: center
transition-duration: 0.6s
tranisiton-delay: 0.2s
cursor: pointer
margin: 12px
color: white
font-weight: 100
@mixin setBlockColor($color)
box-shadow: 0px 0px 35px rgba($color,0.2) inset, 0px 0px 30px rgba($color,0.2)
background-color: transparent
border: solid 1px lighten($color,5)
color: transparent
&:hover
background-color: rgba($color,0.3)
box-shadow: 0px 0px 35px rgba($color,0.2)
&:active , &.active
background-color: $color
box-shadow: 0px 0px 35px $color
// color: white
// transform: scale(0.95)
&:active , &.active
background-color: red
transition-duration: 0s
&.block1
+setBlockColor($colorRed)
&.block2
+setBlockColor($colorYellow)
&.block3
+setBlockColor($colorBlue)
&.block4
+setBlockColor($colorWhite)
.inputStatus
margin-top: 10px
.circle
+size(6px)
display: inline-block
opacity: 0.3
background-color: white
border-radius: 100%
margin: 5px
&.correct,.wrong
opacity: 1
&.wrong .circle
background-color: $colorRed
&.correct .circle
background-color: $colorBlue
//--------------------------------
// 方塊物件
//--------------------------------
var Blocks = function(blockAssign,setAssign){
this.allOn=false
this.blocks=blockAssign.map((d,i)=>
({
name: d.name,
el: $(d.selector),
audio: this.getAudioObject(d.pitch)
})
)
this.soundSets = setAssign.map((d,i)=>
({
name: d.name,
sets: d.sets.map((pitch)=>this.getAudioObject(pitch))
})
)
}
//閃爍單一方塊+聲音(方塊名)
Blocks.prototype.flash=function(note){
let block = this.blocks.find(d=>d.name==note)
if (block){
block.audio.currentTime=0
block.audio.play()
block.el.addClass("active")
setTimeout(()=>{
if (this.allOn==false){
block.el.removeClass("active")
}
},100)
}
}
//點亮所有方塊
Blocks.prototype.turnOnAll=function(note){
this.allOn=true
this.blocks.forEach(d=>{
d.el.addClass("active")
})
}
//關掉所有方塊
Blocks.prototype.turnOffAll=function(note){
this.allOn=false
this.blocks.forEach(d=>{
d.el.removeClass("active")
})
}
//取得聲音物件
Blocks.prototype.getAudioObject=function(pitch){
var audio = new Audio("https://awiclass.monoame.com/pianosound/set/"+ pitch+".wav")
audio.setAttribute("preload","auto")
return audio
}
//播放序列聲音(成功/失敗...)
Blocks.prototype.playSet = function(type){
this.soundSets
.find(set => set.name==type).sets
.forEach(o=>{
o.currentTime=0
o.play()
})
}
//--------------------------------
// 遊戲物件
//--------------------------------
var Game = function (){
//定義關卡
this.blocks = new Blocks(
[
{selector: ".block1", name: "1", pitch: "1"},
{selector: ".block2", name: "2", pitch: "2"},
{selector: ".block3", name: "3", pitch: "3"},
{selector: ".block4", name: "4", pitch: "4"}
],
[
{name: "correct", sets: [1,3,5,8] },
{name: "wrong", sets: [2,4,5.5,7] }
]
)
this.levels = [
"1234",
"12324",
"231234",
"41233412",
"41323134132",
"2342341231231423414232"
]
let _this = this
//下載關卡
$.ajax({
url: "https://2017.awiclass.monoame.com/api/demo/memorygame/leveldata",
success: function(res){
_this.levels = res
}
})
//設定現在在的關卡跟播放間隔
this.currentLevel = 0
this.playInterval = 400
this.mode="waiting"
}
//開始關卡
Game.prototype.startLevel = function(){
// console.log("start Level "+ this.currentLevel)
this.showMessage("Level "+ this.currentLevel)
this.startGame(this.levels[this.currentLevel])
}
//顯示訊息
Game.prototype.showMessage = function(message){
console.log(message)
$(".status").text(message)
}
//開始遊戲(答案)
Game.prototype.startGame = function(answer){
this.mode="gamePlay"
this.answer = answer
let notes = this.answer.split("")
let _this = this
this.showStatus("")
this.timer = setInterval(function(){
let char = notes.shift()
if (!notes.length){
clearInterval(_this.timer)
_this.startUserInput()
}
_this.playNote(char)
},this.playInterval)
}
//播放音符
Game.prototype.playNote = function(note){
console.log(note)
this.blocks.flash(note)
}
//開始輸入模式
Game.prototype.startUserInput = function(){
this.userInput = ""
this.mode="userInput"
}
//使用者輸入
Game.prototype.userSendInput = function(inputChar){
if (this.mode=="userInput"){
let tempString = this.userInput + inputChar
this.playNote(inputChar)
this.showStatus(tempString)
if (this.answer.indexOf(tempString)==0){
if (this.answer==tempString){
this.currentLevel+=1
this.mode=="waiting"
setTimeout(()=>{
this.startLevel()
},1000)
}
this.userInput += inputChar
}else{
this.currentLevel=0
this.mode=="reset"
setTimeout(()=>{
this.startLevel()
},1000)
}
}
}
//顯示回答狀態
Game.prototype.showStatus = function(tempString){
$(".inputStatus").html("")
this.answer.split("").forEach((d,i)=>{
var circle = $("<div class='circle'></div>")
if(i<tempString.length){
circle.addClass("correct")
}
$(".inputStatus").append(circle)
})
if (tempString == this.answer){
$(".inputStatus").addClass("correct")
this.showMessage("Correct!")
setTimeout(()=>{
this.blocks.turnOnAll()
this.blocks.playSet("correct")
},500)
}else{
$(".inputStatus").removeClass("correct")
}
if (tempString==""){
this.blocks.turnOffAll()
}
if (this.answer.indexOf(tempString)!=0){
this.showMessage("Wrong...")
$(".inputStatus").addClass("wrong")
this.blocks.turnOnAll()
this.blocks.playSet("wrong")
}else{
$(".inputStatus").removeClass("wrong")
}
}
//--------------------
// 開新遊戲
var game = new Game()
setTimeout(function(){
game.startLevel()
},1000)
Also see: Tab Triggers