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.

            
              <html> 
<head> 
		<script src="https://code.createjs.com/easeljs-0.7.1.min.js"></script>
		<script src="https://code.createjs.com/tweenjs-0.5.1.min.js"></script>
        <script src="https://code.createjs.com/soundjs-0.5.2.min.js"></script>
  </head> 
<body onLoad="init()"> 
    <div id='container'>
    <canvas id="canvas" width="300" height="250"></canvas> 
    </div>  
</body> 
</html>
  
  
            
          
!
            
              html,body{
  padding:0;
  margin:0;
}

#canvas{
  background-color:#000000;
}

            
          
!
            
              /*
Note: 
EaselJS has crossdomain security restriction, so the sound effect will need to hosted on the same server. 
*/

var stage,
				coloursArray = [],
				userArray,
				computerArray,
				sequenceEnd,
				timer,
				count,
				score,
				playText,
				guessText,
				st,
          manifest;
				
			
			function init()
			{
				stage = new createjs.Stage('canvas');
				stage.enableMouseOver(10); 
				
				createjs.Ticker.addEventListener("tick", stage);
				createjs.Ticker.setFPS(30); 
				
				//colour blocks
				var b1 = createBlock( '#ff0000', 'tl', 45, 18);
				var b2 = createBlock( '#00ff00', 'tr', 155, 18);
				var b3 = createBlock( '#FFFF00', 'bl', 45, 128);
				var b4 = createBlock( '#0000FF', 'br', 155, 128);
				stage.addChild(b1, b2, b3, b4);
		
				coloursArray.push(b1, b2, b3, b4);
				
				for(var i = 0; i < coloursArray.length; i++){
					coloursArray[i].id = i;
					coloursArray[i].cursor = 'pointer';
					coloursArray[i].addEventListener('click', buttonHandler);
				}
				
				
				//play text
				playText = new createjs.Text();
				playText.text = 'CLICK\n TO PLAY';
				playText.color = '#ffffff';
				playText.font =  'bold 18px Arial';
				playText.textAlign = 'center';
				playText.x = stage.canvas.width/2;
				playText.y = stage.canvas.height/2 - 20;
                playText.hitArea  = new createjs.Shape(new createjs.Graphics()
                                    .beginFill("#f00")
                                    .drawRect(-stage.canvas.width/2,-stage.canvas.height/2 + 20,300,250)); 
                playText.addEventListener('click', playHandler);
				stage.addChild(playText);
				
                //guess text
                guessText = new createjs.Text();
                guessText.text = '';
                guessText.color = '#ffffff';
                guessText.font =  'bold 55px Arial';
                guessText.x = stage.canvas.width/2;
				guessText.y = stage.canvas.height/2 - 25;
                guessText.textAlign = 'center';
                guessText.visible = false;
                stage.addChild(guessText);
                
				stage.update();	
                
                
                //load sounds
                if (!createjs.Sound.initializeDefaultPlugins()) { return; }
                var audioPath = 'sounds/';
                manifest = [
                    {id:'chicken_sfx', src:'chicken_sfx.ogg'},
                    {id:'cow_sfx'    , src:'cow_sfx.ogg'},
                    {id:'oink_sfx'   , src:'oink_sfx.ogg'},
                    {id:'sheep_sfx'  , src:'sheep_sfx.ogg'}
                ]
                createjs.Sound.alternateExtensions = ['mp3'];
                createjs.Sound.registerManifest(manifest, audioPath);
                
			}
			

			function playHandler(e)
			{
				playText.visible = false;
				start();
               
			}
			

			function start()
			{
				userArray = [];
				computerArray = [];
				guessText.text = '0';
				guessText.visible = true;
				count = 0;
				score = 0;
				sequenceEnd = false;
				newRound();
			}
			
			
			function newRound()
			{
				var r = Math.floor(Math.random() * coloursArray.length);
				userArray.push(r);
				
				st = setInterval(checkColours, 1000);
			}

			
			function checkColours()
			{
				if(count < userArray.length){
					var value = userArray[count];
					
					createjs.Tween.get(coloursArray[value]).to({alpha:0.5}, 300).to({alpha:1},300);
                    
                    createjs.Sound.play(manifest[value].id);
                    
                    console.log('hello', manifest[value].id );
					
					count++;
					
				}else{
					clearInterval(st);
					computerArray = userArray.concat();
					sequenceEnd = true;
				}
			}
			
			
			function buttonHandler(e)
			{
				
				if(sequenceEnd){
					
					var computerChoice = computerArray.shift();
					var userChoice = e.target.id;
					
					if(userChoice == computerChoice){
						
						createjs.Tween.get(coloursArray[userChoice]).to({alpha:0.5}, 300).to({alpha:1},300);
						
                        createjs.Sound.play(manifest[userChoice].id);
                        
						if(computerArray.length == 0){
							newRound();
							count = 0;
							sequenceEnd = false;
							
							score++;
							guessText.text = String(score);
						}
						
					}else{
						guessText.text = '';
						guessText.visible = false;
						playText.visible = true;
					}
					
				}
			}
			
			
			function createBlock(colour, direction, xpos, ypos)
			{
				var g = new createjs.Graphics();
				g.beginFill(colour);
				
				switch(direction)
				{
					case 'tl':
						g.lineTo(0, 0);
						g.lineTo(100, 0);
						g.lineTo(100, 60);
						g.quadraticCurveTo(60, 60, 58, 100);
						g.lineTo(0, 100);
					break;	
					
					case 'tr':
						g.lineTo(0, 0);
						g.lineTo(100, 0);
						g.lineTo(100, 100);
						g.lineTo(47, 100);
						g.quadraticCurveTo(40, 60, 0, 58);
					break;
						
					case 'bl':
						g.lineTo(0, 0);
						g.lineTo(57, 0);
						g.quadraticCurveTo(60, 50, 100, 49);
						g.lineTo(100, 100);
						g.lineTo(0, 100);
					break;
					
					case 'br':
						g.moveTo(47,0);
						g.lineTo(100,0);
						g.lineTo(100,100);
						g.lineTo(0, 100);
						g.lineTo(0, 50);
						g.quadraticCurveTo(40, 50, 47, 0);
					break;	
				}	

				var s = new createjs.Shape(g);
				s.x = xpos;
				s.y = ypos;
				return s;
			}

            
          
!
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