当前位置: 首页 > article >正文

JavaSwing游戏开发之Camera原理

在这里插入图片描述
在这里插入图片描述

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;
        }
    }
}



http://www.kler.cn/a/505189.html

相关文章:

  • 【leetcode21】344.反转字符串
  • Spring Boot + MyBatis-Flex 配置 ProxySQL 的完整指南
  • uni-app的学习
  • Python 标准库:time——时间的访问和转换
  • 【C】初阶数据结构3 -- 单链表
  • Vue2+OpenLayers给标点Feature添加信息窗体(提供Gitee源码)
  • Java 输入输出流(上)
  • Gitlab流水线配置
  • Java 后端整合 Swagger + Knife4j 接口文档
  • 学员答疑:安卓分屏窗口的TouchableRegion设置流程追踪
  • 【STM32】存储分析深入——堆栈与map文件
  • C++进阶(四)--set和map的介绍与使用
  • 【落羽的落羽 C语言篇】文件操作
  • stack_queue的底层,模拟实现,deque和priority_queue详解
  • 深入探讨DICOM医学影像中的MPPS服务及其具体实现
  • 【原创】大数据治理入门(4)《保护数据隐私:大数据治理的最佳实践》入门必看 高赞实用
  • 战场物联网:通信挑战与最新解决方案综述
  • BUUCTF[ACTF新生赛2020]easyre
  • ubuntu22.04:解决google chrome 访问百度页面加载慢的问题
  • C++ 并发编程之std::find的并发版本
  • centos使用dpdk库
  • 【AI学习】地平线首席架构师苏箐关于自动驾驶的演讲
  • 华为数通HCIE备考经验分享
  • 力扣26题-删除有序数组中的重复项
  • SpringAOP前置——代理模式
  • 光谱相机的光谱分辨率可以达到多少?