Tibor SzászInteraction matters

  • Home
  • About me
  • Projects
  • Reqest price offer
  • Contact me
  • RSS

2009 1202

Konami codes in javascript

Konami codes in javascript If you ever played any platform, fighting or combat games like Contra or Mortal Combat, then you will be familiar with the special key combinations for executions or special attacks that gives you some extra power, effect, or god mode. The Konami code was one of the fist cheat code in the video gaming history, that special combination later adapted by many games.

In the age of websites, many sites also installed small easter eggs for their visitors, just for fun. See the list here.

I played a little with implementing it, and just a few rows were enough to achieve the desired effect.

var p = 0;
var order = [38,38,40,40,37,39,37,39,66,65];
document.onkeyup = function(e)
{
	var key = (window.event) ? event.keyCode : e.keyCode;
	p = (order[p] == key) ? ++p : 0;
	if( p == order.length ) alert('Boom, secrets unlocked');
};


But why stop here?
I created a little cheat engine, that handles multiple codes and triggers a callback functions for them.

var cheatCodes = function( order, callBack )
{
	this.p = 0;
	this.order = order;
	var pass = this;
	addEvent( document, 'keyup', function(e)
	{
		var key = (window.event) ? event.keyCode : e.keyCode;
		pass.p = (pass.order[pass.p] == key) ? ++pass.p : 0;
		if( pass.p == pass.order.length ) callBack();
	});
}
// Scott Andrew’s original addEvent() function
// You can reaplce this to any event wrapper,
// or modify the cheatCodes function for jQuery or MooTools event handler
function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}
// Code is: left, left, right, up, down
var cheat_one = new cheatCodes( [37,37,39,38,40], doSomethingElse );

// Code is: left, right, up, down
var cheat_two = new cheatCodes( [37,39,38,40], doSomething );

function doSomething()
{
	document.body.style.backgroundColor = '#f90';
}

function doSomethingElse()
{
	document.body.style.backgroundColor = '#9f0';
}
Have fun using it :)
Oh, and here are the Konami codes:
Konami codes
Try it now, if you dare.


Comments


Add new comment


Your name
Email
Your avatar
Website
Comment

Briefing

Me myself and I
I love coding, especially for the web. I'm in the online industry for 6 years. My favourite toys are:
JavaScript, PHP, XHTML, MySQL, Canvas, Flash
Váltás magyar nyelvre

Topics

  • Coffee break
  • Design & stuff
  • Generative arts
  • Programming

Friends

  • Kárpáti Róbert
  • Royaljerry
  • Bagi Zoltán
  • Pegazoo
  • Charles Alexander Deli
  • Bence Bangócs
  • Lunakid
  • Rapp design

Feeling so social

  • Facebook
  • IWIW
  • TurulMeme

Tags

javascript (12) php (6) mootools (4) iscaffold (3) codeigniter (3) crud (3) tutorial (2) google (2) renders (2) structure-synth (2) css (2) template (2) free (2) google-buzz (1) script (1) forge (1) hack (1) table (1) open-source (1) rc1 (1) infovis (1) rss (1) interface (1) canvas (1) design, (1) user (1) experience (1) soundcloud (1) resizing (1) joomla (1) bermuda-triangle (1) wordpress (1) drupal (1) data-visualisation (1) data-uri (1) konami-codes (1) tagging (1) slimbox (1) bugfix (1) cfdg (1) blackout (1) firefox-extension (1) utf8 (1) class (1) slideshow (1) nl2br (1) experiment (1) svg (1) library (1) ajax (1) sitebuild (1) tabs (1) inspiration (1) lists (1) drag-and-drop (1) wikipedia (1) dns (1) trick (1)




All rights reserved, ©2008-2010 - Built on CodeIgniter framework - Konami codes - Mostly Valid XHTML 1.1 - Valid CSS 2.1