Tuesday, May 08, 2007

XNA Mouse "Clicks" Update

Zygote's made an interesting point in my previous post about changing the way I detect a click. It certainly would make more sense to detect a "click" as when the state goes from Released to Pressed, rather than Pressed to Released which I have now.

This more accurately detects a "MouseDown" event that will make the interface more responsive.

Thank you!

XNA Mouse "Clicks"

So I've been carrying on with my card game in XNA. I've created some crude textures for the empty card "slot" and managed to find some pictures of playing cards online that I can use for the card faces.

One of the first things I did was sort out all the back end stuff (like shuffling the deck, dealing a card, etc.), I just needed to capture a mouse click on an empty card stack and deal a card.

But when it came to detecting the mouse click I was surprised that not 1 card was dealt when I clicked a card stack, but pretty much half of the deck.

I was using the Mouse.GetState method to check if the LeftButton was Pressed. As this was happening in the Update loop the game was looping around multiple times for as long as I kept the mouse button down.

Pressed really does mean Pressed.

So now I'm storing the previous state of the mouse, and only when the LeftButton state goes from Pressed to Released do I count a click.

EDIT: Detecting Pressed to Released models a "MouseUp" event, and while this may be useful in some situations, in this game a "MouseDown" would make more sense. To detect a MouseDown you count a click as the state going from Released to Pressed.

Now I just need to implement the rules of the game, and some scoring mechanism so scores can be compared.

Friday, May 04, 2007

XNA Resources

Mykre's been kind enough to send over a couple of links to easily show the mouse pointer in an XNA game. I knew I had seen the IsMouseVisible property somewhere but couldn't find it again when I needed it. Seems so obvious that it would be there now...

The links are in the comments for my XNA Mouse and Sprite Tests posts, but for ease of use here they are again:

Thanks Mykre

XNA Mouse and Sprite Tests

Played around with XNA last night again. For the card game I need to be able to display cards on the screen and click a stack of them to deal another card onto it.

So there's two basic things there that I need: display cards, and control the mouse.

I just followed the example for How To Draw a Sprite and I think that this will be all I need to draw cards to the screen. I'm pretty sure I know how to use the SpriteBatch and Texture2D classes now, but whether or not I'm using them correctly is another thing. All I can say is, I followed the examples and it works.

So I looked into showing the mouse pointer. I found a good article about encapsulating the mouse functionality into a class, and I'll probably follow this to tidy up my code, but for now I've just put that in my main Game class.

So I drew a pointer in MSPaint and used that as my cursor. I don't have the DirectX SDK yet (couldn't be bothered to download it last night), so the pointer isn't masked in the way that is described. It looks a little odd having a block of white surrounding the pointer, but for a proof of concept it's fine.

It struck me as odd that there wasn't a simpler way to show the mouse. Maybe there is and I just haven't found it, but then I though that the way I'm using is very flexible. It gives me full control of when I show the mouse, and I can update it only when I need to. Why have needless processing of the mouse when you're in a section of game that doesn't require it?

So for now I've got a random card showing on my screen (which I can show in full screen mode and at a really nice resolution, but that's another story), a pointer on the screen, and a continuously updating line of text which tells me where the pointer actually is.

I'm still a long way from a playable game, but I think I'm beginning to get the basics.

Wednesday, May 02, 2007

XNA Game Studio Express

I tried XNA Game Studio Express when it was still in Beta, but at the weekend I thought I'd try the one that has actually been released. If you don't know what I'm talking about then I guess a quick introduction would be that XNA is a framework for creating games in .Net, and Game Studio Express is an addon to Visual C# Express that lets you create games.

It's all free, so go and download it if you want to try it.

So I went through the 2D tutorial, which teaches you how to display a sprite on the screen and get it moving. That was pretty easy to do, but you're not left with much of a game--more a screen saver of a picture bouncing around your screen.

I tried some of the additions described at the end of the article, and although it was pretty cool adding mouse support to throw the picture around, it still wasn't much of a game.

So I went onto the next tutorial, which was creating a 3D game. And talk about being thrown in at the deep end! The learning curve between the two examples is pretty steep, and I don't have the time to go through it to figure it all out.

So I've settled for a middle ground. I'm going to try and create a 1 player card game based on a game my parents taught me years ago. It's got quite simple rules, and will have simple graphics too, but at the end I hope to have an actual playable game.