简体   繁体   English

Java 中记忆游戏的 GameOver 屏幕

[英]GameOver Screen for a Memory game in Java

I am programming a Memory game in java and I want to make a GameOver-Screen with an image from the sprites folder (I already have the image for it and it is an image with .jpg) after all cards are flipped.我正在用 java 编写一个 Memory 游戏,我想在所有卡片翻转后使用 sprites 文件夹中的图像制作一个 GameOver-Screen(我已经有了它的图像,它是一个带有 .jpg 的图像)。 So when all cards are flipped, the game is actually over and this GameOver-Screen appears.所以当所有的牌都翻完后,游戏实际上就结束了,出现了这个GameOver-Screen。 I have no idea how I can do that.我不知道我怎么能做到这一点。 The only thing I know is that it should start with public void gameOver(){ .我唯一知道的是它应该以public void gameOver(){ Probably it is possible to make something with "if()" and between the brackets you could say that the condition is that all cards are flipped.可能可以用“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();

    }
}

Each time a card is flipped, check if all cards have been flipped.每次翻转卡片时,检查是否所有卡片都翻转过。 Use a loop - you already have one in your code.使用循环 - 您的代码中已经有了循环。 For each index in the for loop... ie each card... check if getIdVisible() is 0.对于 for 循环中的每个索引...即每张卡片...检查 getIdVisible() 是否为 0。

If the card at index i is still hidden, return null and continue running your program is normal.如果索引 i 处的卡仍然隐藏,则返回 null 并继续运行您的程序是正常的。 However, if you get all the way through the loop, that means all cards are visible, which means you can call the gameOver() method, which shows the game over screen.但是,如果您一直通过循环,这意味着所有卡片都是可见的,这意味着您可以调用 gameOver() 方法,该方法显示游戏结束屏幕。

Does that sound like it fits with your program?这听起来是否适合您的程序?

Edit:编辑:

To check if all cards are flipped, one thing you can do is to make MemeryCard[] cards (up in the constructor) a global variable.要检查是否翻转了所有卡片,您可以做的一件事是将 MemeryCard[] 卡片(在构造函数中)设为全局变量。 Then you can just call this method every time you reveal two 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;
}

I'd say call this method in mouseEvent() before you return true for mouseEvent(), and if this returns true you can call the gameOver() method.我会说在为mouseEvent()返回true 之前在mouseEvent()调用此方法,如果返回true,则可以调用gameOver()方法。

As for how to handle rendering the image... I don't know the api you're using for this game, so I can't tell you specifically how to do that.至于如何处理渲染图像......我不知道你在这个游戏中使用的api,所以我不能具体告诉你如何做到这一点。 But at least with this code you should be able to trigger that gameOver.但至少用这段代码你应该能够触发那个游戏结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM