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

2014/2/13

Memory game Trouble

yutiroe yutiroe

2014/2/13

#
How do i have a my memory game spawn 6 different object for 6 times in a 6x6 tiles. I manage to spawn everything 6 times, but they keep overlapping each other. Also I will need to match the matched object and remove them later, how do i do that ?
yutiroe yutiroe

2014/2/13

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.*; import java.util.*; /** * This is the background of my game * * @author (Clinton Ho) * @date (2014/2/5) * @George Brown ID (100904449) */ public class Background extends World { private static int numCardX = 6; private static int numCardY = 6; private static int width = numCardX + 1; private static int length = numCardY + 1; private static int cardSize = 80; private static int numCardsRemain ; private static int totalCards = numCardX*numCardY; private static int spawn1=0,spawn2=0, spawn3=0, spawn4=0, spawn5=0, spawn6=0; public static int random; /** * Constructor for objects of class Background. * */ public Background() { // Create a new world with 6x6 cards, with a card size of 50 cells. // Crete lines to show different location for cards. super(numCardX, numCardY, cardSize); GreenfootImage gfi = new GreenfootImage(width*cardSize, length*cardSize); gfi.setColor(Color.white); gfi.fill(); gfi.setColor(Color.black); int end = numCardY*cardSize-1; for (int i=1; i <= numCardX; i++) gfi.drawLine(i*cardSize, 0, i*cardSize, end); end = numCardX*cardSize-1; for (int i=1; i <= numCardY; i++) gfi.drawLine(0, i*cardSize, end, i*cardSize); setBackground(gfi); for (int i = 0; i< 6; i++) addObject(new Butterfly(),287+(5*i)), 488); for (int i=0; i <6; i++) addObject(new Button(), Greenfoot.getRandomNumber(7),Greenfoot.getRandomNumber(7)); for (int i=0; i <6; i++) addObject(new Robot(), Greenfoot.getRandomNumber(7),Greenfoot.getRandomNumber(7)); for (int i=0; i <6; i++) addObject(new Elephant(), Greenfoot.getRandomNumber(7),Greenfoot.getRandomNumber(7)); for (int i=0; i <6; i++) addObject(new House(), Greenfoot.getRandomNumber(7),Greenfoot.getRandomNumber(7)); for (int i=0; i <6; i++) addObject(new Bee(), Greenfoot.getRandomNumber(7),Greenfoot.getRandomNumber(7)); for (int i=0; i < numCardX; i++) for (int j=0; j < numCardY; j++) addObject(new Cards(), i, j); } }
yutiroe yutiroe

2014/2/13

#
The reason why i addobject new card at the end, is for the card to cover all the other object. In my Actor class, only the card class have coding in it, which is remove the object when user click on it. Please help me, im new to programming and i need help badly
danpost danpost

2014/2/14

#
I think that I would first create the 36 objects and place them in the world (they do not have to be randomly placed -- you could place all six butterflies on the first row, all six buttons on the second, etc.). Then, before adding the cards to cover them, shuffle the objects. There are several method you could imploy to accomplish this. For example, you could do this:
Actor first = getObjectsAt(0, 0, null).get(0); // get actor at top-left
for (int i=0; i<100; i++)
{
    Actor actor = getObjects(null).get(Greenfoot.getRandomNumber(36)); // get a random actor
    first.setLocation(actor.getX(), actor.getY()); // replace random with top-left
    actor.setLocation(0, 0); // complete the swap
    first = actor; // assign new top-left
}
danpost danpost

2014/2/14

#
To compare the objects, you can use something like:
if (firstChoice.getClass().getName().equals(secondChoice.getClass().getName()))
You will need to add instance fields to hold references to the chosen objects ('firstChoice' and 'secondChoice'). Then, you can use their location to re-cover the objects with new Cards objects if they do not match.
You need to login to post a reply.