This site requires JavaScript, please enable it in your browser!
Greenfoot back
JollyGreenGiant
JollyGreenGiant wrote ...

2020/4/26

How do i make a simple memory card game?

6
7
8
9
10
11
12
danpost danpost

2020/5/3

#
danpost wrote...
Okay, You call it isShowing:
return isShowing;
(no round brackets)
(Line 37 of your last post)
JollyGreenGiant JollyGreenGiant

2020/5/3

#
It works now, thank you so much!
JollyGreenGiant JollyGreenGiant

2020/5/3

#
All I need to do is to match the images and that will be it completed.
danpost danpost

2020/5/3

#
Okay. Next thing is what to do when the second card is flipped up. Regardless of whether it matches the first card, you will probably want the card to remain face up for a short moment so the player will get a chance to see what it was. So, you need and int timer field that will run from some positive value down to zero. When it is positive (while the two picked cards are face up), there should not be any user interactions (no mouse clicking). So, add the field:
private int timer;
and then at the beginning of the act method, insert the following lines:
if (timer > 0)
{
    timer--;
    if (timer > 0) return;
    /** next coding step will replace the following */
    firstPicked.turn(-90);
    secondPicked.turn(-90);
    /** ********************************************* */
}
Now, the 'else' part for picking the second card can now be:
else
{
    secondPicked = c;
    timer = 40;
}
Now, after picking two cards, there should be a short delay before the picked cards are rotated. I only have them rotating so you can judge how long the delay lasts. You can adjust the initial value of the timer (from 40 above) to suit what you think is reasonable.
JollyGreenGiant JollyGreenGiant

2020/5/4

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot, and MouseInfo)

/**
 * Write a description of class MemoryGame here.
 * 
 *
 */
public class MemoryGame extends World
{
    Card[] cards = new Card[20];

    private static Card firstPicked;
    private static Card secondPicked;
    private int timer;

    public static int matchTries;
    public static int matchCount;

    public MemoryGame()
    {
        super(4, 5, 100);
        // load array
        // shuffle
        // deal
        for (int i=0; i<cards.length; i++) cards[i] = new Card("g"+(i/2), i/2);
        java.util.Collections.shuffle(java.util.Arrays.asList(cards));
        for (int i=0; i<cards.length; i++) addObject(cards[i], i/5, i%5);

    }

    public void act()
    {
        mouseClicking();
    }

    private void mouseClicking()
    {
        if(Greenfoot.mouseClicked(null)) // any click
        {
            Actor clickedOn = Greenfoot.getMouseInfo().getActor();

            if (clickedOn == null || ! (clickedOn instanceof Card)) return;
            Card c = (Card)clickedOn;
            if (c.isShowing()) return;

            if (firstPicked == null) // first pick
            {
                firstPicked = c;
                c.flipCard();
            }
            else 
            {
                secondPicked = c;
                c.flipCard();
            }
        }

    }

    private int timer()
    {
        if (timer > 0)
        {
            timer--;
            if (timer > 0) return;

            firstPicked.turn(-90);
            secondPicked.turn(-90);

        }
        else
        {
            secondPicked = c;
            timer = 40;
        }
    }

}
JollyGreenGiant JollyGreenGiant

2020/5/4

#
Having issues.
Super_Hippo Super_Hippo

2020/5/4

#
JollyGreenGiant wrote...
Having issues.
Don’t be too precise.
danpost danpost

2020/5/4

#
I was hoping you would end up with this:
public void act()
{
    if (timer > 0)
    {
        timer--;
        if (timer > 0) return;
        firstPicked.turn(-90);
        secondPicked.turn(-90);
    }
    mouseClicking();
}

private void mouseClicking()
{
    if(Greenfoot.mouseClicked(null)) // any click
    {
        Actor clickedOn = Greenfoot.getMouseInfo().getActor();
        if (clickedOn == null || ! (clickedOn instanceof Card)) return;
        Card c = (Card)clickedOn;
        if (c.isShowing()) return;
        if (firstPicked == null) // first pick
        {
            firstPicked = c;
            c.flipCard();
        }
        else
        {
            secondPicked = c;
            c.flipCard();
            timer = 40;
        }
    }
}
Replace line 31 to 76 with this.
JollyGreenGiant JollyGreenGiant

2020/5/4

#
That works now thank you.
JollyGreenGiant JollyGreenGiant

2020/5/4

#
So all I need to do is to have the images match.
JollyGreenGiant JollyGreenGiant

2020/5/4

#
I should create a public void match in the world class.
JollyGreenGiant JollyGreenGiant

2020/5/4

#
private void match
JollyGreenGiant JollyGreenGiant

2020/5/4

#
Taking a closer look at my program, i think i would need to add vars in the mouse clicking method.
danpost danpost

2020/5/4

#
JollyGreenGiant wrote...
Taking a closer look at my program, i think i would need to add vars in the mouse clicking method.
No. I think you have all the vars you need.
danpost danpost

2020/5/4

#
JollyGreenGiant wrote...
I should create a public void match in the world class.
I guess you could. 'private' is fine, however. You would then need to remove lines 7 and 8 above and replace them with:
match();
There are more replies on the next page.
6
7
8
9
10
11
12