SoFunction
Updated on 2025-03-02

JavaScript to create color reversal games

Game rules:

Click the square, and the clicked square and adjacent squares change color. When all blue squares turn orange, complete the task and enter the next level.

<!DOCTYPE html>
<html lang="en">
 <head>
 <meta charset="utf-8">

 <!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
 Remove this if you use the .htaccess -->
 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

 <title>Color flip</title>
 <meta name="description" content="">
 <meta name="author" content="jiamengkai">

 <meta name="viewport" content="width=device-width; initial-scale=1.0">

 <!-- Replace  &  in the root of your domain and delete these references -->
 <link rel="shortcut icon" href="/">
 <link rel="apple-touch-icon" href="/">
 
 <style type="text/css">
  * {
  margin: 0;
  padding: 0;
  font-family: "Microsoft yahei";
  color: #000;
  }
  h1 {
  margin: 10px 0;
  font-size: 300%;
  font-weight: bolder;
  }
  span {
  display: block;
  text-indent: 32px;
  }
  .title {
  position: fixed;
  top: 50%;
  left: 50%;
  width: 300px;
  height: 600px;
  margin: -300px 0 0 -500px;
  }
  .main {
  position: fixed;
  top: 50%;
  left: 50%;
  width: 600px;
  height: 600px;
  margin: -300px 0 0 -150px;
  background: #555;
  border-radius: 8px;
  1border: 5px solid #555;
  }
  .blue, .orange {
  margin: 5px;
  }
  .blue {
  background: #099;
  border-radius: 8px;
  float: left;
  }
  .orange {
  background: #D69C49;
  border-radius: 8px;
  float: left;
  }
  .button { 
    1display: inline-block; 
    zoom: 1; /* zoom and *display = ie7 hack for display:inline-block */ 
    display: block; 
    vertical-align: baseline; 
    margin: 8px 5px; 
    outline: none; 
    cursor: pointer; 
    text-align: center; 
    text-decoration: none; 
    font: 14px/100% Arial, Helvetica, sans-serif; 
    padding: .5em 2em .55em; 
    text-shadow: 0 1px 1px rgba(0,0,0,.3); 
    -webkit-border-radius: .5em;  
    -moz-border-radius: .5em; 
    border-radius: .5em; 
    -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.2); 
    -moz-box-shadow: 0 1px 2px rgba(0,0,0,.2); 
    box-shadow: 0 1px 2px rgba(0,0,0,.2); 
  } 
  .button:hover { 
    text-decoration: none; 
  } 
  .button:active { 
    position: relative; 
    top: 1px; 
  }
  .btn { 
    color: #fef4e9; 
    border: solid 1px #da7c0c; 
    background: #f78d1d; 
    background: -webkit-gradient(linear, left top, left bottom, from(#faa51a), to(#f47a20)); 
    background: -moz-linear-gradient(top, #faa51a, #f47a20); 
    filter: progid:(startColorstr='#faa51a', endColorstr='#f47a20'); 
  } 
  .btn:hover { 
    background: #f47c20; 
    background: -webkit-gradient(linear, left top, left bottom, from(#f88e11), to(#f06015)); 
    background: -moz-linear-gradient(top, #f88e11, #f06015); 
    filter: progid:(startColorstr='#f88e11', endColorstr='#f06015'); 
  } 
  .btn:active { 
    color: #fcd3a5; 
    background: -webkit-gradient(linear, left top, left bottom, from(#f47a20), to(#faa51a)); 
    background: -moz-linear-gradient(top, #f47a20, #faa51a); 
    filter: progid:(startColorstr='#f47a20', endColorstr='#faa51a'); 
  } 
  .operBtn {
  margin-top: 10px;
  width: 200px;
  text-align: center;
  }
  .tipSpan {
  display: inline-block;
  margin: 5px 2px;
  width: 200px;
  text-align: right;
  font-size: 18px;
  }
  .tipNum {
  display: inline-block;
  margin-right: 10px;
  text-align: right;
  width: 60px;
  font-size: 18px;
  }
 </style>
 </head>

 <body>
 <div class="title">
  <h1>Color flip</h1>
  <h2>Game rules:</h2>
  <span>
  Click on the square,Clicked blocks and adjacent blocks change color,When all blue squares turn orange,Complete the task,Go to the next level。
  </span>
  <div class="operBtn">
  <input type="button" class="button btn"  value="restart"/>
  <input type="button" class="button btn"  value="Reset this level"/>
  </div>
  <div class="tipInfo">
  <span class="tipSpan">level:</span><div class="tipNum" >0</div>
  <span class="tipSpan">Number of clicks at this level:</span><div class="tipNum" >0</div>
  <span class="tipSpan">Total number of clicks:</span><div class="tipNum" >0</div>
  </div>
 </div>
 <div class="main" >
 </div>
 </body>
 <script type="text/javascript" src=""></script>
 
 <script type="text/javascript">
 var level = 1;
 var margin = 10;
 var clickNum = 0;
 var clickSumNum = 0;
  = function() {
  
  var mainNode = ("main");
  
  var mainWidth = ;
  var mainHeight = ;
  
  //restart  var reset = ("reset");
   = function() {
  level = 1;
  createNewDiv();
  };
  
  //Reset this level  var resetLevel = ("resetLevel");
   = function() {
  createNewDiv();
  };
  
  //Change the color  function changeColor(obj) {
  if(("class")) {
   if(("class") == "blue") {
   ("class", "orange");
   }else {
   ("class", "blue");
   }
  }
  }
  
  //Change the color  function nodesChangeColor(obj) {
  clickNum += 1;
  clickSumNum += 1;
  var clickNumNode = ("clickNum");
   = clickNum;
  var clickSumNumNode = ("clickSumNum");
   = clickSumNum;
  
  changeColor(obj);
  
  var obj_r = parseInt(("r"));
  var obj_c = parseInt(("c"));
  
  
  if(obj_r-1>0) {
   var topObj = ("r"+(obj_r-1)+"_c"+obj_c);
   changeColor(topObj);
  }
  if(obj_c+1<=level) {
   var rightObj = ("r"+obj_r+"_c"+(obj_c+1));
   changeColor(rightObj);
  }
  if(obj_r+1<=level) {
   var bottomObj = ("r"+(obj_r+1)+"_c"+obj_c);
   changeColor(bottomObj);
  }
  if(obj_c-1>0) {
   var leftObj = ("r"+obj_r+"_c"+(obj_c-1));
   changeColor(leftObj);
  }
  setTimeout(function() {
   //Calculate the number of orange blocks   orangeDivNum();
  },500);
  }
  
  //Calculate the number of orange blocks  function orangeDivNum() {
  var o_nodes = ("orange");
  if(o_nodes.length == level*level) {// Complete all the flip   //Enter the next level   level += 1;
   createNewDiv();
   myAlert();
  }else {}
  }
  
  //Enter the next level  function createNewDiv() {
  var divWidth = mainWidth/level-margin;
  var divHeight = mainHeight/level-margin;
   = "";
  for(var i=1;i<=level;i++) {
   for(var j=1;j<=level;j++) {
   var colorNodes = ("div");
    = "width: "+divWidth+"px;height: "+divHeight+"px;";
   ("class", "blue");
   ("id", "r"+i+"_c"+j);
   ("r", i);
   ("c", j);
    = function() {
    nodesChangeColor(this);
   };
   (colorNodes);
    = "border: 5px solid #555;";
   }
  }
  
  var levelSpan = ("level");
   = level;
  clickNum = 0;
  var clickNumNode = ("clickNum");
   = clickNum;
  }
 };
 </script>
</html>