JSFiddle - React, Tailwind, and code Playground
HTML
<div id="field">
<input class="start-btn" type="button" value="Start Game">
</div>
CSS
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
background-color: #000;
}
#field{
height: 500px;
width: 800px;
margin: 40px auto;
border: 1px solid white;
position: relative;
background-color: green;
}
#field > .start-btn{
margin:5px;
width: 160px;
height: 35px;
font-family: verdana;
font-weight: bold;
border: 1px solid black;
border-radius: 3px;
font-size: 15px;
background-color: #60C4E6;
cursor: pointer;
}
#field > .start-btn:hover{
background-color: #6DD5F7;
}
/*#field > .start-btn:active{
margin-top:4px;
}*/
#field > .ctr{
background-color: white;
height: 100px;
width: 100px;
position: absolute;
}
#field > .ctr > .pika{
height: 90px;
width: 80px;
position: absolute;
background-color: green;
top: 3px;
left:10px;
overflow: hidden;
/*z-index: 2;*/
}
#field > .ctr > .pika > div{
height: 90px;
width: 80px;
background-color: blue;
margin: 0 auto;
transform: translateY(90px);
}
#field > .ctr > .pika > .dead{
background-color: red;
}
#field > .ctr > .hole{
height: 33px;
width: 100px;
position: absolute;
bottom: 0;
left: 0;
background-color: yellow;
/*z-index: 1;*/
}
@-webkit-keyframes bounce{
0% { transform: translateY(90px); }
50% { transform: translateY(0);}
100% { transform: translateY(90px); }
}
.s1{
-webkit-animation-name: bounce;
-webkit-animation-duration: 3s;
}
.s2{
-webkit-animation-name: bounce;
-webkit-animation-duration: 2s;
}
.s3{
-webkit-animation-name: bounce;
-webkit-animation-duration: 1s;
}
JavaScript
var Pika = function(posObj){
this.domObj = null;
this.actionObj = null;
this.dead = false;
this.points = null;
var _this = this;
var ctr = document.createElement('div');
var pika = document.createElement('div');
var div = document.createElement('div');
var hole = document.createElement('div');
ctr.className = "ctr";
pika.className = "pika";
hole.className = "hole";
div.addEventListener('click', function(){
if(!(_this.dead)){
console.log('dead');
_this.actionObj.className += " dead";
_this.dead = true;
}
});
div.addEventListener('webkitAnimationEnd', function(){
_this.dead = false;
_this.actionObj.offsetWidth = _this.actionObj.offsetWidth;
_this.actionObj.className = '';
});
ctr.appendChild(hole);
pika.appendChild(div);
ctr.appendChild(pika);
this.domObj = ctr;
this.actionObj = div;
this.setPikaPos(posObj);
};
Pika.prototype = {
getDomObj : function(){
return this.domObj;
},
setPikaPos : function(posObj){
this.domObj.style.left = posObj.x + "px";
this.domObj.style.top = posObj.y + "px";
},
setPikaAction : function(obj){
this.actionObj.className = obj.t;
this.points = obj.p;
}
};
var Game = function(){
this.str = null;
this.pos = null;
this.holes = 10;
var _this = this;
var start = document.getElementsByClassName('start-btn')[0];
var f = document.getElementById('field');
var newStart = document.getElementsByClassName('ctr');
start.addEventListener('click', function(){
_this.str = Math.floor((Math.random() * 3) + 1);
switch(_this.str){
case 1:
_this.str = "s1";
break;
case 2:
_this.str = "s2";
break;
case 3:
_this.str = "s3";
break;
}
if (newStart.length > 0){
for (var i = newStart.length-1; i >= 0 ; i--) {
f.removeChild(newStart[i]);
}
}
_this.pos = randomPos(_this.holes);
for (var i = 0; i < _this.pos.length ; i++) {
var p = new...