Tutorial: Pirate Memory Matching Game

Try your skills in this memory matching game with a cute pirate theme. If you don't get to find a match after 5 attempts, an arrow appears to allow to quit the game.

Source Code

'-------------------------------------------------
' Source code for tutorial: Memory Matching Game
'-------------------------------------------------

DIM total as integer
DIM attempt as INTEGER
DIM selected as integer
DIM pairValue as string
DIM playedDontMatch AS INTEGER
DIM arrowVisible AS INTEGER
DIM totalCards AS INTEGER 'const holding total cards.
DIM totalMoves as INTEGER
DIM card1v, card2v, card3v, card4v, card5v, card6v, card7v, card8v, card9v, card10v, card11v, card12v AS IMAGE
DIM card1r, card2r, card3r, card4r, card5r, card6r, card7r, card8r, card9r, card10r, card11r, card12r AS IMAGE
DIM question, arrow AS IMAGE
DIM moves AS TEXT
totalCards = 12 '4x3 board

SET VOICE "jenny"
BGIMAGE "bg_ocean_boat_blue"
MUSIC "pirate_polska", 50

' Draw all cards (verso)
SOUND "flip"
card1v = image "card_pirate3", (10, 150)', "fade-in", 500
FRAME card1v, (10, 150)-(160, 300)
card2v = image "card_pirate2", (165, 150)', "fade-in", 500
FRAME card2v, (165, 150)-(315, 300)
card3v = image "card_pirate2", (320, 150)', "fade-in", 500
FRAME card3v, (320, 150)-(470, 300)
card4v = image "card_pirate1", (475, 150)', "fade-in", 500
FRAME card4v, (475, 150)-(625, 300)
card5v = image "card_pirate3", (10, 310)', "fade-in", 500
FRAME card5v, (10, 310)-(160, 460)
card6v = image "card_pirate1", (165, 310)', "fade-in", 500
FRAME card6v, (165, 310)-(315, 460)
card7v = image "card_pirate2", (320, 310)', "fade-in", 500
FRAME card7v, (320, 310)-(470, 460)
card8v = image "card_pirate3", (475, 310)', "fade-in", 500
FRAME card8v, (475, 310)-(625, 460)
card9v = image "card_pirate3", (10, 470)', "fade-in", 500
FRAME card9v, (10, 470)-(160, 620)
card10v = image "card_pirate1", (165, 470)', "fade-in", 500
FRAME card10v, (165, 470)-(315, 620)
card11v = image "card_pirate2", (320, 470)', "fade-in", 500
FRAME card11v, (320, 470)-(470, 620)
card12v = image "card_pirate1", (475, 470)', "fade-in", 500
FRAME card12v, (475, 470)-(625, 620)

SLEEP 3000

' Draw all cards (recto)
SOUND "crunch"
card1r = image "card1", (10, 150)
FRAME card1r, (10, 150)-(160, 300)
card2r = image "card1", (165, 150)
FRAME card2r, (165, 150)-(315, 300)
card3r = image "card1", (320, 150)
FRAME card3r, (320, 150)-(470, 300)
card4r = image "card1", (475, 150)
FRAME card4r, (475, 150)-(625, 300)
card5r = image "card1", (10, 310)
FRAME card5r, (10, 310)-(160, 460)
card6r = image "card1", (165, 310)
FRAME card6r, (165, 310)-(315, 460)
card7r = image "card1", (320, 310)
FRAME card7r, (320, 310)-(470, 460)
card8r = image "card1", (475, 310)
FRAME card8r, (475, 310)-(625, 460)
card9r = image "card1", (10, 470)
FRAME card9r, (10, 470)-(160, 620)
card10r = image "card1", (165, 470)
FRAME card10r, (165, 470)-(315, 620)
card11r = image "card1", (320, 470)
FRAME card11r, (320, 470)-(470, 620)
card12r = image "card1", (475, 470)
FRAME card12r, (475, 470)-(625, 620)

ANIM card1r, "scale", 100, 104
ANIM card2r, "scale", 100, 104
ANIM card3r, "scale", 100, 104
ANIM card4r, "scale", 100, 104
ANIM card5r, "scale", 100, 104
ANIM card6r, "scale", 100, 104
ANIM card7r, "scale", 100, 104
ANIM card8r, "scale", 100, 104
ANIM card9r, "scale", 100, 104
ANIM card10r, "scale", 100, 104
ANIM card11r, "scale", 100, 104
ANIM card12r, "scale", 100, 104

' Set values to the cards to be able to match them.
VALUE card1r, "3"
VALUE card2r, "2"
VALUE card3r, "2"
VALUE card4r, "1"
VALUE card5r, "3"
VALUE card6r, "1"
VALUE card7r, "2"
VALUE card8r, "3"
VALUE card9r, "3"
VALUE card10r, "1"
VALUE card11r, "2"
VALUE card12r, "1"

' Set events
ON CLICK card1r, showCard
ON CLICK card2r, showCard
ON CLICK card3r, showCard
ON CLICK card4r, showCard
ON CLICK card5r, showCard
ON CLICK card6r, showCard
ON CLICK card7r, showCard
ON CLICK card8r, showCard
ON CLICK card9r, showCard
ON CLICK card10r, showCard
ON CLICK card11r, showCard
ON CLICK card12r, showCard

