First off, I am new to all of this and have followed some basic tutorials and have tried to adapt one for myself. I took the basic Card Match memory game and I wanted to make some changes. 2 Decks of 5 cards each for a total of 10 cards. Only 1 card from each deck should match. I thought that should be easy but, I am lost on how to assigning the same randomly selected sprite to a randomly select card in each deck. Also if I touch more than one sprite at the same time it selects both even when the code disables all other sprites after one is touched.PH_v3_copy.aia (224.7 KB)
Your Sprites are hexagonal and overlapping, so they are subject to Sprite Cannibalism.
See this sample app for how to avoid that through use of a global variable to hold the current Sprite or blank if none has been touched:
This is usually done by taking a copy of original lists of cards and sprites, and depleting the cards and sprites from the copies as they are assigned into matches.
Here is how I would code this:
-
Keep global variables:
- A_Flipped: -1 or the Sprite that was flipped in set A
- B_Flipped: -1 or the Sprite that was flipped in set B
-
Keep global lists:
- A_Sprites (5 components, fixed)
- B_Sprites (")
- A_Images (initially empty each round, to be filled with 5 image file names soon)
- B_Images (")
- All_Pics (All images that could be used in Sprites listed above)
-
To reset a round:
- set A_Flipped to -1
- set B_Flipped to -1
- set A_Images to make a list(select random item from All_Pics)
- set B_Images to copy of A_Images
- while length of list(A_Images) < 5 do
- set local temp_image to random item from list All_Pics
- if not(temp_image is in list A_Images) and not(temp_image is in list B_Images) then add temp_image to list A_Images
- while length of list(B_Images) < 5 do
- set local temp_image to random item from list All_Pics
- if not(temp_image is in list A_Images) and not(temp_image is in list B_Images) then add temp_image to list B_Images
- set list A_Images to shuffle(A_Images)
- set list B_Images to shuffle(B_Images)
When a sprite is touched:
- If (touched sprite is in A_Sprites list) and (A_Flipped = -1) then
- set A_Flipped to sprite component
- set index i to index in list A_Sprites of touched component
- set Picture of A_Flipped component to (select item i from list A_Images)
- If (touched sprite is in B_Sprites list) and (B_Flipped = -1) then
- set B_Flipped to sprite component
- set index i to index in list B_Sprites of touched component
- set Picture of B_Flipped component to (select item i from list B_Images)
- if (A_Flipped is in list A_Sprites) and (B_Flipped is in list B_Sprites) then
- if A_Flipped.Picture = B_Flipped.Picture then you have a match, else a mismatch
- unflip the two sprites:
- set their images to card backs
- set A_Flipped back to -1
- set B_Flipped back to -1
P.S. These blocks can be dragged directly into your Blocks Editor workspace.
I appreciate the help. I will give this a try and see if I can grasp the concept of what is going on. Thank you.
I learned a lot from your help. I appreciate you didn't just hand me all the blocks. Working from the text help me understand what was going on and having to locate and use blocks I haven't seen in tutorials help understand the concept. Thanks again.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.