首页 > 代码库 > 小游戏
小游戏
//程序入口
package com.practice.game;
public class Test {
public static void main(String[] args) {
Game g = new Game();
g.startGame();
}
}
//游戏属性和方法
package com.practice.game;
import java.util.Scanner;
public class Game {
Hunter h;
Monster m;
Scanner input = new Scanner(System.in);
public void initial(){
System.out.print("请输入英雄姓名:");
h = new Hunter(input.next());
m = new Monster();
}
public void startGame(){
initial();
String start = null;
System.out.println("--------英雄创建成功,欢迎进入冒险者的世界--------");
System.out.println("你可以:1.查看英雄 2.开始冒险");
int num = input.nextInt();
if(num==1){
heroProperty();
System.out.print("是否开始冒险(y/n):");
start = input.next();
if(start.equals("n")){
System.out.println("游戏结束!");
return;
}
}else if(num==2){
start = "y";
}
do{
System.out.print("请选择你要攻打的怪物等级(1-5)级:");
m.grade = input.nextInt();
m.typeLevel();
monsterProperty();
System.out.print("开始战斗》y/n:");
String battle = input.next();
int a = 0;
while(battle.equals("y")){
a ++;
System.out.println("--------第"+a+"回合》"+h.name+" VS "+m.type+"《开始--------");
h.action(m);
if(m.HP<=0||h.HP<=0){
if(h.HP<=0){
System.out.println("你挂了,勇士请重新来过!");
return;
}
battle = "n";
}
}
System.out.println("==========冒险者游戏==========");
System.out.print("1.查看英雄/2.选择其它怪物吗?/(y/n):");
start = input.next();
if(start.equals("1")){
heroProperty();
System.out.print("选择其它怪物吗?/(y/n):");
start = input.next();
}
}while(start.equals("y"));
System.out.println("游戏结束!");
}
public void heroProperty(){
System.out.println("勇士:"+h.name+"\t等级:"+h.grade);
System.out.println("血量"+h.HP+"/"+h.maxHP+"\t武器:"+h.weapon);
System.out.println("攻击力:"+h.attack+"\t防御力:"+h.defense+"\t闪避值:"+h.dodge);
System.out.println("经验值:"+h.exp+"\t升级所需经验:"+h.expAdd(m));
}
public void monsterProperty(){
System.out.println("名称:"+m.type+"\t级别:"+m.grade+"级妖兽");
System.out.println("血量:"+m.HP+"\t攻击力:"+m.attack+"\t防御力:"+m.defense);
}
}
//人物属性和方法
package com.practice.game;
public class Hunter {
String name;
String weapon = "木剑";
int attack = 10;
int attacks;
int defense = 5;
int maxHP = 100;
int HP = maxHP;
int exp = 0;
int grade = 1;
int dodge = 8;
public Hunter(String name){
this.name = name;
}
public void action(Monster m){
if(HP<=0){
System.out.println("勇士请重头来过!");
return;
}
System.out.println(name+":嘿,妖怪,吃我一招");
attacks = (int)(Math.random()*(10*attack));
m.injured(this);
m.surplusHP();
surplusHP();
if(m.HP<=0){
System.out.println("恭喜你成功杀死了怪物,获得经验"+(m.attack*4));
expAdd(m);
}
}
public int expAdd(Monster m){
this.exp += m.attack*4;
int levelExp = 0;
for(int i=1; i<=grade; i++){
levelExp += i*50;
}
if(exp>=levelExp){
upLevel();
exp -= levelExp;
}
return levelExp;
}
public void upLevel(){
System.out.println("恭喜你升级了!");
this.attack += this.grade*5;
this.defense += this.grade*4;
this.grade += 1;
this.maxHP += this.grade*20;
this.HP = this.maxHP;
this.dodge +=this.grade*3;
}
public void injured(Monster m){
int miss = (int)(Math.random()*(2*this.dodge));
if(miss<this.dodge){
System.out.println(name+"使用凌波微步闪开了,没有受到伤害");
}else{
int lostHP = m.attacks-defense;
if(lostHP<=0){
lostHP = 0;
}
HP -= lostHP;
System.out.println(name+"损失了"+lostHP+"点生命值");
}
}
public void surplusHP(){
System.out.println(name+"剩余生命值为"+HP);
}
}
//怪物属性和方法
package com.practice.game;
public class Monster {
String type;
int HP;
int grade;
int attack;
int attacks;
int defense;
int dodge;
public void injured(Hunter h){
int lostHP = h.attacks-defense;
if(lostHP<=0){
lostHP = 0;
}
HP -= lostHP;
System.out.println(type+"受到了"+lostHP+"点伤害");
if(HP<=0){
return;
}
System.out.println(type+":哇呜,气死我了,咬死你");
attacks = (int)(Math.random()*(2*attack));
h.injured(this);
}
public void surplusHP(){
System.out.println(type+"剩余生命值为"+HP);
}
public void typeLevel(){
switch(grade){
case 1:
type = "哥布林";
HP = 200;
attack = 10;
defense = 10;
break;
case 2:
type = "狼王";
HP = 500;
attack = 50;
defense = 35;
break;
case 3:
type = "丛林巨蟒";
HP = 1000;
attack = 80;
defense = 70;
break;
case 4:
type = "洪荒异兽";
HP = 5000;
attack = 150;
defense = 150;
break;
case 5:
type = "上古龙神";
HP = 10000;
attack = 300;
defense = 250;
break;
}
}
}
小游戏