我是一名编程学生。我已经在c ++中编写了基础编程的基础知识。我现在对Java很新。
最近我对游戏编程产生了浓厚的兴趣。
我的情况:
我的情况:
我有一个英雄级和一个对手级。每个人都有自己的成员和方法。 如何让英雄与竞争对手互动,我是通过使用界面做到这一点的吗?例如,具有未定义攻击方法的接口 并让这两个类实现该接口?
如果是的话
两个类的攻击方法中代码应该是什么样子
喜欢这个
示例:
// heros version of implemented method
public int attack()
{
// idealy when hero attacks, the health value will be reduced by 15 of what it is.
rival1.getHealth(- 15)
}
// rival version of implemented method
public int attack()
{
// idealy when rival attacks, the health value will be reduced by 15 of what it is.
hero1.getHealth(- 15)
}
请帮助我理解为什么我们使用interefaces和anwser来解决我的问题
任何帮助或建议将不胜感激:)。
答案 0 :(得分:5)
我会说你不应该使用界面。更好的方法是使用超类。使用超类,您可以避免重新定义许多方法,这些方法可能会被竞争对手和英雄共享。以下是一个示例实现:
超类:
public abstract class ExampleFighter {
private String name;
private int health;
private boolean isDead = false;
public ExampleFighter(String name, int health) {
this.name = name;
this.health = health;
}
public void attack(ExampleFighter ef) {
int damage = 0;
//calculate damage dealt
damage = 10;
ef.takeDamage(damage);
}
public void takeDamage(int damage) {
//manipulate the amount of damage taken
if(health - damage <= 0) {
health = 0;
isDead = true;
} else {
health -= damage;
}
}
public boolean isDead() {
return isDead;
}
}
子类:
public class ExampleHero extends ExampleFighter {
int reputation; //the general opinion of the hero
public ExampleHero() {
super("Hero Oreh of Herosville", 100);
reputation = 0;
}
public void improveReputation() {
reputation++;
}
}
public class ExampleRival extends ExampleFighter {
public ExampleRival() {
super("Your greatest rival", 101);
}
}
这个系统的副作用是它需要第四节才能真正玩游戏:
public class ExampleGame {
private ExampleHero hero;
private ExampleRival rival;
public static void main(String... args) {
ExampleGame game = new ExampleGame();
game.start();
}
public ExampleGame() {
hero = new ExampleHero();
rival = new ExampleRival();
//what ever other game setup you need to do.
//alternately you could have a load() method
//that takes care of most of this.
}
private void start() {
//make your run loop or query the user for input
//or whatever you need to do. I will create an
//example run loop
boolean running = true;
while(running) {
//this whole block should be moved
//to another method called gameUpdate()
//or something similar but since this
//is a quick example I'll just leave it
//here
hero.attack(rival);
rival.attack(hero);
if(rival.isDead()) {
hero.improveReputation();
System.out.println(“Your rival is dead!");
running = false;
} else if(hero.isDead()) {
System.out.println("you died :(");
running = false;
}
}
}
}
现在这可能看起来有点复杂,但它说明了一个非常重要的概念:关注点的分离。关注点分离涉及放置代码和制作有意义的类。玩家不应该知道对手是谁,玩家可能甚至不知道敌人存在或者站在什么样的地形上。但是玩家应该知道如何管理它的健康,它的名字,如何受到伤害等等。相比之下,游戏对象需要了解所有玩家和敌人,以便它可以告诉他们在屏幕上进行战斗和移动。这是关注点分离的非正式定义,有关更准确的信息,请阅读wikipedia page。在这个例子中,我将英雄和对手分开,以便以后你可以添加更多的敌人而不必每次都修改你的英雄代码。此系统还允许您在不影响玩家或竞争对手的情况下扩展游戏的UI。如果您想在游戏中添加GUI,可以在ExampleGame中添加一个设置GUI的initialize()方法。然后在游戏循环中,您可以调用方法将图像和图形绘制到GUI上。通过分离关注点,您可以使系统更加模块化和易于使用。
你的第二个问题是:为什么我们使用接口?接口是确保其他类具有您需要的行为的一种方式,而无需确切地指定它们应该如何执行。使用接口的典型示例是Comparable接口。 Comparable接口有一个必须实现的方法:compareTo()。此方法的目的是允许对不能使用标准布尔数学运算(&lt;,&gt;,==等)的值对象(思考字符串或文件)进行排名。您可以将其视为签署合同。您(实现接口的类)同意具有一组功能,但是您可以使用该功能。有关更多信息,请阅读java tutorial
我应该在这个答案中添加一个警告:继承不是 最佳 选项。如果您想知道如何正确操作,请查看MVC (Model View Controller)和Component Based Design。即使这些可能不是你正在做的最好的选择,但它们是很好的起点。
答案 1 :(得分:1)
我认为你想要将它分解为Fighter类和FightController类。然后战斗机将被分配给FightController中的英雄或竞争对手。
所以,它基本上就像下面这样(不介意草率的基本代码,我在约2年内没有写过Java,我只是把它打成一团,我不确定它会编译):
public class Fighter {
private int health;
private boolean isTheHero;
public Fighter(int startHealth, boolean hero) {
health = startHealth;
isTheHero = hero;
}
public void adjustHealth(int change) {
if (change > health) {
return 0;
}
health -= change;
return health;
}
public boolean isHero() {
return is_hero;
}
public boolean wasBeaten() {
return health <= 0;
}
}
public class FightController {
private Fighter hero;
private Fighter rival;
private boolean isHerosTurn;
public FightController() {
hero = new Fighter(startHealth, true);
rival = new Fighter(startHealth, false);
isHerosTurn = true;
}
public void takeATurn() {
int hitValue = 15; //Do something to figure out the hit
remainder = 0;
if (hero.wasBeaten() or rival.wasBeaten()) {
sys.out.println("This match is already over");
} else {
if (isHerosTurn) {
remainder = rival.adjustHealth(hitValue);
if (remainder == 0) {
sys.out.println("The HERO wins!!!");
}
} else {
remainder = hero.adjustHealth(hitValue);
if (remainder == 0) {
sys.out.println("The Rival wins. Boo!");
}
}
isHerosTurn = !isHerosTurn;
}
}
}
然后你可以做类似的事情:
controller = new FightController();
controller.takeATurn();
controller.takeATurn();
controller.takeATurn();
controller.takeATurn();
直到比赛结束。
答案 2 :(得分:0)
答案 3 :(得分:0)
请帮助我理解我们为什么 使用接口
实现接口允许类对其承诺提供的行为变得更加正式,它们也会在类和外部世界之间形成契约,并且此协议在构建时由编译器强制执行。如果您的类声称实现了一个接口,那么该接口定义的所有方法必须在其成功编译之前出现在其源代码中。
1。 Why we are implementing interfaces ?
2. Why do we need interfaces in Java?
和,问题的答案
由于您的代码在Rival和Hero这两个类中都有通用的attack()方法,因此最好的方法是在接口中声明该方法。
请记住,以下只是代码段,而不是完整的代码。您可以自己完成。
public interface Fight {
public int attack();
}
public class Hero implements Fight {
public int attack() {
rival1.getHealth(-15);
}
}
public class Rival implements Fight {
public int attack() {
hero1.getHealth(-15);
}
}
答案 4 :(得分:0)
对象通过发送消息进行交互。以这种方式看待它:当玩家攻击时,他将暴力消息发送给其他玩家 reveiveHit 方法。
或者,用一个共同的设计模式来实现它:玩家可以发出攻击,其他玩家观察它的行为,并确定它们是否被击中。
你不应该做的事情:让一个玩家依赖另一个玩家(比如你的例子)。如果你想对比赛进行建模,那么就加一些经理/裁判来进行攻击和效果的记录。
答案 5 :(得分:-2)
这是游戏中角色互相交流的界面。
public interface Character{ ... }
如果角色能够攻击,这是界面。
public interface Fightable{
public void attack(Character character);
}
这些是游戏中实现两个接口的两个类。
public class Hero implements Character, Fightable
{
// heros version of implemented method
public int attack(Character character)
{
// idealy when hero attacks, the health value will be reduced by 15 of what it is.
character.setHealth(-15);
}
}
public class Villon implements Character, Fightable
{
// rival version of implemented method
public int attack(Character character)
{
// idealy when rival attacks, the health value will be reduced by 15 of what it is.
character.setHealth(- 15);
}
}