我想知道如何在游戏制作中使用一个包。该包应该容纳2件物品,可以在“地图”周围的不同房间找到,当找到这两件物品时,可以通过找到老板房来完成游戏。这个包应该是它自己的java类。如何让玩家激活这些物品?
朗姆酒和朗姆酒6是任何想知道的物品房间。(我不希望任何人为我完成这个游戏,我只是想要一些帮助)
//对不起瑞典文本。
//这显然不是所有代码,但其余部分不是必需的。
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;
}
}
答案 0 :(得分:2)
我会创建一个包含两个项目的类包
public class Bag {
Item item1;
Item item2;
}
public class Item {
<any item properties here>
}
答案 1 :(得分:2)
在这种情况下,我相信您需要Item
的课程,以及特定效果的项目类型。
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 class Bag {
private List<Item> items;
public Bag() {
this(new ArrayList<Item>());
}
public Bag(List<Item> items) {
this.items = items;
}
public List<Item> getItems() {
return items;
}
}
当然,您可以使用更智能的方式存储Item
以免混淆它们,因为您仍然需要弄清楚某些东西是药水还是炸弹。
但无论如何,使用它很简单,你需要从列表中取出一个项目,在一个实体上使用它,然后如果它是一个消耗品就从列表中删除它。
答案 2 :(得分:1)
有人找到老板房才能找到物品吗?如果不是:你怎么防止它?
使用我的解决方案时您需要做什么:
请注意,此解决方案仅在只需要两件物品时才有效。如果以后需要10个项目我会建议你使用hashTbale,列表或任何相同的&#34;堆叠&#34;类
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;
}
}
答案 3 :(得分:0)
您可以创建一些Player
类来存储所有玩家数据,并在其中加Bag
来保存项目,例如:
HashMap<String, Item> itemsByName;
并且如果未超过尺寸2,则始终检查何时向其添加新项目。