两人指挥游戏 [英] Two Player Command Game
问题描述
大家好,
我正在制作的Java游戏出现问题.这是飞镖游戏,但是要扔"飞镖,必须输入命令.我设法创建了一种投掷" 3支飞镖的方法,从501中扣除了3支飞镖的最终得分.这是在我的Darts类的"playGame"部分完成的.
我希望能够从已经完成的比赛中制作转弯系统,并且也可以为2个玩家建立转弯系统.谢谢!
这是我的代码:
Hey guys,
I''m having a problem with my java game I am making. It is a darts game, but to "throw" a dart, you must enter a command. I''ve managed to create a way to "throw" 3 darts, with the final score of the 3 darts deducted from 501. This is done in the "playGame" part of my Darts class.
I want to be able to make a turn system from what I have made already, and a way too make it for 2 players. Thanks!
Here is my code:
public class Darts{
int userInput;
public static void main(String[] args) {
menu();
}
public static void menu (){
int userInput;
TextIO.putln("Darts!");
TextIO.putln();
TextIO.putln();
TextIO.putln("Please select an Option :");
TextIO.putln("1 - to Play the Game");
TextIO.putln("2 - for Instructions");
TextIO.putln("3 - to Exit the Game");
userInput = TextIO.getlnInt();
switch (userInput){
case 1 : playGame();
case 2 : instructions();
case 3 : endGame();
default : NotA();
}
}
public static void playGame() {
char anyChar;
Turn currentTurn = new Turn();
currentTurn.thrown[0] = new Throw();
TextIO.putln("What number are you aiming at?");
currentTurn.thrown[0].number = TextIO.getlnInt();
TextIO.putln("Enter 2 for a Double, 3 for a Treble, otherwise enter 1:");
currentTurn.thrown[0].multiplier = TextIO.getlnInt();
TextIO.put("You scored: ");
TextIO.putln(currentTurn.thrown[0].number * currentTurn.thrown[0].multiplier);
currentTurn.thrown[1] = new Throw();
TextIO.putln("What number are you aiming at?");
currentTurn.thrown[1].number = TextIO.getlnInt();
TextIO.putln("Enter 2 for a Double, 3 for a Treble, otherwise enter 1:");
currentTurn.thrown[1].multiplier = TextIO.getlnInt();
TextIO.put("You scored: ");
TextIO.putln(currentTurn.thrown[1].number * currentTurn.thrown[1].multiplier);
currentTurn.thrown[2] = new Throw();
TextIO.putln("What number are you aiming at?");
currentTurn.thrown[2].number = TextIO.getlnInt();
TextIO.putln("Enter 2 for a Double, 3 for a Treble, otherwise enter 1:");
currentTurn.thrown[2].multiplier = TextIO.getlnInt();
TextIO.put("You scored: ");
TextIO.putln(currentTurn.thrown[2].number * currentTurn.thrown[2].multiplier);
TextIO.put("Your score now is: ");
TextIO.putln(currentTurn.sTotal - ((currentTurn.thrown[0].number * currentTurn.thrown[0].multiplier) + (currentTurn.thrown[1].number * currentTurn.thrown[1].multiplier) + (currentTurn.thrown[2].number * currentTurn.thrown[2].multiplier)));
int nTotal = currentTurn.sTotal - ((currentTurn.thrown[0].number * currentTurn.thrown[0].multiplier) + (currentTurn.thrown[1].number * currentTurn.thrown[1].multiplier) + (currentTurn.thrown[2].number * currentTurn.thrown[2].multiplier));
anyChar = TextIO.getAnyChar();
menu();
}
public static void instructions() {
char anyChar;
TextIO.putln();
TextIO.putln("Commands");
TextIO.putln();
TextIO.putln("You have three darts, when prompted you need to enter your command.");
TextIO.putln("Your command will be a number followed by a number.");
TextIO.putln("The numbers will be 1, 2 and 3.");
TextIO.putln("");
TextIO.putln("They represent Single, Double or Triple");
TextIO.putln("E.g. 20 followed by 2 represents Double twenty.");
TextIO.putln();
TextIO.put("Press any key to continue: ");
anyChar = TextIO.getAnyChar();
menu();
}
public static void endGame() {
System.exit(0);
}
public static void NotA() {
menu();
}
}
那是我的主菜单,第一轮转向系统.这是我使用的其他类:
That was my main menu, with the system of taking the first turn. Here are the other classes I use:
public class Turn {
Throw[] thrown = new Throw[3]; // three darts thrown per turn
int sTotal = 501;
}
public class Throw {
int number;
int multiplier; // 3 for a treble, 2 for a double, 1 for anything else
}
推荐答案
您需要考虑一下在现实世界中飞镖比赛的运行方式:
playGame()
函数一直运行到一名玩家降至零.每个玩家轮流进行掷球,总共有3球.每次掷球之后,检查分数以查看游戏是否结束.重复.
所以playGame()
应该看起来像:
You need to think about how a darts match runs in the real world:
TheplayGame()
function runs until one player gets down to zero. Each player takes it in turns to throw, and has a total of 3 throws. After each throw the score is checked to see if the game has ended. Repeat.
SoplayGame()
should look something like:
while not finished
for darts = 1 to 3
score = player1.throw()
if score == 0
finished = true
break;
if finished
break;
// repeat the above for player 2
// repeat the entire process
这篇关于两人指挥游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!