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

2015/3/10

Change Image of a class?

ProfessionalNoob ProfessionalNoob

2015/3/10

#
My program generates a bunch of cards for each player. I was wondering how one would make it so that a person cannot see exactly what cards his opponent has, which is why I want to set them all to another image if so the player can't see them. Here's what I tried so far:
import greenfoot.*; 
 
public class CardWorld extends World
{
    int playerCount = 2;
    int turn;
    boolean turnBoolean;
    Player[] Player = new Player[playerCount];
    public GreenfootImage BackCard = new GreenfootImage("CardBack.png");
     Cards Card = new Cards();
    public CardWorld()
    {    
        super(845, 570, 1);
        //SomeDice myDice = new SomeDice();
        for (int i=0; i<playerCount; i++)
        {
            Player[i] = new Player(); 
            addObject(Player[i], 436, 100+340*i);
        }
    }
 
    public void act()
    {
        
        if (Greenfoot.mouseClicked(Player[turn%playerCount]))
        {  
            Card.setImage(BackCard);
            Player[turn%playerCount].CardSelect();
            turn ++; 
            
        }
         if (turn%playerCount == 1)
        { 
            Card.setImage(BackCard);
        }
    }
 
}
Super_Hippo Super_Hippo

2015/3/10

#
Not tested, but this should work. Variable names should start with a lowercase letter.
for (Object obj: getObjects(Card.class))
{
    Actor a = (Actor) obj;
    a.setImage(backCard);
}
danpost danpost

2015/3/11

#
You should add methods to your Card class for getting and setting the visual state of the card (whether it is shown or hidden). You can add a boolean field for its 'visible' state:
private boolean visible;
private GreenfootImage frontCard;

public void setVisible(boolean state)
{
    visible = state;
    setImage(visible ? frontCard : backCard);
}

public void show()
{
    setVisible(true);
}

public void hide()
{
    setVisible(false);
}

public boolean isVisible()
{
    return visible;
}
As the last statement in your 'generation' method in the Card class, use this:
frontCard = getImage();
ProfessionalNoob ProfessionalNoob

2015/3/11

#
Super_Hippo wrote...
Not tested, but this should work. Variable names should start with a lowercase letter.
for (Object obj: getObjects(Card.class))
{
    Actor a = (Actor) obj;
    a.setImage(backCard);
}
Thank you, but I'm wondering how I can make this so that it restricts to the card location? I want it so that you can see your own cards, but not the other persons.
ProfessionalNoob ProfessionalNoob

2015/3/11

#
danpost wrote...
You should add methods to your Card class for getting and setting the visual state of the card (whether it is shown or hidden). You can add a boolean field for its 'visible' state:
private boolean visible;
private GreenfootImage frontCard;

public void setVisible(boolean state)
{
    visible = state;
    setImage(visible ? frontCard : backCard);
}

public void show()
{
    setVisible(true);
}

public void hide()
{
    setVisible(false);
}

public boolean isVisible()
{
    return visible;
}
As the last statement in your 'generation' method in the Card class, use this:
frontCard = getImage();
Thank you. But I was wondering what I would then do in the world class? Should I call Card.hide() if one player is going?
danpost danpost

2015/3/11

#
You could then add methods in the Player class:
public void hideCards()
{
    for (Card card : cards) card.hide();
}

public void showCards()
{
    for (Card card: cards) card.show();
}
Then to switch to the next player, you could use:
player[turn%playerCount].hideCards();
turn++;
player[turn%playerCount].showCards();
Super_Hippo Super_Hippo

2015/3/11

#
ProfessionalNoob wrote...
Thank you, but I'm wondering how I can make this so that it restricts to the card location? I want it so that you can see your own cards, but not the other persons.
for (Object obj: getObjects(Card.class))
{
    Actor a = (Actor) obj;
    if (a.getX</**...*/ && a.getX>/**...*/ && a.getY()</**...*/ && a.getY()>/**...*/) a.setImage(backCard);
    else a.setImage(frontCard);
    // or instead, every card gets an ID to represent the owner of the card
    Card c = (Card) obj;
    if (c.getID() != myID) c.setImage(backCard);
}
You need to login to post a reply.