我是一年级的编程学生。我已经掌握了 C++ 中程序编程的基础知识。我现在是 Java 的新手。
最近对游戏编程产生了浓厚的兴趣。
我的情况:
我的情况:
我有一个英雄类和一个对手类。每个都有自己的成员和方法。 我怎样才能让英雄与对手互动,我是通过使用界面来做到这一点吗?例如具有未定义攻击方法的接口(interface) 并且两个类都实现了该接口(interface)?
如果是的话
两个类的攻击方法代码应该是什么样子
像这样的东西
例子:
// 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)
}
请帮助我理解为什么我们使用接口(interface)和我的问题的答案
任何帮助或建议将不胜感激:)。
最佳答案
我会说你不应该使用接口(interface)。更好的方法是使用父类(super class)。使用父类(super class),您可以避免重新定义许多可能会被对手和英雄共享的方法。这是一个示例实现:
父类(super class):
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;
}
}
}
}
现在这看起来有点复杂,但它说明了一个非常重要的概念:关注点分离。关注点分离涉及放置代码和创建有意义的类。玩家不应该知道它的对手是谁,玩家甚至可能不知道敌人的存在或者它站在什么样的地形上。但是玩家应该知道如何管理自己的生命值、名字、如何受到伤害等。相比之下,Game 对象需要了解所有玩家和敌人,这样它才能告诉他们战斗并在屏幕上四处移动。这是关注点分离的非正式定义,有关更准确的信息,请阅读 wikipedia page .在这个例子中,我把英雄和对手分开了,这样以后你就可以添加更多的敌人而不必每次都修改你的英雄代码。该系统还允许您在不影响玩家或对手的情况下扩展游戏的 UI。如果您想在游戏中添加 GUI,可以在 ExampleGame 中添加一个 initialize() 方法来设置 GUI。然后在游戏循环中,您可以调用方法将图像和图形绘制到 GUI 上。通过关注点分离,您可以使系统更加模块化和易于使用。
你的第二个问题是:我们为什么要使用接口(interface)?接口(interface)是一种确保其他类具有您需要它们具有的行为的方法,而无需确切指定它们应该如何执行。使用接口(interface)的一个经典示例是 Comparable 接口(interface)。 Comparable 接口(interface)有一个必须实现的方法:compareTo()。此方法的目的是允许对不能使用标准 boolean 数学运算(<、>、== 等)的值对象(想想 String 或 File)进行排名。您可以将其视为签订契约(Contract)。您(实现该接口(interface)的类)同意拥有一组特定的功能,但是您可以让该功能由您决定。有关更多信息,请阅读 java tutorial
我应该为这个答案添加一个警告:继承不是最佳选项。如果您想知道如何正确执行此操作,您应该查找 MVC (Model View Controller)和 Component Based Design .即使这些可能不是您正在做的事情的最佳选择,但它们是很好的起点。
关于java - 在基本的java游戏编程、类和接口(interface)构造方面需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6274271/