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

2012/4/30

Flip a card one at a time and guess

1
2
miko122 miko122

2012/4/30

#
Hello I am trying to make a guessing card game and it should go like this where when it hits run one card should flip over and the user has to guess by letter but so far I can't seem to figure out how to get just one card to flip at a time and only display one of the three pictures. Here is the source code for card class and world class. I haven't yet wrote the code to try and guess the picture so any help would be greatly appreciated. world class: public class LetterGame extends World { private Selector selector; private Message message; private String imageNamesFirstRow = {"ambulance.png", "butterfly.png", "elephant.png"}; private String imageNamesSecondRow = {"frog.png", "house-03.png", "kangaroo.png"}; private String imageNamesThirdRow = {"pig.png", "tent.png", "spider.png"}; /** * Constructor for objects of class LetterGame. * */ public LetterGame() { // Create a new world with 500x500 cells with a cell size of 1x1 pixels. super(500, 500, 1); //This line creates the selector object - DO NOT remove or change this line. selector = new Selector(this); //Place your code for the constructor after this comment. addObject( new Message(), 250, 480); message = new Message(); //card = newCard(); for (int i=0;i<3;i++) { addObject( new Card(selector, imageNamesFirstRow, imageNamesFirstRow. charAt (0)), 100, 100); addObject( new Card(selector, imageNamesFirstRow, imageNamesFirstRow. charAt (0)), 250, 100); addObject( new Card(selector, imageNamesFirstRow, imageNamesFirstRow. charAt (0)), 400, 100); addObject( new Card(selector, imageNamesSecondRow, imageNamesSecondRow. charAt (0)), 100, 250); addObject( new Card(selector, imageNamesSecondRow, imageNamesSecondRow. charAt (0)), 250, 250); addObject( new Card(selector, imageNamesSecondRow, imageNamesSecondRow. charAt (0)), 400, 250); addObject( new Card(selector, imageNamesThirdRow, imageNamesThirdRow. charAt (0)), 100, 400); addObject( new Card(selector, imageNamesThirdRow, imageNamesThirdRow. charAt (0)), 250, 400); addObject( new Card(selector, imageNamesThirdRow, imageNamesThirdRow. charAt (0)), 400, 400); } //and before this comment } public void act() { selector.newCard(); } public void showGuessMessage() { message.showGuessImage(); } public void showCorrectMessage() { message.showCorrectAnswerImage(); } card class public class Card extends Actor { private Selector selector; private GreenfootImage backgroundImage; private boolean faceUp; private GreenfootImage faceImage; private String letter; private String cardImage= {"ambulance","butterfly","elephant","frog","house", "kangaroo","pig","spider","tent"}; public Card(Selector sel, String cardImages, char let) { selector = sel; sel.addCard(this); createInitialImage(); faceImage= new GreenfootImage(cardImages); faceUp=false; letter= ""+ let; //Add your code for the constructor after this comment } /** * Act - do whatever the Card wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { turnOver(); } public void turnOver() { //setImage(faceImage); if (faceUp){ setImage(backgroundImage); faceUp=false; } else{ setImage(faceImage); faceUp= true;} } private void createInitialImage() { backgroundImage = new GreenfootImage(100, 100); backgroundImage.setColor(java.awt.Color.BLUE); backgroundImage.fillRect(0, 0, 100, 100); setImage(backgroundImage); } }
danpost danpost

2012/4/30

#
In your Card class act method, you have turnOver. This will have all your cards constantly changing images. The only reason you might want an act method for a Card class object is to check: if faceUp is true and user input recieved, compare, and if match, reset for next Card test, else reset input status. Also, I am not sure if this is what you intended, but in your world constructor, the 'for' loop that adds the Card objects in the world will, as is, add 27 cards to the world. One more thing: why pass both the name of the image file and the first character of the same? Just pass the image filename which includes its own first character. As is, you will always have the same images in the same locations. If that is what you intended then one array of String for the image filenames is sufficient. And the for loop to add the Card objects in the world could be as few one internal statement.
String[] imageNames = {"ambulance.png", "butterfly.png", "elephant.png",
               "frog.png", "house-03.png", "kangaroo.png",
               "pig.png", "tent.png", "spider.png" };
with a for loop of
for (int i = 0; i < 9; i++)
{
    addObject(new Card(selector, imageNames[i]), 100 + 150 * (i % 3), 100 + 150 * ((i - (i % 3)) / 3));
}
You can 'shuffle' the image filenames in the imageNames array to re-arrange the pictures.
miko122 miko122

2012/4/30

#
When I add this in it won't compile. It tells me it cannot find the symbol- variable imageNames
danpost danpost

2012/4/30

#
It appears that you left out the array index. It should be imageNames that it finds. Did you include the '' after imageNames in the for loop?
miko122 miko122

2012/4/30

#
I had it with the i but when i would take it out it wouldnt compile because it needs .class after imageNames but even after putting that in it still wouldn't compile
danpost danpost

2012/4/30

#
Show the code and exactly what error message you are getting.
miko122 miko122

2012/4/30

#
for (int i=0;i<9;i++) { addObject (new Card(selector, imageNames), 100+150 * (i % 3), 100+150 * ((i-(i % 3))/ 3)); } if i have i in the it says it can't find the variable if i dont have i it says .class is expected
davmac davmac

2012/4/30

#
Do you have this part:
String[] imageNames = {"ambulance.png", "butterfly.png", "elephant.png",  
               "frog.png", "house-03.png", "kangaroo.png",  
               "pig.png", "tent.png", "spider.png" }; 
... anywhere?
danpost danpost

2012/4/30

#
You need the 'i' in there. Show the variable declaration statement for imageNames (in the same class, of course)
miko122 miko122

2012/4/30

#
Sorry this stuff is a bit confusing for me, I am just trying to pass a class. public LetterGame() { // Create a new world with 500x500 cells with a cell size of 1x1 pixels. super(500, 500, 1); //This line creates the selector object - DO NOT remove or change this line. selector = new Selector(this); //Place your code for the constructor after this comment. addObject( new Message(), 250, 480); message = new Message(); //card = newCard(); String imageNames = {"ambulance.png", "butterfly.png", "elephant.png", "frog.png", "house-03.png", "kangaroo.png", "pig.png", "tent.png", "spider.png" }; for (int i=0;i<9;i++) { addObject (new Card(selector, imageNames), 100+150 * (i % 3), 100+150 * ((i-(i % 3))/ 3)); } this is what i have inside my lettergame world that have changed from original post. I have 3 rows i need to make and i assigned 3 images per row and from what i had before all three images would appear when the cards would flip.
danpost danpost

2012/4/30

#
The code I provided will produce 3 rows of 3 images per row in exactly the same locations as you had. Can you comment out the code that does not compile and upload the scenario with source so we can take a look?
miko122 miko122

2012/4/30

#
Any chance i could get an email address so it would be easier to talk back and forth
miko122 miko122

2012/4/30

#
Any chance i could get an email address so it would be easier to talk back and forth
danpost danpost

2012/4/30

#
Also, needing correction was your message = new Message(); and addObject(new Message(), 250, 480); lines which were in the reverse order (and instead of creating two different Message objects, now creates and saves the same one).
yanks101 yanks101

2012/4/30

#
miko ill offer u $100 for your lab 7 and practice 4 if they are completed
There are more replies on the next page.
1
2