The Konami Code. You’ve seen it for years, dialed it in on your favorite emulators uh, vintage consoles, and the recent resurrection of the code all over the web has plucked at our nostalgic heart strings – even going so far as to make us grudgingly accept a new lease on the life of the dreaded lens flare effect.
The Code, as it is officially accepted:
…followed, of course, by hitting START if you’re playing Contra. (though some of us have sworn a blood oath from our childhood days that there’s an extra B, A in there and then SELECT, START. It’s superstition, I know.)
Not too long ago, KonamiCodeSites.com reworked their page to accept the code in a form that works with iPhone gestures. Their version goes something like this:
(SWIPE) UP, UP, DOWN, DOWN, LEFT, RIGHT, LEFT, RIGHT
That’s all well and good, but that’s a website, and I do games. I also know there are plenty of people out there creating games on Windows Phone 7 using the XNA Game Studio 4.0 Beta that deserve to have the code for themselves. So I figured I might as well throw some code together and present – for your consideration, the Konami Code for WP7 and XNA.
Note: To use this code yourself, you’ll need to download the XNA Game Studio 4.0 Beta. You don’t need a phone – the tools come with an emulator!
Note #2: This code assumes a “LandscapeLeft” orientation. More on that later.
C# – KonamiCode.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace KonamiCodeForWP7
{
// A delegate type that represents the code has either been entered right or wrong
public delegate void CodeEnteredEventHandler(object sender, EventArgs e);
class KonamiCode
{
public event CodeEnteredEventHandler CodeEnteredRight;
public event CodeEnteredEventHandler CodeEnteredWrong;
enum DirType
{
DirTypeUp,
DirTypeDown,
DirTypeLeft,
DirTypeRight
};
DirType[] correctCodeDirs = new DirType[8] {
DirType.DirTypeUp,
DirType.DirTypeUp,
DirType.DirTypeDown,
DirType.DirTypeDown,
DirType.DirTypeLeft,
DirType.DirTypeRight,
DirType.DirTypeLeft,
DirType.DirTypeRight };
int gestureCount = 0;
public void checkGesture(GestureSample gs)
{
bool wrongCode = false;
switch(correctCodeDirs[gestureCount])
{
case DirType.DirTypeUp:
if (gs.Delta.Y > 0 || (Math.Abs(gs.Delta.Y) < Math.Abs(gs.Delta.X)))
wrongCode = true;
break;
case DirType.DirTypeDown:
if (gs.Delta.Y < 0 || (Math.Abs(gs.Delta.Y) < Math.Abs(gs.Delta.X)))
wrongCode = true;
break;
case DirType.DirTypeRight:
if (gs.Delta.X < 0 || (Math.Abs(gs.Delta.Y) > Math.Abs(gs.Delta.X)))
wrongCode = true;
break;
case DirType.DirTypeLeft:
if (gs.Delta.X > 0 || (Math.Abs(gs.Delta.Y) > Math.Abs(gs.Delta.X)))
wrongCode = true;
break;
}
if (wrongCode == true)
{
//wrong type, reset the count, send event
gestureCount = 0;
EventArgs e = new EventArgs();
CodeEnteredWrong.Invoke(this, e);
}
else
{
gestureCount++;
if (gestureCount >= 8)
{
//reset the code, fire the event
gestureCount = 0;
EventArgs e = new EventArgs();
CodeEnteredRight.Invoke(this, e);
}
}
}
}
}
To Use the Class
It’s really just three things:
- Instantiate a KonamiCode class.
- Subscribe to the CodeEnteredRight event (and, if you want, the CodeEnteredWrong event)
- Feed it GestureSample objects.
It’s easy!
Getting it Into a Default Project
This part is up to your individual taste – I just made a quick modification to the Game1.cs file Constructor, Update, and Draw calls.
Game1()
//**Start Code For This Sample - REPLACE YOUR GAME1 CONSTRUCTOR WITH THIS
KonamiCode konami = new KonamiCode();
Color clearColor = Color.CornflowerBlue;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333);
konami.CodeEnteredRight += new CodeEnteredEventHandler(konami_CodeEnteredRight);
konami.CodeEnteredWrong += new CodeEnteredEventHandler(konami_CodeEnteredWrong);
TouchPanel.EnabledGestures = GestureType.Flick;
}
void konami_CodeEnteredWrong(object sender, EventArgs e)
{
clearColor = Color.Tomato;
}
void konami_CodeEnteredRight(object sender, EventArgs e)
{
clearColor = Color.MediumSeaGreen;
}
//**End Code For This Sample
Update
//**Start Code For This Sample - REPLACE YOUR UPDATE LOOP WITH THIS
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
while (TouchPanel.IsGestureAvailable)
{
GestureSample gs = TouchPanel.ReadGesture();
konami.checkGesture(gs);
}
base.Update(gameTime);
}
//**End Code For This Sample
Draw
//**Start Code For This Sample - REPLACE YOUR DRAW METHOD WITH THIS
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(clearColor);
// TODO: Add your drawing code here
base.Draw(gameTime);
}
//**End Code For This Sample
When It’s All Done…
It starts out Cornflower Blue, then looks like this (MediumSeaGreen) if you get it right:
And this (Tomato) if you get it wrong:
If you get it wrong, the code resets to the beginning; you can just start over until you get it right.
Some Notes About Orientation
It’s about the default layout. XNA Framework defaults its own orientation to “LandscapeLeft” in the emulator (rotated counter-clockwise from straight up-and-down, so the “head” is pointed left). The deltas you’ll get back from your gestures will therefore be mapped like this.
Pardon my poor graphics – as you can see, when we’re looking for the “flick” delta, we need to take into account the way the XNA Framework’s initial setup regards the orientation of the device. By default, if the player turns the device around, the game will not respond by changing its own notion of what is “down” or “left”.
The code that I’ve written assumes LandscapeLeft.
Other Notes
The magic of the Konami Code is in that place where it’s making vector determinations about whether or not a flick should be considered left, right, up, or down. You’ll notice I leave no quarter for diagonals, nor do I care particularly about the length of the flick. There are easily a million better ways to make this determination – please feel free to improve on this crude attempt, and make a code worthy of the next great game that needs the Next Great Cheat Code!
Remember – even if you haven’t toyed around with making games before, Windows Phone 7 with XNA Game Studio 4.0 makes it easier than ever, and you can get started for free!



