小游戏——金庸奇侠传(JAVA,对面向对象的进一步了解)

                                                                                               金庸群侠传

 

游戏运行界面:

   小游戏——金庸奇侠传(JAVA,对面向对象的进一步了解)_第1张图片 小游戏——金庸奇侠传(JAVA,对面向对象的进一步了解)_第2张图片

 

游戏实现源码:

 1 package game.logic;
 2 //人类
 3 public class Person {
 4 public String name;  //名字
 5  
 6 Person(){
 7  
 8 }
 9  
10 Person(String name){
11 this.name = name;
12 }
13 public String getName() {
14 return name;
15 }
16  
17 public void setName(String name) {
18 this.name = name;
19 }
20  
21 }

 

 1 package game.logic;
 2  
 3 //会武功的人类
 4 public class KongFuPerson extends Person {
 5  
 6 protected int blood = 1000; // 默认都是1000点血
 7  
 8 KongFuPerson() {
 9  
10 }
11  
12 public int getBlood() {
13 return blood;
14 }
15  
16 public void setBlood(int blood) {
17 this.blood = blood;
18 }
19  
20 KongFuPerson(String name) {
21 this.name = name;
22 }
23  
24 // 普通招数
25 public void commonSkill(KongFuPerson P) {
26 P.blood -= 50;  //攻击敌人50点血
27 }
28  
29 //杀招式
30 public void bestSkill(KongFuPerson P) {
31 P.blood -= 100;  //攻击敌人100点血  
32 }
33 }

 

 

1 package game.logic;
2  
3 //武功绝学类(接口)
4 public interface Secrets {
5 public void secrets(KongFuPerson P);
6 }

 

 

 1 package game.logic;
 2  
 3 //武功高手类
 4 public class KongFuMaster extends KongFuPerson implements Secrets{
 5  
 6 public void secrets(KongFuPerson P) {
 7 P.blood -= 300;//攻击敌人300点血
 8  
 9 }
10  
11 }

 

 

 1 package game.roles;
 2  
 3 import game.logic.KongFuMaster;
 4 import game.logic.KongFuPerson;
 5  
 6 //令狐冲类
 7 public class Linghuchong extends KongFuMaster{
 8  
 9 public Linghuchong(){
10  
11 }
12  
13 public Linghuchong(String name){
14 this.name = name;
15 }
16 public void secrets(KongFuPerson P) {
17 // TODO Auto-generated method stub
18 super.secrets(P);
19 System.out.println(this.getName()+"使用武功绝学独孤九剑攻击"+P.getName()+"300点血");
20 }
21  
22 public void commonSkill(KongFuPerson P) {
23 // TODO Auto-generated method stub
24 super.commonSkill(P);
25 System.out.println(this.getName()+"使用越女剑法攻击"+P.getName()+"50点血");
26 }
27  
28 public void bestSkill(KongFuPerson P) {
29 // TODO Auto-generated method stub
30 super.bestSkill(P);
31 System.out.println(this.getName()+"使用华山剑法攻击"+P.getName()+"100点血");
32 }
33  
34  
35 }

 

 

 1 package game.roles;
 2  
 3 import game.logic.KongFuMaster;
 4 import game.logic.KongFuPerson;
 5  
 6 //欧阳锋类
 7 public class Ouyangfeng extends KongFuMaster{
 8 public Ouyangfeng(){
 9  
10 }
11  
12 public Ouyangfeng(String name){
13 this.name = name;
14 }
15 public void secrets(KongFuPerson P) {
16 // TODO Auto-generated method stub
17 super.secrets(P);
18 System.out.println(this.getName()+"使用武功绝学蛤蟆功攻击"+P.getName()+"300点血");
19 }
20  
21 public void commonSkill(KongFuPerson P) {
22 // TODO Auto-generated method stub
23 super.commonSkill(P);
24 System.out.println(this.getName()+"使用毒蛇攻击"+P.getName()+"50点血");
25 }
26  
27 public void bestSkill(KongFuPerson P) {
28 // TODO Auto-generated method stub
29 super.bestSkill(P);
30 System.out.println(this.getName()+"使用毒蛇拐杖攻击"+P.getName()+"100点血");
31 }
32 }

 

 

  1 package game.app;
  2  
  3 import game.logic.KongFuMaster;
  4 import game.roles.Linghuchong;
  5 import game.roles.Ouyangfeng;
  6  
  7 import java.util.Scanner;
  8  
  9 public class Game {
 10  
 11 public static Scanner in = new Scanner(System.in);
 12 public static int choice;//用户的选择
 13 public static KongFuMaster player;//玩家
 14 public static KongFuMaster computer;//电脑玩家
 15 public static boolean isFirst = true;//默认玩家打电脑
 16  
 17 //游戏角色初始化方法
 18 public static void init(){
 19 switch(choice){
 20 case 1:
 21 player = new Linghuchong("令狐冲");
 22 computer = new Ouyangfeng("欧阳锋");
 23 break;
 24 case 2:
 25 player = new Ouyangfeng("欧阳锋");
 26 computer = new Linghuchong("令狐冲");
 27 break;
 28 }
 29 System.out.println("您选择了:"+ player.getName());
 30 }
 31  
 32 //显示游戏菜单
 33 public static void Menu(){
 34 System.out.println("********欢迎进入金庸群侠传***********");
 35 System.out.println("1.令狐冲       2.欧阳锋");
 36 System.out.println("请选择:");
 37 choice = in.nextInt();
 38 }
 39  
 40 //游戏PK方法
 41 public static void beginCombat(KongFuMaster player , KongFuMaster computer){
 42 //生成一个1-3之间的随机数
 43 int temp = (int)(Math.random()*10)%3;   //0-2
 44 temp++;
 45 switch(temp){
 46 case 1:
 47 if(Game.isFirst){
 48 player.commonSkill(computer);
 49 }else{
 50 computer.commonSkill(player);
 51 }
 52 break;
 53 case 2:
 54 if(Game.isFirst){
 55 player.bestSkill(computer);
 56 }else{
 57 computer.bestSkill(player);
 58 }
 59 break;
 60 case 3:
 61 if(Game.isFirst){
 62 player.secrets(computer);
 63 }else{
 64 computer.secrets(player);
 65 }
 66 break;
 67 }
 68 Game.isFirst = !Game.isFirst;
 69 }
 70  
 71 //判断游戏是否结束
 72 public static boolean isComplete(){
 73 if(Game.player.getBlood()<=0||Game.computer.getBlood()<=0){
 74 return true;
 75 }else{
 76 return false;
 77 }
 78 }
 79  
 80 //显示游戏结果
 81 public static void showResult(){
 82 if(Game.player.getBlood()<=0){
 83 System.out.println("君子报仇十年不晚,咱们后会有期。。。。");
 84 }else{
 85 System.out.println("别羡慕哥,哥只是个传说。。。。");
 86 }
 87 }
 88  
 89 public static void main(String[] args){
 90 Game.Menu();//显示菜单
 91 Game.init();//初始化游戏
 92  
 93 do{
 94 //开始PK
 95 Game.beginCombat(player, computer);
 96 }while(!Game.isComplete());//判断是否结束
 97  
 98 Game.showResult();//显示游戏结果
 99 }
100 }

 

你可能感兴趣的:(小游戏——金庸奇侠传(JAVA,对面向对象的进一步了解))