java二人对打游戏

生成两个class文件,在同一个包中调用

第一个:

package com.neuedu.demo2;

import java.util.Random;

/**
 * 玩家类
 * @author Administrator
 *
 */
public class Player {
	
	public String pname; //名字
	public double health; //当前生命值
	public double yaoshui;//药水增加的血
	public int maxHealth; //生命值上限
	
	public int minAttack; //攻击力下限
	public int maxAttack; //攻击力上限
	
	public double baoji; //暴击几率   默认2倍伤害
	
	//判断当前对象是否存活
	public boolean isAlive(){
		return health > 0;
	}
	
	/**
	 * 攻击另一个Player对象
	 * @param other
	 */
	public void attack(Player other){
		//判断双方是否都存活
		if(!(isAlive() && other.isAlive())){
			//其中有一个人挂掉
			return;
		}
		//计算本次的攻击力
		int a = getRandomAttack();
		//判断暴击
		double x = Math.random();
		if(x < baoji){
			a *= 2;
			System.out.print("[暴击!!]");
		}
		
		//减去对方的生命值
		other.health -= a;
		//调用动画显示效果
		System.out.println(pname+"对"+other.pname+"造成了"+a+"点伤害,"+other.pname+"剩余生命值:"+other.health);
		//判断被打者是否挂了
		if(other.health <= 0){
			System.out.println(other.pname+"挂了");
		}
		//如果没死的话增加血量

	}
	
	//使用药水
	public void drug()
	{
		   yaoshui = Math.round(maxHealth *0.15);
			health =health+yaoshui;
			System.out.print("["+pname+"使用血药,增加了" +yaoshui+"血量!]");
	}
	
	/**
	 * 获得[minAttack,maxAttack]区间的随机整数
	 * @return
	 */
	private int getRandomAttack(){
		Random r = new Random();
		int x = r.nextInt(maxAttack - minAttack + 1) + minAttack;
		return x;
	}
	

}

第二个:

package com.neuedu.demo2;

public class Test {

	/**
	 * 练习:
	 * 追加一个药水类(Drug)
	 *      名字: 字符串
	 * 		属性:  回复量    整数
	 * 
	 * 为Player追加一个喝药水的方法  useDrug(Drug d)
	 *      为使用者恢复当前生命值,回复量为对象d声明数量
	 * 
	 * 在main方法的循环中,进行判断,当赵四的生命值低于30的时候,使用药水进行回复
	 * 
	 * 注意:回复量不能超过生命值上限
	 * 
	 * 
	 * 
	 * @param args
	 * @throws InterruptedException
	 */
	public static void main(String[] args) throws InterruptedException {
		Player p1 = new Player();
		p1.pname = "赵四";
		p1.health = 500;
		p1.maxHealth = 500;
		p1.minAttack = 25;
		p1.maxAttack = 60;
		p1.baoji = 0.1;
		
		
		Player p2 = new Player();
		p2.pname = "刘能";
		p2.health = 500;
		p2.maxHealth = 500;
		p2.minAttack = 25;
		p2.maxAttack = 60;
		p2.baoji = 0.1;
		
		while(p1.isAlive() && p2.isAlive()){
			p1.attack(p2);
			if(p2.health>0)
			{
				if(p2.health<100)
				{
					p2.drug();
				}
			}
			else{
				break;
			}
			p2.attack(p1);
			if(p1.health>0)
  • 1
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以的,我可以为您提供一个简单的Java文字版格斗游戏的示例代码。以下是一个基本的游戏框架: ```java import java.util.Scanner; public class FightingGame { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("欢迎来到文字版格斗游戏!"); System.out.print("请输入玩家姓名:"); String playerName = scanner.nextLine(); System.out.println("\n游戏开始!"); System.out.println(playerName + " VS 电脑"); // 初始化玩家和电脑的血量 int playerHealth = 100; int computerHealth = 100; // 游戏循环 while (playerHealth > 0 && computerHealth > 0) { System.out.println("\n轮到玩家攻击!"); System.out.print("请输入攻击力(1-20):"); int playerAttack = scanner.nextInt(); // 生成电脑的攻击力 int computerAttack = (int) (Math.random() * 20) + 1; // 更新血量 playerHealth -= computerAttack; computerHealth -= playerAttack; System.out.println("玩家受到了" + computerAttack + "点伤害,剩余血量:" + playerHealth); System.out.println("电脑受到了" + playerAttack + "点伤害,剩余血量:" + computerHealth); } // 判断输赢 if (playerHealth > 0 && computerHealth <= 0) { System.out.println("\n恭喜,玩家获胜!"); } else if (playerHealth <= 0 && computerHealth > 0) { System.out.println("\n很遗憾,电脑获胜!"); } else { System.out.println("\n平局!"); } scanner.close(); System.out.println("\n游戏结束!"); } } ``` 这个简单的游戏允许玩家和电脑进行回合制的攻击,直到其中一方的血量归零。您可以根据需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值