0

在标准绘图中有一个isKeyPressed()功能。该函数返回一个boolean指示参数是否被按下。

在我正在制作的基于文本的游戏中,用户将按下yn确定结果。有时,但并非总是如此,会跳过以下故事文本。这是我的代码。我在eclipse中运行这个。这不是我的全部代码,但我认为问题出在哪里。

while(true){

else if(stage == 6){

StdDraw.textLeft(0, 700,"'so, ya name's "+ name + ", huh?'");

StdDraw.textLeft(0, 680,"'What an odd name if i do say so.'");

StdDraw.textLeft(0, 660,"'you're lucky i found you when i did, the dark forest is dangerous!'");

StdDraw.textLeft(0, 620,"'So, you heading to Ivangard, right?'(y/n)");
            counter = 1;

if(StdDraw.isKeyPressed('Y')&&frame%8==0){

stage = 7;

            }
else if (StdDraw.isKeyPressed('N')&&frame%8==0){
                    stage = 9;
            }
        }
else if(stage == 7){

StdDraw.textLeft(0, 700,"'well lucky you! i'm going in the same direction!'");

    StdDraw.textLeft(0, 680,"'Well i'm Program "+ random +", nice to meet you.'");

    StdDraw.textLeft(0, 660,"'take this rusty dagger. may be of use!'");

    StdDraw.textLeft(0, 640,"'ill see you in Ivangaurd!'");

                    StdDraw.textLeft(0, 500,"continue?(O)");

                    if(StdDraw.isKeyPressed('O')&&frame%12==0){

                        stage = 11;

                        programn = random;

                        fist = 0;

                        rustyd = 1;

                }

                }



            else if(stage == 9){

        StdDraw.textLeft(0, 700,"'Well, if you need me, ill be in Ivangaurd'");

        StdDraw.textLeft(0, 680,"'I'm Program #"+ random +", nice to meet you.'");

        StdDraw.textLeft(0, 660,"'take this rusty dagger. may be of use!'");

                StdDraw.textLeft(0, 620,"continue?(O)");

                counter = 1;

                programn = random;

                fist = 0;

                rustyd = 1;



                if(StdDraw.isKeyPressed('O')&&frame%12==0){

                    stage = 11;

            }

            }

else if(stage == 11){

        StdDraw.textLeft(0, 700,"Part 2: The virus");

    StdDraw.textLeft(0, 660,"as you leave the dark forest, The voice comes back");

    StdDraw.textLeft(0, 640,"A great evil is coming to this land.");

        StdDraw.textLeft(0, 620,"a virus, a plague the program...");

        StdDraw.textLeft(0, 600,"unlike the program, all the virus does is eat.");

                StdDraw.textLeft(0, 580,"That is why we need you.");

StdDraw.textLeft(0, 560,"you are human, uneatable to the virus. All other warriors would fall were you stand.");

                StdDraw.textLeft(0, 540,"continue?(0)");

                if(StdDraw.isKeyPressed('O')&&frame%12==0){

                    stage = 10;

                }

            }

}

问题出在 y/n 选项之后,它会跳过以下文本。

新文本在这里。我是新手编码员,所以我可能不明白你告诉我要做什么。

else if(stage == 6){
            StdDraw.textLeft(0, 700,"'so, ya name's "+ name + ", huh?'");
            StdDraw.textLeft(0, 680,"'What an odd name if i do say so.'");
            StdDraw.textLeft(0, 660,"'you're lucky i found you when i did, the dark forest is dangerous!'");
            StdDraw.textLeft(0, 620,"'So, you heading to Ivangard, right?'(y/n)");
            counter = 1;
            if(StdDraw.isKeyPressed('Y')&&frame%8==0){
                stage = 7;

            }
            if(StdDraw.isKeyPressed('N')&&frame%8==0){
                stage = 9;
            }
        }
4

2 回答 2

2

StdDraw.isKeyPressed()接受一个int不是char

public static boolean isKeyPressed(int keycode)

如果当前正在按下键码,则返回 true,否则返回 false

StdDraw.isKeyPressed

有关键码的描述,请参见KeyEvent.java

更新: OP要求举个例子:

if(StdDraw.isKeyPressed(KeyEvent.VK_Y) {
   // True if the 'y' key is currently being pressed
}
于 2012-07-27T04:30:59.263 回答
1

好的,您尝试更改 else-if 语句的执行路径。你不能这样做。

if (animal == sheep) {

   System.out.println("Fox in a sheep skin");
   animal = fox;

} else if (animal == fox) {

   System.out.println("hide the sheep");

}

现在基本上,如果animal == sheep然后执行第一条语句,否则如果执行animal == fox第二条语句。您无法更改侧边的流程,因为它已经决定了执行路径。

就像来到岔路口,只能单向走,不能双向走……

if (animal == sheep) {

   System.out.println("Fox in a sheep skin");
   animal = fox;

} 

if (animal == fox) {

   System.out.println("hide the sheep");

}

会做你想做的事,它更昂贵,因为它需要检查条件,但它会强制执行流通过每个语句。

更新

我怀疑您误解了在哪里进行更改(没有任何证据可以说明,很难知道)

这应该是它的样子(为了便于阅读,我把文本语句去掉了)

if(stage == 6){

    //... some text

    if(StdDraw.isKeyPressed('Y')&&frame%8==0){
        stage = 7;
    } else if (StdDraw.isKeyPressed('N')&&frame%8==0){
        stage = 9;
    }
}

if(stage == 7){

    //... some text

    if(StdDraw.isKeyPressed('O')&&frame%12==0){

        stage = 11;

        programn = random;

        fist = 0;

        rustyd = 1;

    }

}

if(stage == 9) {

    //... some text

    if(StdDraw.isKeyPressed('O')&&frame%12==0){

        stage = 11;

    }

}

if(stage == 11){

    //... some text

    if(StdDraw.isKeyPressed('O')&&frame%12==0){

        stage = 10;

    }

}
于 2012-07-27T02:51:44.567 回答