Wednesday 29 September 2010

Meet Swordy

Today saw the start of a new journey for me. A new journey that will see me learning new things, and thus proving an old dog can learn new tricks.

A friend is teaching me to program in C# (C Sharp) and how to write games using the XNA framework for the Xbox 360.

So sitting in the Costa Coffee franchise at work like true geeks we sat with coffee’s (or in my case tea) and out laptop and netbooks having a hands on introduction to games programming.

stickyIt was in this initial session that I came up with what I am convince will become a classic video game character of the 21st Century Swordy. Swordy was created out of necessity of needing a sprite to move around the screen when a direction arrow is pressed.enemy

But in true video game tradition Swordy needed a nemesis, and so in an equally rushed drawing I came up with a badly drawn ghost from Pacman as Swordy’s nemesis.

In our little hack-a-thon (if you can call a session of hogging the comfy seats in the coffee shop for just over an hour of geekiness a hack-a-thon) the enemy homed in on Swordy. Which when moving Swordy around the screen gave an almost Robotron like feel to the basic program being developed. If you remember in the classic game Robotron the enemies homed in on the players character. And to get this AI (if you can even call it AI) is an amazingly simple bit of code. Which I give the extract for below:

foreach (character e in enemy)
{
    e.update(gameTime);
    if (e.getpos().X < swordy.getpos().X)
    {
        e.movex(1);
    }
    if (e.getpos().X > swordy.getpos().X)
    {
        e.movex(-1);
    }
    if (e.getpos().Y < swordy.getpos().Y)
    {
        e.movey(1);
    }
    if (e.getpos().Y > swordy.getpos().Y)
    {
        e.movey(-1);
    }

}

Basically for each enemy we compare it’s position to that of Swordy and move the enemy closer towards Swordy. Which as I said has the effect on screen of making it look like the enemies are homing in on Swordy.

Anyway that makes it look like I know what I am talking about, which I don’t. I’m just impressed (easily I know) with how well the Robotron homing enemy effect looks with such simple code. The whole area of enemy AI is a complicated one and has evolved over the years, to which point in modern big budget games the AI can be pretty sophisticated.

Anyway that about sums up todays lesson, I’m looking forward to next weeks now.

No comments: