用Java实现森林冰火人游戏

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;

public class ForestFireGame extends JFrame implements KeyListener {
    private static final long serialVersionUID = 1L;
    private static final int CELL_SIZE = 50;
    private static final int ROWS = 10;
    private static final int COLS = 10;

    private int playerRow;
    private int playerCol;
    private boolean[][] isTree;
    private boolean[][] isFire;
    private boolean[][] isWater;
    private int playerHealth;
    private int gameTime;
    private int difficultyLevel;
    private boolean gameOver;

    public ForestFireGame() {
        setTitle("森林冰火人");
        setSize(CELL_SIZE * COLS, CELL_SIZE * ROWS);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

        addKeyListener(this);

        playerRow = ROWS / 2;
        playerCol = COLS / 2;
        playerHealth = 3;
        gameTime = 60;
        difficultyLevel = 1;
        gameOver = false;

        isTree = new boolean[ROWS][COLS];
        isFire = new boolean[ROWS][COLS];
        isWater = new boolean[ROWS][COLS];

        generateTrees();
        generateWater();

        setVisible(true);

        startGame();
    }

    private void startGame() {
        new Thread(() -> {
            while (!gameOver) {
                try {
                    Thread.sleep(1000);
                    gameTime--;

                    if (gameTime <= 0) {
                        gameOver = true;
                    }

                    spreadFire();
                    moveEnemies();

                    repaint();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void generateTrees() {
        for (int row = 0; row < ROWS; row++) {
            for (int col = 0; col < COLS; col++) {
                if (Math.random() < 0.3) {
                    isTree[row][col] = true;
                }
            }
        }
    }

    private void generateWater() {
        for (int row = 0; row < ROWS; row++) {
            for (int col = 0; col < COLS; col++) {
                if (Math.random() < 0.1) {
                    isWater[row][col] = true;
                }
            }
        }
    }

    private void spreadFire() {
        for (int row = 0; row < ROWS; row++) {
            for (int col = 0; col < COLS; col++) {
                if (isTree[row][col] && Math.random() < 0.1) {
                    isFire[row][col] = true;
                }
            }
        }
    }

    private void moveEnemies() {
        for (int row = 0; row < ROWS; row++) {
            for (int col = 0; col < COLS; col++) {
                if (isTree[row][col] && Math.random() < 0.1) {
                    int direction = (int) (Math.random() * 4);

                    switch (direction) {
                        case 0: // Up
                            if (row > 0 && !isFire[row - 1][col] && !isWater[row - 1][col]) {
                                isTree[row][col] = false;
                                isTree[row - 1][col] = true;
                            }
                            break;
                        case 1: // Down
                            if (row < ROWS - 1 && !isFire[row + 1][col] && !isWater[row + 1][col]) {
                                isTree[row][col] = false;
                                isTree[row + 1][col] = true;
                            }
                            break;
                        case 2: // Left
                            if (col > 0 && !isFire[row][col - 1] && !isWater[row][col - 1]) {
                                isTree[row][col] = false;
                                isTree[row][col - 1] = true;
                            }
                            break;
                        case 3: // Right
                            if (col < COLS - 1 && !isFire[row][col + 1] && !isWater[row][col + 1]) {
                                isTree[row][col] = false;
                                isTree[row][col + 1] = true;
                            }
                            break;
                    }
                }
            }
        }
    }

    private void movePlayer(int newRow, int newCol) {
        if (newRow >= 0 && newRow < ROWS && newCol >= 0 && newCol < COLS && !isFire[newRow][newCol] && !isWater[newRow][newCol]) {
            playerRow = newRow;
            playerCol = newCol;

            if (isTree[playerRow][playerCol]) {
                isTree[playerRow][playerCol] = false;
                spreadFire();
                playerHealth--;
                if (playerHealth <= 0) {
                    gameOver = true;
                }
            }

            if (isWater[playerRow][playerCol]) {
                isWater[playerRow][playerCol] = false;
                playerHealth++;
            }

            repaint();
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        for (int row = 0; row < ROWS; row++) {
            for (int col = 0; col < COLS; col++) {
                int x = col * CELL_SIZE;
                int y = row * CELL_SIZE;

                if (isTree[row][col]) {
                    g.setColor(Color.GREEN);
                    g.fillRect(x, y, CELL_SIZE, CELL_SIZE);
                } else if (isFire[row][col]) {
                    g.setColor(Color.RED);
                    g.fillRect(x, y, CELL_SIZE, CELL_SIZE);
                } else if (isWater[row][col]) {
                    g.setColor(Color.BLUE);
                    g.fillRect(x, y, CELL_SIZE, CELL_SIZE);
                }

                if (row == playerRow && col == playerCol) {
                    g.setColor(Color.BLUE);
                    g.fillOval(x + 5, y + 5, CELL_SIZE - 10, CELL_SIZE - 10);
                }
            }
        }

        g.setColor(Color.BLACK);
        g.drawString("生命值: " + playerHealth, 10, CELL_SIZE * ROWS + 20);
        g.drawString("时间: " + gameTime, 10, CELL_SIZE * ROWS + 40);
        g.drawString("难度级别: " + difficultyLevel, 10, CELL_SIZE * ROWS + 60);

        if (gameOver) {
            g.setColor(Color.RED);
            g.setFont(new Font("Arial", Font.BOLD, 30));
            g.drawString("游戏结束", CELL_SIZE * COLS / 2 - 100, CELL_SIZE * ROWS / 2);
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (gameOver) {
            return;
        }

        int keyCode = e.getKeyCode();

        switch (keyCode) {
            case KeyEvent.VK_UP:
                movePlayer(playerRow - 1, playerCol);
                break;
            case KeyEvent.VK_DOWN:
                movePlayer(playerRow + 1, playerCol);
                break;
            case KeyEvent.VK_LEFT:
                movePlayer(playerRow, playerCol - 1);
                break;
            case KeyEvent.VK_RIGHT:
                movePlayer(playerRow, playerCol + 1);
                break;
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {}

    @Override
    public void keyReleased(KeyEvent e) {}

    public static void main(String[] args) {
        SwingUtilities.invokeLater(ForestFireGame::new);
    }
}

  • 21
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java实现森林冰火人的示例代码: IcePeople类: ```java public class IcePeople { private Play play; private PlayGame playGame; private int score = 0; private boolean isLive = true; public IcePeople(Play play) { this.play = play; this.playGame = play.getPlayGame(); } public void gain() { score += 10; } public void move(boolean up, boolean left, boolean right) { if (isLive) { int x = playGame.getIcePeopleX(); int y = playGame.getIcePeopleY(); if (up) { y -= 1; } if (left) { x -= 1; } if (right) { x += 1; } if (playGame.getMap(x, y) == 3) { score += 100; playGame.setMap(x, y, 0); } if (playGame.getMap(x, y) == 4) { isLive = false; playGame.setMap(x, y, 0); playGame.setIcePeopleLive(false); playGame.setIcePeopleDie(); } playGame.setIcePeopleX(x); playGame.setIcePeopleY(y); } } public void verdict(int i) { if (i == 1) { playGame.setIcePeopleWin(); } else if (i == 2) { playGame.setIcePeopleDie(); } playGame.updateScore(score); } public void run() { while (isLive) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } if (playGame.isIcePeopleLive()) { playGame.repaint(); } } } } ``` FirePeople类: ```java public class FirePeople { private Play play; private PlayGame playGame; private int score = 0; private boolean isLive = true; public FirePeople(Play play) { this.play = play; this.playGame = play.getPlayGame(); } public void gain() { score += 10; } public void move(boolean up, boolean left, boolean right) { if (isLive) { int x = playGame.getFirePeopleX(); int y = playGame.getFirePeopleY(); if (up) { y -= 1; } if (left) { x -= 1; } if (right) { x += 1; } if (playGame.getMap(x, y) == 3) { score += 100; playGame.setMap(x, y, 0); } if (playGame.getMap(x, y) == 4) { isLive = false; playGame.setMap(x, y, 0); playGame.setFirePeopleLive(false); playGame.setFirePeopleDie(); } playGame.setFirePeopleX(x); playGame.setFirePeopleY(y); } } public void verdict(int i) { if (i == 1) { playGame.setFirePeopleWin(); } else if (i == 2) { playGame.setFirePeopleDie(); } playGame.updateScore(score); } public void run() { while (isLive) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } if (playGame.isFirePeopleLive()) { playGame.repaint(); } } } } ``` PlayGame类: ```java public class PlayGame extends JPanel implements KeyListener { private static final long serialVersionUID = 1L; private int[][] map; private int level; private int icePeopleX, icePeopleY; private int firePeopleX, firePeopleY; private boolean icePeopleLive = true; private boolean firePeopleLive = true; private boolean isWin = false; private boolean isDie = false; private int score = 0; public PlayGame(int level) { this.level = level; map = Map.getMap(level); icePeopleX = Map.getIcePeopleX(level); icePeopleY = Map.getIcePeopleY(level); firePeopleX = Map.getFirePeopleX(level); firePeopleY = Map.getFirePeopleY(level); } public void paint(Graphics g) { super.paint(g); for (int y = 0; y < 13; y++) { for (int x = 0; x < 13; x++) { switch (map[y][x]) { case 0: g.drawImage(Map.getGrass(), x * 50, y * 50, null); break; case 1: g.drawImage(Map.getWall(), x * 50, y * 50, null); break; case 2: g.drawImage(Map.getDiamond(), x * 50, y * 50, null); break; case 3: g.drawImage(Map.getIce(), x * 50, y * 50, null); break; case 4: g.drawImage(Map.getFire(), x * 50, y * 50, null); break; } } } g.setColor(Color.WHITE); g.setFont(new Font("微软雅黑", Font.BOLD, 20)); g.drawString("Score: " + score, 20, 50); if (icePeopleLive) { g.drawImage(Map.getIcePeople(), icePeopleX * 50, icePeopleY * 50, null); } if (firePeopleLive) { g.drawImage(Map.getFirePeople(), firePeopleX * 50, firePeopleY * 50, null); } if (isWin) { g.drawImage(Map.getWin(), 0, 0, null); } if (isDie) { g.drawImage(Map.getDie(), 0, 0, null); } } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_UP) { icePeople.move(true, false, false); } if (key == KeyEvent.VK_LEFT) { icePeople.move(false, true, false); } if (key == KeyEvent.VK_RIGHT) { icePeople.move(false, false, true); } if (key == KeyEvent.VK_W) { firePeople.move(true, false, false); } if (key == KeyEvent.VK_A) { firePeople.move(false, true, false); } if (key == KeyEvent.VK_D) { firePeople.move(false, false, true); } if (key == KeyEvent.VK_SPACE) { if (icePeopleLive && firePeopleLive) { if (icePeopleX == firePeopleX && icePeopleY == firePeopleY) { if (score % 100 >= 50) { icePeople.gain(); } else { firePeople.gain(); } } } } if (key == KeyEvent.VK_ENTER) { if (isWin || isDie) { new Play(level); } } if (icePeopleLive && firePeopleLive) { if (map[icePeopleY][icePeopleX] == 2) { score += 10; map[icePeopleY][icePeopleX] = 0; } if (map[firePeopleY][firePeopleX] == 2) { score += 10; map[firePeopleY][firePeopleX] = 0; } if (score >= 200) { isWin = true; icePeople.verdict(1); firePeople.verdict(2); } if (!icePeopleLive) { isDie = true; icePeople.verdict(2); firePeople.verdict(1); } if (!firePeopleLive) { isDie = true; icePeople.verdict(1); firePeople.verdict(2); } } repaint(); } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } public int getMap(int x, int y) { return map[y][x]; } public void setMap(int x, int y, int value) { map[y][x] = value; } public int getIcePeopleX() { return icePeopleX; } public void setIcePeopleX(int icePeopleX) { this.icePeopleX = icePeopleX; } public int getIcePeopleY() { return icePeopleY; } public void setIcePeopleY(int icePeopleY) { this.icePeopleY = icePeopleY; } public int getFirePeopleX() { return firePeopleX; } public void setFirePeopleX(int firePeopleX) { this.firePeopleX = firePeopleX; } public int getFirePeopleY() { return firePeopleY; } public void setFirePeopleY(int firePeopleY) { this.firePeopleY = firePeopleY; } public boolean isIcePeopleLive() { return icePeopleLive; } public void setIcePeopleLive(boolean icePeopleLive) { this.icePeopleLive = icePeopleLive; } public boolean isFirePeopleLive() { return firePeopleLive; } public void setFirePeopleLive(boolean firePeopleLive) { this.firePeopleLive = firePeopleLive; } public void setIcePeopleDie() { icePeopleLive = false; } public void setFirePeopleDie() { firePeopleLive = false; } public void setIcePeopleWin() { isWin = true; } public void setFirePeopleWin() { isWin = true; } public void updateScore(int score) { this.score += score; } } ``` Play类: ```java public class Play extends JFrame { private static final long serialVersionUID = 1L; private PlayGame playGame; private IcePeople icePeople; private FirePeople firePeople; public Play(int level) { playGame = new PlayGame(level); icePeople = new IcePeople(this); firePeople = new FirePeople(this); Thread icePeopleThread = new Thread(() -> icePeople.run()); Thread firePeopleThread = new Thread(() -> firePeople.run()); icePeopleThread.start(); firePeopleThread.start(); add(playGame); addKeyListener(playGame); setTitle("森林冰火人"); setSize(650, 550); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setVisible(true); } public PlayGame getPlayGame() { return playGame; } public static void main(String[] args) { new Play(1); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值