在标准绘图中有一个isKeyPressed()
函数。该函数返回boolean
,表示参数是否被按下。
在我制作的基于文字的游戏中,用户将按y
或n
来确定结果。有时,但并非总是如此,跳过以下故事文本。这是我的代码。我在日食中运行它。这不是我的所有代码,而是我认为问题所在。
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;
}
}
答案 0 :(得分:2)
StdDraw.isKeyPressed()
接收int
而不是char
public static boolean isKeyPressed(int keycode)
如果当前正在按下键码,则返回true,否则返回
有关密钥代码的说明,请参阅KeyEvent.java。
更新: OP要求举例:
if(StdDraw.isKeyPressed(KeyEvent.VK_Y) {
// True if the 'y' key is currently being pressed
}
答案 1 :(得分: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
,那么第一个语句执行ELSE if 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;
}
}