下面是一个简单的Java跳一跳游戏实现,使用Swing创建图形界面,玩家通过空格键控制角色跳跃到目标平台。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class JumpGame extends JFrame {

    private static final int WIDTH = 800;
    private static final int HEIGHT = 600;
    private static final int GROUND_HEIGHT = 100;
    private static final int CHARACTER_SIZE = 30;
    private static final int PLATFORM_HEIGHT = 20;
    private static final int MIN_PLATFORM_WIDTH = 60;
    private static final int MAX_PLATFORM_WIDTH = 120;
    private static final int MIN_PLATFORM_GAP = 150;
    private static final int MAX_PLATFORM_GAP = 250;
    private static final int JUMP_SPEED = 15;
    private static final int GRAVITY = 1;
    
    private int characterX = 100;
    private int characterY = HEIGHT - GROUND_HEIGHT - CHARACTER_SIZE;
    private int characterVelocity = 0;
    private boolean isJumping = false;
    
    private int[] platformsX = new int[10];
    private int[] platformsY = new int[10];
    private int[] platformsWidth = new int[10];
    
    private int score = 0;
    private int currentPlatform = 0;
    private int scrollOffset = 0;
    
    private Random random = new Random();
    private Timer timer;
    
    public JumpGame() {
        setTitle("Java跳一跳游戏");
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        
        // 初始化游戏平台
        initializePlatforms();
        
        // 设置键盘监听
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_SPACE && !isJumping) {
                    jump();
                }
            }
        });
        
        // 设置游戏循环定时器
        timer = new Timer(20, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                update();
                repaint();
            }
        });
        
        timer.start();
    }
    
    private void initializePlatforms() {
        // 创建初始平台
        platformsX[0] = 150;
        platformsY[0] = HEIGHT - GROUND_HEIGHT - PLATFORM_HEIGHT;
        platformsWidth[0] = 100;
        
        // 创建后续平台
        for (int i = 1; i < platformsX.length; i++) {
            createPlatform(i);
        }
    }
    
    private void createPlatform(int index) {
        int prevX = platformsX[index - 1] + platformsWidth[index - 1];
        int gap = MIN_PLATFORM_GAP + random.nextInt(MAX_PLATFORM_GAP - MIN_PLATFORM_GAP);
        platformsX[index] = prevX + gap;
        platformsY[index] = HEIGHT - GROUND_HEIGHT - PLATFORM_HEIGHT - random.nextInt(100);
        platformsWidth[index] = MIN_PLATFORM_WIDTH + random.nextInt(MAX_PLATFORM_WIDTH - MIN_PLATFORM_WIDTH);
    }
    
    private void jump() {
        if (!isJumping) {
            characterVelocity = -JUMP_SPEED;
            isJumping = true;
        }
    }
    
    private void update() {
        // 更新角色位置
        characterY += characterVelocity;
        characterVelocity += GRAVITY;
        
        // 检查是否落地
        if (characterY > HEIGHT - GROUND_HEIGHT - CHARACTER_SIZE) {
            characterY = HEIGHT - GROUND_HEIGHT - CHARACTER_SIZE;
            characterVelocity = 0;
            isJumping = false;
        }
        
        // 检查平台碰撞
        for (int i = 0; i < platformsX.length; i++) {
            if (isOnPlatform(i)) {
                characterY = platformsY[i] - CHARACTER_SIZE;
                characterVelocity = 0;
                isJumping = false;
                
                // 计分逻辑
                if (i > currentPlatform) {
                    score += (i - currentPlatform) * 10;
                    currentPlatform = i;
                }
                break;
            }
        }
        
        // 角色到达屏幕中央后滚动场景
        if (characterX > WIDTH / 2) {
            scrollOffset = characterX - WIDTH / 2;
        }
        
        // 检查游戏结束条件
        if (characterY > HEIGHT) {
            gameOver();
        }
        
        // 检查是否需要生成新平台
        if (platformsX[platformsX.length - 1] - scrollOffset < WIDTH + 100) {
            for (int i = 0; i < platformsX.length - 1; i++) {
                platformsX[i] = platformsX[i + 1];
                platformsY[i] = platformsY[i + 1];
                platformsWidth[i] = platformsWidth[i + 1];
            }
            createPlatform(platformsX.length - 1);
        }
    }
    
    private boolean isOnPlatform(int platformIndex) {
        int platformX = platformsX[platformIndex] - scrollOffset;
        int platformY = platformsY[platformIndex];
        int platformWidth = platformsWidth[platformIndex];
        
        return characterX + CHARACTER_SIZE > platformX && 
               characterX < platformX + platformWidth &&
               characterY + CHARACTER_SIZE >= platformY &&
               characterY + CHARACTER_SIZE <= platformY + PLATFORM_HEIGHT &&
               characterVelocity >= 0;
    }
    
    private void gameOver() {
        timer.stop();
        int choice = JOptionPane.showConfirmDialog(
            this, 
            "游戏结束!你的分数是: " + score + "\n想再玩一次吗?", 
            "游戏结束", 
            JOptionPane.YES_NO_OPTION
        );
        
        if (choice == JOptionPane.YES_OPTION) {
            resetGame();
            timer.start();
        } else {
            System.exit(0);
        }
    }
    
    private void resetGame() {
        characterX = 100;
        characterY = HEIGHT - GROUND_HEIGHT - CHARACTER_SIZE;
        characterVelocity = 0;
        isJumping = false;
        score = 0;
        currentPlatform = 0;
        scrollOffset = 0;
        initializePlatforms();
    }
    
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        
        // 绘制背景
        g.setColor(new Color(135, 206, 235));
        g.fillRect(0, 0, WIDTH, HEIGHT);
        
        // 绘制地面
        g.setColor(new Color(34, 139, 34));
        g.fillRect(0, HEIGHT - GROUND_HEIGHT, WIDTH, GROUND_HEIGHT);
        
        // 绘制平台
        g.setColor(new Color(139, 69, 19));
        for (int i = 0; i < platformsX.length; i++) {
            int x = platformsX[i] - scrollOffset;
            if (x > -100 && x < WIDTH + 100) {
                g.fillRect(x, platformsY[i], platformsWidth[i], PLATFORM_HEIGHT);
            }
        }
        
        // 绘制角色
        g.setColor(new Color(220, 20, 60));
        g.fillOval(characterX, characterY, CHARACTER_SIZE, CHARACTER_SIZE);
        g.setColor(Color.WHITE);
        g.fillOval(characterX + 5, characterY + 5, 8, 8);
        g.fillOval(characterX + 17, characterY + 5, 8, 8);
        g.setColor(Color.BLACK);
        g.fillOval(characterX + 7, characterY + 7, 4, 4);
        g.fillOval(characterX + 19, characterY + 7, 4, 4);
        
        // 绘制分数
        g.setColor(Color.BLACK);
        g.setFont(new Font("Arial", Font.BOLD, 24));
        g.drawString("分数: " + score, 20, 30);
        
        // 绘制游戏说明
        g.setFont(new Font("Arial", Font.PLAIN, 16));
        g.drawString("按空格键跳跃", WIDTH - 150, 30);
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JumpGame().setVisible(true);
            }
        });
    }
}

运行要求

要运行此游戏,您需要:
·Java开发环境(JDK 8或更高版本)
·支持Swing的Java运行时环境

将代码保存为JumpGame.java,然后使用以下命令编译和运行:

javac JumpGame.java
java JumpGame

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