Java内存游戏的GameOver屏幕

Java内存游戏的GameOver屏幕,java,Java,我正在用java编程一个记忆游戏,我想在所有卡片翻转后,用sprites文件夹中的一个图像(我已经有了它的图像,它是一个带有.jpg的图像)在屏幕上制作一个游戏。所以当所有的牌都翻转时,游戏实际上结束了,这个GameOver屏幕出现了。我不知道我该怎么做。我唯一知道的是,它应该以public void gameOver(){开头。也许可以用“if()”做一些东西,在括号之间,你可以说条件是所有的牌都翻转了 // Memory.java import ch.aplu.jgamegrid.*; //

我正在用java编程一个记忆游戏,我想在所有卡片翻转后,用sprites文件夹中的一个图像(我已经有了它的图像,它是一个带有.jpg的图像)在屏幕上制作一个游戏。所以当所有的牌都翻转时,游戏实际上结束了,这个GameOver屏幕出现了。我不知道我该怎么做。我唯一知道的是,它应该以
public void gameOver(){
开头。也许可以用“if()”做一些东西,在括号之间,你可以说条件是所有的牌都翻转了

// Memory.java
import ch.aplu.jgamegrid.*; // Imports the Library of Gamegrid, on which this code is based
import ch.aplu.util.*;


public class Memory extends GameGrid implements GGMouseListener {


    private boolean isReady = true;
    private MemoryCard card1;
    private MemoryCard card2;
    public Memory() {

        super(6, 6, 115, null, null, false);

        MemoryCard[] cards = new MemoryCard[36];

        for (int i = 0; i < 36; i++) {

            if (i < 18)
                cards[i] = new MemoryCard(i);
            else

                cards[i] = new MemoryCard(i - 18);

            addActor(cards[i], getRandomEmptyLocation());
            cards[i].show(1);
        }

        addMouseListener(this, GGMouse.lPress);

        // Application thread used to flip back cards 
        doRun();
        show();
        while (true) {
            // Wait until there is something to do
            Monitor.putSleep();
            delay(1000);
            // Flip cards back
            card1.show(1);
            card2.show(1);
            isReady = true;
            // Rearm mouse events
            setMouseEnabled(true);
        }
    }


    // imports the GGMouse package
    public boolean mouseEvent(GGMouse mouse) {

        Location location = toLocation(mouse.getX(), mouse.getY());
        MemoryCard card = (MemoryCard) getOneActorAt(location);

        // Card already flipped->no action
        if (card.getIdVisible() == 0)
            return true;
        // Show picture
        card.show(0);
        if (isReady) {
            isReady = false;
            card1 = card;
        } else {

            card2 = card;
            // Pair found, let them visible

            if (card1.getId() == card2.getId())
                isReady = true;
            else {
                // Disable mouse events until application thread flipped back cards
                setMouseEnabled(false);
                Monitor.wakeUp();
            }

        }
        return true;
    }


    public static void main(String[] args) {
        new Memory();

    }
}
//Memory.java
import ch.aplu.jgamegrid.*;//导入此代码所基于的Gamegrid库
导入ch.aplu.util.*;
公共类内存扩展GameGrid实现GGMouseListener{
私有布尔值isReady=true;
私人记忆卡1;
私人记忆卡2;
公共内存(){
super(6,6,115,null,null,false);
MemoryCard[]卡片=新的MemoryCard[36];
对于(int i=0;i<36;i++){
如果(i<18)
卡片[i]=新的记忆卡(i);
其他的
卡片[i]=新的记忆卡(i-18);
addActor(cards[i],getRandomEmptyLocation());
卡片[i].展示(1);
}
addMouseListener(这个,GGMouse.lPress);
//用于翻转卡片的应用程序线程
多伦();
show();
while(true){
//等到有事情要做
Monitor.putSleep();
延迟(1000);
//倒回卡片
卡片1.展示(1);
卡片2.展示(1);
isReady=真;
//重新武装鼠标事件
setMouseEnabled(true);
}
}
//导入GGMouse包
公共布尔mouseEvent(GGMouse-mouse){
Location-Location=toLocation(mouse.getX(),mouse.getY());
MemoryCard=(MemoryCard)getOneActorAt(位置);
//卡片已翻转->无操作
if(card.getIdVisible()==0)
返回true;
//展示图片
卡片显示(0);
如果(isReady){
isReady=false;
card1=卡片;
}否则{
card2=卡片;
//找到一对,让它们可见
if(card1.getId()==card2.getId())
isReady=真;
否则{
//禁用鼠标事件,直到应用程序线程翻转回卡
setMouseEnabled(false);
Monitor.wakeUp();
}
}
返回true;
}
公共静态void main(字符串[]args){
新内存();
}
}

每次翻转卡片时,检查是否所有卡片都已翻转。使用循环-代码中已经有一个循环。对于For循环中的每个索引…即每张卡片…检查getIdVisible()是否为0

如果索引i处的卡仍然隐藏,则返回null并继续运行程序是正常的。但是,如果您一直通过循环,则意味着所有卡都可见,这意味着您可以调用gameOver()方法,该方法在屏幕上显示游戏

这听起来和你的计划相符吗

编辑:

要检查是否所有卡片都已翻转,可以做的一件事是将MemeryCard[]cards(在构造函数中向上)设置为全局变量。然后,每次显示两张卡片时,可以调用此方法:

public boolean areAllCardsFlipped() {

    // Start looking through each card
    for (int i = 0; i < 36; i++) {

        // We found a card that is NOT flipped. Stop here, because clearly all cards are not flipped.
        if (cards[i].getIdVisible() != 0) {
            return false;
        }
    }

    // We have looked at all cards, and we know that each one is flipped. We can return true.
    return true;
}
public boolean areAllCardsFlipped(){
//开始检查每张卡片
对于(int i=0;i<36;i++){
//我们发现一张牌没有翻转。请停在这里,因为显然所有的牌都没有翻转。
if(卡片[i].getIdVisible()!=0){
返回false;
}
}
//我们已经看过所有的牌,我们知道每一张都是翻转的。我们可以返回真的。
返回true;
}
在为mouseEvent()返回true之前,我建议在
mouseEvent()
中调用此方法,如果返回true,则可以调用
gameOver()
方法


至于如何处理渲染图像…我不知道你在这个游戏中使用的api,所以我无法具体告诉你如何操作。但至少通过这段代码,你应该能够触发gameOver。

那么我应该在代码中添加什么,以便调用gameOver()方法?我的第二个问题是,如何对gameOver()方法进行编码,以便在游戏结束时使用sprites文件夹中的图像作为屏幕?我现在已经实现了代码,但有一个小问题。
public boolean areAllCardsFlipped(){
中的修饰符不起作用,必须删除(来自程序的建议)。我想我使用的API是JGameGrid.jar库。