【发布时间】:2024-04-28 20:20:03
【问题描述】:
我是一个相对较新的程序员,我正在使用 Java 构建一个刽子手游戏。我的问题主要与我所说的“时间和/或事件”有关(如果您觉得有更好的描述,请告诉我)。
当我的应用程序开始执行时,会发生一些事情:1) 它显示一条问候消息(“Welcome to Java Hangman...”),2) 它调用 startgame(),startgame() 所做的事情之一是调用drawman(o),这显示一个空的绞刑架,3)如果你赢了(当然你会因为游戏刚刚开始),显示一个问题供用户尝试回答(所有这些代码都在下面)。
我的问题/我想做的是:1)每 2 秒有问候消息“闪烁”(出现和消失),同时屏幕上正在“构建”空绞刑架(manZero()), 2)“建造”空绞架,从“顶部”或“底部”开始,每 2 秒显示一个不同的绞架“件”(字符串),直到完成,3)一旦绞架的最后一块,如果“建立”,问候消息停止“闪烁”,然后提示用户第一个问题。
最终结果:游戏开始,问候语“闪烁”,而绞刑架正在“建造”中,最后一块绞刑架添加后,问候语停止“闪烁”并提出问题。
用另一种方式描述,输出看起来像这样:
//application begins execution
System.out.println("Welcome to Java Hangman...");// greet flashes every two seconds
// meanwhile the gallows is being built below greet
public void manZero()
{
System.out.println("_____"); // 2 sec after that this string appears("last piece")
System.out.println("| |"); // 2 seconds after that this string appears
System.out.println("| "); // 2 seconds after that this string appears
System.out.println("| "); // 2 seconds later this string appears
System.out.println("| "); // first this string appears
当“最后一块”出现在输出中时,问候停止闪烁,空的绞刑架(manZero())完好无损,然后向用户提出第一个问题(即出现在输出中)。
这是一个多阶段的问题,我意识到 Stack 之类的东西要简洁,但由于这些问题都涉及类似的问题,我认为这将是一个很好的并且可能很有趣的话题。此外,由于我是编程新手,请提供带有建议的示例,最好是使用现有代码的完整示例,因为我不需要太多时间就能发现自己陷入困境。
现在游戏在命令下运行,但我有兴趣添加一些挥杆功能,如果您想帮助我或想查看整个程序的代码,请发送电子邮件至 reedwilliams8404@gmail.com
感谢您的时间和投入,我对您的想法很感兴趣。
/*
main, greeting, call to start
*/
public class HangmanMain
{
public static void main(String[] args)
{
System.out.println("Welcome to Java Hangman...");
HangmanGame hangmanGame = new HangmanGame(initQuestions());
try
{
hangmanGame.startGame();
}
/*
startGame(), call to drawman(O), pose first question
*/
public HangmanGame(List<Question> questions)
{
this.questions = questions;
manDrawer = new ManDrawer();
questionPoser = new QuestionPoser();
}
// sets/resets values
public void startGame() throws IOException
{
manDrawer.drawMan(0);
incorrectGuesses = 0;
guesses = 6;
// for each Question question in questions
for(Question question : questions)
{
// not winning
if(!isWinning)
{
break;
}
// winning
else
{
questionPoser.poseQuestion(question);
askQuestionUntilCorrectOrOutOfGuesses(question);
handleQuestionSuccessOrGameFailure(question);
}
}
/*
"builds" gallows
*/
//gets value of field incorrectGuesses, determines corresponding method to call,
// calls corresponding method, corresponding method displays/draws hangman image
public class ManDrawer
{
// determines which man#() to call based on the value of incorrectGuesses
public void drawMan(int incorrectGuesses)
{
switch (incorrectGuesses) {
case 0:
manZero();
break;
case 1:
manOne();
break;
//...
// value of incorrectGuesses is 0
public void manZero()
{
System.out.println("_____");
System.out.println("| |");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
}
// value of incorrectGuesses is 1
public void manOne()
{
System.out.println("_____");
System.out.println("| |");
System.out.println("| o");
System.out.println("| ");
System.out.println("| ");
}
//...
【问题讨论】:
标签: java events command-line command