Thursday, June 25, 2009

Pacman (XNA) - Update 1

I've made a little progress with the Pacman XNA game. I managed to create the Pacman character, move him, animate him and rotate him according to the direction he is going. Now, moving the character wasn't a big deal since I've done that before, but animating and rotating was new to me. I don't have much else to say, so I'll briefly run though how each works:

Animation
This is done using a sprite sheet, which I've recently learned is multiple related images all in one image file. Simply search sprite sheet on google and you'll see many examples. Of course mine wasn't as complicated as some of the others out there as it only has four different positions (though I could have gotten away with three). Anyway, basically all you have to do is organize the sheet into rectangles around each individual sprite and loop through them. There is a parameter to the draw method that asks which rectangle you'd like to display from the sheet. Check out my code, or just read some tutorials. It's not too hard to figure out.

Rotation
Rotation was also fairly easy. In the same draw method, there is a parameter for rotation. It asks for a float value and works in radians. So I just kept track of the rotation of the character, so that when it moved in a different direction the character faced in that direction as well. The MathHelper class was helpful for this, particularly since Pacman really only ever faces up, down, left or right. You'll also have to find the center around which to rotate. This can simply be done by finding the width and height of each sprite (not the entire sheet) and then dividing each by 2, and using those values as the x and y parameters of a Vector2. It's simple.

If you have any questions, just ask. Maybe one day I'll actually get the energy to put up some tutorials, but that might take a while if no one is asking for them. If you check out the code, you might find what you're looking for in there anyway. At the very least, you'll have an animated Pacman moving around your screen.

No comments:

Post a Comment