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;
        }
    }
}
相关推荐
Java知识日历6 分钟前
SpringBoot整合Grizzly,提高您网站的并发能力
java·spring boot·后端·spring
Allen Bright14 分钟前
【JVM-7】JVM 命令行工具 jstack 的使用和具体应用案例
java·开发语言·jvm
东北赵四15 分钟前
JVM之垃圾回收器ZGC概述以及垃圾回收器总结的详细解析
java·jvm·算法
Allen Bright19 分钟前
【JVM-5】深入解析JVM垃圾回收器:分类与原理
java·jvm
阿七想学习42 分钟前
数据结构《Map&Set&哈希表》
java·数据结构·算法
for6243 分钟前
langchain4j执行源码分析
java·langchain
xiaoshiguang31 小时前
LeetCode:131. 分割回文串
java·算法·leetcode
做一个有信仰de人1 小时前
【面试题】Spring/SpringBoot部分[2025/1/6 ~ 2025/1/12]
java·spring boot·spring·面试
数据小爬虫@1 小时前
利用 Java 爬虫获取 1688 商品评论的实践指南
java·开发语言·爬虫
九转成圣1 小时前
IntelliJ IDEA Type Hierarchy Scope Pattern 学习指南
java·ide·intellij-idea