james.lu.75491856 0 Junior Poster

code:

#I meant memory card
import pygame, sys,random
from functools import partial
def image(x=50,y=100,default=True,nondefault_color=(0,0,0)):
        if default:
            color = hiddencolor
        else:
            color = nondefault_color
        print color
        image = pygame.Surface((x,y))
        image.fill(color)
        pygame.draw.lines(image, [0,0,0],False, [(0,0),(0,100),(50,100),(50,0),(0,0)], 1)
        rect = image.get_rect()
        return image
def card(pos):
    x= pos[0]/50 #integer divison
    y= pos[1]/100
    if (x,y) in flashcards:
        return flashcards[x,y]
class Flashcard(pygame.sprite.Sprite):
    def __init__(self, location,shown):
        pygame.sprite.Sprite.__init__(self)
        self.image = image()
        self.hidden = self.image
        self.shown = shown
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location
    def flip(self,show=False,other=None):
    try:
        if other.shown == self.shown and show:
        return "delete"
        elif self.image == self.hidden:
        self.image = self.shown
        print"show"
        else:
        self.image = self.hidden
        print"hid"
    except:
        if self.image == self.hidden:
        self.image = self.shown
        print"show"
        else:
        self.image = self.hidden
        print"hid"
        print self.hidden
pygame.init()
pygame.display.init()
screen = pygame.display.set_mode([950,900])
screen.fill([255,255,255])
pygame.display.flip()
flashcards ={}
selected = []
hiddencolor = (255,255,0) #<-        can be changed  V 
showns = [image(default=False,nondefault_color=(random.randint(0,255),random.randint(0,255),random.randint(0,255))) for i in range(15)]
random.shuffle(showns)
notdone = [tuple((x,y))for x in range(5) for y in range(6)]
for i in showns:
    a = random.sample(notdone,1)[0]
    notdone.remove(a)
    b = random.sample(notdone,1)[0]
    notdone.remove(b)
    flashcards[a] = Flashcard((a[0]*20,a[1]*100),i)
    flashcards[b] = Flashcard((a[0]*20,b[1]*100),i)
print flashcards
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            current = card(event.pos)
            if current != None:
                selected.append(current)
                print"clicked",selected
                result = current.flip(other=selected[0])
                if len(selected) == 2:
                    if result == "delete":
                        pygame.time.delay(250)
                        del selected[0]
                        del selected[1]
                        print"delete"
                    else:
            for i in selected:
                i.flip(True)
            selected = []
    for flashcard in flashcards:
    flashcard = flashcards[flashcard]
        screen.blit(flashcard.image, flashcard.rect)

Debug info:

(71, 167, 27)
(80, 249, 187)
(217, 167, 83)
(37, 185, 65)
(240, 37, 79)
(89, 109, 102)
(131, 68, 179)
(233, 31, 118)
(252, 119, 40)
(125, 217, 122)
(4, 19, 18)
(23, 127, 189)
(232, 180, 83)
(176, 133, 137)
(77, 25, 197)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
(255, 255, 0)
{(1, 3): <Flashcard sprite(in 0 groups)>, (3, 0): <Flashcard sprite(in 0 groups)
>, (2, 1): <Flashcard sprite(in 0 groups)>, (0, 3): <Flashcard sprite(in 0 group
s)>, (2, 5): <Flashcard sprite(in 0 groups)>, (4, 0): <Flashcard sprite(in 0 gro
ups)>, (1, 2): <Flashcard sprite(in 0 groups)>, (3, 3): <Flashcard sprite(in 0 g
roups)>, (4, 4): <Flashcard sprite(in 0 groups)>, (1, 5): <Flashcard sprite(in 0
 groups)>, (0, 4): <Flashcard sprite(in 0 groups)>, (4, 1): <Flashcard sprite(in
 0 groups)>, (1, 1): <Flashcard sprite(in 0 groups)>, (3, 2): <Flashcard sprite(
in 0 groups)>, (0, 0): <Flashcard sprite(in 0 groups)>, (4, 5): <Flashcard sprit
e(in 0 groups)>, (2, 2): <Flashcard sprite(in 0 groups)>, (1, 4): <Flashcard spr
ite(in 0 groups)>, (0, 5): <Flashcard sprite(in 0 groups)>, (4, 2): <Flashcard s
prite(in 0 groups)>, (1, 0): <Flashcard sprite(in 0 groups)>, (3, 5): <Flashcard
 sprite(in 0 groups)>, (0, 1): <Flashcard sprite(in 0 groups)>, (3, 1): <Flashca
rd sprite(in 0 groups)>, (0, 2): <Flashcard sprite(in 0 groups)>, (2, 0): <Flash
card sprite(in 0 groups)>, (4, 3): <Flashcard sprite(in 0 groups)>, (2, 3): <Fla
shcard sprite(in 0 groups)>, (3, 4): <Flashcard sprite(in 0 groups)>, (2, 4): <F
lashcard sprite(in 0 groups)>}
clicked [<Flashcard sprite(in 0 groups)>]
show
clicked [<Flashcard sprite(in 0 groups)>, <Flashcard sprite(in 0 groups)>]
show
hid
<Surface(50x100x32 SW)>
hid
<Surface(50x100x32 SW)>
clicked [<Flashcard sprite(in 0 groups)>]
show
clicked [<Flashcard sprite(in 0 groups)>, <Flashcard sprite(in 0 groups)>]
show
hid
<Surface(50x100x32 SW)>
hid
<Surface(50x100x32 SW)>
clicked [<Flashcard sprite(in 0 groups)>]
show
clicked [<Flashcard sprite(in 0 groups)>, <Flashcard sprite(in 0 groups)>]
show
hid
<Surface(50x100x32 SW)>
hid
<Surface(50x100x32 SW)>
clicked [<Flashcard sprite(in 0 groups)>]
show
clicked [<Flashcard sprite(in 0 groups)>, <Flashcard sprite(in 0 groups)>]
hid
show
<Surface(50x100x32 SW)>
hid
<Surface(50x100x32 SW)>
clicked [<Flashcard sprite(in 0 groups)>]
show
clicked [<Flashcard sprite(in 0 groups)>, <Flashcard sprite(in 0 groups)>]
show
hid
<Surface(50x100x32 SW)>
hid
<Surface(50x100x32 SW)>
clicked [<Flashcard sprite(in 0 groups)>]
show
clicked [<Flashcard sprite(in 0 groups)>, <Flashcard sprite(in 0 groups)>]
show
hid
<Surface(50x100x32 SW)>
hid
<Surface(50x100x32 SW)>

Also, in order to see the graphics, I have to drog the window below the taskbar.(Without releasing it, the program is paused if you are dragging it)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.