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);
}
}
用Java实现森林冰火人游戏
最新推荐文章于 2024-05-30 09:08:27 发布