Java 文字游戏。如何使用袋子?
我想知道在我制作的游戏中如何使用包。这个袋子应该装着2件物品,可以在地图上不同的房间里找到,当这两件物品都找到时,游戏就可以通过找到老板的房间来完成。包应该是它自己的java类。我如何让玩家激活这些物品 rum4和rum6是任何想知道的人的物品房间 我不想让任何人帮我完成这场比赛,我只想得到一些帮助 //对不起,瑞典语文本 //这显然不是全部代码,但其余部分不是必需的Java 文字游戏。如何使用袋子?,java,class,jcreator,Java,Class,Jcreator,我想知道在我制作的游戏中如何使用包。这个袋子应该装着2件物品,可以在地图上不同的房间里找到,当这两件物品都找到时,游戏就可以通过找到老板的房间来完成。包应该是它自己的java类。我如何让玩家激活这些物品 rum4和rum6是任何想知道的人的物品房间 我不想让任何人帮我完成这场比赛,我只想得到一些帮助 //对不起,瑞典语文本 //这显然不是全部代码,但其余部分不是必需的 import java.util.Scanner; public class Spel { public static voi
import java.util.Scanner;
public class Spel
{
public static void main(String[] args) {
Rum start = new Rum("Du är i en mörk och fuktig källare."," En källare. ");
Rum rum1 = new Rum("Du är mitt i en snöstorm!", "En snöstorm. ");
Rum rum2 = new Rum("Du hittade ett svärd!", "Ett hus. ");
Rum rum3 = new Rum("Du gick in i en fälla, slå över 3 för att fly norrut.", "En skog. ");
Rum rum4 = new Rum("Jaha... här fanns det ingenting.", "En äng. ");
start.north = rum1;
start.east = rum2;
start.south = rum3;
rum1.south = start;
rum1.east = rum4;
rum2.west = start;
rum2.north = rum4;
rum3.fälla = new trap();
rum3.north = start;
rum4.west = rum1;
rum4.south = rum2;
Rum current = start;
while(true) {
System.out.println(current);
System.out.println("Vart vill du gå? (n,s,v,o)");
char c = new Scanner(System.in).next().charAt(0);
switch(c) {
case 'n':
current = current.north;
break;
case 's':
current = current.south;
break;
case 'v':
current = current.west;
break;
case 'o':
current = current.east;
break;
}
if (current.fälla != null){
current.fälla.rulla();
}
// if (monster){
// System.out.println("Du kan nu döda monsteret");
if(current == null) {
System.out.println("Du ramlar av världen och dör †††");
System.out.println("Försök igen");
current = start;
}
}
您可以创建一些玩家类来存储所有玩家数据,并将包放入其中以存放物品,例如: HashMapItemsByName
在添加新项目时,如果未超过2号,请始终检查。您可以创建一些玩家类来存储所有玩家数据,并将包放入其中以容纳项目,例如: HashMapItemsByName
在添加新物品时,如果没有超过2号,请务必进行检查。我会创建一个包含两件物品的班级包
public class Bag {
Item item1;
Item item2;
}
public class Item {
<any item properties here>
}
我将创建一个包含两个项目的类包
public class Bag {
Item item1;
Item item2;
}
public class Item {
<any item properties here>
}
在这种情况下,我相信您需要一个项目类,以及特定效果的项目类型
public abstract class Item {
public abstract void useItemOn(Entity target);
}
public class Entity {
private int maxHitPoints;
private int hitPoints;
private boolean isDead;
...
public void inflictDamage(int damage) {
hitPoints -= damage;
if(hitPoints < 0) {
this.isDead = true;
}
}
public void heal(int healPoints) {
hitPoints += healPoints;
if(hitPoints > maxHitPoints) {
this.hitPoints = maxHitPoints;
}
}
...
}
public class Player extends Entity {
...
}
public class Enemy extends Entity {
...
}
public Bomb extends Item {
@Override
public void useItemOn(Entity target) {
target.inflictDamage(20);
}
}
public Potion extends Item {
@Override
public void useItemOn(Entity target) {
target.heal(20);
}
}
然后
当然,你可以用更聪明的方法来储存物品,不要把它们混在一起,因为你仍然需要弄清楚什么东西是药剂还是炸弹
但无论如何,使用它很简单,您需要从列表中获取一个项目,在实体上使用它,然后从列表中删除它(如果它是耗材)。在这种情况下,我相信您需要一个项目类,以及特定效果的项目类型
public abstract class Item {
public abstract void useItemOn(Entity target);
}
public class Entity {
private int maxHitPoints;
private int hitPoints;
private boolean isDead;
...
public void inflictDamage(int damage) {
hitPoints -= damage;
if(hitPoints < 0) {
this.isDead = true;
}
}
public void heal(int healPoints) {
hitPoints += healPoints;
if(hitPoints > maxHitPoints) {
this.hitPoints = maxHitPoints;
}
}
...
}
public class Player extends Entity {
...
}
public class Enemy extends Entity {
...
}
public Bomb extends Item {
@Override
public void useItemOn(Entity target) {
target.inflictDamage(20);
}
}
public Potion extends Item {
@Override
public void useItemOn(Entity target) {
target.heal(20);
}
}
然后
当然,你可以用更聪明的方法来储存物品,不要把它们混在一起,因为你仍然需要弄清楚什么东西是药剂还是炸弹
但是无论如何,使用它很简单,你需要从列表中取出一个项目,在一个实体上使用它,然后如果它是一个消耗品,就从列表中删除它。有人能在找到项目之前找到老板的房间吗?如果否:如何预防 使用我的解决方案时必须执行的操作: 在游戏开始时创建包 只要找到其中一个项:调用setFirstItemFound或setSecondItemFound方法。 我不知道您想如何启用boss room,但您可以使用isBothItemsFound方法检查是否可以找到我 请注意,此解决方案仅在只需要两项时有效。如果以后需要10个项目,我建议您使用hashTbale、list或任何同等的堆栈类
public class Bag {
private static boolean firstItemFound = false;
private static boolean secondItemFound = false;
public Bag() {
super();
firstItemFound = false;
secondItemFound = false;
}
/* Sets the status to of the first Item to found */
public static void setFirstItemFound() {
firstItemFound = true;
}
/* Checks if the first item has been found */
public static boolean isFirstItemFound() {
return firstItemFound;
}
/* Sets the status to of the second Item to found */
public static boolean isFSecondItemFound() {
return secondItemFound;
}
/* Checks if the second item has been found */
public static void setSecondItemFound() {
secondItemFound = true;
}
/* Checks if both items has been found */
public static boolean isBothItemsFound() {
boolean bothItemsFound = false;
if (firstItemFound && secondItemFound){
bothItemsFound = true;
}
return bothItemsFound;
}
/* Sets the status to of both Items to not found */
public static void clearBag() {
firstItemFound = false;
secondItemFound = false;
}
}有人能在找到物品之前找到老板的房间吗?如果否:如何预防 使用我的解决方案时必须执行的操作: 在游戏开始时创建包 只要找到其中一个项:调用setFirstItemFound或setSecondItemFound方法。 我不知道您想如何启用boss room,但您可以使用isBothItemsFound方法检查是否可以找到我 请注意,此解决方案仅在只需要两项时有效。如果以后需要10个项目,我建议您使用hashTbale、list或任何同等的堆栈类
public class Bag {
private static boolean firstItemFound = false;
private static boolean secondItemFound = false;
public Bag() {
super();
firstItemFound = false;
secondItemFound = false;
}
/* Sets the status to of the first Item to found */
public static void setFirstItemFound() {
firstItemFound = true;
}
/* Checks if the first item has been found */
public static boolean isFirstItemFound() {
return firstItemFound;
}
/* Sets the status to of the second Item to found */
public static boolean isFSecondItemFound() {
return secondItemFound;
}
/* Checks if the second item has been found */
public static void setSecondItemFound() {
secondItemFound = true;
}
/* Checks if both items has been found */
public static boolean isBothItemsFound() {
boolean bothItemsFound = false;
if (firstItemFound && secondItemFound){
bothItemsFound = true;
}
return bothItemsFound;
}
/* Sets the status to of both Items to not found */
public static void clearBag() {
firstItemFound = false;
secondItemFound = false;
}
}您的意思是要创建一个类,作为一个包,并保存玩家找到的对象?你试过什么?此外,这些对象应该是地图中的特殊字符,比如G代表黄金,对吗?一般提示:当真的{do.something}->经常导致生命/死亡外观和/或其他问题时,你应该注意这样想。使用一些变量而不是像gameNotFinished这样的true。您可以使用gameNotFinished作为全局变量,并在游戏完成后对其进行更改。我还想知道为什么你没有使用start.west=anyrum之类的东西你是说你想创建一个类来充当包,并保存玩家找到的对象?你试过什么?此外,这些对象应该是地图中的特殊字符,比如G代表黄金,对吗?一般提示:当真的{do.something}->经常导致生命/死亡外观和/或其他问题时,你应该注意这样想。使用一些变量而不是像gameNotFinished这样的true。您可以使用gameNotFinished作为全局变量,并在游戏完成后对其进行更改。另外,我想知道为什么你没有使用像start.west=anyrum这样的东西。我只想要一个bag类。我不想让它太复杂。我认为缺乏结构只会使它更复杂我只想要一个袋子类。我不想把它弄得太复杂。我想结构的缺乏只会使它变得更复杂但是我如何让玩家激活这些物品呢?也许我应该把它放在问题里,我会像在现实生活中那样去做。玩家从袋子中取出一件物品并激活它。所以这个包需要一个take方法,而这个物品需要一个activate方法。EpicPandaForce非常棒,除了你之外,其他人都做了我想做的事情:但是我怎样才能让玩家activa呢
这些东西怎么样?也许我应该把它放在问题里,我会像在现实生活中那样去做。玩家从袋子中取出一件物品并激活它。因此,袋子需要一个take方法,物品需要一个activate方法。EpicPandaForce非常棒,但你完全按照我的意思做了: