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

2016/4/26

Making a Memory Game

1
2
3
4
danpost danpost

2016/5/1

#
carter wrote...
Now when I click 2 incorrect cards. The card clicked first will stay, while the card clicked second will wait a small amount, and then flip back, while the card clicked first is still showing the image. I changed a lot of code and probably messed up bad somewhere, but cannot find where. Thanks for your help beforehand
The 'if(2 cards flipped)' code does not flip the first flipped card back in the 'else' part like it should.
carter carter

2016/5/1

#
How could I make it do so?
danpost danpost

2016/5/1

#
carter wrote...
How could I make it do so?
You need to save a reference to the first card flipped.
carter carter

2016/5/1

#
So if I do
public static Cards firstClicked
and
if(firstClicked == null)
{
firstClicked = this
//in card1-card6
}
how can I do
if(matching pair flipped)
{
    method
}
else
{
flip firstClicked
flip card clicked 2nd
}
danpost danpost

2016/5/1

#
You should really try and get away from numbering the cards and using a different class for each card. Everything you need to do in a matching card game can be done with just one class for all the cards. The only things a card needs to retain are the two images assigned to it and which card matches it. A timer is optional to help in creating animations between sets of choices. The world class can do all the work in assigning the proper objects to the field references (for the images and the matching card) while creating the cards. Then they can be shuffled and added into the world (from the world class. I created one and ended up with this act method in the Card class:
public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        if (first == null) // first card picked
        {
            first = this;
            flip();
        }
        else if (second == null) // second card picked
        {
            second = this;
            flip();
            Greenfoot.delay(30);
            if (first != match) // not matching
            {
                first.flip();
                flip();
            }
            else paired++; // matching
            
            // prepare for next set to compare
            first = null;
            second = null;
        }
    }
}
using the following fields:
static int paired; // counts matches made during a level
static Card first, second; // holds the clicked on cards

GreenfootImage[] images = new GreenfootImage[2]; // the images for this actor
Card match; // the card that matches this one
The flip method was simply this:
private void flip()
{
    setImage(getImage() == images[0] ? images[1] : images[0]);
}
which basically says, "whatever my images is, change it to the other one".
carter carter

2016/5/2

#
I have no idea how I would do it with one class, and I need it done by Tuesday. I would rather just stick with the way I am doing it now, unless I could get a LOT of help.
danpost danpost

2016/5/2

#
carter wrote...
I have no idea how I would do it with one class, and I need it done by Tuesday. I would rather just stick with the way I am doing it now, unless I could get a LOT of help.
I uploaded the scenario I was talking about. It is Match Game Demo.
IronRod IronRod

2016/5/2

#
Can you help me please! Scenario: There is a man in the middle. There are different coloured balloons moving towards him from all four directions. He has to pop the balloons before they reach him. He can only pop the balloon if the colour of his head corresponds with the colour of the balloon. Problem: I am having problems with the boolean variables that compare the colour of the man's head and balloon. Here is the code (this is in the code of the bullet that hits the balloon):
public static boolean redBalloon=true;
public static boolean blueBalloon=false;
public static boolean yellowBalloon=false;
public static boolean redHead=true;
public static boolean blueHead=false;
public static boolean yellowHead=false;
public void act()
{
hitAndMiss();
}
public void hitAndMiss()
    {           
        getBalloonColour();
        getHeadColour();
        pop();           
    }
    public void pop()
    {
        Actor balloon = getOneIntersectingObject(Balloon.class);
        if (balloon != null)
        {
            if (redHead==true && redBalloon==true||blueHead==true && blueBalloon==true||yellowHead==true && yellowBalloon==true)
         {
             getWorld().removeObject(balloon);           
         }
         else
         {
             getWorld().removeObject(this);            
         }
        }
    }
    public void getBalloonColour()
    {
        Actor balloon = getOneIntersectingObject(Balloon.class);
        if (balloon != null)
        {
            if (balloon.getImage().equals("balloon1.png"))
        {
            redBalloon=true;
            blueBalloon=false;
            yellowBalloon=false;
        }
        if (balloon.getImage().equals("balloon2.png"))
        {
            redBalloon=false;
            blueBalloon=true;
            yellowBalloon=false;
        }
        if (balloon.getImage().equals("balloon3.png"))
        {
            redBalloon=false;
            blueBalloon=false;
            yellowBalloon=true;
        } 
        }
    }
    public void getHeadColour()
    {
        Head head = (Head) getWorld().getObjects(Head.class).get(0);
        if (head.getImage().equals("button-red.png"))
        {
            redHead=true;
            blueHead=false;
            yellowHead=false;
        }
        if (head.getImage().equals("button-blue.png"))
        {
            redHead=false;
            blueHead=true;
            yellowHead=false;
        }
        if (head.getImage().equals("button-yellow.png"))
        {
            redHead=false;
            blueHead=false;
            yellowHead=true;
        }  
    }
}
Please help!
danpost danpost

2016/5/2

#
@IronRed, you should start a new discussion thread on your issue. I will try to help when I can (am a bit busy right now). However, I can immediately say that you should not be using static variables for the colors of the different actors.
IronRod IronRod

2016/5/2