' Display total moves
moves = TEXT "Moves: " + STR(0), (250, 50)
COLOR moves, #ffffff, #000000, 3

' Show question
LET question = image "question", (500, 50)
ON CLICK question, playQuestion

' To go to next level
arrow = IMAGE "arrow_right", (500,500), "hide"

' Play question now.
playQuestion

'-------------------- SUBs ---------------------------
SUB playQuestion
VOICE "Find pairs of matching cards."
END SUB

SUB disableCards
DISABLE card1r
DISABLE card2r
DISABLE card3r
DISABLE card4r
DISABLE card5r
DISABLE card6r
DISABLE card7r
DISABLE card8r
DISABLE card9r
DISABLE card10r
DISABLE card11r
DISABLE card12r
END SUB

SUB enableCards
ENABLE card1r
ENABLE card2r
ENABLE card3r
ENABLE card4r
ENABLE card5r
ENABLE card6r
ENABLE card7r
ENABLE card8r
ENABLE card9r
ENABLE card10r
ENABLE card11r
ENABLE card12r
END SUB

SUB showCard
disableCards
HIDE _OBJECT
SOUND "flip2", "no-wait"

IF selected = 0 THEN
pairValue = VALUE(_OBJECT)
IF _OBJECT = card1r THEN
selected = 1
END IF
IF _OBJECT = card2r THEN
selected = 2
END IF
IF _OBJECT = card3r THEN
selected = 3
END IF
IF _OBJECT = card4r THEN
selected = 4
END IF
IF _OBJECT = card5r THEN
selected = 5
END IF
IF _OBJECT = card6r THEN
selected = 6
END IF
IF _OBJECT = card7r THEN
selected = 7
END IF
IF _OBJECT = card8r THEN
selected = 8
END IF
IF _OBJECT = card9r THEN
selected = 9
END IF
IF _OBJECT = card10r THEN
selected = 10
END IF
IF _OBJECT = card11r THEN
selected = 11
END IF
IF _OBJECT = card12r THEN
selected = 12
END IF
ELSE
IF selected > 0 THEN
updateMoves
' Check if this second card matches the first.
IF pairValue = VALUE(_OBJECT) THEN
'YES
total = total + 2
attempt = 0
HIDE arrow
arrowVisible = 0
playSound
ELSE
'NO, roll back this card and previous card.
IF playedDontMatch = 0 THEN
playedDontMatch = 1
VOICE "Oops, those cards dont match.", "no-wait" 'Say this only once.
SLEEP 2000
END IF
attempt = attempt + 1
SOUND "error", "no-wait"
SLEEP 500 ' Give time to see the card
SHOW _OBJECT
IF selected = 1 THEN
SHOW card1r
END IF
IF selected = 2 THEN
SHOW card2r
END IF
IF selected = 3 THEN
SHOW card3r
END IF
IF selected = 4 THEN
SHOW card4r
END IF
IF selected = 5 THEN
SHOW card5r
END IF
IF selected = 6 THEN
SHOW card6r
END IF
IF selected = 7 THEN
SHOW card7r
END IF
IF selected = 8 THEN
SHOW card8r
END IF
IF selected = 9 THEN
SHOW card9r
END IF
IF selected = 10 THEN
SHOW card10r
END IF
IF selected = 11 THEN
SHOW card11r
END IF
IF selected = 12 THEN
SHOW card12r
END IF
END IF
WAIT
selected = 0 'Reset for new attempt.
END IF
END IF

WAIT
enableCards

IF total = totalCards THEN
showWin
WAIT
quitNow
END IF

IF attempt >= 5 THEN
showArrow
END IF

END SUB

SUB updateMoves
totalMoves = totalMoves + 1
SET TEXT moves, "Moves: " + STR(totalMoves)
END SUB

SUB playSound
IF total < totalCards THEN
SOUND "correct2", "no-wait"
ELSE
SOUND "correct2"
END IF
END SUB

SUB showWin
SOUND "win", "no-wait"
PARTICLES "small_star", (70, 180)
PARTICLES "small_star", (225, 180)
PARTICLES "small_star", (380, 180)
PARTICLES "small_star", (535, 180)
SLEEP 100
SOUND "win", "no-wait"
PARTICLES "small_star", (70, 340)
PARTICLES "small_star", (225, 340)
PARTICLES "small_star", (380, 340)
PARTICLES "small_star", (535, 340)
SLEEP 100
SOUND "win", "no-wait"
PARTICLES "small_star", (70, 500)
PARTICLES "small_star", (225, 500)
PARTICLES "small_star", (380, 500)
PARTICLES "small_star", (535, 500)
SLEEP 2000
END SUB

'Arrow to go to next level.
SUB showArrow
IF arrowVisible = 0 THEN
arrowVisible = 1
SHOW arrow
WAIT
ANIM arrow , "yoyo", (500,500)-(520,500), 1000
ON CLICK arrow, quitNow
END IF
END SUB

SUB quitNow
HIDE arrow
RELOAD
END SUB


Go back to list of Tutorials.