JAVA回合制pk游戏

创建玩家类


/*
 * 玩家类
 * 属性:名字,类型,血量,防御,攻击
 * 行为:自我介绍,pk
 */
public class Player {
    //封装:将属性设为private,提供公共的get和set方法间接访问,提升安全性
    private String name;//名字
    private String type;//职业
    private int life;//生命值
    private int defense;//防御
    private int attack;//攻击

    //自我介绍
    public void say(){
        System.out.println("我叫"+name+",是一个"+type+",生命值"+life+",防御"+defense+",攻击"+attack);
    }

    /*
    * pk的方式,和另一个玩家pk;
    * @param p
    * */
    public void pk(Player p){
        //定义一个标记,0表示我方进攻,1表示敌方进攻
        int flag = 0;//默认我方先进攻
        //回合制pk,直到一方死亡
        while (true){
            //每次都显示剩余的生命值
            this.say();
            p.say();
            if (flag == 0) {
                //我方攻击:敌方生命值-(我方攻击力-敌方防御力)
                int harm = this.attack-p.defense;//得到伤害

                int sj = (int) Math.round(Math.rando
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Java 回合游戏的代码示例: ```java import java.util.Scanner; public class TurnBasedGame { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int playerHealth = 100; int enemyHealth = 100; boolean playerTurn = true; boolean gameEnd = false; System.out.println("Welcome to the turn-based game!"); System.out.println("You will fight against an enemy with 100 health points."); System.out.println("Your health points are also 100."); System.out.println("You can either attack or defend in your turn."); System.out.println("The enemy will always attack in its turn."); System.out.println("Let's begin!\n"); while (!gameEnd) { if (playerTurn) { System.out.println("Your turn:"); System.out.println("Enter 1 to attack, enter 2 to defend."); int choice = scanner.nextInt(); if (choice == 1) { int damage = (int) (Math.random() * 10) + 10; System.out.println("You attacked the enemy and dealt " + damage + " damage."); enemyHealth -= damage; } else if (choice == 2) { System.out.println("You defended and received only half of the enemy's attack."); playerHealth -= 5; } else { System.out.println("Invalid input, turn skipped."); } playerTurn = false; } else { System.out.println("\nEnemy's turn:"); int damage = (int) (Math.random() * 10) + 10; System.out.println("The enemy attacked you and dealt " + damage + " damage."); playerHealth -= damage; playerTurn = true; } System.out.println("\nCurrent status:"); System.out.println("Your health points: " + playerHealth); System.out.println("Enemy's health points: " + enemyHealth + "\n"); if (playerHealth <= 0) { System.out.println("You lost the game."); gameEnd = true; } else if (enemyHealth <= 0) { System.out.println("You won the game!"); gameEnd = true; } } } } ``` 这个游戏有两个角色,玩家和敌人,每个角色都有100点生命值。玩家可以选择攻击或防御,敌人则总是攻击。每个回合,一个角色执行一个动作,然后另一个角色执行一个动作。如果一个角色的生命值降至0或以下,游戏结束。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值