java-回合制小游戏

定义一个玩家类,类中包含职业,血量,攻击上限,攻击力下限,护甲等属性


在此类中定义一个attack方法,方法的参数为被攻击的玩家对象. (Player other)
 方法中的算法为:
用当前玩家的攻击力  攻击参数传递过来的另一个玩家对象,实际伤害为攻击力-被攻击玩家对象的护甲值。
(功击力在功击力上限和下限之间产生一个随机值  
可以使用Math.random()函数得到一个0-1之间的随机数,如:
(int)(Math.random()*(攻击力上限-下限))+ 攻击力下限)
        被攻击的玩家血量=当前血量-实际伤害。


测试:
  在main方法创建两个玩家对象
       1. 职业剑圣  ,血量1000,攻击力50-100,护甲为0
       2. 职业战士  ,血量2000,攻击力25-50,护甲为25


    定义一个死循环,循环退出条件为某个玩家血量小于或等于0

        在循环体内部,分别调用剑圣的attack方法和战士的acctak方法玩成回合制攻击,在循环过程中打印当前玩家对象的本次实际伤害,和被攻击玩家的血量值。某个玩家血量为0时退出循环,并打印XXX玩家死亡

public class Player {
	Player(String job,double hp,double au,double ad,double armor){
		this.job=job;this.hp=hp;
		this.au=au;this.ad=ad;
		this.armor=armor;
	}
    String job;//职位
    double hp,au,ad,armor;//生命,攻击上限,攻击下限,护甲
    public void attack(Player other){
    	double hurt=Math.random()*(au-ad)+ad;
    	if(hurt<=other.armor) hurt=0;
    	else{
    		hurt-=other.armor;;
    	}
    	other.hp-=hurt;
    	System.out.println(job+"对"+other.job+"造成了"+hurt+"点伤害");
    	System.out.println(job+"当前血量为:"+hp);
    	System.out.println(other.job+"当前血量为:"+other.hp);
    }
    public boolean live(Player other){
    	if(hp<=0) {
    		System.out.println(job+"已死亡      "+other.job+"获得战斗胜利");
    		return false;
    	}
    	return true;
    }
	public static void main(String[] args) {
		Player p1=new Player("剑圣",500,20,10,5);
		Player p2=new Player("圣骑士",1000,10,5,10);
		while(true)
		{
			p1.attack(p2);
			if(!p2.live(p1)){
				break;
			}
			p2.attack(p1);
			if(!p1.live(p2)){
				break;
			}
		}
	}
}


  • 6
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
JavaSwing 是一个 Java 的 GUI 工具箱,可以用于构建桌面应用程序。回合制游戏通常需要一个 GUI 来显示游戏状态和交互。下面是一个简单的回合制游戏示例,使用 JavaSwing 实现。 首先,需要创建一个 JFrame 来作为游戏窗口。在 JFrame 中添加一个 JPanel 作为游戏画布。在 JPanel 中绘制游戏状态和交互元素。使用 JButton 或其他组件来实现交互。 接着,创建一个游戏循环,在每个回合更新游戏状态并重新绘制 JPanel。可以使用 Java 的 Timer 类来实现循环。在每个回合中,可以让玩家进行操作,如选择一个单位或移动一个单位。然后更新游戏状态并重新绘制 JPanel。 最后,需要处理用户输入。可以使用 JavaSwing 的事件处理机制来处理按钮点击或其他用户操作。在事件处理器中更新游戏状态,并重新绘制 JPanel。 这是一个简单的示例代码,实现了一个回合制游戏: ```java import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.*; public class TurnBasedGame extends JFrame implements ActionListener { private JPanel gamePanel; private JButton actionButton; private JLabel statusLabel; private int currentPlayer; private Random random; public TurnBasedGame() { super("Turn-Based Game"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 300); // Create game panel gamePanel = new JPanel(); gamePanel.setLayout(new BorderLayout()); add(gamePanel); // Create action button actionButton = new JButton("End turn"); actionButton.addActionListener(this); gamePanel.add(actionButton, BorderLayout.SOUTH); // Create status label statusLabel = new JLabel("Player 1's turn"); gamePanel.add(statusLabel, BorderLayout.NORTH); // Initialize game state currentPlayer = 1; random = new Random(); // Show frame setVisible(true); } public void actionPerformed(ActionEvent e) { // End current player's turn currentPlayer = 3 - currentPlayer; // Switch player statusLabel.setText("Player " + currentPlayer + "'s turn"); // Do AI turn if (currentPlayer == 2) { // Do some random action int action = random.nextInt(3); if (action == 0) { statusLabel.setText("AI attacked"); } else if (action == 1) { statusLabel.setText("AI healed"); } else { statusLabel.setText("AI skipped"); } } } public static void main(String[] args) { new TurnBasedGame(); } } ``` 这个示例实现了一个简单的回合制游戏,玩家和 AI 交替进行回合。点击按钮将结束当前玩家的回合,并开始下一个玩家的回合。AI 的回合是随机的,有三种可能的操作:攻击、治疗或跳过。在每个回合中,状态标签将显示当前回合的玩家或 AI 的名称和操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值