JavaSwing游戏开发之Camera原理


java 复制代码
package org.timer;

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

public class GameCameraWithObjects extends JPanel implements KeyListener {
    // 游戏世界大小
    private final int worldWidth = 2000;
    private final int worldHeight = 1500;

    // 摄像机位置
    private int cameraX = 0;
    private int cameraY = 0;

    // 摄像机视野大小
    private final int viewWidth = 800;
    private final int viewHeight = 600;

    // 玩家位置
    private int playerX = 400;
    private int playerY = 300;

    // 世界中的对象
    private final List<GameObject> gameObjects = new ArrayList<>();
    private final Random random = new Random();

    public GameCameraWithObjects() {
        setPreferredSize(new Dimension(viewWidth, viewHeight));
        addKeyListener(this);
        setFocusable(true);
        generateGameObjects(50); // 生成 50 个随机对象
    }

    // 生成游戏世界中的随机对象
    private void generateGameObjects(int count) {
        for (int i = 0; i < count; i++) {
            int x = random.nextInt(worldWidth);
            int y = random.nextInt(worldHeight);
            int size = 40; // 随机大小 10-40
//            int size = random.nextInt(30) + 10; // 随机大小 10-40
            Color color = new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
            gameObjects.add(new GameObject(x, y, size, color));
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        // 填充背景
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(0, 0, viewWidth, viewHeight);

        // 绘制世界边界
        g.setColor(Color.BLACK);
        g.drawRect(-cameraX, -cameraY, worldWidth, worldHeight);

        // 绘制所有对象
        for (GameObject obj : gameObjects) {
            int screenX = obj.x - cameraX;
            int screenY = obj.y - cameraY;

            // 只绘制在视野内的对象
            if (screenX + obj.size > 0 && screenX < viewWidth && screenY + obj.size > 0 && screenY < viewHeight) {
                g.setColor(obj.color);
                g.fillRect(screenX, screenY, obj.size, obj.size);
                g.drawString("("+obj.x+","+obj.y+")", screenX, screenY);
            }
        }

        // 绘制玩家
        g.setColor(Color.RED);
        int screenX = playerX - cameraX;
        int screenY = playerY - cameraY;
        g.fillRect(screenX - 10, screenY - 10, 20, 20);

        // 显示调试信息
        g.setColor(Color.BLACK);
        g.drawString("Camera: (" + cameraX + ", " + cameraY + ")", 10, 20);
        g.drawString("Player: (" + playerX + ", " + playerY + ")", 10, 40);
        g.drawString("Objects: " + gameObjects.size(), 10, 60);
    }

    // 更新摄像机位置
    private void updateCamera() {
//        cameraX = playerX - viewWidth / 2;
        cameraX = Math.max(0, Math.min(playerX - viewWidth / 2, worldWidth - viewWidth));
//        cameraY = Math.max(0, Math.min(playerY - viewHeight / 2, worldHeight - viewHeight));
        cameraY = Math.max(0, Math.min(playerY - viewHeight / 2, worldHeight - viewHeight));
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int speed = 10;

        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP -> playerY = Math.max(0, playerY - speed);
            case KeyEvent.VK_DOWN -> playerY = Math.min(worldHeight, playerY + speed);
            case KeyEvent.VK_LEFT -> playerX = Math.max(0, playerX - speed);
            case KeyEvent.VK_RIGHT -> playerX = Math.min(worldWidth, playerX + speed);
        }

        // 更新摄像机
        updateCamera();
        repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {}
    @Override
    public void keyTyped(KeyEvent e) {}

    public static void main(String[] args) {
        JFrame frame = new JFrame("Game Camera with Objects");
        GameCameraWithObjects gamePanel = new GameCameraWithObjects();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(gamePanel);
        frame.pack();
        frame.setVisible(true);
    }

    // 内部类表示游戏对象
    private static class GameObject {
        int x, y, size;
        Color color;

        public GameObject(int x, int y, int size, Color color) {
            this.x = x;
            this.y = y;
            this.size = size;
            this.color = color;
        }
    }
}
相关推荐
零千叶21 分钟前
【面试】AI大模型应用原理面试题
java·设计模式·面试
坐吃山猪5 小时前
SpringBoot01-配置文件
java·开发语言
我叫汪枫5 小时前
《Java餐厅的待客之道:BIO, NIO, AIO三种服务模式的进化》
java·开发语言·nio
yaoxtao5 小时前
java.nio.file.InvalidPathException异常
java·linux·ubuntu
Swift社区7 小时前
从 JDK 1.8 切换到 JDK 21 时遇到 NoProviderFoundException 该如何解决?
java·开发语言
DKPT8 小时前
JVM中如何调优新生代和老生代?
java·jvm·笔记·学习·spring
phltxy8 小时前
JVM——Java虚拟机学习
java·jvm·学习
seabirdssss9 小时前
使用Spring Boot DevTools快速重启功能
java·spring boot·后端
喂完待续9 小时前
【序列晋升】29 Spring Cloud Task 微服务架构下的轻量级任务调度框架
java·spring·spring cloud·云原生·架构·big data·序列晋升