#
Ok, I understand. I have, however, started a new discussion thread called Balloon-Popping Game. Please (when you're free) help me over there.
danpost danpost

2016/5/2

#
IronRod wrote...
Ok, I understand. I have, however, started a new discussion thread called Balloon-Popping Game. Please (when you're free) help me over there.
Well, that is all you needed to do. It was not necessary to ruin the continuity of this discussion.
danpost danpost

2016/5/2

#
carter wrote...
I have no idea how I would do it with one class, and I need it done by Tuesday. I would rather just stick with the way I am doing it now, unless I could get a LOT of help.
My two classes were as follows: The world class (Table):
import greenfoot.*;
import java.util.*;

/**
 * PROJECT: Match Game Demo
 * AUTHOR: danpost (greenfoot.org username)
 * DATE:  March 1, 2016
 * 
 * DESCRIPTION: a simple (in most ways) demonstration of a match game scenario.
 */
public class Table extends World
{
    int level = 0;
    
    public Table()
    {
        super(800, 410, 1);
        // creates a dark gray background color
        getBackground().setColor(java.awt.Color.darkGray);
        getBackground().fill();
        // creates the first level
        upLevel();
    }
    
    private void upLevel()
    {
        // initialize class field
        Card.paired = 0; // to count matches as they are made
         // clear the world of (possible) old objects
        removeObjects(getObjects(null));
        // up the level number (if not maxed
        level++;
        if (level > 7) level--;
        // define number of pairs for this level
        int pairs = level+1;
        // shuffle the image numbers (so the same level does not always use the same images)
        Integer[] fileNums = { 1, 2, 3, 4, 5, 6, 7, 8 };
        Collections.shuffle(Arrays.asList(fileNums));
        // build an array of cards for this level (cards created and assigned to the array)
        Card[] cards = new Card[pairs*2];
        for (int i=0; i<pairs*2; i++) cards[i] = new Card();
        // assign the cards the references to their face images and their matching cards
        // (1 <> 2, 3 <> 4, 5 <> 6, etc.) then shuffles the cards in the array
        for (int i=0; i<cards.length; i+=2)
        {
            // notify cards of their matching cards
            cards[i].setMatch(cards[i+1]);
            cards[i+1].setMatch(cards[i]);
            // set same face image to both cards
            cards[i].setImage(fileNums[i/2]);
            cards[i+1].setImage(fileNums[i/2]);
        }
        Collections.shuffle(Arrays.asList(cards)); // shuffle cards
        // add the cards into the world (at calculated locations)
        int rows = cards.length < 10 ? 2 : 3; // determine number of rows
        int loWide = cards.length/rows; // determine minimum cards per row
        int xtra = cards.length-rows*loWide; // number of extra cards to add into rows
        int xtrasPlaced = 0; // to track number of extras placed in world
        int y0 = getHeight()/2-125*(rows-1)/2; // determine starting y-coordinate for rows
        for (int r=0; r<rows; r++)
        {
            // determine number of cards for this row
            int wide = loWide +(xtra > 0 ? 1-(xtra+r)%2 : 0);
            // determine starting x-coordinate for this row 
            int x0 = getWidth()/2-125*(wide-1)/2;
            // add cards for this row into world
            for (int c=0; c<wide; c++)
                addObject(cards[r*loWide+c+xtrasPlaced], x0+c*125, y0+r*125);
            if (wide > loWide) xtrasPlaced++; // update extras placed
        }
    }
    
    public void act()
    {
        // proceeds to next level if all cards are matched
        if (Card.paired == level+1) upLevel();
    }
}
and the Actor class (Card):
import greenfoot.*;
import java.awt.Color;

public class Card extends Actor
{
    static int paired; // counts matches made during a level
    static Card first, second; // holds the clicked on cards
    
    GreenfootImage[] images = new GreenfootImage[2]; // the images for this actor
    Card match; // the card that matches this one
    
    /** used to obtain the matching card to this one */
    public void setMatch(Card c)
    {
        match = c;
    }
    
    /** used to obtain the face up image of this card (and creates a random back image) */
    public void setImage(int front)
    {
        // gets and sizes its face image
        images[1] = new GreenfootImage("fractal"+front+".png");
        images[1].scale(100, 100);
        // chooses random color part values for the fill color of the back image
        int red = 128+Greenfoot.getRandomNumber(128);
        int blue = 128+Greenfoot.getRandomNumber(128);
        int green = 128+Greenfoot.getRandomNumber(128);
        // creates and paints the back image with fill color
        images[0] = new GreenfootImage(100, 100);
        images[0].setColor(new Color(red, blue, green));
        images[0].fill();
        // draws a rectangle (or square) on the back image
        images[0].setColor(Color.black);
        images[0].drawRect(5, 5, 90, 90);
        // sets the initial image of the card to the back image
        setImage(images[0]);
    }
    
    /** checks for and processes clicks on this card */
    public void act()
    {
        if (Greenfoot.mouseClicked(this) && getImage() == images[0])
        {
            if (first == null) // first clicked card
            {
                first = this;
                flip();
                return;
            }
            if (second == null) // second clicked card
            {
                second = this;
                flip();
                Greenfoot.delay(30);
                if (first != match) // not a match
                {
                    first.flip();
                    flip(); // or 'this.flip();' or 'second.flip();'
                }
                else paired++; // matching
                
                // clears picks to allow for new ones
                first = null;
                second = null;
            }
        }
    }
        
    /** switches the image of this card between its two assigned images */
    private void flip()
    {
        setImage(getImage() == images[0] ? images[1] : images[0]);
    }
}
Use this as a guide to the general format.
carter carter

2016/5/2

#
Where do I define the images?
danpost danpost

2016/5/2

#
carter wrote...
Where do I define the images?
Line 9 of the Card class defines the array called 'images' (or is that not what you are referring to?).
carter carter

2016/5/3

#
Where do I put the new GreenfootImage ("_____.png")
There are more replies on the next page.
1
2
3
4