肉鸽游戏(Python版)

简单点吧

 

import java.util.Random;
import java.util.Scanner;

public class RogueGame {
    private static final int BOARD_SIZE = 10;
    private static final int MAX_ENEMIES = 5;
    private static final int MAX_HEALTH = 10;
    private static final int MAX_DAMAGE = 3;

    private char[][] board;
    private int playerX;
    private int playerY;
    private int playerHealth;
    private int enemiesRemaining;
    private Random random;
    private Scanner scanner;

    public RogueGame() {
        board = new char[BOARD_SIZE][BOARD_SIZE];
        random = new Random();
        scanner = new Scanner(System.in);
    }

    public void play() {
        initializeBoard();
        initializePlayer();
        initializeEnemies();

        while (playerHealth > 0 && enemiesRemaining > 0) {
            printBoard();
            System.out.print("Enter your move (w/a/s/d): ");
            String move = scanner.nextLine();
            processMove(move);
            moveEnemies();
        }

        if (playerHealth <= 0) {
            System.out.println("Game Over! You were defeated.");
        } else {
            System.out.println("Congratulations! You defeated all enemies.");
        }
    }

    private void initializeBoard() {
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                board[i][j] = '.';
            }
        }
    }

    private void initializePlayer() {
        playerX = random.nextInt(BOARD_SIZE);
        playerY = random.nextInt(BOARD_SIZE);
        playerHealth = MAX_HEALTH;
        board[playerY][playerX] = '@';
    }

    private void initializeEnemies() {
        enemiesRemaining = MAX_ENEMIES;
        for (int i = 0; i < MAX_ENEMIES; i++) {
            int enemyX = random.nextInt(BOARD_SIZE);
            int enemyY = random.nextInt(BOARD_SIZE);
            if (enemyX != playerX && enemyY != playerY) {
                board[enemyY][enemyX] = 'E';
            }
        }
    }

    private void printBoard() {
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
    }

    private void processMove(String move) {
        int newPlayerX = playerX;
        int newPlayerY = playerY;

        if (move.equalsIgnoreCase("w")) {
            newPlayerY--;
        } else if (move.equalsIgnoreCase("s")) {
            newPlayerY++;
        } else if (move.equalsIgnoreCase("a")) {
            newPlayerX--;
        } else if (move.equalsIgnoreCase("d")) {
            newPlayerX++;
        }

        if (isValidMove(newPlayerX, newPlayerY)) {
            board[playerY][playerX] = '.';
            playerX = newPlayerX;
            playerY = newPlayerY;
            board[playerY][playerX] = '@';
            checkEncounter();
        } else {
            System.out.println("Invalid move! Try again.");
        }
    }

    private boolean isValidMove(int x, int y) {
        return x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE;
    }

    private void checkEncounter() {
        if (board[playerY][playerX] == 'E') {
            int enemyDamage = random.nextInt(MAX_DAMAGE) + 1;
            playerHealth -= enemyDamage;
            System.out.println("You encountered an enemy and took " + enemyDamage + " damage!");
            enemiesRemaining--;
        }
    }

    private void moveEnemies() {
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                if (board[i][j] == 'E') {
                    int newX = j;
                    int newY = i;
                    while ((newX == j && newY == i) || !isValidMove(newX, newY)) {
                        int direction = random.nextInt(4);
                        if (direction == 0) {
                            newX = j - 1;
                        } else if (direction == 1) {
                            newX = j + 1;
                        } else if (direction == 2) {
                            newY = i - 1;
                        } else if (direction == 3) {
                            newY = i + 1;
                        }
                    }
                    char target = board[newY][newX];
                    if (target == '@') {
                        int playerDamage = random.nextInt(MAX_DAMAGE) + 1;
                        playerHealth -= playerDamage;
                        System.out.println("An enemy attacked you and dealt " + playerDamage + " damage!");
                    } else {
                        board[i][j] = '.';
                        board[newY][newX] = 'E';
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        RogueGame game = new RogueGame();
        game.play();
    }
}

 

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值