標準描画には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;
}
}