Die Karten können mit einem Mausklick aufgedeckt werden. Falls Sie eigene Bilder verwenden möchten, speichern Sie Ihre neue Bilder lokal im userhome/gamegrid/sprites. Die Bilder werden beim Erstellen des Jars automatisch mitverpackt. Unsere Bilder sind 113 x 113 Pixel gross. Falls Sie eine andere Grössen verwenden möchten, müssen Sie die Zeile
|
![]() |
Programmcode:
// Memory.java import ch.aplu.jgamegrid.*; import ch.aplu.util.*; public class Memory extends GameGrid implements GGMouseListener { private boolean isReady = true; private MemoryCard card1; private MemoryCard card2; public Memory() { super(4, 4, 115, null, null, false); MemoryCard[] cards = new MemoryCard[16]; for (int i = 0; i < 16; i++) { if (i < 8) cards[i] = new MemoryCard(i); else cards[i] = new MemoryCard(i - 8); addActor(cards[i], getRandomEmptyLocation()); cards[i].show(1); } addMouseListener(this, GGMouse.lPress); doRun(); show(); // Application thread used to flip back cards while (true) { Monitor.putSleep(); // Wait until there is something to do delay(1000); card1.show(1); // Flip cards back card2.show(1); isReady = true; setMouseEnabled(true); // Rearm mouse events } } public boolean mouseEvent(GGMouse mouse) { Location location = toLocation(mouse.getX(), mouse.getY()); MemoryCard card = (MemoryCard)getOneActorAt(location); if (card.getIdVisible() == 0) // Card already flipped->no action return true; card.show(0); // Show picture if (isReady) { isReady = false; card1 = card; } else { card2 = card; if (card1.getId() == card2.getId()) // Pair found, let them visible isReady = true; else { setMouseEnabled(false); // Disable mouse events until application thread flipped back cards Monitor.wakeUp(); } } return true; } public static void main(String[] args) { new Memory(); } } // --------------------- class MemoryCard ------------------------------------------- class MemoryCard extends Actor { private int id; public MemoryCard(int id) { super("sprites/card" + id + ".gif", "sprites/cardback.gif"); this.id = id; } public int getId() { return id; } } |