css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

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.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!--
Basic logic created with stackoverflow 
sources. No plagiarism intented.
Design: George Zafiris
-->
<html>

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
</head>

<body><center>
  <div id='simon'>
    <div>
      <input id='myButton' type='button' value='New Game' data-bind='click: newGame' />
    </div>
    <div id='gameOverMessage' data-bind='visible: gameOver'>Game Over!</div>
    <div id='highScore'>High Score:<span data-bind='text: highScore'></span></div>

    <div id='gameBoard'>
      <!--ko foreach: Squares -->

   <div class='simonSquare' data-bind="
        style: { backgroundColor: color() } , 
        css: activeCSS, 
        click: $root.guess">&nbsp;
      </div>

      <!-- /ko -->
    </div>
  </div></center>
</body
            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Eagle+Lake);

* {font-family: 'Eagle Lake', cursive;}

.active {
  background-color: white !important;
  color: black;
}
#gameBoard {
  width: 500px;
  background:linear-gradient(to right,#222333,#4411aa);
  height:500px;
  box-shadow:0px 10px 20px #552233;
  border-radius:0px 80px 0px;
}
.simonSquare {
  width: 200px;
  border-radius:0px 80px 0px;
  float: left;
  margin: 5%;
  height: 200px;
  
}

#highScore {
  margin-top:50px;
  margin-bottom:50px;
  font-size:30px;
  color:#eee;
}

#gameOverMessage {
  margin-top:50px;
  margin-bottom:50px;
  font-size:30px;
  color:#eee;
}

#myButton {
  margin-top:20px;
  box-shadow:0px 10px 20px black;
  border-radius:50px;
  height:50px;
  width:300px;
  border:2px solid #441188;
  background-color:#111122;
  color:#eee;
}

#myButton:hover {
  margin-top:20px;
  box-shadow:0px 10px 20px black;
  border-radius:50px;
  height:50px;
  width:300px;
  border:2px solid #111122;
  background-color:#441188;
  color:#eee;
}

body {
  background:linear-gradient(to right,#222333,#441188);
}
            
          
!
            
              var simonSquare = function(color) {

	var self = this;

	self.color = ko.observable(color);

	self.isActive = ko.observable(false);

	self.activeCSS = ko.computed(function() {
			return self.isActive() ? "active" : "";

	});

	self.flash = function(callback) {
		self.isActive(!self.isActive());
		setTimeout(function() {self.isActive(!self.isActive()); 
			if (callback) {
				callback();
			}}, 300);
	};

};

var simon = function() {

	var self = this;

	self.Squares = ko.observableArray([
			new simonSquare('red'),
			new simonSquare('blue'),
			new simonSquare('green'),
			new simonSquare('yellow')
		]);

	self.Pattern = [];

	self.playPattern = function(index) {
		var index = index || 0;
		self.Pattern[index].flash(function() {
		if (self.Pattern[index + 1]) {
			setTimeout(function() {
				self.playPattern(index + 1);
			}, 200)	;
		}
		else {
			self.canGuess = true;
		}
	});
	};

	self.highScore = ko.observable(0);
	//Stack Snippets are sandboxed, no local storage
	//self.highScore = ko.observable(localStorage.getItem("SimonSaysHighScore"));
	self.canGuess = false;
	self.gameOver = ko.observable(false);
	self.currentPositionBeingGuessed = 0;

	self.guess = function(guessedSquare) {
		if (!self.canGuess) return;
		guessedSquare.flash(function() {
			if (self.Pattern[self.currentPositionBeingGuessed] == guessedSquare) {
				if (self.currentPositionBeingGuessed == (self.Pattern.length -1 )) { // completed pattern
					if (self.Pattern.length > self.highScore()) {
						self.highScore(self.Pattern.length);
					}
					self.newRound();
				}
				else {
					self.currentPositionBeingGuessed++;
				}
			}
			else {
				self.gameOver(true);
				/* Stack Snippets are sandboxed, no local storage
				if(typeof(Storage) !== "undefined") {
					localStorage.setItem("SimonSaysHighScore", self.Pattern.length -1);
				} */
				self.canGuess = false;
			}	
		});
	}

	self.newRound = function() {
		self.canGuess = false;
		self.currentPositionBeingGuessed = 0;
		self.Pattern.push(self.Squares()[Math.floor(Math.random()*self.Squares().length)]);
		setTimeout(self.playPattern, 500);
	}

	self.newGame = function() {
		self.gameOver(false);
		self.Pattern = [];
		self.newRound();
	};

};

simon = new simon();
ko.applyBindings(simon);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console