2011-06-08 57 views
1

我是一年級的編程學生。我用C++編寫了先進的編程基礎知識。我現在對Java很新。需要幫助基本的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) 
     } 

請幫助我理解爲什麼我們使用界面和,對我的問題的答案

任何幫助或建議將不勝感激:)。

回答

-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); 
    } 
} 
+0

-1 ..兩個類應以「class」和小寫public爲前綴。 – 2011-06-08 05:02:26

0

請幫助我理解爲什麼我們 使用接口

實現一個接口允許類變得更加正式的關於它的承諾也提供行爲,他們形成了階級之間的合同外部世界,並且這個合同在編譯器的編譯時執行。如果您的類聲明實現了一個接口,那麼在該類成功編譯之前,該接口定義的所有方法都必須出現在其源代碼中。

1。 2 Why we are implementing interfaces ?
2。Why do we need interfaces in Java?

,並在anwser我的問題

因爲你的代碼有普通攻擊(在兩個類即對手,英雄)方法,最好的辦法是宣佈在接口中的方法。

請記住,以下只是一段代碼段,而不是完整的代碼。你可以自己完成它。

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); 
     } 
    } 
0

對象通過發送消息交互。看看這樣:當一名玩家攻擊時,那麼他會將其暴力消息發送給其他玩家reveiveHit方法。

或者,用一個通用的設計模式實現它:玩家可以發出攻擊和其他玩家觀察它的行爲和決定,如果他們已經被擊中。

你不應該這樣做:讓一個玩家依賴另一個玩家(例如你的例子)。如果你想模擬一場比賽,然後添加一些經理/裁判,做記錄的攻擊和影響。

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(); 

進行,直到遊戲結束。

4

我會說你不應該使用一個接口。更好的方法是使用超類。有了超類,你可以避免重新定義可能會被敵手和英雄共享的許多方法。下面是一個示例實現:

超類:

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。在這個例子中,我將英雄和對手分開,以便以後可以添加更多的敵人,而不必每次修改英雄代碼。該系統還可以讓您在不影響玩家或對手的情況下擴展遊戲的用戶界面。如果你想添加一個GUI到遊戲中,你可以在ExampleGame中添加一個initialize()方法來設置GUI。然後在遊戲循環中,您可以調用將圖像和圖形繪製到GUI上的方法。隨着擔心的分離,您可以使系統更加模塊化,易於使用。

你的第二個問題是:爲什麼我們要使用接口?接口是確保其他類具有您需要它們的行爲的一種方式,而無需詳細說明應該如何執行它。一個使用接口的經典例子是Comparable接口。 Comparable接口有一個必須實現的方法:compareTo()。此方法的目的是允許對無法使用標準布爾數學運算(<,>,==等)的值對象(思考字符串或文件)進行排序。您可以將其視爲像簽署合同一樣。您(實現該接口的類)同意擁有一組特定的功能,但是您可以根據自己的實際情況制定該功能。欲瞭解更多信息,請閱讀java tutorial

我要告誡添加到這樣的回答:繼承是不是最佳選擇。如果你想知道如何去做,你應該看看MVC (Model View Controller)Component Based Design。即使這些可能不是你正在做的最好的選擇,但他們是很好的起點。