memory
Deutsch   English   

MEMORY

 

 

GAME DESCRIPTION

 

16 cards, which match in pairs, are laid face down at the start. The player reveals two cards with two mouse clicks. If they find two matching cards, the cards remain face up and the turn continues. Otherwise, both cards are turned face down again and a new turn begins. The aim is to reveal all cards in as few turns as possible.

In the main part, the cards are distributed and the programme continues with an endless loop.

 

 

In this loop, the programme execution is first paused with putSleep() until two different cards are revealed. The programme execution is then continued with wakeUp(). The loop first checks whether the game has been ended prematurely by clicking the Close button. After a delay of 1 second, the two cards are covered again.

Program:

# Memory.py

from gamegrid import *

# ---------------- class MemoryCard ----------------
class MemoryCard(Actor):
    def __init__(self, cardNb):
        Actor.__init__(self,["sprites/cardback.gif",
                             "sprites/card"+str(cardNb)+".gif"])
        self.cardNb = cardNb
    def getCardNb(self):
        return self.cardNb 
            
def pressCallback(e):
    global card1, card2, takeFirst, nbMoves, isMouseEnabled
    if not isMouseEnabled:
        return
    loc = toLocationInGrid(e.getX(), e.getY())  
    card = getOneActorAt(loc)

    if card.getIdVisible() == 1: # card already visible->ignore
        return True
        
    card.show(1) # make card visible
    if takeFirst: # first card taken
        takeFirst = False
        card1 = card
        setStatusText("# of moves: " + str(nbMoves))
    else:  # second card taken
        card2 = card
        if card1.getCardNb() == card2.getCardNb(): 
            if isGameOver():
                setStatusText("Game over in "+str(nbMoves)+" trials")
            else:
               takeFirst = True  # yes->may continue with next move
               setStatusText("# of moves: "+str(nbMoves)+".Pair accepted.")
        else: # no->reject this move, hide the two cards
            isMouseEnabled = False
            wakeUp()

def isGameOver():
   gameOver = True
   for card in getActors():
       if card.getIdVisible() == 0:
          gameOver = False
   return gameOver
   
# ---------------- main ----------------------
takeFirst = True
isMouseEnabled = True
nbMoves = 1
makeGameGrid(4, 4, 115, Color.black,False,mousePressed=pressCallback)
addStatusBar(30)
setStatusText("Make your choice of card pairs with mouse clicks")
setTitle("Memory")
for i in range(16):
    if i < 8:
        card = MemoryCard(i)
    else:
        card = MemoryCard(i - 8)
    loc = getRandomEmptyLocation()
    addActor(card, loc)   
    card.show(0)  
show()
doRun()

while True:
    putSleep()
    if isDisposed():
       break
    delay(1000)
    card1.show(0)
    card2.show(0)
    takeFirst = True
    setStatusText("# of moves: " + str(nbMoves) + ".Next move.")
    nbMoves += 1
    isMouseEnabled = True
  
► Copy to clipboard

 

 

Explanations of the programme code

 
1. Actor.__init__(self, ["sprites/cardback.gif", "sprites/card" + str(cardNb) + ".gif"]): Each card has two images: the front (e.g. card1.gif) and the back (cardback.gif). These must be grouped together in square brackets (list).

2. getCardNb() returns the card number.
3.
addActor(card, loc): the cards are distributed randomly across the 16 fields

4. if card1.getCardNb() == card2.getCardNb(): checks whether the two cards are the same
5. putSleep(): waits until the cards are turned back over

6. wakeUp(): the game continues

7. isMouseEnabled(False): deactivates mouse events