Writing Hears card game in C#

O

Olmar

Hi,

I am trying to write a version of the Hears card game. I can't figue
out what is the best way to detect clicks on the cards. Using the
cards.dll interface as described in tutorials like
http://www.publicjoe.f9.co.uk/csharp/card00.html . Can anyone give me
suggestions how to detect which card is clicked and how to redraw it
at the center of the sceen. For now all i know is that I have to
detect the X and Y coordinates of the mouse. But then what, I have to
go through the location of each card to get which one is actually
clicked. That seems kinda tedious since in the beginning of the game a
card in the middle is only slighly exposed, then when a card nex to it
is played, the area of the card gets bigger. How should I keep track
of that. Is it better to make some object like a button that actually
displays cards ( I still want to keep the same look as the WIndows
Hearts). Please help me since I am a total newbie at C#. Any
suggestions ( and sample code ) would be deeply appreciated

Thank you in advance
 
P

Peter Duniho

[...] Can anyone give me
suggestions how to detect which card is clicked and how to redraw it
at the center of the sceen. For now all i know is that I have to
detect the X and Y coordinates of the mouse. But then what, I have to
go through the location of each card to get which one is actually
clicked. That seems kinda tedious since in the beginning of the game a
card in the middle is only slighly exposed, then when a card nex to it
is played, the area of the card gets bigger. How should I keep track
of that.

Short of making each card its own control and letting .NET (Windows) do
the hit-testing for you, then you pretty much do have to go through the
location of each card until you get one that matches.

As far as overlap goes, you're talking about a basic z-order issue. All
you have to do to address that is make sure that you hit-test from top to
bottom. You won't have to worry about exactly what portion of the card is
exposed that way...if you're clicking within an area of a card that is
obscured by a card on top, that obscuring card will test positive for the
hit before you get to the one being covered up. You stop testing on the
first card that tests positive for a hit.

For example, let's assume you've got an array of stacks of cards and each
stack of card is an array of cards. Let's also assume that you maintain
each stack array in top to bottom order (that is, the first card in the
array is the card at the top of the stack). Let's also assume that the
card data structure has a rectangle property that exposes the boundary
within which the card is considered to be clicked on. Finally, let's
assume that each stack is entirely independent of every other stack (that
is, the only overlap is within a card). Then you get something like this:

public Card CardHitTest(Card[][] stacks, Point ptClick)
{
foreach (Card cardCur in stacks)
{
if (cardCur.rectangleBounds.Contains(ptClick))
{
return cardCur;
}
}

return null;
}

I'm not familiar with the game you mention, but you should be able to
adapt the above idea to suit your needs. The key is to maintain some kind
of ordered collection of cards, and have the order be according to how the
cards lay on top of each other, with the top-most card being the first
card in the collection.

As far as redrawing goes, if you can implement the code to draw the cards
in the first place, then all you have to do is maintain your card data so
that you know where each card should be drawn, update that information
when the position of a card changes, and then invalidate the areas that
need to be redrawn so that they do get redrawn (that is, for each card
that moves, invalidate the rectangles describing the old boundary of the
card and the new boundary of the card). The redrawing part is pretty
simple.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top