金庸群侠传

游戏运行界面:

 

游戏实现源码:

 package game.logic;
//人类
public class Person {
public String name; //名字 Person(){ } Person(String name){
this.name = name;
}
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }
 package game.logic;

 //会武功的人类
public class KongFuPerson extends Person { protected int blood = 1000; // 默认都是1000点血 KongFuPerson() { } public int getBlood() {
return blood;
} public void setBlood(int blood) {
this.blood = blood;
} KongFuPerson(String name) {
this.name = name;
} // 普通招数
public void commonSkill(KongFuPerson P) {
P.blood -= 50; //攻击敌人50点血
} //杀招式
public void bestSkill(KongFuPerson P) {
P.blood -= 100; //攻击敌人100点血
}
}
 package game.logic;

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

 //武功高手类
public class KongFuMaster extends KongFuPerson implements Secrets{ public void secrets(KongFuPerson P) {
P.blood -= 300;//攻击敌人300点血 } }
 package game.roles;

 import game.logic.KongFuMaster;
import game.logic.KongFuPerson; //令狐冲类
public class Linghuchong extends KongFuMaster{ public Linghuchong(){ } public Linghuchong(String name){
this.name = name;
}
public void secrets(KongFuPerson P) {
// TODO Auto-generated method stub
super.secrets(P);
System.out.println(this.getName()+"使用武功绝学独孤九剑攻击"+P.getName()+"300点血");
} public void commonSkill(KongFuPerson P) {
// TODO Auto-generated method stub
super.commonSkill(P);
System.out.println(this.getName()+"使用越女剑法攻击"+P.getName()+"50点血");
} public void bestSkill(KongFuPerson P) {
// TODO Auto-generated method stub
super.bestSkill(P);
System.out.println(this.getName()+"使用华山剑法攻击"+P.getName()+"100点血");
} }
 package game.roles;

 import game.logic.KongFuMaster;
import game.logic.KongFuPerson; //欧阳锋类
public class Ouyangfeng extends KongFuMaster{
public Ouyangfeng(){ } public Ouyangfeng(String name){
this.name = name;
}
public void secrets(KongFuPerson P) {
// TODO Auto-generated method stub
super.secrets(P);
System.out.println(this.getName()+"使用武功绝学蛤蟆功攻击"+P.getName()+"300点血");
} public void commonSkill(KongFuPerson P) {
// TODO Auto-generated method stub
super.commonSkill(P);
System.out.println(this.getName()+"使用毒蛇攻击"+P.getName()+"50点血");
} public void bestSkill(KongFuPerson P) {
// TODO Auto-generated method stub
super.bestSkill(P);
System.out.println(this.getName()+"使用毒蛇拐杖攻击"+P.getName()+"100点血");
}
}
 package game.app;

 import game.logic.KongFuMaster;
import game.roles.Linghuchong;
import game.roles.Ouyangfeng; import java.util.Scanner; public class Game { public static Scanner in = new Scanner(System.in);
public static int choice;//用户的选择
public static KongFuMaster player;//玩家
public static KongFuMaster computer;//电脑玩家
public static boolean isFirst = true;//默认玩家打电脑 //游戏角色初始化方法
public static void init(){
switch(choice){
case 1:
player = new Linghuchong("令狐冲");
computer = new Ouyangfeng("欧阳锋");
break;
case 2:
player = new Ouyangfeng("欧阳锋");
computer = new Linghuchong("令狐冲");
break;
}
System.out.println("您选择了:"+ player.getName());
} //显示游戏菜单
public static void Menu(){
System.out.println("********欢迎进入金庸群侠传***********");
System.out.println("1.令狐冲 2.欧阳锋");
System.out.println("请选择:");
choice = in.nextInt();
} //游戏PK方法
public static void beginCombat(KongFuMaster player , KongFuMaster computer){
//生成一个1-3之间的随机数
int temp = (int)(Math.random()*10)%3; //0-2
temp++;
switch(temp){
case 1:
if(Game.isFirst){
player.commonSkill(computer);
}else{
computer.commonSkill(player);
}
break;
case 2:
if(Game.isFirst){
player.bestSkill(computer);
}else{
computer.bestSkill(player);
}
break;
case 3:
if(Game.isFirst){
player.secrets(computer);
}else{
computer.secrets(player);
}
break;
}
Game.isFirst = !Game.isFirst;
} //判断游戏是否结束
public static boolean isComplete(){
if(Game.player.getBlood()<=0||Game.computer.getBlood()<=0){
return true;
}else{
return false;
}
} //显示游戏结果
public static void showResult(){
if(Game.player.getBlood()<=0){
System.out.println("君子报仇十年不晚,咱们后会有期。。。。");
}else{
System.out.println("别羡慕哥,哥只是个传说。。。。");
}
} public static void main(String[] args){
Game.Menu();//显示菜单
Game.init();//初始化游戏 do{
//开始PK
Game.beginCombat(player, computer);
}while(!Game.isComplete());//判断是否结束 Game.showResult();//显示游戏结果
}
}