I'm writing a version of the game Memory. I have two groups- one for the "covers" and one for the cards itself. The covers are the ones that go ontop of the cards in order to hide the cards. The problem that I can't figure out is when a user clicks on one of the cards, I used the kill()
in order to remove the cover card and the card underneath shows (essentially its being flipped), however I can't figure out how to find the position of the card underneath inside the group. How can I find out which card the user clicked on?
Asked
Active
Viewed 83 times
0

sloth
- 99,095
- 21
- 171
- 219

Shahaad Boss
- 1
- 1
- 2
- 6
-
I suggest you replace the title with something like your last sentence. An index is only one way of identifying a card. See my answer for another. – Terry Jan Reedy Feb 14 '16 at 23:06
-
1Possible duplicate of [Pygame mouse clicking detection](http://stackoverflow.com/questions/10990137/pygame-mouse-clicking-detection) – sloth Feb 16 '16 at 10:02
1 Answers
-1
The answer depends on how you display the images. Here is an mcve that uses a Button subclass. This allows instances to carry indentifying information and to use a bound method as the command.
import tkinter as tk
root = tk.Tk()
class Card(tk.Button):
hide = 'XXX'
def __init__(self, txt):
super().__init__(root, text=self.hide)
self.txt = txt
self.exposed = False
def flip(self):
self['text'] = self.hide if self.exposed else self.txt
self.exposed = not self.exposed
for i, txt in enumerate(('one', 'two')):
card = Card(txt)
card['command'] = card.flip
card.grid(row=0, column=i)
#root.mainloop() # uncomment if run on command line without -i

Terry Jan Reedy
- 18,414
- 3
- 40
- 52
-
Whoops, after posting this answer, I noticed that the tag is `pygame` rather than `tkinter`. People often use tkinter for card games like this. I don't know which of the tkinter alternatives can be adapted to pygame. – Terry Jan Reedy Feb 14 '16 at 23:37